use mineswipe::grid::{State, Grid}; #[test] fn first_move() { let (rows, columns, mines) = (9, 6, 20); let mut grid = Grid::new(rows, columns, mines).unwrap(); println!("{:?}", grid); assert_eq!(grid.state, State::Start); let start_index = rows*(columns-1); grid.dig(start_index).unwrap(); println!("{:?}", grid); assert!(no_mines(&grid, &grid.cells[start_index].neighbours(rows, columns))); assert!(!grid.cells[start_index].mine); assert_eq!(grid.state, State::Ongoing); } #[test] fn dig_twice() { let (rows, columns, mines) = (9, 6, 20); let mut grid = Grid::new(rows, columns, mines).unwrap(); let index = rows*(columns-1); grid.dig(index).unwrap(); assert!(no_mines(&grid, &grid.cells[index].neighbours(rows, columns))); let result = grid.dig(index); assert!(result.is_err()); } #[test] fn illegal_index() { let (rows, columns, mines) = (9, 6, 20); let mut grid = Grid::new(rows, columns, mines).unwrap(); let index = rows*columns; let result = grid.dig(index); assert!(result.is_err()); } #[test] fn illegal_grids() { let (rows1, columns1, mines1) = (1, 7, 2); let (rows2, columns2, mines2) = (7, 1, 2); let grid1 = Grid::new(rows1, columns1, mines1); let grid2 = Grid::new(rows2, columns2, mines2); assert!(grid1.is_err() && grid2.is_err()); } /// Checks if all the given cells are not mines. fn no_mines(grid: &Grid, cells: &Vec) -> bool { cells.iter().all(|index| !grid.cells[index.clone()].mine) }