Java Language only! You will create a two person Tic-Tac-Toe game using a two di
ID: 3866428 • Letter: J
Question
Java Language only!
You will create a two person Tic-Tac-Toe game using a two dimensional array to store the data. The program will prompt the X and O players to enter what square they want by typing in the horizontal and vertical position of the square. For example:
What square player X? 0 2
| |
-----
| |
-----
X| |
What square player O? 1 1
| |
-----
|O|
-----
X| |
And so on.
If the player inputs incorrect data then the program should prompt them to re-enter the data.
EXTRA CREDIT (10 points):
Make it so your program knows when a player has won (and ends the game with an announcement).
Explanation / Answer
Explanation::
Code::
import java.io.*;
class TicTacToe{
public static void main(String args[])throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
/*
Creating 3x3 2D array of string AND initializing it to empty string "".
*/
String state[][]=new String[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
state[i][j]="";
}
}
/*currentPlayer is used to indicate player "O" is playing
or player "X" is playing currently*/
String currentPlayer;
boolean matchDraw=true;
do{
/*
This loop is created just to choose whether X starts or O starts.
And prompts user if invalid data is entered.
*/
System.out.print("Enter current player(O/X)::");
currentPlayer=br.readLine();
if(currentPlayer.equals("O") || currentPlayer.equals("X")){
break;
}
System.out.println("Enter Valid Player.");
}while(true);
while(allCheck(state)){
/*
This while loop runs until all the boxes are filled or
either of the player wins the game!!
*/
int x,y;
do{
/*
This do while loop checks if entered two numbers are valid
or not(out of range).
*/
System.out.print("What square player "+currentPlayer+"?");
String s[]=br.readLine().split(" ");
x=Integer.parseInt(s[1]);
y=Integer.parseInt(s[0]);
if(x>=0&&x<=2&&y>=0&&y<=2){
if(!state[x][y].equals("")){
//if both numbers are valid but the cell is
//already marked then this condition is executed.
System.out.println("This cell is taken!!");
}else{
break;
}
}else{
System.out.println("Enter Valid cell co-ordinates.");
}
}while(true);
state[x][y]=currentPlayer; // fills the cell with currentPlayer
printState(state); // prints the current state of game
if(win(state,currentPlayer,x,y)){
/*This function checks if currentPlayer is winning*/
matchDraw=false;
System.out.println("Player "+currentPlayer+" won this game!!");
break;
}
/*
Below just switching between currentPlayer is done.
*/
if(currentPlayer.equals("O")){
currentPlayer="X";
}else{
currentPlayer="O";
}
}
if(matchDraw){
System.out.println("==============Match Draw==============");
}
System.out.println("====@@@@===GAME OVER===@@@@====");
}
public static void printState(String state[][]){
/*
Prints the state of the game.
*/
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(j==2){
if(!state[i][j].equals("")){
System.out.println(state[i][j]);
}else{
System.out.println();
}
}
else{
if(!state[i][j].equals("")){
System.out.print(state[i][j]+"|");
}else{
System.out.print("|");
}
}
}
if(i<2){
System.out.println("-----");
}
}
}
public static boolean allCheck(String state[][]){
/*
Function checks if all cells are taken or not.
*/
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(state[i][j].equals("")){
return true;
}
}
}
return false;
}
public static boolean win(String state[][],String currentPlayer,int x,int y){
/*
Here in this function all the possible coordinates are checked.
Every possibility is hard coded.
*/
if(x==0 && y==0){
if(state[x][y+1].equals(currentPlayer)&&state[x][y+2].equals(currentPlayer)){
return true;
}
else if(state[x+1][y].equals(currentPlayer)&&state[x+2][y].equals(currentPlayer)){
return true;
}
else if(state[x+1][y+1].equals(currentPlayer)&&state[x+2][y+2].equals(currentPlayer)){
return true;
}
}else if(x==0 && y==2){
if(state[x][y-1].equals(currentPlayer)&&state[x][y-2].equals(currentPlayer)){
return true;
}
else if(state[x+1][y].equals(currentPlayer)&&state[x+2][y].equals(currentPlayer)){
return true;
}
else if(state[x+1][y-1].equals(currentPlayer)&&state[x+2][y-2].equals(currentPlayer)){
return true;
}
}else if(x==2 && y==0){
if(state[x][y+1].equals(currentPlayer)&&state[x][y+2].equals(currentPlayer)){
return true;
}
else if(state[x-1][y].equals(currentPlayer)&&state[x-2][y].equals(currentPlayer)){
return true;
}else if(state[x-1][y+1].equals(currentPlayer)&&state[x-2][y+2].equals(currentPlayer)){
return true;
}
}else if(x==2 && y==2){
if(state[x][y-1].equals(currentPlayer)&&state[x][y-2].equals(currentPlayer)){
return true;
}
else if(state[x-1][y].equals(currentPlayer)&&state[x-2][y].equals(currentPlayer)){
return true;
}else if(state[x-1][y-1].equals(currentPlayer)&&state[x-2][y-2].equals(currentPlayer)){
return true;
}
}
else if(x==0 && y==1){
if(state[x][y-1].equals(currentPlayer) && state[x][y+1].equals(currentPlayer)){
return true;
}else if(state[x+1][y].equals(currentPlayer) && state[x+2][y].equals(currentPlayer)){
return true;
}
}else if(x==2 && y==1){
if(state[x][y-1].equals(currentPlayer) && state[x][y+1].equals(currentPlayer)){
return true;
}else if(state[x-1][y].equals(currentPlayer) && state[x-2][y].equals(currentPlayer)){
return true;
}
}else if(x==1 && y==0){
if(state[x-1][y].equals(currentPlayer) && state[x+1][y].equals(currentPlayer)){
return true;
}else if(state[x][y+1].equals(currentPlayer) && state[x][y+2].equals(currentPlayer)){
return true;
}
}else if(x==1 && y==2){
if(state[x-1][y].equals(currentPlayer) && state[x+1][y].equals(currentPlayer)){
return true;
}else if(state[x][y-1].equals(currentPlayer) && state[x][y-2].equals(currentPlayer)){
return true;
}
}else if(x==1 && y==1){
if(state[x-1][y-1].equals(currentPlayer)&&state[x+1][y+1].equals(currentPlayer)){
return true;
}else if(state[x-1][y+1].equals(currentPlayer)&&state[x+1][y-1].equals(currentPlayer)){
return true;
}else if(state[x-1][y].equals(currentPlayer)&&state[x+1][y].equals(currentPlayer)){
return true;
}else if(state[x][y-1].equals(currentPlayer)&&state[x][y+1].equals(currentPlayer)){
return true;
}
}
return false;
}
}
OUTPUT::
GAME TEST RUN 1::
C:PRO>javac TicTacToe.java
C:PRO>java TicTacToe
Enter current player(O/X)::O
What square player O?0 1
||
-----
O||
-----
||
What square player X?1 1
||
-----
O|X|
-----
||
What square player O?0 0
O||
-----
O|X|
-----
||
What square player X?0 2
O||
-----
O|X|
-----
X||
What square player O?2 0
O||O
-----
O|X|
-----
X||
What square player X?1 0
O|X|O
-----
O|X|
-----
X||
What square player O?1 2
O|X|O
-----
O|X|
-----
X|O|
What square player X?2 1
O|X|O
-----
O|X|X
-----
X|O|
What square player O?2 2
O|X|O
-----
O|X|X
-----
X|O|O
==============Match Draw==============
====@@@@===GAME OVER===@@@@====
C:PRO>
GAME TEST RUN 2::
C:PRO>javac TicTacToe.java
C:PRO>java TicTacToe
Enter current player(O/X)::X
What square player X?0 1
||
-----
X||
-----
||
What square player O?2 2
||
-----
X||
-----
||O
What square player X?0 0
X||
-----
X||
-----
||O
What square player O?2 1
X||
-----
X||O
-----
||O
What square player X?0 2
X||
-----
X||O
-----
X||O
Player X won this game!!
====@@@@===GAME OVER===@@@@====
C:PRO>
Thank you!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.