Пример #1
0
  private boolean isValidMove(GamePacket packet) {
    char piece = packet.getGamePiece();
    int x = packet.getxPos();
    int y = packet.getyPos();

    return board.insertPos(x, y, piece);
  }
Пример #2
0
  private void startGame() {

    int turn = 0;
    gameOver = false;
    board = new Board(BOARD_SIZE, BOARD_SIZE);

    // TODO clean up this logic
    while (!gameOver) {
      try {
        PlayerHandler currentPlayer = players.get(turn % MAX_PLAYERS);
        PlayerHandler otherPlayer = players.get((turn + 1) % MAX_PLAYERS);
        System.out.println("Notifying player " + currentPlayer.piece + " to make move");
        currentPlayer.output.writeObject(true);
        System.out.println("Waiting for player " + currentPlayer.piece + " to make move");
        GamePacket packet = (GamePacket) currentPlayer.input.readObject();
        if (packet.getGamePiece() == currentPlayer.piece) {
          if (isValidMove(packet)) {
            currentPlayer.output.writeObject(false);
            currentPlayer.output.writeObject(packet);
            otherPlayer.output.writeObject(packet);
            System.out.println("Valid move");
            if (board.isWinner(packet.getGamePiece())) {
              System.out.println("Player " + currentPlayer.piece + " wins!");
              currentPlayer.output.writeObject("You Win Player " + currentPlayer.piece);
              otherPlayer.output.writeObject("You lose Player " + otherPlayer.piece);
              endGame();
            }
            board.display();
            turn++;
            if (turn == (BOARD_SIZE * BOARD_SIZE)) {
              for (PlayerHandler p : players) {
                p.output.writeObject("Tie Game!");
              }
              endGame();
            }

          } else {
            System.out.println("Invalid move");
          }
        } else {
          System.out.println("Expected a move from " + currentPlayer.piece);
        }
      } catch (IOException ioex) {
        System.err.println("Error writing to player socket");
      } catch (ClassNotFoundException cnfex) {
        System.err.println("Invalid object type found");
      }
    }
  }