In: Computer Science
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 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 stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win.
Write a program that emulates a Tic Tac Toe game.
When you are done, a typical session will look like this:
Welcome to tic-tac-toe. Enter coordinates for your move following the X and O prompts. 1 2 3 A | | ----- B | | ----- C | | X:A2 1 2 3 A |X| ----- B | | ----- C | | O:B3 1 2 3 A |X| ----- B | |O ----- C | |
And so on. Illegal moves will prompt the user again for a new move. A win or a stalemate will be announced, mentioning the winning side if any. The program will terminate whenever a single game is complete.
This file has the general framework of the TicTacToe class. An object of this class will represent a tic-tac-toe "board". The board will be represented internally by a two dimensional array of characters (3 x 3), and by a character indicating who's turn it is ('X' or 'O'). These are stored in the class instance variables as follows.
private char[][] board; private char player; // 'X' or 'O'
You will need to define the following methods:
1. A constructor:
public TicTacToe()
to create an empty board, with initial value of a space (' ')
2. play method
public play(String position)
if position represents a valid move (e.g., A1, B3), add the current player's symbol to the board and return true. Otherwise, return false.
3. switchTurn method
public void switchTurn()
switches the current player from X to O, or vice versa.
4. won method
public boolean won()
Returns true if the current player has filled three in a row, column or either diagonal. Otherwise, return false.
5. stalemate method
public boolean stalemate()
Returns true if there are no places left to move;
6. printBoard method
public void printBoard()
prints the current board
7. Use the following test code in your main method to create a TicTacToe object and print it using the printBoard method given, so as to test your code. Your printBoard method should produce the first board in the example above.
public static void main(String[] args) { Scanner in = new Scanner(System.in); TicTacToe game = new TicTacToe(); System.out.println("Welcome to Tic-tac-toe"); System.out.println("Enter coordinates for your move following the X and O prompts"); while(!game.stalemate()) { game.printBoard(); System.out.print(game.getPlayer() + ":"); //Loop while the method play does not return true when given their move. //Body of loop should ask for a different move while(!game.play(in.next())) { System.out.println("Illegal move. Enter your move."); System.out.print(game.getPlayer() + ":"); } //If the game is won, call break; if(game.won()) break; game.printBoard(); //Switch the turn game.switchTurn(); } game.printBoard(); if(game.won()) { System.out.println("Player "+game.getPlayer()+" Wins!!!!"); } else { System.out.println("Stalemate"); } }
Please find the programs below:
TicTacToe.java, copy and paste and name the file TicTacToe.java:
public class TicTacToe {
private int[][] board = new int[3][3];
public static final int X = 1, O = -1;
public static final int EMPTY = 0;
public int player = X;
int remCells = 3 * 3;
public void printBoard() {
StringBuilder s = new
StringBuilder("1 2 3\n");
for (int i = 0; i < 3; i++)
{
int c = 65 +
i;
s.append(Character.toString((char) c));
for (int j = 0;
j < 3; j++) {
switch (board[i][j]) {
case X:
s.append(" X ");
break;
case O:
s.append(" O ");
break;
case EMPTY:
s.append(" ");
break;
}
if (j < 2) {
s.append("|");
}
}
if (i < 2)
{
s.append("\n-------------\n");
}
}
System.out.println(s.toString());
}
public boolean staleMate() {
return remCells == 0;
}
public boolean play(String input) {
char row = input.charAt(0);
int col = input.charAt(1) -
'0';
if (row < 'A' || row > 'C' ||
col < 1 || col > 3) {
return
false;
}
if (board[row - 'A'][col - 1] !=
EMPTY) {
return
false;
}
board[row - 'A'][col - 1] =
player;
remCells--;
return true;
}
public String getPlayer() {
return player == X ? "X" :
"O";
}
public boolean won() {
if ((board[0][0] + board[0][1] +
board[0][2] == X * 3) || (board[1][0] + board[1][1] + board[1][2]
== X * 3)
|| (board[2][0] + board[2][1] + board[2][2] == X
* 3)
|| (board[0][0] + board[1][0] + board[2][0] == X
* 3)
|| (board[0][1] + board[1][1] + board[2][1] == X
* 3)
|| (board[0][2] + board[1][2] + board[2][2] == X
* 3)
|| (board[0][0] + board[1][1] + board[2][2] == X
* 3)
|| (board[2][0] + board[1][1] + board[0][2] == X
* 3)) {
player =
X;
return
true;
}
if ((board[0][0] + board[0][1] +
board[0][2] == O * 3) || (board[1][0] + board[1][1] + board[1][2]
== O * 3)
|| (board[2][0] + board[2][1] + board[2][2] == O
* 3)
|| (board[0][0] + board[1][0] + board[2][0] == O
* 3)
|| (board[0][1] + board[1][1] + board[2][1] == O
* 3)
|| (board[0][2] + board[1][2] + board[2][2] == O
* 3)
|| (board[0][0] + board[1][1] + board[2][2] == O
* 3)
|| (board[2][0] + board[1][1] + board[0][2] == O
* 3)) {
player =
O;
return
true;
}
return false;
}
public void switchTurn() {
player = -player;
}
}
I have written the driver program separately:
TicTacToeDriver.java, copy and paste the program below and name the file as TicTacToeDriver.java
import java.util.Scanner;
public class TicTacToeDriver {
public static void main(String[] args) {
Scanner in = new
Scanner(System.in);
TicTacToe game = new
TicTacToe();
System.out.println("Welcome to
Tic-tac-toe");
System.out.println("Enter
coordinates for your move following the X and O prompts \n");
while (!game.staleMate())
{
game.printBoard();
System.out.print(game.getPlayer() + ":");
// Loop while
the method play does not return true when given their
// move.
// Body of loop
should ask for a different move
while
(!game.play(in.next())) {
System.out.println("Illegal move. Enter your
move.");
System.out.print(game.getPlayer() + ":");
}
// If the game
is won, call break;
if
(game.won())
break;
//
game.printBoard();
// Switch the
turn
game.switchTurn();
}
game.printBoard();
if (game.won()) {
System.out.println("Player " + game.getPlayer() + "
Wins!!!!");
} else {
System.out.println("Stalemate");
}
in.close();
}
}
Please find the program screenshots with the output:
TicTacToe.java please follow the line numbers:
TicTacToeDriver.java
Output:
Please provide a feedback by clicking on the feedback button
1 public class TicTacToe { private int[] [] board = new int[3][3]; public boolean staleMate = false; public static final int X - 1, 0 - -1; public static final int EMPTY = 0; public int player = x; 3 \n"); public void printBoard() { StringBuilder s = new StringBuilder(" 1 2 for (int i = 0; i < 3; i++) { int c = 65 + i; s.append(Character.toString((char) c)); for (int j = 0; j < 3; j++) { switch (board[i][j]) { case X: s.append(" X "); break; case 0: s.append(" O "); break; case EMPTY: s.append(" "); break; if (i < 2) { S.append("i"); if (i < 2) { S.append("\n-- ---\n"); System.out.println(s.toString(); 370 public boolean stalemate() { return staleMate; 38 40 410 public boolean play(String input) { char row = input.charAt(0); int col = input.charAt(1) - '0'; if (row < 'A' || row > 'C' || col < 1 || col > 3) { return false; if (board[row - 'A'][col - 1] != EMPTY) { return false; board[row - 'A'][col - 1] = player; return true;
board[row - 'A'][col - 1] - player; return true; public String getPlayer() { return player == X ? "X" : "O": public boolean won() { if (Cboard[0][0] + board[0][1] + board[0][2] == x + 3) 11 (board[1][0] + board[1][1] + board[1][2] == x + 3) 11 (board[2][0] + board[2][1] + board[2][2] -- X * 3) 11 (board[0][0] + board[1][0] + board[2][0] -- X * 3) II (board[0][1] + board[1][1] + board[2][1] == X * 3) II (board[0][2] + board[1][2] + board[2][2] == X * 3) II (board[0][0] + board[1][1] + board[2][2] == X * 3) II (board[2][0] + board[1][1] + board[0][2] == X * 3)) { player = x; return true; if (Cboard[0][0] + board[0][1] + board[0][2] -- 0 * 3) 11 (board[1][%] + board[1][1] + board[1][2] -- 0 * 3) 11 (board[2][0] + board[2][1] + board[2][2] =- 0 * 3) 11 (board[0][0] + board[1][0] + board[2][0] == 0 * 3) II (board[0][1] + board[1][1] + board[2][1] == 0 * 3) II (board[0][2] + board[1][2] + board[2][2] == 0 * 3) II (board[0][0] + board[1][1] + board[2][2] == 0 * 3) II (board[2][0] + board[1][1] + board[0][2] == 0 * 3)) { player = 0; return true; return false; 830 84 85 86 public void switchTurn() { player = -player; } 87
1 import java.util.Scanner; 3 public class TicTacToeDriver { public static void main(String[] args) { 40 Scanner in = new Scanner(System.in); TicTacToe game = new TicTacToe(); System.out.println("Welcome to Tic-tac-toe"); System.out.println("Enter coordinates for your move following the X and O prompts \n"); while (!game. stalemate() { game.printBoard; System.out.print(game.getPlayer() + ":"); // Loop while the method play does not return true when given their // move. // Body of loop should ask for a different move while (!game.play(in.next()) { System.out.println("Illegal move. Enter your move."); System.out.print(game.getPlayer() + ":"); // If the game is won, call break; if (game.won ) break; // game.printBoardo; // Switch the turn game. switchTurno; game.printBoard(); if (game.won() { System.out.println("Player " + game.getPlayer() + " Wins!!!!"); } else { System.out.println("Stalemate"); in.close(); 40 }
Welcome to Tic-tac-toe Enter coordinates for your move following the X and o prompts ovo AWN 1 2 3 ALL ------------- BIT ----- X:A1 1 2 3 AXIL ----------- BIT 17 18 0:01 1 2 AXTI 3 19 BOLT NNN X:01 1 2 AXIL 3 ---- NNNNM BOLT CXIL 0:B2 1 2 AXIL 3 33 34 35 36 BOOT --- CXII X:02 1 2 AXTI 3 BO0 CXXL 0:B3 1 2 AXIL 3 BO|0|0 49 50 51 52 53 - CXXI Player O Wins!!!! Line 53 Column 1
1 public class TicTacToe { private int[] [] board = new int[3][3]; public boolean staleMate = false; public static final int X - 1, 0 - -1; public static final int EMPTY = 0; public int player = x; 3 \n"); public void printBoard() { StringBuilder s = new StringBuilder(" 1 2 for (int i = 0; i < 3; i++) { int c = 65 + i; s.append(Character.toString((char) c)); for (int j = 0; j < 3; j++) { switch (board[i][j]) { case X: s.append(" X "); break; case 0: s.append(" O "); break; case EMPTY: s.append(" "); break; if (i < 2) { S.append("i"); if (i < 2) { S.append("\n-- ---\n"); System.out.println(s.toString(); 370 public boolean stalemate() { return staleMate; 38 40 410 public boolean play(String input) { char row = input.charAt(0); int col = input.charAt(1) - '0'; if (row < 'A' || row > 'C' || col < 1 || col > 3) { return false; if (board[row - 'A'][col - 1] != EMPTY) { return false; board[row - 'A'][col - 1] = player; return true;
We were unable to transcribe this image
We were unable to transcribe this image
Welcome to Tic-tac-toe Enter coordinates for your move following the X and o prompts ovo AWN 1 2 3 ALL ------------- BIT ----- X:A1 1 2 3 AXIL ----------- BIT 17 18 0:01 1 2 AXTI 3 19 BOLT NNN X:01 1 2 AXIL 3 ---- NNNNM BOLT CXIL 0:B2 1 2 AXIL 3 33 34 35 36 BOOT --- CXII X:02 1 2 AXTI 3 BO0 CXXL 0:B3 1 2 AXIL 3 BO|0|0 49 50 51 52 53 - CXXI Player O Wins!!!! Line 53 Column 1