A little help in TicTacToe
I wrote this code that works and does everything alright and plays the game, but i need the game to be able to check if a square is already occupied and tell the user to input again. I am not doing well there. here's what i have...
// tic tac toe game
import java.util.Scanner;// import the scanner
publicclass TicTacToe
{// start of class
publicstatic String board [][] =new String [3][3];// create a 3x3 array called board
publicstaticvoid main(String[] args)//main method
{// start main
Scanner input =new Scanner( System.in );// create scanner to read inputs
String name1;// player1's name
String name2;// player2's name
String current ="O";// the current mark
int choice;//the choice user inputs
int cheat;// cheatcode
int moveCount = 0;// count the number of moves made
int check = 0;
System.out.println("Enter Player1's name: " );//prompt
name1 = input.nextLine();//read input
System.out.println("Enter Player2's name: " );//prompt
name2 = input.nextLine();//read input
for (int i = 0; i < 3; i++)
{//start of loop
for (int j = 0; j < 3; j++)
{//start of loop
board[i][j] =" ";// initializes board to empty spaces
}//end of loop
}//end of loop
System.out.println();// the next few lines are dedicated to print a board
System.out.println ("************************" );
for (int row = 0; row < 3; row++ )
{//start of loop
for (int column = 0; column < 3; column++ )
{//start of loop
System.out.printf ("****\n" );
}//end of loop
System.out.println ("************************" );
}//end of loop
int winner = 0;// winner
while (winner == 0)
{//start of gignormous loop
if (current == ("O"))// switch between players
{
current ="X";
System.out.printf("%s, make your move.", name1 );//prompt
choice = input.nextInt();//read input
++moveCount;
if ( moveCount == 9)
winner = 5;
if ( choice <= 0 || choice >9 )// prevent user from going crazy and input weird numbers
{
System.out.printf("%s, can't do that homey.", name1 );//prompt
choice = input.nextInt();//read input
if ( choice == 333)
winner = 3;
}
else
{
xmark(choice);// mark x by passing choice to xmark
winner = CheckOneWin ( winner );// check for win for player1...passes winner...blah blah
for (int row1 = 0; row1 != 3; row1++ )
{
System.out.println ("*************************" );
System.out.println ("****" );
for (int column1 = 0; column1 != 3; column1++ )
{
System.out.printf ("%4s%4s" ,"*" , board[row1][column1] );
}
System.out.printf ("%s\n%s\n" ,"*" ,"****" );
}
System.out.println ("*************************" );
}
}
else
{
current ="O";// switch back to o
System.out.printf("%s, make your move.", name2 );//prompt
choice = input.nextInt();//read input
++moveCount;
if ( moveCount == 9 )
winner = 5;
if ( choice <= 0 || choice > 9 )// prevent user from going crazy
{
System.out.printf("%s, can't do that homey.", name2 );//prompt
choice = input.nextInt();//read input
if ( choice == 333)
winner = 3;
}
omark(choice);// pass choice to method omark
winner = CheckTwoWin ( winner );// check for winner by passing winner to method CheckTwoWin
for (int row2 = 0; row2 != 3; row2++ )
{
System.out.println ("*************************" );
System.out.println ("****" );
for (int column2 = 0; column2 != 3; column2++ )
{
System.out.printf ("%4s%4s" ,"*" , board[row2][column2] );
}
System.out.printf ("%s\n%s\n" ,"*" ,"****" );
}
System.out.println ("*************************" );
}
if (winner == 1)// tells that player1 has won
{
System.out.printf("\n%s wins, way to go homey!\n", name1);
}
if (winner == 3)// tells that player1 has won with cheat code
{
System.out.printf("\n%s wins...with a cheat...don't you have any real skills?\n", name1);
}
if (winner == 2)// tells that player2 has won
{
System.out.printf("\n%s wins, way to go homey!\n", name2);
}
if (winner == 4)// tells that player2 has won with cheat code
{
System.out.printf("\n%s wins...with a cheat...don't you have any real skills?\n", name2);
}
if (winner == 5)// tells that a tie occurs
{
System.out.printf("The game is tied, looks like %s and %s are evenly matched...\n", name1, name2);
}
}//end of hugmongus while loop
}// end method main
publicstaticvoid xmark (int x )// use x to recieve choice
{
int thingy = x;// thingy is the private copy of x
switch ( thingy )// switch for thingy
{
case 1:
if (board[0][0] =="O")
System.out.println("can't override dude...");
else
board[0][0] ="X";// mark X on board [0][0] if case is
break;
case 2:
if (board[0][1] =="O")
System.out.println("can't override dude...");
else
board[0][1] ="X";// mark X...board[0][1]...
break;
case 3:
if (board[0][2] =="O")
System.out.println("can't override dude...");
else
board[0][2] ="X";// blah
break;
case 4:
if (board[1][0] =="O")
System.out.println("can't override dude...");
else
board[1][0] ="X";//blah
break;
case 5:
if (board[1][1] =="O")
System.out.println("can't override dude...");
else
board[1][1] ="X";// yadayadyada
break;
case 6:
if (board[1][2] =="O")
System.out.println("can't override dude...");
else
board[1][2] ="X";
break;
case 7:
if (board[2][0] =="O")
System.out.println("can't override dude...");
else
board[2][0] ="X";
break;
case 8:
if (board[2][1] =="O")
System.out.println("can't override dude...");
else
board[2][1] ="X";
break;
case 9:
if (board[2][2] =="O")
System.out.println("can't override dude...");
else
board[2][2] ="X";
break;
}
}
publicstaticvoid omark (int o )// o recieves choice
{
int thingy = o;// same thing as last method
switch ( thingy )// switch for thingy
{
case 1:
if (board[0][0] =="X")
System.out.println("can't override dude...");
else
board[0][0] ="O";// mark X on board [0][0] if case is
break;
case 2:
if (board[0][1] =="X")
System.out.println("can't override dude...");
else
board[0][1] ="O";// mark X...board[0][1]...
break;
case 3:
if (board[0][2] =="X")
System.out.println("can't override dude...");
else
board[0][2] ="O";// blah
break;
case 4:
if (board[1][0] =="X")
System.out.println("can't override dude...");
else
board[1][0] ="O";//blah
break;
case 5:
if (board[1][1] =="X")
System.out.println("can't override dude...");
else
board[1][1] ="O";// yadayadyada
break;
case 6:
if (board[1][2] =="X")
System.out.println("can't override dude...");
else
board[1][2] ="O";
break;
case 7:
if (board[2][0] =="X")
System.out.println("can't override dude...");
else
board[2][0] ="O";
break;
case 8:
if (board[2][1] =="X")
System.out.println("can't override dude...");
else
board[2][1] ="O";
break;
case 9:
if (board[2][2] =="X")
System.out.println("can't override dude...");
else
board[2][2] ="O";
break;
}
}
publicstaticint CheckOneWin(int win1 )// recieves winner with win1
{
int win2 = win1;// win2 = new copy of win1
if ( ( board[0][0] =="X" ) && ( board [0][1] =="X" ) && ( board [0][2] =="X" ) )// if these 3 spaces are all X
win2 = 1;// player1 wins and win2 is set to 1
if ( ( board[1][0] =="X" ) && ( board [1][1] =="X" ) && ( board [1][2] =="X" ) )// same thing again
win2 = 1;
if ( ( board[2][0] =="X" ) && ( board [2][1] =="X" ) && ( board [2][2] =="X" ) )
win2 = 1;
if ( ( board[0][0] =="X" ) && ( board [1][0] =="X" ) && ( board [2][0] =="X") )
win2 = 1;
if ( ( board[0][1] =="X" ) && ( board [1][1] =="X" ) && ( board [2][1] =="X" ) )
win2 = 1;
if ( ( board[0][2] =="X" ) && ( board [1][2] =="X" ) && ( board [2][2] =="X" ) )
win2 = 1;
if ( ( board[0][0] =="X" ) && ( board [1][1] =="X" ) && ( board [2][2] =="X" ) )
win2 = 1;
if ( ( board[0][2] =="X" ) && ( board [1][1] =="X" ) && ( board [2][0] =="X" ) )
win2 = 1;
return win2;// returns win2
}
publicstaticint CheckTwoWin(int win3 )// recieve winner with win3
{
int win4 = win3;//blah
if ( ( board[0][0] =="O" ) && ( board [0][1] =="O" ) && ( board [0][2] =="O" ) )//all that good stuff
win4 = 1;
if ( ( board[1][0] =="O" ) && ( board [1][1] =="O" ) && ( board [1][2] =="O" ) )
win4 = 1;
if ( ( board[2][0] =="O" ) && ( board [2][1] =="O" ) && ( board [2][2] =="O" ) )
win4 = 1;
if ( ( board[0][0] =="O" ) && ( board [1][0] =="O" ) && ( board [2][0] =="O" ) )
win4 = 1;
if ( ( board[0][1] =="O" ) && ( board [1][1] =="O" ) && ( board [2][1] =="O" ) )
win4 = 1;
if ( ( board[0][2] =="O" ) && ( board [1][2] =="O" ) && ( board [2][2] =="O" ) )
win4 = 1;
if ( ( board[0][0] =="O" ) && ( board [1][1] =="O" ) && ( board [2][2] =="O" ) )
win4 = 1;
if ( ( board[0][2] =="O" ) && ( board [1][1] =="O" ) && ( board [2][0] =="O" ) )
win4 = 1;
return win4;//returns win4
}
}//end of this huge and hard class
Now if a user tries to override a already occupied square, it will say "dude, can't override..." and automically hand the move to next player without letting the user move again...someopne please help me...

