Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Random;
- import tester.*;
- import javalib.impworld.*;
- // import java.awt.Color;
- // import javalib.worldimages.*;
- class GameWorld extends World {
- public static final int GAME_WIDTH = 500;
- public static final int GAME_HEIGHT = 500;
- public WorldScene makeScene() {
- return null;
- }
- // public void onTick() {
- //
- // }
- //
- // // left-mouse clicker represents revealing
- // // the current target cell based on the mouse's position (by a posn)
- // // right-mouse clicker flags
- // public void onMouseClicked(Posn pos) {
- // }
- }
- class Board {
- int rows;
- int columns;
- int mines;
- Board(int rows, int columns, int mines) {
- this.rows = rows;
- this.columns = columns;
- this.mines = mines;
- }
- // returns an initialized board
- ArrayList<ArrayList<Cell>> initBoard() {
- // Arraylist of Arraylist; outer layer denotes rows; inner layer denotes the
- // columns in each row
- // this just makes a new cell for the entire row*column grid
- ArrayList<ArrayList<Cell>> grid = new ArrayList<ArrayList<Cell>>(rows);
- for (int i = 0; i < rows; i++) {
- ArrayList<Cell> horizontals = new ArrayList<Cell>(columns);
- for (int j = 0; j < columns; j++) {
- Cell newCell = new Cell(new ArrayList<Cell>(8), 0);
- for (int k = 0; k < 8; k++) {
- newCell.cells.add(k, new Cell(new ArrayList<Cell>(0), -1));// dummy iniatializer
- }
- horizontals.add(newCell);
- }
- grid.add(horizontals);
- }
- return grid;
- }
- // inserts mines randomly to a given board;
- ArrayList<ArrayList<Cell>> initMines(ArrayList<ArrayList<Cell>> grid) {
- int minesInserted = 0;
- Random a = new Random();
- int m = a.nextInt(this.rows);
- int n = a.nextInt(this.columns);
- while (this.mines > minesInserted) {
- if (grid.get(m).get(n).value == 0) {
- grid.get(m).get(n).value = 1;
- minesInserted++;
- }
- m = a.nextInt(rows);
- n = a.nextInt(columns);
- }
- return grid;
- }
- // add links
- // Horizontal, vertical, L-R diagonal, R-L diagonal
- // start with the outer square, then move inwards
- // top and bottom
- ArrayList<ArrayList<Cell>> initLinks(ArrayList<ArrayList<Cell>> grid) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- if (j - 1 >= 0) {
- grid.get(i).get(j).cells.set(0, grid.get(i).get(j - 1)); // left
- }
- if (i - 1 >= 0 && j - 1 >= 0) {
- grid.get(i).get(j).cells.set(1, grid.get(i - 1).get(j - 1)); // top left
- }
- if (i - 1 >= 0) {
- grid.get(i).get(j).cells.set(2, grid.get(i - 1).get(j)); // top
- }
- if (i - 1 >= 0 && j + 1 < columns) {
- grid.get(i).get(j).cells.set(3, grid.get(i - 1).get(j + 1)); // top right
- }
- if (j + 1 < columns) {
- grid.get(i).get(j).cells.set(4, grid.get(i).get(j + 1)); // right
- }
- if (i + 1 < rows && j + 1 < columns) {
- grid.get(i).get(j).cells.set(5, grid.get(i + 1).get(j + 1)); // bottom right
- }
- if (i + 1 < rows) {
- grid.get(i).get(j).cells.set(6, grid.get(i + 1).get(j)); // bottom
- }
- if (i + 1 < rows && j - 1 >= 0) {
- grid.get(i).get(j).cells.set(7, grid.get(i + 1).get(j - 1)); // bottom left
- }
- }
- }
- // Add mines
- // generate list of all unique i, j coordinate pairs
- // list of arrays of len 2
- return grid;
- }
- }
- // represents a cell with its neighbors and its value
- class Cell {
- ArrayList<Cell> cells;
- int value; // 1 represents mine, 0 represents nothing
- Cell(ArrayList<Cell> cells, int value) {
- this.cells = cells;
- this.value = value;
- }
- // method to count the number of surrounding mines of this cell
- int countMines() {
- int counter = 0;
- for (int i = 0; i < 8; i++) {
- if (this.cells.get(i).value == 1) {
- counter++;
- }
- }
- return counter;
- }
- }
- class Utils {
- void linkHorizontalCells(Cell a, Cell b) {
- a.cells.set(4, b);
- b.cells.set(0, a);
- }
- void linkVerticalCells(Cell top, Cell bottom) {
- top.cells.set(6, bottom);
- bottom.cells.set(2, top);
- }
- // void generateRightCell(Cell a) {
- // Cell b = new Cell(new ArrayList<Cell>(8), 0);
- // a.cells.set(4, b);
- // b.cells.set(0, a);
- // }
- }
- class ExamplesMineSweeper {
- ArrayList<ArrayList<Cell>> base = new ArrayList<ArrayList<Cell>>();
- // is the method initBoard returning an initialized board
- void testInitBoard(Tester t) {
- ArrayList<ArrayList<Cell>> testGrid = new Board(2, 2, 0).initBoard();
- ArrayList<ArrayList<Cell>> expected = new ArrayList<ArrayList<Cell>>();
- ArrayList<Cell> row1 = new ArrayList<Cell>();
- ArrayList<Cell> row2 = new ArrayList<Cell>();
- row1.add(new Cell(new ArrayList<Cell>(8), 0));
- row1.add(new Cell(new ArrayList<Cell>(8), 0));
- row2.add(new Cell(new ArrayList<Cell>(8), 0));
- row2.add(new Cell(new ArrayList<Cell>(8), 0));
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 2; j++) {
- row1.get(j).cells.add(new Cell(new ArrayList<Cell>(0), -1));
- row2.get(j).cells.add(new Cell(new ArrayList<Cell>(0), -1));
- }
- }
- expected.add(row1);
- expected.add(row2);
- t.checkExpect(testGrid, expected);
- }
- // is the method initBoard returning an initialized board
- void testInitBoard2(Tester t) {
- ArrayList<ArrayList<Cell>> grid = new Board(1, 2, 0).initBoard();
- ArrayList<ArrayList<Cell>> expect = new ArrayList<ArrayList<Cell>>();
- ArrayList<Cell> row = new ArrayList<Cell>();
- row.add(new Cell(new ArrayList<Cell>(), 0));
- row.add(new Cell(new ArrayList<Cell>(), 0));
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 2; j++) {
- row.get(j).cells.add(new Cell(new ArrayList<Cell>(0), -1));
- }
- }
- expect.add(row);
- t.checkExpect(grid, expect);
- }
- // is the method initMines returning the right amount of mines at the right
- // place
- void testInitMines(Tester t) {
- ArrayList<ArrayList<Cell>> grid = new Board(2, 2, 0).initBoard();
- new Board(2, 2, 0).initMines(grid);
- int counter = 0;
- for (int i = 0; i < grid.size(); i++) {
- for (int j = 0; j < grid.get(0).size(); j++) {
- if (grid.get(i).get(j).value == 1) {
- counter++;
- }
- }
- }
- t.checkExpect(counter, 0);
- ArrayList<ArrayList<Cell>> grid2 = new Board(2, 2, 4).initBoard();
- new Board(2, 2, 4).initMines(grid2);
- int counter2 = 0;
- for (int i = 0; i < grid2.size(); i++) {
- for (int j = 0; j < grid2.get(0).size(); j++) {
- if (grid2.get(i).get(j).value == 1) {
- counter2++;
- }
- }
- }
- t.checkExpect(counter2, 4);
- // a test for random seed
- }
- // is the method initLinks returning a linked arraylist of cells of correct
- // positions
- void testInitLinks(Tester t) {
- Board b = new Board(1, 2, 0);
- ArrayList<ArrayList<Cell>> testGrids = b.initBoard();
- ArrayList<ArrayList<Cell>> lists = b.initLinks(testGrids);
- t.checkExpect(lists.get(0).get(0).cells.get(4).value, lists.get(0).get(1).value);
- t.checkExpect(lists.get(0).get(1).cells.get(0).value, lists.get(0).get(0).value);
- Board b1 = new Board(1, 3, 2);
- ArrayList<ArrayList<Cell>> testGrids1 = b1.initBoard();
- ArrayList<ArrayList<Cell>> lists1 = b1.initMines(b1.initLinks(testGrids1));
- t.checkExpect(lists1.get(0).get(0).cells.get(4).value, lists1.get(0).get(1).value);
- t.checkExpect(lists1.get(0).get(1).cells.get(0).value, lists1.get(0).get(0).value);
- t.checkExpect(lists1.get(0).get(1).cells.get(4).value, lists1.get(0).get(2).value);
- t.checkExpect(lists1.get(0).get(2).cells.get(0).value, lists1.get(0).get(1).value);
- }
- // is the method countMines returning the right numbers of mines nearby this
- // cell?
- void testCountMines(Tester t) {
- Board b = new Board(1, 2, 0);
- ArrayList<ArrayList<Cell>> testGrids = b.initBoard();
- ArrayList<ArrayList<Cell>> lists = b.initLinks(testGrids);
- lists.get(0).get(0).value = 1;
- t.checkExpect(lists.get(0).get(1).countMines(), 1);
- t.checkExpect(lists.get(0).get(0).countMines(), 0);
- Board b2 = new Board(2, 2, 0);
- ArrayList<ArrayList<Cell>> testGrids2 = b2.initBoard();
- ArrayList<ArrayList<Cell>> lists2 = b2.initLinks(testGrids2);
- lists2.get(0).get(0).value = 1;
- lists2.get(0).get(1).value = 1;
- lists2.get(1).get(1).value = 1;
- for (int i = 0; i < 8; i++) {
- System.out.println(lists2.get(1).get(0).cells.get(i).value);
- }
- t.checkExpect(lists2.get(1).get(0).countMines(), 3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement