Question
Using two dimensional arrays, create a program to play game of Tic Tac Toe. Your program enables you to play against the computer. Use the
Using two dimensional arrays, create a program to play game of Tic Tac Toe. Your program enables you to play against the computer.
Use the following skeleton:
import java.util.*;
public class TicTacToe
{
static Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
char[][] board = new char[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;++j)
board[i][j]=' ';
while (true)
{
computerPlay(board);
displayBoard(board);
if(checkWin(board,'X'))
{
System.out.println("COmputer Wins");
System.exit(0);
}
if(checkTie(board))
{
System.out.println("Tie game");
System.exit(0);
}
playerPlays(board);
displayBoard(board);
if(checkWin(board,'O'))
{
System.out.println("Player Wins");
System.exit(0);
}
if(checkTie(board))
{
System.out.println("Tie game");
System.exit(0);
}
}
}
// Prompt the user for row & column index. Continue asking
// until an empty cell is selected. set the cell to 'O'
public static void playerPlays(char[][] board)
{
}
// Check by row, column, and diagonals
public static boolean checkWin(char[][] board,char ch)
{
}
// check for tie. If there no empty cells, then it is a tie
public static boolean checkTie(char[][] board)
{
}
// Display the board
public static void displayBoard(char[][] board)
{
}
// Continue generating random values for row and col until an
// empty cell selected. Set the cell to 'X'
public static void computerPlay(char[][]board)
{
}
}
Sample run:
----jGRASP exec: java TicTacToe
_______
| | | |
-------
|X| | |
-------
| | | |
-------
Enter row and col:
0 0
_______
|O| | |
-------
|X| | |
-------
| | | |
-------
_______
|O|X| |
-------
|X| | |
-------
| | | |
-------
Enter row and col:
1 0
Enter row and col:
1 1
_______
|O|X| |
-------
|X|O| |
-------
| | | |
-------
_______
|O|X| |
-------
|X|O|X|
-------
| | | |
-------
Enter row and col:
2 2
_______
|O|X| |
-------
|X|O|X|
-------
| | |O|
-------
Player Wins
please write out the code completely even the given parts thanks
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started