I need to design and program the game of Tic-tac-toe that we generally call X/0
ID: 3549029 • Letter: I
Question
I need to design and program the game of Tic-tac-toe that we generally call X/0 game. You may assume that the board is 3x3.
The program need to have two classes ( class TicTacToeGame, class Player)
ON JAVA PROGRAMMING
Class TicTacToeGame
private ??? [][] board;
private Player player1, player2;
// the class has the methods hasWinner() and play().
public void play(){
// This is the engine of the game. It initiates and
maintains it until it ends
}
The output should be like this
Player 1: Insert the coordinates of your next move as follows:
row: 1
col: 1
|X| | |
| | | |
| | | |
Player 2: Insert the coordinates of your next move as follows:
row: 1
col: 2
|X|O| |
| | | |
| | | |
and so on
Explanation / Answer
CCode Java Tic Tac Toe, text mode Human x Human
Create a project named 'TicTacToe', and add the following 6 classes, run and be happy.
-->TicTacToe.java
public class TicTacToe {
public static void main(String[] args) {
Game game = new Game();
}
}
-->Board.java
public class Board {
private int[][] Board= new int[3][3];
public Board(){
clearBoard();
}
public void clearBoard(){
for(int line=0 ; line<3 ; line++)
for(int column=0 ; column<3 ; column++)
Board[line][column]=0;
}
public void showBoard(){
System.out.println();
for(int line=0 ; line<3 ; line++){
for(int column=0 ; column<3 ; column++){
if(Board[line][column]==-1){
System.out.print(" X ");
}
if(Board[line][column]==1){
System.out.print(" O ");
}
if(Board[line][column]==0){
System.out.print(" ");
}
if(column==0 || column==1)
System.out.print("|");
}
System.out.println();
}
}
public int getPosition(int[] attempt){
return Board[attempt[0]][attempt[1]];
}
public void setPosition(int[] attempt, int player){
if(player == 1)
Board[attempt[0]][attempt[1]] = -1;
else
Board[attempt[0]][attempt[1]] = 1;
}
public int checkLines(){
for(int line=0 ; line<3 ; line++){
if( (Board[line][0] + Board[line][1] + Board[line][2]) == -3)
return -1;
if( (Board[line][0] + Board[line][1] + Board[line][2]) == 3)
return 1;
}
return 0;
}
public int checkColumns(){
for(int column=0 ; column<3 ; column++){
if( (Board[0][column] + Board[1][column] + Board[2][column]) == -3)
return -1;
if( (Board[0][column] + Board[1][column] + Board[2][column]) == 3)
return 1;
}
return 0;
}
public int checkDiagonals(){
if( (Board[0][0] + Board[1][1] + Board[2][2]) == -3)
return -1;
if( (Board[0][0] + Board[1][1] + Board[2][2]) == 3)
return 1;
if( (Board[0][2] + Board[1][1] + Board[2][0]) == -3)
return -1;
if( (Board[0][2] + Board[1][1] + Board[2][0]) == 3)
return 1;
return 0;
}
public boolean fullBoard(){
for(int line=0 ; line<3 ; line++)
for(int column=0 ; column<3 ; column++)
if( Board[line][column]==0 )
return false;
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.