Sunday 28 August 2016

Chapter 8 Exercise 9, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

***8.9 (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a 3 * 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create a program for playing tic-tac-toe. The program prompts two players to enter an X token and O token alternately. Whenever a token is entered, the program redisplays the board on the console and determines the status of the game (win, draw, or continue). Here is a sample run:

|  |  |  |
|  |  |  |
|  |  |  |
Enter a row (0, 1, or 2) for player X: 1
Enter a column (0, 1, or 2) for player X: 1
|  |  |  |
|  |X|  |
|   |  |  |
Enter a row (0, 1, or 2) for player O: 1
Enter a column (0, 1, or 2) for player O: 2
|  |  |  |
|  |X|O|
|  |  |  |



import java.util.Scanner;
 
 
 
/* Strategies:
 * 1. Create 3x3 array of character to represent the board
 * 2. Function to display the board
 * 3. Function to check if there is a winner. Return winner, X or O. Return 'N' if there is no winner. Return 'F' if the board is full without any winner
 */
public class ProgrammingEx8_9 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  char[][] board = new char[3][3];
  char player = 'X';
  char status;
  int row, col;
 
  displayBoard(board);
 
  while ((status = checkBoard(board)) == 'N') {
 
   System.out.print("Enter a row (0, 1, or 2) for player " + player
     + ":");
   row = input.nextInt();
 
   System.out.print("Enter a column (0, 1, or 2) for player " + player
     + ":");
   col = input.nextInt();
 
   if (board[col][row] != '\u0000') {
    System.out.println("The row and column is occupied.");
    continue;
   } else {
    board[col][row] = player;
    if (player == 'O') {
     player = 'X';
    } else {
     player = 'O';
    }
 
   }
    
   displayBoard(board);
 
  }
   
  if(status == 'F')
  System.out.println("It is a draw.");
  else
  System.out.println("The winner is " + status);
   
 
 }
 
 public static char checkBoard(char board[][]) {
  char status = 'F';
 
  // Check if the board is full
  outer: for (int i = 0; i < board.length; i++) {
   for (int j = 0; j < board[0].length; j++) {
    if (board[i][j] == '\u0000') {
     status = 'N';
     break outer;
    }
   }
 
  }
 
  // Check column 1
  if (board[0][0] == board[0][1] && board[0][0] == board[0][2]
    && board[0][0] != '\u0000') {
   status = board[0][0];
  }
 
  // Check column 2
  if (board[1][0] == board[1][1] && board[1][0] == board[1][2]
    && board[1][0] != '\u0000') {
   status = board[1][0];
  }
 
  // Check column 3
  if (board[2][0] == board[2][1] && board[2][0] == board[2][2]
    && board[2][0] != '\u0000') {
   status = board[2][0];
  }
 
  // Check row 1
  if (board[0][0] == board[1][0] && board[0][0] == board[2][0]
    && board[0][0] != '\u0000') {
   status = board[0][0];
  }
 
  // Check row 2
  if (board[0][1] == board[1][1] && board[0][1] == board[2][1]
    && board[0][1] != '\u0000') {
   status = board[0][1];
  }
 
  // Check row 3
  if (board[0][2] == board[1][2] && board[0][2] == board[2][2]
    && board[0][2] != '\u0000') {
   status = board[0][2];
  }
 
  // Check diagonal 1
  if (board[0][0] == board[1][1] && board[0][0] == board[2][2]
    && board[0][0] != '\u0000') {
   status = board[0][0];
  }
 
  // Check diagonal 2
  if (board[2][0] == board[1][1] && board[2][0] == board[0][2]
    && board[2][0] != '\u0000') {
   status = board[0][0];
  }
 
  return status;
 
 }
 
 public static void displayBoard(char board[][]) {
 
  for (int i = 0; i < board.length; i++) {
   for (int j = 0; j < board[1].length; j++) {
    System.out.print("|" + board[j][i]);
   }
 
   System.out.println("|");
  }
 }
 
}

No comments :

Post a Comment