Facebook
From Ivory Marten, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 79
  1.  
  2.  
  3. import ecs100.*;
  4. import java.awt.Color;
  5. import java.util.*;
  6. import java.io.*;
  7. import java.nio.file.*;
  8.  
  9.  
  10. /**
  11.  * The board game involves moving a piece on a row of squares.
  12.  * Each square on the board has a positive integer on it.
  13.  * The piece can move to the left or right.
  14.  * When moving to the left, the piece must move the number of
  15.  *  steps specified in the cell immediately to its left.
  16.  * When moving to the right, the piece must move the number of
  17.  *  steps specified in the cell immediately to its right.
  18.  * For example:
  19.  *  If the piece is currently at the cell marked P on the following board:
  20.  *       +---+---+---+---+---+---+---+
  21.  *       | 3 | 4 | 2 | P | 3 | 1 | 5 |
  22.  *       +---+---+---+---+---+---+---+
  23.  *  then the piece could move 2 steps to the left, or 3 steps to the right.
  24.  *  If the piece is placed in the cell marked P on the following board:
  25.  *       +---+---+---+---+---+---+---+
  26.  *       | 3 | 4 | 2 | 4 | 3 | P | 5 |
  27.  *       +---+---+---+---+---+---+---+
  28.  *  then the piece can't move to the right (because 5 steps to the right would
  29.  *  take it off the board) but it can move 3 steps to the left.
  30.  *
  31.  * Depending on the numbers on the board, the piece may be able to get to lots
  32.  * of different square, or to very few squares.
  33.  * For example (if the squares are counted from 0)
  34.  *  In the board above, starting at square number 4, the piece could get to
  35.  *  squares 3, 1, 6, 5, 2, but couldn't get to squares 0 or 4.
  36.  
  37.  *  In this board, starting at square number 3 (underlined)
  38.  *       +---+---+---+---+---+---+---+---+---+
  39.  *       | 3 | 2 | 1 | 5 | 3 | 3 | 1 | 4 | 1 |
  40.  *       +---+---+---+===+---+---+---+---+---+
  41.  *  the piece could get to squares 3, 2, 0, 7, 8, and 6, but not 1, 4, or 5.
  42.  *
  43.  * You have to complete the findPlaces method to find all the squares that the piece
  44.  * could get to from a given starting position.
  45.  * The findPlaces method must construct and return a Set of all the indexes of the
  46.  * squares the piece can get to (including the starting point).
  47.  * It should not re-visit any square that it has
  48.  * already been to because that would lead to an infinite loop!
  49.  */
  50.  
  51.  
  52. public class BoardGame{
  53.  
  54.     /**
  55.      * Recursive method to find all the places on the board that
  56.      *   the piece could get to from pos
  57.      * places is the Set of places that it has been to.
  58.      * The method should add new positions it finds to places.
  59.      */
  60.  
  61.     public void findPlaces(List<Integer> board, int pos, Set<Integer> places){
  62.         /*# YOUR CODE HERE */
  63.  
  64.     }
  65.  
  66.  
  67.     /**
  68.      * Top level method to find all the places on the board that the
  69.      * piece could get to if from the given starting position.
  70.      */
  71.     public Set<Integer> findPlaces(List<Integer> board, int startPos){
  72.         Set<Integer> places = new HashSet<Integer>();
  73.         findPlaces(board, startPos, places);
  74.         return places;
  75.     }
  76.  
  77.  
  78.     // Testing code ===============================================
  79.  
  80.  
  81.     public static final double LEFT = 10;
  82.     public static final double TOP = 10;
  83.     public static final double SIZE = 40;
  84.  
  85.     private List<Integer> board = new ArrayList<Integer>();
  86.     /**
  87.     * Setup the GUI
  88.     */
  89.     public void setupGUI(){
  90.         UI.addSlider("Random Board: choose size", 10, 20, (double v)->{makeBoard((int)v);});
  91.         UI.addTextField("Specify board: list numbers", (String s)->{makeBoard(s);});
  92.         UI.addButton("Test findPlaces", this::testFindPlaces);
  93.         UI.addButton("Quit", UI::quit);
  94.         UI.setDivider(0.0);
  95.         UI.setFontSize(20);
  96.     }
  97.  
  98.     /**
  99.      * Calls the findPlaces method and draws all the places
  100.      * it finds on the current board
  101.      */
  102.     public void testFindPlaces(){
  103.         if (board.isEmpty()){UI.println("No Board"); return;}
  104.         int start = board.size()/2;
  105.         Set<Integer> places = findPlaces(board, start);
  106.         drawBoard(board, start, places);
  107.     }
  108.  
  109.  
  110.     public static void main(String[] arguments){
  111.         new BoardGame().setupGUI();
  112.     }
  113.  
  114.  
  115.     /** Make a board of the given size with random numbers */
  116.     public void makeBoard(int size){
  117.         List<Integer> ans = new ArrayList<Integer>();
  118.         for(int i = 0; i<size; i++){
  119.             ans.add((int)(Math.random()*size*.8)+1);
  120.         }
  121.         board = Collections.unmodifiableList(ans);
  122.         drawBoard(board);
  123.     }
  124.  
  125.     /** Make a board with the numbers in the string */
  126.     public void makeBoard(String nums){
  127.         List<Integer> ans = new ArrayList<Integer>();
  128.         Scanner sc = new Scanner(nums);
  129.         while(sc.hasNext()){
  130.             if (sc.hasNextInt()) {
  131.                 ans.add(sc.nextInt());
  132.             }
  133.             else {
  134.                 sc.next();
  135.             }
  136.         }
  137.         board = Collections.unmodifiableList(ans);
  138.         drawBoard(board);
  139.     }
  140.    
  141.  
  142.  
  143.     /** Draw the board */
  144.     public void drawBoard(List<Integer> board){
  145.         UI.clearGraphics();
  146.         UI.setColor(Color.black);
  147.         UI.setLineWidth(2);
  148.         for (int i=0; i<board.size(); i++){
  149.             UI.drawRect(LEFT+i*SIZE, TOP, SIZE, SIZE);
  150.             UI.drawString(""+board.get(i), LEFT+(i+0.5)*SIZE-8, TOP+SIZE/2+10);
  151.         }
  152.     }
  153.     /** Draw the board with the starting position and the places it can get to */
  154.     public void drawBoard(List<Integer> board, int start, Set<Integer> places){
  155.         drawBoard(board);
  156.         // draw set of found places.
  157.         UI.setColor(Color.green);
  158.         double rad = SIZE/2-3;
  159.         for (int i : places){
  160.              UI.drawRect(LEFT+(i+.5)*SIZE-rad, TOP+SIZE/2-rad, 2*rad, 2*rad);
  161.         }
  162.         // draw start
  163.         UI.setColor(Color.blue);
  164.         UI.drawString(""+board.get(start), LEFT+(start+0.5)*SIZE-8, TOP+SIZE/2+10);
  165.  
  166.         UI.setColor(Color.black);
  167.  
  168.         UI.drawString("Blue number is the starting point", LEFT, TOP+SIZE*2);
  169.         UI.drawString("Green are the places it can get to", LEFT, TOP+SIZE*3);
  170.     }
  171.  
  172.     //================================================
  173.     // DO NOT CHANGE ANYTHING BELOW THIS LINE!
  174.     // IF YOU CHANGE THIS TO MAKE YOUR CODE COMPILE,
  175.     // THE AUTOMATED MARKING WILL FAIL AND GIVE YOU ZERO
  176.  
  177.     /**
  178.      * Does nothing, but compiling with this method ensures that the method
  179.      *  headers have not been changed.
  180.      */
  181.     public void checkCompile(){
  182.         Set<Integer> places = findPlaces(board, 0);
  183.     }
  184.  
  185. }
  186.