I wrote the following program for a tic tac toe game in java and though it compl
ID: 3568005 • Letter: I
Question
I wrote the following program for a tic tac toe game in java and though it complies it does not work properly can someone help me fix it
The program must do the following:
*1. Sets in counts to 0 and print board to screen
*2. Displays whose turn it is
*3. Change players turn
*4. Takes in row and column entry and places x or o in that space according to player. Also check for occupied spaces
*5. Checks for any 3 in row combination for a winner and check if all 9 spaces are full with no winner for a tie
*6. Initiate board
*7. Allow the user to continue playing until s/he chooses to quit then display updated win and tie count
import java.util.*;
public class TicTacToe {
private boolean player1Turn = true;
private int rows;
private char columns;
private char [][] board = new char [3][3];
private int player1WinCount, player2WinCount, catCount;
Scanner in = new Scanner(System.in);
/*************************************************************
*
* Name: TicTacToe_LeeBenson default constructor
* Description: sets in counts to 0 and calls initateBoard
*
* Inputs: none
* Returned value: Board
* Preconditions: none
*
*************************************************************/
public TicTacToe ()
{
player1WinCount = 0;
player2WinCount = 0;
catCount = 0;
initiateBoard();
}
/*************************************************************
*
* Name: whoseTurn method
* Description: displays whose turn it is
*
* Inputs: none
* Returned value: returns player1Turn
* Preconditions: none
*
*************************************************************/
public boolean whoseTurn()
{
if(player1Turn)
{
System.out.println("First Players turn X");
}
else
{
System.out.println("Second Players turn O");
}
return player1Turn;
}
/*************************************************************
*
* Name: changePlayer method
* Description: changes players turn
*
* Inputs: none
* Returned value:
* Preconditions: none
*
*************************************************************/
public void changePlayer()
{
if(player1Turn)
{
player1Turn = false;
}
else
{
player1Turn = true;
}
}
/*************************************************************
*
* Name: getMove method
* Description: takes in row and column entry and places x or o in that space
* according to player. also checks for occupied spaces
*
* Inputs: int row, char column
* Returned value: x or o or "space already taken"
* Preconditions:
*
*************************************************************/
public void getMove()
{
whoseTurn();
System.out.println("Please enter row number");
rows = in.nextInt();
System.out.println("Please enter column letter");
columns = in.next().charAt(0);
if(rows < 1 || rows > 3 || (columns != 'A' && columns != 'a' &&
columns != 'B' && columns != 'b' && columns != 'C' && columns != 'c'))
{
System.out.println("Invalid Entry");
getMove();
}
else
{
if(columns == 'a' || columns == 'A')
{
if(board[rows -1][0] == ' ')
{
if(player1Turn)
{
board[rows -1][0] = 'X';
}
else
{
board[rows -1][0] = 'O';
}
}
else
{
System.out.println("Space already occupied");
getMove();
}
}
if(columns == 'b' || columns == 'B')
{
if(board[rows -1][1] == ' ')
{
if(player1Turn)
{
board[rows -1][1] = 'X';
}
else
{
board[rows -1][1] = 'O';
}
}
else
{
System.out.println("Space already occupied");
getMove();
}
}
if(columns == 'c' || columns == 'C')
{
if(board[rows -1][2] == ' ')
{
if(player1Turn)
{
board[rows -1][2] = 'X';
}
else
{
board[rows -1][2] = 'O';
}
}
else
{
System.out.println("Space already occupied");
getMove();
}
}
}
drawBoard();
checkWinner();
changePlayer();
getMove();
}
/*************************************************************
*
* Name: boardFull method
* Description: checks checks if all 9 spaces are full
*
* Inputs: none
* Returned value: ifFull
* Preconditions: none
* @param b
*
*************************************************************/
public boolean boardFull(boolean b) {
boolean isFull = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
isFull = false;
}
}
}
return isFull;
}
/*************************************************************
*
* Name: checkWinner method
* Description: checks for any 3 in row combination for a winner
* also checks if all 9 spaces are full with no winner for a tie
*
* Inputs: none
* Returned value: winner player 1, winner player 2, cats game, or nothing
* Preconditions: none
*
*************************************************************/
public boolean checkWinner() {
//use these two for testing
char[] allX = { 'X', 'X', 'X'};
char[] allO = { 'O', 'O', 'O'};
// these are the eight possible ways to get three in row
char[] row0 = {board[0][0], board[1][0], board[2][0]};
char[] row1 = {board[0][1], board[1][1], board[2][1]};
char[] row2 = {board[0][2], board[1][2], board[2][2]};
char[] col0 = {board[0][0], board[0][1], board[0][2]};
char[] col1 = {board[1][0], board[1][1], board[1][2]};
char[] col2 = {board[2][0], board[2][1], board[2][2]};
char[] dia0 = {board[0][0], board[1][1], board[2][2]};
char[] dia1 = {board[2][0], board[1][1], board[0][2]};
//if you want to, for loop through them put them in another array.
char[][] coll = {row0, row1, row2, col0, col1, col2, dia0, dia1};
//test for X winning combinations:
boolean isWinner = true;
for (int i = 0; i < coll.length ; i++) {
for (int j=0; j < allX.length; j++) {
if (coll[i][j] != allX[j]) {
isWinner = false;
}
}
}
if (isWinner)
{
System.out.println("Winner Winner Chicken Dinner Player 1");
//increments the amount of games that player 1 has won
player1WinCount++;
wasWinner();
}
//test for O winning combinations:
else {
isWinner = true;
for (int i = 0; i < coll.length ; i++) {
for (int j=0; j < allO.length; j++) {
if (coll[i][j] != allO[j]) {
isWinner = false;
}
}
}
if (isWinner) {
System.out.println("Winner Winner Chicken Dinner Player 2");
//increments the amount of games that player2 has won
player2WinCount++;
wasWinner();
}
//if neither of those cases is true then there is no winner
else {
if ( this.boardFull(true) );
}
}
return false;
}
/*************************************************************
*
* Name: initiateBoard
* Description: initiates board
*
* Inputs: none
* Returned value: n/a
* Preconditions: none
*
*************************************************************/
public void initiateBoard()
{
for(int i = 0; i<3; i++)
{
for(int j = 0; j<3; j++)
{
board[i][j] = ' ';
}
}
}
/*************************************************************
*
* Name: wasWinner method
* Description: allows the user to continue playing till they chooses to quit
* displays updated win and tie count
*
* Inputs: Y for yes for continuing to play
* Returned value: n/a
* Preconditions: none
*
*************************************************************/
public void wasWinner()
{
initiateBoard();
System.out.println("Would you like to continue");
if(in.next().equalsIgnoreCase("Y"))
{
System.out.println("Player 1 wins:"+ player1WinCount);
System.out.println("Player 2 wins:"+ player2WinCount);
System.out.println("Cat Games:"+ catCount);
getMove();
}
else
{
System.out.println("Player 1 wins:"+ player1WinCount);
System.out.println("Player 2 wins:"+ player2WinCount);
System.out.println("Cat Games:"+ catCount);
System.exit(0);
}
}
/*************************************************************
*
* Name: drawBoard
* Description: prints board to screen
*
* Inputs: none
* Returned value:
* Preconditions: none
*
*************************************************************/
public void drawBoard()
{
System.out.println(" a b c");
for(int i = 0; i<3; i++)
{
System.out.print(i + 1);
for(int j = 0; j<3; j++)
{
if(board[i][j] != 'X' && board[i][j] != 'O' && board[i][j] != ' ')
{
System.err.println("Board error: exiting");
System.exit(1);
}
if( j == 2)
{
System.out.println(board[i][j]);
if(i !=2)
{
System.out.println(" -----");
}
}
else
{
System.out.print(board[i][j] + "|");
}
}
}
}
}
the other file is the demo file and the code for it is:
public class Demo
{
public static void main(String[] args)
{
TicTacToe game = new TicTacToe();
game.drawBoard();
game.getMove();
}
}
can you please fix it and tell me why you made the changes you made?
Explanation / Answer
Here you go :)
The only part that was worng is thecheckWinner() method.
I've highlighted the changes i did and commented beside it.
import java.util.*;
public class TicTacToe {
private boolean player1Turn = true;
private int rows;
private char columns;
private char [][] board = new char [3][3];
private int player1WinCount, player2WinCount, catCount;
Scanner in = new Scanner(System.in);
/*************************************************************
*
* Name: TicTacToe_LeeBenson default constructor
* Description: sets in counts to 0 and calls initateBoard
*
* Inputs: none
* Returned value: Board
* Preconditions: none
*
*************************************************************/
public TicTacToe ()
{
player1WinCount = 0;
player2WinCount = 0;
catCount = 0;
initiateBoard();
}
/*************************************************************
*
* Name: whoseTurn method
* Description: displays whose turn it is
*
* Inputs: none
* Returned value: returns player1Turn
* Preconditions: none
*
*************************************************************/
public boolean whoseTurn()
{
if(player1Turn)
{
System.out.println("First Players turn X");
}
else
{
System.out.println("Second Players turn O");
}
return player1Turn;
}
/*************************************************************
*
* Name: changePlayer method
* Description: changes players turn
*
* Inputs: none
* Returned value:
* Preconditions: none
*
*************************************************************/
public void changePlayer()
{
if(player1Turn)
{
player1Turn = false;
}
else
{
player1Turn = true;
}
}
/*************************************************************
*
* Name: getMove method
* Description: takes in row and column entry and places x or o in that space
* according to player. also checks for occupied spaces
*
* Inputs: int row, char column
* Returned value: x or o or "space already taken"
* Preconditions:
*
*************************************************************/
public void getMove()
{
whoseTurn();
System.out.println("Please enter row number");
rows = in.nextInt();
System.out.println("Please enter column letter");
columns = in.next().charAt(0);
if(rows < 1 || rows > 3 || (columns != 'A' && columns != 'a' &&
columns != 'B' && columns != 'b' && columns != 'C' && columns != 'c'))
{
System.out.println("Invalid Entry");
getMove();
}
else
{
if(columns == 'a' || columns == 'A')
{
if(board[rows -1][0] == ' ')
{
if(player1Turn)
{
board[rows -1][0] = 'X';
}
else
{
board[rows -1][0] = 'O';
}
}
else
{
System.out.println("Space already occupied");
getMove();
}
}
if(columns == 'b' || columns == 'B')
{
if(board[rows -1][1] == ' ')
{
if(player1Turn)
{
board[rows -1][1] = 'X';
}
else
{
board[rows -1][1] = 'O';
}
}
else
{
System.out.println("Space already occupied");
getMove();
}
}
if(columns == 'c' || columns == 'C')
{
if(board[rows -1][2] == ' ')
{
if(player1Turn)
{
board[rows -1][2] = 'X';
}
else
{
board[rows -1][2] = 'O';
}
}
else
{
System.out.println("Space already occupied");
getMove();
}
}
}
drawBoard();
checkWinner();
changePlayer();
getMove();
}
/*************************************************************
*
* Name: boardFull method
* Description: checks checks if all 9 spaces are full
*
* Inputs: none
* Returned value: ifFull
* Preconditions: none
* @param b
*
*************************************************************/
public boolean boardFull(boolean b) {
boolean isFull = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
isFull = false;
}
}
}
return isFull;
}
/*************************************************************
*
* Name: checkWinner method
* Description: checks for any 3 in row combination for a winner
* also checks if all 9 spaces are full with no winner for a tie
*
* Inputs: none
* Returned value: winner player 1, winner player 2, cats game, or nothing
* Preconditions: none
*
*************************************************************/
public boolean checkWinner() {
//use these two for testing
char[] allX = { 'X', 'X', 'X'};
char[] allO = { 'O', 'O', 'O'};
// these are the eight possible ways to get three in row
char[] row0 = {board[0][0], board[1][0], board[2][0]};
char[] row1 = {board[0][1], board[1][1], board[2][1]};
char[] row2 = {board[0][2], board[1][2], board[2][2]};
char[] col0 = {board[0][0], board[0][1], board[0][2]};
char[] col1 = {board[1][0], board[1][1], board[1][2]};
char[] col2 = {board[2][0], board[2][1], board[2][2]};
char[] dia0 = {board[0][0], board[1][1], board[2][2]};
char[] dia1 = {board[2][0], board[1][1], board[0][2]};
//if you want to, for loop through them put them in another array.
char[][] coll = {row0, row1, row2, col0, col1, col2, dia0, dia1};
//test for X winning combinations:
boolean isWinner = false;
for (int i = 0; i < coll.length ; i++)
{
int k=0; //count to keep track how many X's are there in a row/col/dia
for (int j=0; j < allX.length; j++)
{
if (coll[i][j] == allX[j])
{
k++; //incrementing count whenever it is equal
}
else break; //breaking when not equal...not necessary but saves time.
}
if(k==3){isWinner=true;break;} //if all the three matches then he is the winner...break to prevent further evaluations.
}
if (isWinner)
{
System.out.println("Winner Winner Chicken Dinner Player 1");
//increments the amount of games that player 1 has won
player1WinCount++;
wasWinner();
}
//test for O winning combinations:
//same as above comments
else {
isWinner = false;
for (int i = 0; i < coll.length ; i++)
{
int k=0;
for (int j=0; j < allO.length; j++)
{
if (coll[i][j] == allO[j])
{
k++;
}
else break;
}
if(k==3){isWinner=true;break;}
}
if (isWinner) {
System.out.println("Winner Winner Chicken Dinner Player 2");
//increments the amount of games that player2 has won
player2WinCount++;
wasWinner();
}
//if neither of those cases is true then there is no winner
else {
if ( this.boardFull(true) );
}
}
return false;
}
/*************************************************************
*
* Name: initiateBoard
* Description: initiates board
*
* Inputs: none
* Returned value: n/a
* Preconditions: none
*
*************************************************************/
public void initiateBoard()
{
for(int i = 0; i<3; i++)
{
for(int j = 0; j<3; j++)
{
board[i][j] = ' ';
}
}
}
/*************************************************************
*
* Name: wasWinner method
* Description: allows the user to continue playing till they chooses to quit
* displays updated win and tie count
*
* Inputs: Y for yes for continuing to play
* Returned value: n/a
* Preconditions: none
*
*************************************************************/
public void wasWinner()
{
initiateBoard();
System.out.println("Would you like to continue");
if(in.next().equalsIgnoreCase("Y"))
{
System.out.println("Player 1 wins:"+ player1WinCount);
System.out.println("Player 2 wins:"+ player2WinCount);
System.out.println("Cat Games:"+ catCount);
getMove();
}
else
{
System.out.println("Player 1 wins:"+ player1WinCount);
System.out.println("Player 2 wins:"+ player2WinCount);
System.out.println("Cat Games:"+ catCount);
System.exit(0);
}
}
/*************************************************************
*
* Name: drawBoard
* Description: prints board to screen
*
* Inputs: none
* Returned value:
* Preconditions: none
*
*************************************************************/
public void drawBoard()
{
System.out.println(" a b c");
for(int i = 0; i<3; i++)
{
System.out.print(i + 1);
for(int j = 0; j<3; j++)
{
if(board[i][j] != 'X' && board[i][j] != 'O' && board[i][j] != ' ')
{
System.err.println("Board error: exiting");
System.exit(1);
}
if( j == 2)
{
System.out.println(board[i][j]);
if(i !=2)
{
System.out.println(" -----");
}
}
else
{
System.out.print(board[i][j] + "|");
}
}
}
}
}