Esempio n. 1
0
  @Test
  public void playGame() {
    Board b = new Board(false);
    b.toString(); // just need to know, that it works
    // wb
    // bw
    assertEquals("Black", b.getState(3, 4), STATE.BLACK);
    assertEquals("Black", b.getState(4, 3), STATE.BLACK);
    assertEquals("White", b.getState(3, 3), STATE.WHITE);
    assertEquals("White", b.getState(4, 4), STATE.WHITE);

    b.markNextMoves(); // mark available moves

    assertEquals("Black", b.getState(3, 4), STATE.BLACK);
    assertEquals("Black", b.getState(4, 3), STATE.BLACK);
    assertEquals("White", b.getState(3, 3), STATE.WHITE);
    assertEquals("White", b.getState(4, 4), STATE.WHITE);

    // new possible moves all Black of Course
    assertEquals("Selectable", b.getState(2, 3), STATE.SELECTABLE);
    assertEquals("Selectable", b.getState(4, 5), STATE.SELECTABLE);
    assertEquals("Selectable", b.getState(3, 2), STATE.SELECTABLE);
    assertEquals("Selectable", b.getState(5, 4), STATE.SELECTABLE);

    assertEquals(true, b.isNextPlayerBlack());

    // make Illegal Move
    assertFalse(b.makeMove(1, 1));

    // nothing has changed
    assertEquals("Black", b.getState(3, 4), STATE.BLACK);
    assertEquals("Black", b.getState(4, 3), STATE.BLACK);
    assertEquals("White", b.getState(3, 3), STATE.WHITE);
    assertEquals("White", b.getState(4, 4), STATE.WHITE);

    // new possible moves all Black of Course
    assertEquals("Selectable", b.getState(2, 3), STATE.SELECTABLE);
    assertEquals("Selectable", b.getState(4, 5), STATE.SELECTABLE);
    assertEquals("Selectable", b.getState(3, 2), STATE.SELECTABLE);
    assertEquals("Selectable", b.getState(5, 4), STATE.SELECTABLE);

    // make Legal Move
    assertTrue(b.makeMove(2, 3));
    assertEquals(1, b.getWhiteStones());
    assertEquals(4, b.getBlackStones());

    assertEquals(false, b.isNextPlayerBlack()); // white has next move
    // nothing has changed
    assertEquals("Black", b.getState(2, 3), STATE.BLACK); // move we made
    assertEquals("Black", b.getState(3, 4), STATE.BLACK);
    assertEquals("Black", b.getState(4, 3), STATE.BLACK);
    assertEquals("White", b.getState(3, 3), STATE.BLACK);
    assertEquals("White", b.getState(4, 4), STATE.WHITE);

    b.markNextMoves();
    b.toString(); // just need to know, that it works

    // try this move again
    assertFalse(b.makeMove(2, 3));
  }
Esempio n. 2
0
  public static void main(String[] args) {
    int[][] blocks = {
      {1, 2, 3},
      {4, 5, 6},
      {7, 8, 0}
    };

    Board b = new Board(blocks);
    // b.isGoal();
    // System.out.println(b.isGoal());

    // System.out.println(b.dimension());
    System.out.println(b.toString());

    // System.out.println( b.calcManhattan(0,0) );
    // System.out.println( b.calcManhattan(0,1) );

    for (Board n : b.neighbors()) {
      System.out.println(n.toString());
      System.out.println();
    }

    Board twin = b.twin();

    System.out.println("\n TWIN: \n" + twin.toString());
  }
  /**
   * Starts a game of TicTacToe
   *
   * @return
   */
  private Player startGame() {

    display.out(board.toString());
    Player winningPlayer = new Player(0, R.none);
    Player currentPlayer = player1;
    boolean isPlaying = true;

    while (isPlaying) {

      if (!currentPlayer.isAI()) {
        board.addPlayerAt(playerMove(), currentPlayer);
      } else {
        try {
          Thread.sleep(1000);
        } catch (Exception e) {
          System.out.print(e.getMessage());
        }
        board.addPlayerAt(compMove(), currentPlayer);
      }
      if (board.testVictory()) {
        isPlaying = false;
        winningPlayer = currentPlayer;
        display.out(String.format(R.winnerIs, winningPlayer.getName()));
      } else if (testDraw()) {
        isPlaying = false;
        display.out(R.draw);
      } else {
        currentPlayer = (currentPlayer == player1) ? player2 : player1;
      }
      display.out(board.toString());
    }
    return winningPlayer;
  }
Esempio n. 4
0
 /**
  * handler for client input
  *
  * <p>make requested mutations on game state if applicable, then return appropriate message to the
  * user.
  *
  * @param input
  * @return if input is a valid user input string according to the problem specification the String
  *     that will be sent to the user or "bye" if the input is "bye". Otherwise return null
  */
 private synchronized String handleRequest(String input) {
   String regex = "(look)|(dig \\d+ \\d+)|(flag \\d+ \\d+)|" + "(deflag \\d+ \\d+)|(help)|(bye)";
   if (!input.matches(regex)) {
     // invalid input
     return null;
   }
   String[] tokens = input.split(" ");
   if (tokens[0].equals("look")) {
     // no modifications needed, so do nothing and we'll return the board at the bottom of this
     // method
   } else if (tokens[0].equals("help")) {
     return HELP_MESSAGE;
   } else if (tokens[0].equals("bye")) {
     return BYE; // if thread sees this return value, it will end itself
   } else {
     int x = Integer.parseInt(tokens[1]);
     int y = Integer.parseInt(tokens[2]);
     if (tokens[0].equals("dig")) {
       boolean isBoom = board.dig(x, y);
       if (isBoom)
         return BOOM; // if thread sees this return value, it will cut the connection iff debug is
                      // false
     } else if (tokens[0].equals("flag")) {
       board.setFlaggedValue(x, y, true);
     } else if (tokens[0].equals("deflag")) {
       board.setFlaggedValue(x, y, false);
     }
   }
   // if we get here, then we want to return a BOARD message
   return board.toString();
 }
Esempio n. 5
0
 @Test
 public void shouldWorkToString() {
   assertEquals(
       "Board:\n"
           +
           /*012345678*/
           /*0*/ "☼☼☼☼☼☼☼☼☼\n"
           +
           /*1*/ "☼1 ♣   ♠☼\n"
           +
           /*2*/ "☼#2  &  ☼\n"
           +
           /*3*/ "☼# 3 ♣ ♠☼\n"
           +
           /*4*/ "☼☺  4   ☼\n"
           +
           /*5*/ "☼   ♥ H☻☼\n"
           +
           /*6*/ "☼x H ҉҉҉☼\n"
           +
           /*7*/ "☼& &    ☼\n"
           +
           /*8*/ "☼☼☼☼☼☼☼☼☼\n"
           + "\n"
           + "Bomberman at: [1,4]\n"
           + "Other bombermans at: [[4,5], [7,1], [7,3], [3,1], [5,3]]\n"
           + "Meat choppers at: [[5,2], [1,7], [3,7]]\n"
           + "Destroy walls at: [[1,2], [1,3]]\n"
           + "Bombs at: [[1,1], [2,2], [3,3], [4,4], [7,5]]\n"
           + "Blasts: [[5,6], [6,6], [7,6]]\n"
           + "Expected blasts at: [[1,1], [2,1], [1,2], [2,2], [3,2], [2,3], [3,3], [4,3], [3,4], [4,4], [5,4], [4,5], "
           + "[7,5], [6,5], [7,4], [7,6], [7,1], [6,1], [7,2], [7,3], [6,3]]",
       board.toString());
 }
Esempio n. 6
0
  /*====================================
  swipe right: swipes right, combining necessary tiles
  returns true if a swipe was able to happen and did happen,
  false if you cannot swipe right
  ====================================*/
  public boolean swipeR() {
    // to compare to new one after a swipe
    String old = _board.toString();

    // for each row
    for (int r = 0; r < 4; r++) {
      // first to combine similar tiles
      // for the first 3 tiles
      for (int c = 3; c > 0; c--) {
        // if you find a meaningful element . . .
        if (!_board.get(r, c).equals(0)) {
          // for the rest of the tiles . . .
          for (int n = c - 1; n >= 0; n--) {
            if (_board.get(r, c).equals(_board.get(r, n))) {
              _board.set(r, c, 2 * (Integer) _board.get(r, c));
              _board.set(r, n, 0);
              break;
            } else if (!_board.get(r, n).equals(0)) break;
          }
        }
      }
      // then to get rid of 0s and move everything over
      // for the first 3 tiles again
      for (int c = 3; c > 0; c--) {
        // if you find a 0
        if (_board.get(r, c).equals(0)) {
          // for the rest of the tiles . . .
          for (int n = c - 1; n >= 0; n--) {
            if (!_board.get(r, n).equals(0)) {
              _board.set(r, c, _board.get(r, n));
              _board.set(r, n, 0);
              break;
            }
          }
        }
      }
    }
    if (old.equals(_board.toString())) return false;
    return true;
  }
  /**
   * main for unit tests
   *
   * @param args
   */
  public static void main(String[] args) {
    In in = new In(args[0]);
    int N = in.readInt();
    int[][] blocks = new int[N][N];
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        blocks[i][j] = in.readInt();
      }
    }
    Board initial = new Board(blocks);

    // test
    StdOut.println(initial.dimension());
    StdOut.println(initial.hamming());
    StdOut.println(initial.manhattan());
    StdOut.println(initial.isGoal());
    StdOut.println(initial.toString());
    StdOut.println(initial.twin().toString());
    for (Board b : initial.neighbors()) {
      StdOut.println(b.toString());
    }
  }
