Question

Java project In a game of tic-tac-toe, two players take turns marking an available cell in...

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 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:

Output:

--------------

| | | |

--------------

| | | |

--------------

| | | |

--------------

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

So on …

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class TicTacToe {

public static void initializeBoard(char board[][]) {

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

board[i][j] = '*';

}

public static boolean validMove(char board[][], int x, int y) {

if (x < 0 || x > 2 || y < 0 || y > 2)

return false;

if (board[x][y] == 'X' || board[x][y] == 'O')

return false;

else

return true;

}

public static void displayBoard(char board[][]) {

int t;

for (t = 0; t < 3; t++) {

System.out.print(" " + board[t][0] + " | " + board[t][1] + " | " + board[t][2]);

if (t != 2)

System.out.print("\n---|---|---\n");

}

System.out.println();

}

// public static void getInput(char board[][], char marker, int x, int y) {

//

// }

public static void markBoard(char board[][], int x, int y, char marker) {

board[x][y] = marker;

}

public static boolean gameOver(char board[][]) {

if (checkHorizontal(board, 'X')) {

printWinner('X');

return true;

}

if (checkVertical(board, 'X')) {

printWinner('X');

return true;

}

if (checkDiagonal(board, 'X')) {

printWinner('X');

return true;

}

if (checkHorizontal(board, 'O')) {

printWinner('O');

return true;

}

if (checkVertical(board, 'O')) {

printWinner('O');

return true;

}

if (checkDiagonal(board, 'O')) {

printWinner('O');

return true;

}

if (checkTie(board)) {

printWinner('T');

return true;

}

return false;

}

public static boolean checkHorizontal(char board[][], char marker) {

int i, j, count;

for (i = 0; i < 3; i++) {

count = 0;

for (j = 0; j < 3; j++)

if (board[i][j] == marker)

count++;

if (count == 3)

return true;

}

return false;

}

public static boolean checkVertical(char board[][], char marker) {

int i, j, count;

for (i = 0; i < 3; i++) {

count = 0;

for (j = 0; j < 3; j++)

if (board[j][i] == marker)

count++;

if (count == 3)

return true;

}

return false;

}

public static boolean checkDiagonal(char board[][], char marker) {

if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] == marker)

return true;

if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] == marker)

return true;

return false;

}

public static boolean checkTie(char board[][]) {

int i, j;

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

if (board[i][j] == '*')

return false;

return true;

}

public static void printWinner(char marker) {

if (marker == 'T')

System.out.println("TIE GAME!\n");

else

System.out.println("The winner is " + marker + " !!!\n");

}

public static void main(String[] args) {

// TODO Auto-generated method stub

// srand(time(0));

// int start=rand()%2;

char board[][] = new char[3][3];

char exit;

char f, s;

int x = 0, y = 0;

f = 'X';

s = 'O';

initializeBoard(board);

displayBoard(board);

Scanner sc = new Scanner(System.in);

while (true) {

// getInput(board, f, x, y);

for (;;) {

System.out.print("Player " + f + " Enter a Row and Column (between 1-3): ");

x = sc.nextInt();

y = sc.nextInt();

x--;

y--;

if (validMove(board, x, y))

break;

System.out.print("Invalid Move: Please Try Again\n\n");

}

markBoard(board, x, y, f);

displayBoard(board);

if (gameOver(board))

break;

for (;;) {

System.out.print("Player " + f + " Enter a Row and Column (between 1-3): ");

x = sc.nextInt();

y = sc.nextInt();

x--;

y--;

if (validMove(board, x, y))

break;

System.out.print("Invalid Move: Please Try Again\n\n");

}

markBoard(board, x, y, s);

displayBoard(board);

if (gameOver(board))

break;

}

}

}
==============================================
SEE OUTPUT


Thanks, PLEASE COMMENT if there is any concern. PLEASE UPVOTE

Add a comment
Know the answer?
Add Answer to:
Java project In a game of tic-tac-toe, two players take turns marking an available cell in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an...

    (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...

  • 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...

    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 traditional Tic Tac Toe game, two players take turns to mark grids with noughts and...

    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...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    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...

  • Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...

    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...

  • Write a program to Simulate a game of tic tac toe in c#

    Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1)    ...

  • I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program...

    I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional String 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: a. Displays the contents of the board array. b. Allows player 1 to select a location...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players...

    Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players to play a game of tic-tac-toc. 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 Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT