Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When I try to use the number 5 then 7 and then 3 , the program isn\'t working pr

ID: 3759635 • Letter: W

Question

When I try to use the number 5 then 7 and then 3 , the program isn't working properly. It keeps running and instead of blocking the number 3 because I took the 7, it takes another random number. and supposedly it needs to show the "you win." but nothing. Can you tell me what I am doing wrong?
WRONG OUTPUT:

package tictactoegame;

import java.util.Scanner;

public class TicTacToeGame
{
//Globals
static int b1,b2,b3,b4,b5,b6,b7,b8,b9;
static final int NUM_CELLS = 9;
static boolean isGameOver = false;
static Scanner kb = new Scanner(System.in);
static int row1,row2,row3,col1,col2,col3,dia1,dia2;
  
public static void main(String[] args)
{
b1=b2=b3=b4=b5=b6=b7=b8=b9=0;
  
while(!isGameOver)
{
displayBoard();
doUser();
if(!isGameOver) doComputer();

}//while

}//end main()
  
public static void doUser()
{
boolean valid = false;
  
while(!valid)
{
System.out.print("Your move: ");
int urMove = kb.nextInt();
switch(urMove)
{
case 1: if(b1==0){ b1=4; valid=true; } break;
case 2: if(b2==0){ b2=4; valid=true; } break;
case 3: if(b3==0){ b3=4; valid=true; } break;
case 4: if(b4==0){ b4=4; valid=true; } break;
case 5: if(b5==0){ b5=4; valid=true; } break;
case 6: if(b6==0){ b6=4; valid=true; } break;
case 7: if(b7==0){ b7=4; valid=true; } break;
case 8: if(b8==0){ b8=4; valid=true; } break;
case 9: if(b9==0){ b9=4; valid=true; } break;
}//switch
if(!valid) System.out.println(" POSITION TAKEN!");
}//end while !valid
checkBoard();
}//end doUser()
  
public static void checkBoard()
{
row1=row2=row3=col1=col2=col3=dia1=dia2=0;
  
row1=b1+b2+b3;
row2=b4+b5+b6;
row3=b7+b8+b9;
col1=b1+b4+b7;
col2=b2+b5+b8;
col3=b3+b6+b9;
dia1=b1+b5+b9;
dia2=b3+b5+b7;
  
if( isSum(3) )
{
displayBoard();
System.out.println(" ***I WIN***");
isGameOver = true;
}
else
if( isSum(12) )
{
displayBoard();
System.out.println(" ***YOU WIN***");
isGameOver = true;
}
else
if(!(b1==0||b2==0||b3==0||b4==0||b5==0||b6==0||b7==0||b8==0||b9==0))
{
displayBoard();
System.out.println(" ***TIE GAME***");
isGameOver = true;
}

}//end checkBoard()
  
public static void doComputer()
{
int cell=0;
  
if( ( cell=lookForSum(2) ) !=0 )
{
setMove(cell); }
else
if( ( cell=lookForSum(8) ) !=0 )
{
setMove(cell);
}
else
if( ( cell=lookForSum(1) ) !=0 )
{
setMove(cell);
}
else
if( ( cell=lookForSum(4) ) !=0 )
{
setMove(cell);
}
else
{
if(b9==0) cell = 9;
else if(b8==0) cell = 8;
else if(b7==0) cell = 7;
else if(b6==0) cell = 6;
else if(b5==0) cell = 5;
else if(b4==0) cell = 4;
else if(b3==0) cell = 3;
else if(b2==0) cell = 2;
else if(b1==0) cell = 1;
setMove(cell);
}
  
checkBoard();
  
}//end doComputer
  
public static void setMove(int cell)
{
switch(cell)
{
case 1: if(b1==0){ b1=1; } break;
case 2: if(b2==0){ b2=1; } break;
case 3: if(b3==0){ b3=1; } break;
case 4: if(b4==0){ b4=1; } break;
case 5: if(b5==0){ b5=1; } break;
case 6: if(b6==0){ b6=1; } break;
case 7: if(b7==0){ b7=1; } break;
case 8: if(b8==0){ b8=1; } break;
case 9: if(b9==0){ b9=1; } break;
}//switch

}//end setCell()
  
public static int lookForSum(int val)
{
int result = 0; //not found
  
for(int i=1; i<8; i++)
{
switch(i)
{
case 1: if(row1==val) result = findEmpty(i); break;
case 2: if(row2==val) result = findEmpty(i); break;
case 3: if(row3==val) result = findEmpty(i); break;
case 4: if(col1==val) result = findEmpty(i); break;
case 5: if(col2==val) result = findEmpty(i); break;
case 6: if(col3==val) result = findEmpty(i); break;
case 7: if(dia1==val) result = findEmpty(i); break;
case 8: if(dia2==val) result = findEmpty(i); break;
}
}//end for
  
return result;
}//end lookFor()
  
public static boolean isSum(int val)
{
boolean result = false; //not found
  
for(int i=1; i<8; i++)
{
switch(i)
{
case 1: if(row1==val) result = true; break;
case 2: if(row2==val) result = true; break;
case 3: if(row3==val) result = true; break;
case 4: if(col1==val) result = true; break;
case 5: if(col2==val) result = true; break;
case 6: if(col3==val) result = true; break;
case 7: if(dia1==val) result = true; break;
case 8: if(dia2==val) result = true; break;
}
}//end for
  
return result;
}//end isFor()
  
public static int findEmpty(int tri)
{
int result = 0;
  
switch(tri)
{
case 1: //row1
if(b1==0){ result = 1;}
else if(b2==0){ result = 2;}
else if(b3==0){ result = 3;}
break;
case 2: //row2
{
if(b4==0){ result = 4;}
else if(b5==0){ result = 5;}
else if(b6==0){ result = 6;}
}
break;
case 3: //row3
{
if(b7==0){ result = 7;}
else if(b8==0){ result = 8;}
else if(b9==0){ result = 9;}
}
break;
case 4: //col1
if(b1==0){ result = 1;}
else if(b4==0){ result = 4;}
else if(b7==0){ result = 7;}
break;
case 5: //col2
if(b2==0){ result = 2;}
else if(b5==0){ result = 5;}
else if(b8==0){ result = 8;}
break;
case 6: //col3
if(b3==0){ result = 3;}
else if(b6==0){ result = 6;}
else if(b9==0){ result = 9;}
break;
case 7: //dia1
if(b1==0){ result = 1;}
else if(b5==0){ result = 5;}
else if(b9==0){ result = 9;}
break;
case 8: //dia2
if(b3==0){ result = 3;}
else if(b5==0){ result = 5;}
else if(b7==0){ result = 7;}
break;
  
}
  
return result;
  
}//end findEmpty()
  
public static boolean isBoardFull()
{
return !(b1==0||b2==0||b3==0||b4==0||b5==0||b6==0||b7==0||b8==0||b9==0);
}
  
public static void displayBoard()
{
System.out.println(" ");
System.out.print(" " + doCell('1',b1) + " |" );
System.out.print(" " + doCell('2',b2) + " |" );
System.out.print(" " + doCell('3',b3) + " " );
System.out.println(" ---+---+---");
System.out.print(" " + doCell('4',b4) + " |" );
System.out.print(" " + doCell('5',b5) + " |" );
System.out.print(" " + doCell('6',b6) + " " );
System.out.println(" ---+---+---");
System.out.print(" " + doCell('7',b7) + " |" );
System.out.print(" " + doCell('8',b8) + " |" );
System.out.print(" " + doCell('9',b9) + " " );
System.out.println();
  
}//end displayBoard()
  
public static char doCell(char count, int cell)
{
char result = ' ';
if( cell==0) result = count;
else if(cell==1) result = 'O';
else if(cell==4) result = 'X';
return result;
}//end doCell()
  
}

