SHARE
TWEET

Untitled

a guest Mar 25th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3. import tester.*;
  4. import javalib.impworld.*;
  5. // import java.awt.Color;
  6. // import javalib.worldimages.*;
  7.  
  8. class GameWorld extends World {
  9.   public static final int GAME_WIDTH = 500;
  10.   public static final int GAME_HEIGHT = 500;
  11.  
  12.   public WorldScene makeScene() {
  13.     return null;
  14.   }
  15.  
  16.   // public void onTick() {
  17.   //
  18.   // }
  19.   //
  20.   // // left-mouse clicker represents revealing
  21.   // // the current target cell based on the mouse's position (by a posn)
  22.   // // right-mouse clicker flags
  23.   // public void onMouseClicked(Posn pos) {
  24.   // }
  25.  
  26. }
  27.  
  28. class Board {
  29.   int rows;
  30.   int columns;
  31.   int mines;
  32.  
  33.   Board(int rows, int columns, int mines) {
  34.     this.rows = rows;
  35.     this.columns = columns;
  36.     this.mines = mines;
  37.   }
  38.  
  39.   // returns an initialized board
  40.   ArrayList<ArrayList<Cell>> initBoard() {
  41.     // Arraylist of Arraylist; outer layer denotes rows; inner layer denotes the
  42.     // columns in each row
  43.     // this just makes a new cell for the entire row*column grid
  44.     ArrayList<ArrayList<Cell>> grid = new ArrayList<ArrayList<Cell>>(rows);
  45.     for (int i = 0; i < rows; i++) {
  46.       ArrayList<Cell> horizontals = new ArrayList<Cell>(columns);
  47.       for (int j = 0; j < columns; j++) {
  48.         Cell newCell = new Cell(new ArrayList<Cell>(8), 0);
  49.         for (int k = 0; k < 8; k++) {
  50.           newCell.cells.add(k, new Cell(new ArrayList<Cell>(0), -1));// dummy iniatializer
  51.         }
  52.         horizontals.add(newCell);
  53.       }
  54.       grid.add(horizontals);
  55.     }
  56.     return grid;
  57.   }
  58.  
  59.   // inserts mines randomly to a given board;
  60.   ArrayList<ArrayList<Cell>> initMines(ArrayList<ArrayList<Cell>> grid) {
  61.     int minesInserted = 0;
  62.     Random a = new Random();
  63.     int m = a.nextInt(this.rows);
  64.     int n = a.nextInt(this.columns);
  65.     while (this.mines > minesInserted) {
  66.       if (grid.get(m).get(n).value == 0) {
  67.         grid.get(m).get(n).value = 1;
  68.         minesInserted++;
  69.       }
  70.       m = a.nextInt(rows);
  71.       n = a.nextInt(columns);
  72.     }
  73.     return grid;
  74.   }
  75.  
  76.   // add links
  77.   // Horizontal, vertical, L-R diagonal, R-L diagonal
  78.   // start with the outer square, then move inwards
  79.   // top and bottom
  80.   ArrayList<ArrayList<Cell>> initLinks(ArrayList<ArrayList<Cell>> grid) {
  81.  
  82.     for (int i = 0; i < rows; i++) {
  83.       for (int j = 0; j < columns; j++) {
  84.         if (j - 1 >= 0) {
  85.           grid.get(i).get(j).cells.set(0, grid.get(i).get(j - 1)); // left
  86.         }
  87.         if (i - 1 >= 0 && j - 1 >= 0) {
  88.           grid.get(i).get(j).cells.set(1, grid.get(i - 1).get(j - 1)); // top left
  89.         }
  90.         if (i - 1 >= 0) {
  91.           grid.get(i).get(j).cells.set(2, grid.get(i - 1).get(j)); // top
  92.         }
  93.         if (i - 1 >= 0 && j + 1 < columns) {
  94.           grid.get(i).get(j).cells.set(3, grid.get(i - 1).get(j + 1)); // top right
  95.         }
  96.         if (j + 1 < columns) {
  97.           grid.get(i).get(j).cells.set(4, grid.get(i).get(j + 1)); // right
  98.         }
  99.         if (i + 1 < rows && j + 1 < columns) {
  100.           grid.get(i).get(j).cells.set(5, grid.get(i + 1).get(j + 1)); // bottom right
  101.         }
  102.         if (i + 1 < rows) {
  103.           grid.get(i).get(j).cells.set(6, grid.get(i + 1).get(j)); // bottom
  104.         }
  105.         if (i + 1 < rows && j - 1 >= 0) {
  106.           grid.get(i).get(j).cells.set(7, grid.get(i + 1).get(j - 1)); // bottom left
  107.         }
  108.       }
  109.     }
  110.  
  111.     // Add mines
  112.     // generate list of all unique i, j coordinate pairs
  113.     // list of arrays of len 2
  114.  
  115.     return grid;
  116.   }
  117. }
  118.  
  119. // represents a cell with its neighbors and its value
  120. class Cell {
  121.  
  122.   ArrayList<Cell> cells;
  123.   int value; // 1 represents mine, 0 represents nothing
  124.  
  125.   Cell(ArrayList<Cell> cells, int value) {
  126.     this.cells = cells;
  127.     this.value = value;
  128.   }
  129.  
  130.   // method to count the number of surrounding mines of this cell
  131.   int countMines() {
  132.     int counter = 0;
  133.     for (int i = 0; i < 8; i++) {
  134.       if (this.cells.get(i).value == 1) {
  135.         counter++;
  136.       }
  137.     }
  138.     return counter;
  139.   }
  140. }
  141.  
  142. class Utils {
  143.   void linkHorizontalCells(Cell a, Cell b) {
  144.     a.cells.set(4, b);
  145.     b.cells.set(0, a);
  146.   }
  147.  
  148.   void linkVerticalCells(Cell top, Cell bottom) {
  149.     top.cells.set(6, bottom);
  150.     bottom.cells.set(2, top);
  151.   }
  152.  
  153.   // void generateRightCell(Cell a) {
  154.   // Cell b = new Cell(new ArrayList<Cell>(8), 0);
  155.   // a.cells.set(4, b);
  156.   // b.cells.set(0, a);
  157.   // }
  158.  
  159. }
  160.  
  161. class ExamplesMineSweeper {
  162.  
  163.   ArrayList<ArrayList<Cell>> base = new ArrayList<ArrayList<Cell>>();
  164.  
  165.   // is the method initBoard returning an initialized board
  166.   void testInitBoard(Tester t) {
  167.     ArrayList<ArrayList<Cell>> testGrid = new Board(2, 2, 0).initBoard();
  168.     ArrayList<ArrayList<Cell>> expected = new ArrayList<ArrayList<Cell>>();
  169.     ArrayList<Cell> row1 = new ArrayList<Cell>();
  170.     ArrayList<Cell> row2 = new ArrayList<Cell>();
  171.     row1.add(new Cell(new ArrayList<Cell>(8), 0));
  172.     row1.add(new Cell(new ArrayList<Cell>(8), 0));
  173.     row2.add(new Cell(new ArrayList<Cell>(8), 0));
  174.     row2.add(new Cell(new ArrayList<Cell>(8), 0));
  175.     for (int i = 0; i < 8; i++) {
  176.       for (int j = 0; j < 2; j++) {
  177.         row1.get(j).cells.add(new Cell(new ArrayList<Cell>(0), -1));
  178.         row2.get(j).cells.add(new Cell(new ArrayList<Cell>(0), -1));
  179.       }
  180.     }
  181.  
  182.     expected.add(row1);
  183.     expected.add(row2);
  184.  
  185.     t.checkExpect(testGrid, expected);
  186.   }
  187.  
  188.   // is the method initBoard returning an initialized board
  189.   void testInitBoard2(Tester t) {
  190.     ArrayList<ArrayList<Cell>> grid = new Board(1, 2, 0).initBoard();
  191.     ArrayList<ArrayList<Cell>> expect = new ArrayList<ArrayList<Cell>>();
  192.     ArrayList<Cell> row = new ArrayList<Cell>();
  193.     row.add(new Cell(new ArrayList<Cell>(), 0));
  194.     row.add(new Cell(new ArrayList<Cell>(), 0));
  195.     for (int i = 0; i < 8; i++) {
  196.       for (int j = 0; j < 2; j++) {
  197.         row.get(j).cells.add(new Cell(new ArrayList<Cell>(0), -1));
  198.       }
  199.     }
  200.  
  201.     expect.add(row);
  202.  
  203.     t.checkExpect(grid, expect);
  204.   }
  205.  
  206.   // is the method initMines returning the right amount of mines at the right
  207.   // place
  208.   void testInitMines(Tester t) {
  209.     ArrayList<ArrayList<Cell>> grid = new Board(2, 2, 0).initBoard();
  210.     new Board(2, 2, 0).initMines(grid);
  211.     int counter = 0;
  212.     for (int i = 0; i < grid.size(); i++) {
  213.       for (int j = 0; j < grid.get(0).size(); j++) {
  214.         if (grid.get(i).get(j).value == 1) {
  215.           counter++;
  216.         }
  217.       }
  218.     }
  219.  
  220.     t.checkExpect(counter, 0);
  221.  
  222.     ArrayList<ArrayList<Cell>> grid2 = new Board(2, 2, 4).initBoard();
  223.     new Board(2, 2, 4).initMines(grid2);
  224.     int counter2 = 0;
  225.     for (int i = 0; i < grid2.size(); i++) {
  226.       for (int j = 0; j < grid2.get(0).size(); j++) {
  227.         if (grid2.get(i).get(j).value == 1) {
  228.           counter2++;
  229.         }
  230.       }
  231.     }
  232.  
  233.     t.checkExpect(counter2, 4);
  234.  
  235.     // a test for random seed
  236.  
  237.   }
  238.  
  239.   // is the method initLinks returning a linked arraylist of cells of correct
  240.   // positions
  241.   void testInitLinks(Tester t) {
  242.     Board b = new Board(1, 2, 0);
  243.     ArrayList<ArrayList<Cell>> testGrids = b.initBoard();
  244.     ArrayList<ArrayList<Cell>> lists = b.initLinks(testGrids);
  245.  
  246.     t.checkExpect(lists.get(0).get(0).cells.get(4).value, lists.get(0).get(1).value);
  247.     t.checkExpect(lists.get(0).get(1).cells.get(0).value, lists.get(0).get(0).value);
  248.  
  249.     Board b1 = new Board(1, 3, 2);
  250.     ArrayList<ArrayList<Cell>> testGrids1 = b1.initBoard();
  251.  
  252.     ArrayList<ArrayList<Cell>> lists1 = b1.initMines(b1.initLinks(testGrids1));
  253.     t.checkExpect(lists1.get(0).get(0).cells.get(4).value, lists1.get(0).get(1).value);
  254.     t.checkExpect(lists1.get(0).get(1).cells.get(0).value, lists1.get(0).get(0).value);
  255.     t.checkExpect(lists1.get(0).get(1).cells.get(4).value, lists1.get(0).get(2).value);
  256.     t.checkExpect(lists1.get(0).get(2).cells.get(0).value, lists1.get(0).get(1).value);
  257.  
  258.   }
  259.  
  260.   // is the method countMines returning the right numbers of mines nearby this
  261.   // cell?
  262.   void testCountMines(Tester t) {
  263.     Board b = new Board(1, 2, 0);
  264.     ArrayList<ArrayList<Cell>> testGrids = b.initBoard();
  265.     ArrayList<ArrayList<Cell>> lists = b.initLinks(testGrids);
  266.     lists.get(0).get(0).value = 1;
  267.  
  268.     t.checkExpect(lists.get(0).get(1).countMines(), 1);
  269.     t.checkExpect(lists.get(0).get(0).countMines(), 0);
  270.  
  271.     Board b2 = new Board(2, 2, 0);
  272.     ArrayList<ArrayList<Cell>> testGrids2 = b2.initBoard();
  273.     ArrayList<ArrayList<Cell>> lists2 = b2.initLinks(testGrids2);
  274.     lists2.get(0).get(0).value = 1;
  275.     lists2.get(0).get(1).value = 1;
  276.     lists2.get(1).get(1).value = 1;
  277.     for (int i = 0; i < 8; i++) {
  278.       System.out.println(lists2.get(1).get(0).cells.get(i).value);
  279.     }
  280.  
  281.     t.checkExpect(lists2.get(1).get(0).countMines(), 3);
  282.   }
  283.  
  284. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top