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 is the code I have written till now..
import java.util.Scanner;
public class TicTacToe{
public static void main(String[] args) {
}
private char[][] board;
private char player; // 'X' or 'O'
/*
* Instantiate board to be a 3 by 3 char array of spaces.
* Set player to be 'X'.
*/
public TicTacToe() {
/*
* Step 1: create an empty board, with an initial value
* of a space (' ')
*/
}
/*
* If s represents a valid move, add the current player's symbol to the board and return true.
* Otherwise return false.
*/
public boolean play(String s) {
/* Step 2: Fill in here with your own
* play logic, and replace the return with you
* own.
*/
return false;
}
/*
* Switches the current player from X to O, or O to X.
*/
public void switchTurn() {
// Step 3: Fill in with your code to toggle between
// 'X' and 'O'
}
/*
* Returns true if the current player has won the game.
* Three in a row, column or either diagonal.
* Otherwise, return false.
*/
public boolean won() {
/* Step 5: Fill in the code for the won method. This method
* should return true if the current player has 3 in-a-row
* in any row, column or diagonal. Otherwise, return false.
*/
return false; // TODO: replace with your own return statement.
}
/*
* Returns true if there are no places left to move
*/
public boolean stalemate() {
/*
* Step 4: Fill in the code for the stalemate method. It
* should return true if there are no more places to move
* on the board. Otherwise, return false return false;
*/
return true; // replace with your own return
}
public char getPlayer() {
return player;
}
public void print() {
System.out.println();
System.out.println("\t 1 2 3");
System.out.println();
System.out.println("\tA "+board[0][0]+"|"+board[0][1]+"|"+board[0][2]);
System.out.println("\t -----");
System.out.println("\tB "+board[1][0]+"|"+board[1][1]+"|"+board[1][2]);
System.out.println("\t "+"-----");
System.out.println("\tC "+board[2][0]+"|"+board[2][1]+"|"+board[2][2]);
System.out.println();
}
/*
* Step 6: Main Method for Final Step - Delete your main method
* and uncomment this one.
* Runs the game by getting input from the user, making the
* appropriate moves, and prints who won or if it was a stalemate.
*/
/*
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()) {
//Print the game
//Prompt player for their move
//Loop while the method play does not return true when given their move.
//Body of loop should ask for a different move
//If the game is won, call break;
//Switch the turn
}
game.print();
if(game.won()){
System.out.println("Player "+game.getPlayer()+" Wins!!!!");
} else {
System.out.println("Stalemate");
}
} */
}Answer:
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
In a game of Tic Tac Toe, two players take turns making an available cell in...
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...
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...
(Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a 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 in the grid have been filled with tokens and neither player has achieved a win. Create...
Java project In a game of tic-tac-toe, two players take turns marking 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 draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create...
In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...
Please do this in Java (Java FX) Tic-Tac-Toe game (aka X's and O's) in which the player, who is "X", plays against the application, which is "O". The game should be built using the basic window-with-canvas code used for the various tutorials and course assignments. The user plays by dragging "X" tokens onto the Tic-Tac-Toe board area after which the application will automatically play by moving an "O" onto the board area. When a game is over the app should...
Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter...
You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...