Using two dimensional arrays, create a program to play game of Tic Tac Toe. Your
ID: 3822490 • Letter: U
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 following skeleton and complete the given methods.
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);
}
}
}
public static void playerPlays(char[][] board)
{
// Prompt the user for row & column index. Continue asking
// until an empty cell is selected. set the cell to 'O'
}
public static boolean checkWin(char[][] board,char ch)
{
// Check by row, column, and diagonals
}
public static boolean checkTie(char[][] board)
{
// check for tie. If there no empty cells, then it is a tie
}
public static void displayBoard(char[][] board)
{
// Display the board
}
public static void computerPlay(char[][]board)
{
// Continue generating random values for row and col until an
// empty cell selected. Set the cell to 'X'
}
}
Explanation / Answer
public class TicTacToeGame
{
private String[][]board;
static String K = "K";
static String M = "M";
public TicTacToe()
{
board = new String[4][4];
}
public void printBoard()
{
System.out.println();
for (int x = 0; x < board.length; x++)
{
for (int y = 0; y < board[x].length; y++)
{
if (board[x][y] == null)
{
System.out.print("_");
}
else
{
System.out.print(board[x][y]);
}
if (y< 2)
{
System.out.print("|");
}
else
{
System.out.println();
}
}
}
System.out.println();
}
public Boolean checkWinner(String play)
{
int playInRow = 0;
int playD1 = 0;
int playD2 = 0;
int[] playInColumn = new int[board[0].length];
for (int x = 0; x < board.length; x++)
{
playInRow = 0;
for (int y = 0; y < board[x].length; y++)
{
if (null == board[x][y])
{
continue;
}
if (board[x][y].equals(play))
{
playInRow++;
playInColumn[y]++;
if (x == y)
{
playD1++;
}
else if (2 == x + y)
{
playD2++;
}
}
}
if (playInRow == 3)
{
return true;
}
}
if (3 == playD1 || 3 == playD2)
{
return true;
}
for (int x = 0; x < playInColumn.length; x++)
{
if (playInColumn[x] == 3)
{
return true;
}
}
return false;
}
public void makeMove(Scanner stdin, String play)
{
int row;
int colum;
Boolean goodInput = false;
while(!goodInput)
{
row = -1;
colum = -1;
System.out.println ("Enter coordinates to play your " + play);
if (stdin.hasNextInt())
{
r = stdin.nextInt();
}
if (stdin.hasNextInt())
{
c = stdin.nextInt();
}
else
{
stdin.nextLine();
System.out.println("Both inputs must be integers between 0 and 2.");
continue;
}
if ((row < 0) || (row > 2) || (colum < 0) || (colum
> 2)) {
System.out.println("Both inputs should be integers between 0 and 2.");
continue;
}
else if (board[row][colum] != null )
{
System.out.println("That location occupied");
continue;
}
else
{
board[row][colum] = play;
return;
}
}
return;
}
public static void main(String[] args)
{
TicTacToeGame tttg = new TicTacToeGame();
Scanner stdin = new Scanner(System.in);
int moves = 0;
System.out.println("Let's play TicTacToe -- K goes first");
ttt.printBoard();
while (moves < 9)
{
tttg.makeMove(stdin, tttg.K);
moves++;
if (moves > 4)
{
if (ttt.checkWinner(K))
{
System.out.println(K + " You Win!!!");
break;
}
}
tttg.printBoard();
tttg.makeMove(stdin, tttg.M);
moves++;
if (moves > 4)
{
if (tttg.checkWinner(M))
{
System.out.println(M + " You Win!!!");
break;
}
}
tttg.printBoard();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.