Esempio n. 8
0
  public static void main(String[] args) {
    int[][] data = Io.openFile();

    Board game = new Board(data);

    try {
      game.solvePuzzle(0, 0);
    } catch (Exception victory) {
      System.out.println("Success, A solution has been found - results in output.txt");
      String solution = game.toString();
      System.out.println(solution);
      Io.writeString(solution);
      System.exit(0);
    }
    System.out.println("Sorry, No Solution.");
  }
Esempio n. 9
0
 /** Test of toString method, of class Board. */
 @Test
 public void testToStringBoard() {
   System.out.println("Board.toString");
   Board instance = new Board();
   String expResult =
       "===\n"
           + "  - b b b b b b -\n"
           + "  w - - - - - - w\n"
           + "  w - - - - - - w\n"
           + "  w - - - - - - w\n"
           + "  w - - - - - - w\n"
           + "  w - - - - - - w\n"
           + "  w - - - - - - w\n"
           + "  - b b b b b b -\n"
           + "Next move: B\n"
           + "Moves: 0\n"
           + "===";
   String result = instance.toString();
   assertEquals(expResult, result);
 }
Esempio n. 10
0
  public static void main(String... args) {
    boolean done = false;
    Scanner scanner = new Scanner(System.in);
    Board board = new Board();
    Board.BoardValue current = Board.BoardValue.ME;
    do {
      System.out.println("Current Board State (" + current.name() + "'s Turn):");
      System.out.println(board.toString());
      if (current == Board.BoardValue.ME) {
        System.out.print("Choose a column: ");
        int column = scanner.nextInt();
        Board.setInColumn(board, column, Board.BoardValue.ME);
        current = Board.BoardValue.AI;
      } else {
        board = AI.getAIChoice(board);
        current = Board.BoardValue.ME;
      }
    } while (!done);

    scanner.close();
  }
Esempio n. 11
0
  public static void main(String[] args) {
    // for each command-line argument

    // read in the board specified in the filename
    String filename = args[0];
    In in = new In(filename);
    //        StdOut.println(filename);
    int N = in.readInt();
    //        StdOut.println(N);
    int[][] tiles = new int[N][N];
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        tiles[i][j] = in.readInt();
      }
    }

    // solve the slider puzzle
    Board initial = new Board(tiles);
    StdOut.println(initial.toString());
    StdOut.println(initial.hamming());
    StdOut.println(initial.manhattan());
  } // unit tests (not graded)
Esempio n. 12
0
  public void startMatch() {

    if (antherGame(R.StartQuestion) == n) {
      return;
    }
    player1 = new Player(1, R.player1Name, false, R.player1Mark);
    player2 = new Player(2, R.player2Name, true, R.player2Mark);
    board = new Board(player1, player2);

    display.out(R.instructions);
    display.out(R.numPad);
    display.out(R.begin);

    do {
      board.reset();
      Player winningPlayer = startGame();
      winningPlayer.addWin();
      display.score(winningPlayer, player1, player2);
      display.out(board.toString());
    } while (antherGame(R.startAnotherGame) != R.n);

    display.out(R.gameOver);
    ;
  }
Esempio n. 13
0
 /** Prints the game situation. */
 private void update() {
   System.out.println("\ncurrent game situation: \n\n" + board.toString() + "\n");
 }
Esempio n. 14
0
 public String toString() {
   return current.toString();
 }
Esempio n. 15
0
 @Before
 public void setUp() throws Exception {
   b.addBall(0, 0);
   b.addBall(0, 2);
   System.out.println(b.toString());
 }
Esempio n. 16
0
 @After
 public void tearDown() throws Exception {
   System.out.println(b.toString());
 }
Esempio n. 17
0
 /**
  * Prints the board.
  *
  * @return String user-friendly representation of the board.
  */
 public String printBoard() {
   return board.toString();
 }