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...

[23289 byte] By [Razaaca] at [2007-11-27 3:46:10]
# 1

Yikes! There's a lot of refactoring (break it down into smaller blocks of code and place these in separate methods) that can be done.

To solve your problem, I would have another method that returms a boolen. The method checks to see what is

stored in that slot of the 2D array. Eg default values in each spot could be a . or a space or anything you choose.

hen if the slot has the default value return true.

Then when a player attempts a move call that method.

if(spaceIsAvailable(0,0) {

player makes move

switch players

} else {

print error message

// do not switch players so when you go around again the same player makes a move

}

floundera at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 2
I don't exactly know how to operate a boolean method. never learned about it...;_;
Razaaca at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 3
A boolean method is no different to any other method, it just has a return type of boolean. Then in your method you have code that determines if the space chosen is available and returns true or false based on that logic.
floundera at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 4
Can you just show me a layout? Like some example code? Like i said... i have NO idea how boolean works...
Razaaca at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 5

Yes you do, you just refuse to allow your brain to think.

public boolean isSpaceAvailable(int x, int y) {

if(space is available) {

return true;

} else {

return false;

}

}

floundera at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 6

no, but i have never ever seen a boolean...seriously.

Although let me try.

So i call this method, pass on the choice of the user. in the code, something like

boolean check;

if check = true

pass on the value to mark it

else

error meesage

In the method:

public boolean Check(choice1)

if it is available

return true

else return false

what i don;t get is if the return of the true/false, what is it for? isn't boolean like a variable type? If so, then why is the arguments that it recieves integer? Also, is the true/false for int x/y? or is it for the check that i declared? If it's for the check, then what do x/y do?

Razaaca at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 7

Boolean is a primitive that can have two values - true or false. Run this and see if you understand:

class OddsEvens {

int[] numbers = {1,2,4,5,7,8};

public void checkNumbers() {

for(int index = 0; index < numbers.length; index++) {

System.out.print(numbers[index] + " is ");

if(isEven(numbers[index])) {

System.out.println("even");

} else {

System.out.println("odd");

}

}

}

private boolean isEven(int value) {

if(value %2 == 0) {

return true;

} else {

return false;

}

}

public static void main(String[] args) {

OddsEvens oe = new OddsEvens();

oe.checkNumbers();

}

}

floundera at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 8
Can i have a switch inside of a boolean method?if yes, how would it work?i kinda get it, but not exactly...
Razaaca at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 9
private boolean isEven(int value) {return value %2 == 0;}
DrLaszloJamfa at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 10
> Can i have a switch inside of a boolean method?> if yes, how would it work?> > i kinda get it, but not exactly...A switch statement is the same, anywhere you can write it. It makes no difference. Don't psych yourself out.
DrLaszloJamfa at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 11
return value %2 == 0;I realise there are several ways to make that code better but I thought for someone who is struggling to understand things, I would spell it out and not take shortcuts ;)
floundera at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 12

public static boolean xcheck ( int x1 ) // use x to recieve choice

{

int thingy = x1; // thingy is the private copy of x

switch ( thingy ) // switch for thingy

{

case 1:

if ( board[0][0] == " " )

return false;

else

return true;

break;

So this should be no problem right? But when i compile it says that i have to have a return statement at bottom. So should i make it return true or false?

Razaaca at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 13

You're sort of understanding. To answer your question, if thingy is not 1 you bypass this case and it never hits a return statement. So yes you would need soemthing at the bottom. However, the code can be made a lot more simpler. Pass in two int parameters, the x and y co-ord. Then your method only needs one line:

return board[x][y].equals(" ");

floundera at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 14
...uhh...but my program requires one user input only. It assigns each box a number like so:123456789And the user simply enters a single number, it doesn't have them entering the row and the column...
Razaaca at 2007-7-12 8:49:54 > top of Java-index,Java Essentials,New To Java...
# 15
Actually, i got it. I finished it. it works perfectly now. Thank you very much to all that helped me!Which includes Flounder, Dr.Laslo, Prometheuzz, fastmike, and Lopatin in the other threads. Thanks to all!
Razaaca at 2007-7-21 20:55:30 > top of Java-index,Java Essentials,New To Java...