In this project you will be tasked with writing a tic-tac-toe game in JAVA This
ID: 3563155 • Letter: I
Question
In this project you will be tasked with writing a tic-tac-toe game in JAVA
This program should be playable by two humans alternating time at the keyboard.
[70] Task:
Your program will start by displaying the empty board and letting player one make their move. The player should enter 1-9 to indicate which position they want to place their X or O, with 1-9 indicating the positions on the numpad of a keyboard. The program should continue prompting for each player move until a winner is discovered or a tie is reached. You can assume that only integers will be entered by the users. If the user enters a square that already has something in it, you should give them an error message, display the board again and reprompt for input.
Once the game has finished, you should prompt them on whether they would like to play another game. You should also keep track of the number of wins, ties and draws.
Implementation:
You must divide this task up into methods. If the majority of your code is in your main, you will receive very little credit. At a bare minimum you are going to want a method to display the board, a method to determine who is currently winning, and a method to take in input from the user.
It tends to be quite easy to have errors in the methods looking for a win or draw, so I suggest splitting this method up (checking for horizontal XXX or vertical XXX). You can also have methods that check if X has won or if O has won if you wish.
Explanation / Answer
please rate - thanks
any questions - ask
sample run
----jGRASP exec: java TicTacToe2Human
1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
player X turn
Please enter your move (1-9):
1
X | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
player O turn
Please enter your move (1-9):
2
X | O | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
player X turn
Please enter your move (1-9):
3
X | O | X
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
player O turn
Please enter your move (1-9):
3
spot occupied enter again
player O turn
Please enter your move (1-9):
5
X | O | X
---+---+---
4 | O | 6
---+---+---
7 | 8 | 9
player X turn
Please enter your move (1-9):
4
X | O | X
---+---+---
X | O | 6
---+---+---
7 | 8 | 9
player O turn
Please enter your move (1-9):
6
X | O | X
---+---+---
X | O | O
---+---+---
7 | 8 | 9
player X turn
Please enter your move (1-9):
7
X | O | X
---+---+---
X | O | O
---+---+---
X | 8 | 9
The winner is X
another game(y/n)?y
1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
player X turn
Please enter your move (1-9):
5
1 | 2 | 3
---+---+---
4 | X | 6
---+---+---
7 | 8 | 9
player O turn
Please enter your move (1-9):
7
1 | 2 | 3
---+---+---
4 | X | 6
---+---+---
O | 8 | 9
player X turn
Please enter your move (1-9):
6
1 | 2 | 3
---+---+---
4 | X | X
---+---+---
O | 8 | 9
player O turn
Please enter your move (1-9):
4
1 | 2 | 3
---+---+---
O | X | X
---+---+---
O | 8 | 9
player X turn
Please enter your move (1-9):
1
X | 2 | 3
---+---+---
O | X | X
---+---+---
O | 8 | 9
player O turn
Please enter your move (1-9):
9
X | 2 | 3
---+---+---
O | X | X
---+---+---
O | 8 | O
player X turn
Please enter your move (1-9):
2
X | X | 3
---+---+---
O | X | X
---+---+---
O | 8 | O
player O turn
Please enter your move (1-9):
3
X | X | O
---+---+---
O | X | X
---+---+---
O | 8 | O
player X turn
Please enter your move (1-9):
8
X | X | O
---+---+---
O | X | X
---+---+---
O | X | O
The winner is X
another game(y/n)?n
SUMMARY OF WINS:
Games won by player X: 2
Games won by player O: 0
Tie games: 0
----jGRASP: operation complete.
----------------------------------
CODE
import java.util.*;
public class TicTacToe2Human
{public static void main(String[] args)
{Scanner in= new Scanner(System.in);
char[][] board={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
char player='X',done=' ';
int row,col,move,x=0,o=0,tie=0;
do
{
done=' ';
player='X';
initBoard(board);
printBoard(board);
while(done==' ') //while game not done
{move=getMove(in,player); //get move
row=move/3; //convert to row and column
col=move%3;
while(board[row][col]=='X'||board[row][col]=='O') //check if occupied
{System.out.println("spot occupied enter again"); //if yes must get it again
move=getMove(in,player);
row=move/3;
col=move%3;
}
board[row][col] = player; //mark board
printBoard(board); //print board
done=hasPlayerWon(board); //check if game over
if(done==' ') //if not over switch player
player=getPlayer(player);
}
gameOver(done); //game over-report winner
if(done=='X') //count wins
x++;
else if(done=='O')
o++;
else
tie++;
System.out.print("another game(y/n)?" ); //again?
}while(Character.toUpperCase(in.next().charAt(0))=='Y'); //keep playing if y entered
summary(x,o,tie); //otherwise game over --report wins
}
public static void summary(int x,int o, int tie) //print summary
{System.out.println("SUMMARY OF WINS:");
System.out.println("Games won by player X: "+x);
System.out.println("Games won by player O: "+o);
System.out.println("Tie games: "+tie);
}
public static void initBoard(char[][] board) //initialize vboard 1-9
{int i,j;
char c='1';
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{board[i][j]=c++;
}
}
public static char getPlayer(char player) //switch player
{ if (player == 'X')
player = 'O';
else
player = 'X';
return player;
}
public static void gameOver(char done) //report winner
{if(done=='t')
System.out.println("the game is a tie");
else
System.out.println("The winner is "+done);
}
public static int getMove(Scanner in,char player) //get move, checking for validity
{int n;
System.out.println("player "+player+" turn");
System.out.println("Please enter your move (1-9):");
n=in.nextInt();
while(n<1||n>9)
{System.out.println("must be between 1 and 9");
System.out.println("Please enter your move (1-9):");
n=in.nextInt();
}
return n-1;
}
public static char hasPlayerWon(char board[][]) //check for winner
{int i,j;
for(i=0; i<3; i++)
if(board[i][0]==board[i][1]&&board[i][0]==board[i][2]) //check row all same
return board[i][0];
for(i=0; i<3; i++)
if(board[0][i]==board[1][i]&&board[0][i]==board[2][i]) //check column all same
return board[0][i];
if(board[0][0]==board[1][1]&&board[1][1]==board[2][2]) //check diagnol 1 all same
return board[0][0];
if(board[0][2]==board[1][1]&&board[1][1]==board[2][0]) //check other diagnol all same
return board[0][2];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(board[i][j]!='X'&&board[i][j]!='O') //check any unused spots-if none, it's a tie
return ' ';
return 't';
}
public static void printBoard(char[][] board) //print board
{int row,col;
for(row=0; row<3; row++)
{for(col=0; col < 3; col++)
{
System.out.print(" " + board[row][col] + " ");
if(col!=2)
System.out.print("|");
}
System.out.println();
if (row !=2)
System.out.println("---+---+---");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.