Пример #1
0
  public static void main(String[] s) throws IOException {
    char moves[] = new char[9], player = (int) Math.round(Math.random() * 1) == 1 ? 'X' : 'O';

    int move = 0;

    boolean winner = false, draw = false, init = false;

    for (int i = 0; i < moves.length; i++) moves[i] = '*';

    drawScreen(moves, player, move, init);

    init = true;

    while (!winner && !draw) {
      player = (player == 'X') ? 'O' : 'X';
      move = checkMove(moves, getInput());
      drawScreen(moves, player, move, init);
      winner = checkForWinner(moves, player);
      draw = checkForDraw(moves);
    }

    if (winner) {
      newLine(3);
      System.out.println("Winner!");
      System.out.println("Player " + player + " wins!");
    } else {
      newLine(3);
      System.out.println("Draw!");
      System.out.println("Nobody wins.");
    }
  }
Пример #2
0
 /*
     this method draws the entire game screen and
     is only to be called after the current move is
     verified as legal using checkMove()
 */
 public static void drawScreen(char[] moves, char player, int move, boolean started)
     throws IOException {
   newLine(7);
   if (started) moves[move - 1] = player;
   System.out.println("Welcome to Tic Tac Toe!");
   System.out.println();
   drawBoard(true, moves);
   newLine(2);
   drawBoard(false, moves);
   newLine(1);
   System.out.println("Using the top board as a key, press the");
   System.out.print("number corresponding to the spot you wish to take.");
   newLine(1);
   System.out.println("Player " + ((player == 'X') ? 'O' : 'X') + "'s turn");
   System.out.print("Which spot would you like to take?: ");
 }