コード例 #1
0
 private void showResult() {
   if (mWinner == null) {
     // No one won
     mLogger.print("This match ended in a draw.");
   } else {
     mLogger.print("Player " + mWinner.name() + " won the game!");
   }
 }
コード例 #2
0
  public void start() {
    Logger logger = new ConsoleLogger();
    logger.print("Welcome to Connect 4\n");
    logger.print("\n");

    // Set game state to STARTED
    mGameState = GameState.STARTED;
    mCurrentPlayer = 0;
    printBoard();

    // Continuously take turns while the game is incomplete
    while (mGameState != GameState.COMPLETE) {
      // If no more moves are available, set game state to complete and break out of loop
      if (mBoard.isFull()) {
        mGameState = GameState.COMPLETE;
        continue;
      }

      // Otherwise, take turn for current player
      Player currentPlayer = mPlayers.get(mCurrentPlayer);
      takeTurnForPlayer(currentPlayer);

      // Print the current state of the board
      printBoard();

      // If someone won, set the game state to complete
      mWinner = getWinner();
      if (mWinner != null) {
        mGameState = GameState.COMPLETE;
        continue;
      }

      // Otherwise, set the next player in line
      mCurrentPlayer++;
      if (mCurrentPlayer >= mNumberOfPlayers) mCurrentPlayer = 0;

      // Amateur technique for pausing application, so we can more easily see the game progress
      try {
        Thread.sleep(TURN_DELAY);
      } catch (InterruptedException e) {
        mLogger.error(e.getMessage());
        e.printStackTrace();
      }
    }

    showResult();
  }
コード例 #3
0
 private void printBoard() {
   mLogger.print(mBoard.toString());
   mLogger.print("\n");
 }