Output TicTacToe EC (run) #2 1213 1 23 41 51 6 71819 Your move: 5 0 1 23 71819 Your move: 7 01013 Your move: 3 Your move:

Explanation / Answer

import java.util.Scanner;


public class TicTacToe
{
private int counter;
private char position[]=new char[10];
private char player;
  
  
public static void main(String args[])
{
String ch;
TicTacToe Toe=new TicTacToe();
do
{
Toe.newBoard();
Toe.play();
System.out.println (" Like to play again (Enter 'yes')? ");
Scanner in =new Scanner(System.in);
ch=in.nextLine();
System.out.println("ch value is "+ch);
}
while (ch.equals("yes"));
  
  
}
public void newBoard()
{
  
char positiondef[] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9'};
int j;
counter = 0;
player = 'X';
for (j=1; j<10; j++)
position[j]=positiondef[j];
currentBoard();
  
  
}
public String currentBoard()
{
System.out.println( " " );
  
System.out.println( " " );
System.out.println( " " + position [1] + " | " +position [2]+ " | " +positio[3]);
System.out.println( " | | " );

System.out.println( " ___|____|___ " );

System.out.println( " " +position [4]+ " | " +position [5]+ " | " +position [6]);
System.out.println( " | | " );

System.out.println( " ___|____|___ " );
System.out.println( " " +position [7]+ " | " +position [8]+ " | " +position [9]);
System.out.println( " | | " );

System.out.println( " | | " );

System.out.println( " " );
return "currentBoard";
}
  
public void play()
{
int spot;
char blank = ' ';
  
System.out.println( "Player " + getPlayer() +" will go first and be the letter 'X'" );
  
do {
currentBoard();
  
System.out.println( " Player " + getPlayer() +" choose a position." );
  
boolean posTaken = true;
while (posTaken) {
// System.out.println( "position is taken, please enter a valid space");
Scanner in =new Scanner (System.in);
spot=in.nextInt();
posTaken = checkPosition(spot);
if(posTaken==false)
position[spot]=getPlayer();
}
  
System.out.println( "Nice move." );
  
currentBoard();

  
nextPlayer();
}while ( checkWinner() == blank );
  
}
  
public char checkWinner()
{
char Winner = ' ';
  
  

if (position[1] == 'X' && position[2] == 'X' && position[3] == 'X') Winner = 'X';
if (position[4] == 'X' && position[5] == 'X' && position[6] == 'X') Winner = 'X';
if (position[7] == 'X' && position[8] == 'X' && position[9] == 'X') Winner = 'X';
if (position[1] == 'X' && position[4] == 'X' && position[7] == 'X') Winner = 'X';
if (position[2] == 'X' && position[5] == 'X' && position[8] == 'X') Winner = 'X';
if (position[3] == 'X' && position[6] == 'X' && position[9] == 'X') Winner = 'X';
if (position[1] == 'X' && position[5] == 'X' && position[9] == 'X') Winner = 'X';
if (position[3] == 'X' && position[5] == 'X' && position[7] == 'X') Winner = 'X';
if (Winner == 'X' )
{
System.out.println("Player1 wins the game." );
return Winner;
}
  
  

if (position[1] == 'O' && position[2] == 'O' && position[3] == 'O') Winner = 'O';
if (position[4] == 'O' && position[5] == 'O' && position[6] == 'O') Winner = 'O';
if (position[7] == 'O' && position[8] == 'O' && position[9] == 'O') Winner = 'O';
if (position[1] == 'O' && position[4] == 'O' && position[7] == 'O') Winner = 'O';
if (position[2] == 'O' && position[5] == 'O' && position[8] == 'O') Winner = 'O';
if (position[3] == 'O' && position[6] == 'O' && position[9] == 'O') Winner = 'O';
if (position[1] == 'O' && position[5] == 'O' && position[9] == 'O') Winner = 'O';
if (position[3] == 'O' && position[5] == 'O' && position[7] == 'O') Winner = 'O';
if (Winner == 'O' )
{
System.out.println( "Player2 wins the game." );
return Winner;
}
  
  

for(int j=1;j<10;j++)
{
if(position[j]=='X' ||position[j]=='O')
{
if(j==9)
{
char Draw='D';
System.out.println(" Game is stalemate ");
return Draw;
}
continue;
}
else
break;
  
}
  
return Winner;
}
  
public boolean checkPosn(int spot)
{
  
  
if (position[spot] == 'X' || position[spot] == 'O')
{
System.out.println("That posn is already taken, please choose another");
return true;
}
else {
return false;
}
  

}
  
  
  
public void nextPlayer()
{
if (player == 'X')
player = 'O';
else player = 'X';
  
}
  
public String getTitle()
{
return "Tic Tac Toe" ;
}
  
public char getPlayer()
{
return player;
}
  
}

this code complete working..check it once

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote