This commit is contained in:
platane
2025-03-23 10:32:07 +01:00
parent da5e045399
commit 85e229a04d
2 changed files with 41 additions and 9 deletions

View File

@@ -9,14 +9,22 @@ jobs:
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1 - uses: oven-sh/setup-bun@v1
- uses: dtolnay/rust-toolchain@stable
- run: bun install --frozen-lockfile - run: bun install --frozen-lockfile
- run: bun run build
working-directory: packages/solver-r
- run: npm run type - run: npm run type
- run: npm run lint
- run: bun test - run: bun test
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: cargo test
working-directory: packages/solver-r
- run: npm run lint
test-action: test-action:
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@@ -18,7 +18,7 @@ pub struct Point {
} }
#[wasm_bindgen] #[wasm_bindgen]
#[derive(Copy, Clone)] #[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u8)] #[repr(u8)]
pub enum Cell { pub enum Cell {
Empty = 0, Empty = 0,
@@ -39,6 +39,16 @@ pub struct Grid {
#[wasm_bindgen] #[wasm_bindgen]
impl Grid { impl Grid {
pub fn create_empty(width: i8, height: i8) -> Grid {
let n = (width as usize) * (height as usize);
let cells = (0..n).map(|_| Cell::Empty).collect();
Grid {
width,
height,
cells,
}
}
pub fn create(width: i8, height: i8, data: js_sys::Uint8Array) -> Grid { pub fn create(width: i8, height: i8, data: js_sys::Uint8Array) -> Grid {
let cells = data let cells = data
.to_vec() .to_vec()
@@ -71,17 +81,31 @@ impl Grid {
type Snake = [Point; 5]; type Snake = [Point; 5];
pub fn get_index(grid: &Grid, x: i8, y: i8) -> usize { pub fn get_index(grid: &Grid, x: i8, y: i8) -> usize {
return (x * grid.height + y) as usize; return (x as usize) * (grid.height as usize) + (y as usize);
} }
#[wasm_bindgen] pub fn get_cell(grid: &Grid, p: &Point) -> Cell {
pub fn get_cell(grid: &Grid, x: i8, y: i8) -> Cell { let i = get_index(grid, p.x, p.y);
let i = get_index(grid, x, y);
return grid.cells[i]; return grid.cells[i];
} }
pub fn set_cell(grid: &mut Grid, p: &Point, value: Cell) -> () {
let i = get_index(&grid, p.x, p.y);
grid.cells[i] = value;
}
#[test] #[test]
fn it_works() { fn grid_create() {
assert_eq!(2 + 2, 4); let grid = Grid::create_empty(30, 10);
assert_eq!(grid.width, 30);
assert_eq!(grid.height, 10);
assert_eq!(get_cell(&grid, &Point { x: 2, y: 3 }), Cell::Empty);
}
#[test]
fn grid_setter() {
let mut grid = Grid::create_empty(20, 10);
set_cell(&mut grid, &Point { x: 12, y: 3 }, Cell::Color1);
assert_eq!(get_cell(&grid, &Point { x: 12, y: 3 }), Cell::Color1);
} }