TicTacToe help!!!
I am trying to make a TicTacToe game, and this is what i have, when i choose the coordinates, it gives an "exception"
class TicTacToeOtherClass
{//start class
private String board[][];
public String TicTacToeOtherClass()
{// start constructor
board =new String [3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
board[i][j] =" ";
return"";
}//end constructor
publicvoid set(int i,int j, String current)
{
if (board[i][j].equals(" "))
board[i][j] = current;
}
public String playBoard()
{
System.out.println();
System.out.println ("******************" );
for (int row = 0; row < 3; row++ )
{
for (int column = 0; column < 3; column++ )
{
System.out.printf ("****\n" );
}
System.out.println ("******************" );
}
return"";
}
}// end of class
// tic tac toe game
import java.util.Scanner;
publicclass TicTacToeTest
{
publicstaticvoid main(String[] args)
{
Scanner input =new Scanner( System.in );
String current ="X";
TicTacToeOtherClass game =new TicTacToeOtherClass();
int display = 0;
while ( display == 0)
{
System.out.print( game.playBoard() );
System.out.printf("%s, choose your row.", current );
int row = input.nextInt();
if ( row <= 0 || row > 2 )
{
System.out.printf("%s, can't do that homey.", current );
row = input.nextInt();
}
else
{
System.out.printf("%s, choose your column.", current );
int column = input.nextInt();
game.set( row, column, current );
if ( current.equals("X"))
current ="O";
else
current ="X";
}
}
}
}
[4152 byte] By [
Razaaca] at [2007-11-27 3:29:20]

> I am trying to make a TicTacToe game, and this is> what i have, when i choose the coordinates, it gives> an "exception"An exception is not specific enough, I'm afraid.
Exception in thread "main" java.lang.NullPointerExceptionat TicTacToeOtherClass.set(TicTacToeOtherClass.java:17)at TicTacToeTest.main(TicTacToeTest.java:35)
> Exception in thread "main"
> java.lang.NullPointerException
> at
> TicTacToeOtherClass.set(TicTacToeOtherClass.java:17)
> at TicTacToeTest.main(TicTacToeTest.java:35)
board[j] is probably still null because you have not initialized it.
Hint: a constructor does not have a return type.
yeah,if i don't use it, it gives me this error that says, "missing return statement".
constructors dont return anything
it should look like this
public TicTacToeOtherClass()
{// start constructor
board = new String [3][3];
for ( int i = 0; i < 3; i++)
for ( int j = 0; j < 3; j++)
board[i][j] = " ";
}//end constructor
but it gives me this if i don't add the return thingy:- Java Compiler -C:\javaprog\TicTacToeOtherClass.java:13: missing return statement}//end constructor^1 errorOutput completed (1 sec consumed) - Normal Termination
did you get rid of the String on the first line of the constructor?youve got to delete the return statement, which you are, and the return type, which im not sure your doing
okay, ti works now, but if i enter the coordinates, it doesn't display the X and O on the board, it just gives back an empty board and asks for the next player to move...plzplazplaz help me! This is very important, i also need a segment of code to check for winners...can someone please help me?
hmmm this is some messy code, partly because im pretty sure it isnt possible to make an efficient tic tac toe console application lol
but the reason nothing is being displayed when you enter the coordinates is this. think about it, you input your coordinates, your next step is to update your board array so that it has the info. but your program doesnt do anything that will display that new info to the user. all you basically do is you have all the in the two dimensional array of where there is an X and where there is an O, but you have no methods or anything that will display these new changes. and personally i think that making that in a console application would be very clunky, thats why i suggest making such a game with java 2d graphics
theres alot of neat sites with tutorials and exapmples for such games
oh and read up on void methods =) it'll help you alot
good luck buddy
dude, i can't use GUI, it's just not allowed. Also, where do i find void tutorials?
there's this guy who did this,
public String toString()
{
String r = "";
for (int i = 0; i < ROWS; i++)
{
r = r + "|";
for (int j = 0; j < COLUMNS; j++)
r = r + board[i][j];
r = r + "|\n";
}
return r;
}
It works, but it isn't a grid...
can you help me use the same stragedy on mine?
well if gui isnt allowed, then i guess youve got to call playBoard() to redraw the board everytime you make a move
(make playBoard() void)
and instead of calling it like this System.out.print( game.playBoard() );
simply write game.playBoard();
youve got to make playBoard() display the data in the board array along with drawing the board its self with the asterisks
as for the void thing, just look at any basic java documentation or java introduction online, and im sure they will have a good explanation on when and how to use void methods
- Java Compiler -
C:\javaprog\TicTacToeOtherClass.java:20: '(' expected
public void String playBoard()
^
1 error
Output completed (0 sec consumed) - Normal Termination
Or is that a wrong way to declare it?
Also, can i just have more hints? I am a noob and i can't think right now cause i am too tired. I stayed up really late for this...If you can just give me the code, that would be great, but that really doesn't help me, so...can i have more"code-oriented" hints?
haha well im just trying to do what i can
to fix your declaration, just take out the String
the basic construction of a method is first you declare whether it is public or private, second you declare the return type(what type of variable the method returns), then you write the method name with the arguments in parentheses
the return type can be either int
char
double
String
etc ... or void
void means the method doesnt return anything, so instead of the return type you write void
here you wrote void, then String, doesnt make sense because void means the method returns nothing, and String means the method returns a String
Thanks you, i got that done, nwo how bout helping me with the rest of code? I am really tired right now and i am dying, my brain is a mess and i can;t think. I shouldn; have procrastinated...
> Thanks you, i got that done, nwo how bout helping me
> with the rest of code?
You don't need help, you need a servant!
> I am really tired right now
> and i am dying, my brain is a mess and i can;t think.
> I shouldn; have procrastinated...
Then stop programming and get some rest.
Okay...geez, you don't have to snap at me...Sorry to Lopatin if i somehow offended you as prometheuzz pointed out.
I am gonna get rest, except this is due tomorrow...oh well. I got this other version done.
Can you look at it and see if you cna make improvements?
// java final project
// tic tac toe game
import java.util.Scanner;
public class TicTacToe
{
public static String board [][] = new String [3][3];
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
int boxNumber;
String name1;
String name2;
String current = "X";
int choice;
int box = 0;
System.out.println( "Enter Player1's name: " );
name1 = input.nextLine();
System.out.println( "Enter Player2's name: " );
name2 = input.nextLine();
for ( int i = 0; i < 3; i++)
{
for ( int j = 0; j < 3; j++)
{
board[i][j] = " ";
}
}
System.out.println();
System.out.println ( "******************" );
for (boxNumber = 1; boxNumber <= 3; boxNumber++)
{
for ( int row = 0; row < 1; row++ )
{
for ( int column = 0; column < 3; column++ )
{
System.out.printf ( "****\n" );
}
System.out.println ( "******************" );
}
}
int winner = 0;
while (winner == 0)
{
System.out.printf( "%s, make your move.", name1 );
choice = input.nextInt();
if ( choice <= 0 || choice > 9 )
{
System.out.printf( "%s, can't do that homey.", name1 );
choice = input.nextInt();
}
else
{
xmark(choice);
winner = CheckOneWin ( winner );
if (current == ("X"))
{
current = "O";
for ( int row1 = 0; row1 < 3; row1++ )
{
System.out.println ( " ******************" );
System.out.println ( " ****" );
for ( int column1 = 0; column1 < 3; column1++ )
{
System.out.printf ( "%3s%3s" , "*" , board[row1][column1] );
}
System.out.printf ( "%4s\n%s\n" , "*" , " ***" );
}
System.out.println ( " ******************" );
System.out.printf( "%s, make your move.", name2 );
choice = input.nextInt();
if ( choice <= 0 || choice >9 )
{
System.out.printf( "%s, can't do that homey.", name2 );
choice = input.nextInt();
}
else
{
omark(choice);
winner = CheckTwoWin ( winner );
for ( int row2 = 0; row2 < 3; row2++ )
{
System.out.println ( " ******************" );
System.out.println ( " ****" );
for ( int column2 = 0; column2 < 3; column2++ )
{
System.out.printf ( "%3s%3s" , "*" , board[row2][column2] );
}
System.out.printf ( "%4s\n%s\n" , "*" , " ***" );
}
System.out.println ( " ******************" );
}
}
else
current = "X";
}
if (winner == 1)
{
System.out.printf("\n%s wins, way to go homey!", name1);
}
if (winner == 2)
{
System.out.printf("\n%s wins, way to go homey!", name2);
}
}//end of hugmongus while loop
}
public static void xmark ( int x )
{
int thingy = x;
switch ( thingy )
{
case 1:
board[0][0] = "X";
break;
case 2:
board[0][1] = "X";
break;
case 3:
board[0][2] = "X";
break;
case 4:
board[1][0] = "X";
break;
case 5:
board[1][1] = "X";
break;
case 6:
board[1][2] = "X";
break;
case 7:
board[2][0] = "X";
break;
case 8:
board[2][1] = "X";
break;
case 9:
board[2][2] = "X";
break;
}
}
public static void omark ( int o )
{
int thingy = o;
switch ( thingy )
{
case 1:
board[0][0] = "O";
break;
case 2:
board[0][1] = "O";
break;
case 3:
board[0][2] = "O";
break;
case 4:
board[1][0] = "O";
break;
case 5:
board[1][1] = "O";
break;
case 6:
board[1][2] = "O";
break;
case 7:
board[2][0] = "O";
break;
case 8:
board[2][1] = "O";
break;
case 9:
board[2][2] = "O";
break;
}
}
public static int CheckOneWin( int win1 )
{
int win2 = win1;
for ( int check = 0; check < 3 ; check++ )
{
if ( ( board[check][0] == "X" ) && ( board [check][1] == "X" ) && ( board [check][2] == "X" ) )
win2 = 1;
if ( ( board[0][check] == "X" ) && ( board [1][check] == "X" ) && ( board [2][check] == "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;
}
public static int CheckTwoWin( int win3 )
{
int win4 = win3;
for ( int check2 = 0; check2 < 3 ; check2++ )
{
if ( ( board[check2][0] == "O" ) && ( board [check2][1] == "O" ) && ( board [check2][2] == "O" ) )
win4 = 2;
if ( ( board[0][check2] == "O" ) && ( board [1][check2] == "O" ) && ( board [2][check2] == "O" ) )
win4 = 2;
}
if ( ( board[0][0] == "O" ) && ( board [1][1] == "O" ) && ( board [2][2] == "O" ) )
win4 = 2;
if ( (board[0][2] == "O" ) && ( board [1][1] == "O" ) && ( board [2][0] == "O" ) )
win4 = 2;
return win4;
}
}//end
> Okay...geez, you don't have to snap at me...Sorry to
> Lopatin if i somehow offended you as prometheuzz
> pointed out.
> I am gonna get rest, except this is due tomorrow...oh
> well. I got this other version done.
> Can you look at it and see if you cna make
> improvements?
You'll have to tell what's wrong with it.
One thing that is going wrong is that you should not compare Strings with the == operator.
Don't do:board[check2][0] == "O";
but use their equals(...) method:board[check2][0].equals("O");
Details:
http://access1.sun.com/FAQSets/newtojavatechfaq.html#9
Oh, nothing is wrong with it, it runs fine, it's just that the board is kinda messed up and that's it.
The == works, but i'll change it to .equals if it works better.
If you can help me with a method that checks if a square is already taken and tells the user that, that would be nice. It is not needed tho.
no man no offence at all
i enjoy helping
this would be a method that checks if a spot is already taken
public boolean spotTaken(int row, int column){
if(board[row][column].equals(" "))
return false; // if spot empty return false
else return true; // if spot taken return true
}
as for the .equals() thing, i can go into a whole lecture(which i wont lol) about this memory saving feature that in some circumstances allows Strings to be compared with ==
LOL, lectures, i think that my teachers can help me with that...i had enough of those in the past weeks...brrrr....
No, wait, the method you providd is for the one with the two classes...right?I need one for the one that i posted on this page. check reply #17