Tic Tac Toe Java

need some guidance please!!!!

Page 1 of 1

7 Replies - 10806 Views - Last Post: 26 March 2009 - 01:51 AM Rate Topic: -----

#1 jlm476   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-March 09

Tic Tac Toe Java

Posted 25 March 2009 - 01:28 AM

I have an assignment due soon. I have written a code as far as I could, but I need some guidance on how to get it to compile, and build the board for a Tic Tac Toe Game.

This is my assignment:

One of the most fundamental things we do in gaming is make decisions. These decisions are based on the current state of the game environment, user inputs, and other factors. Even something as simple as Tic-Tac-Toe requires decision making if the computer is playing against a human. In this lab we concentrate on analyzing the Tic-Tac-Toe board to determine if X or O has won the game.
Consider the following Tic-Tac-Toe board:

Posted Image

Clearly, X has won the game. But how would the computer know this?
Let us represent the board as a two-dimensional array of numbers, with three rows and three columns. A zero in any location means the board position is blank. A one in any position represents an X. A two in any position represents an O.
In this example, there are one’s in positions [0][0], [1][1], [2][1], and [2][2]. You should be able to figure out where the zero’s and two’s are.
Your program must do the following:
1. Read a two-dimensional array and display the game board as shown above.
2. Examine all three rows, all three columns, and both diagonals to see if there are three one’s (X’s) in a row. If so, display a message that says X has won the game.
3. Examine all three rows, all three columns, and both diagonals to see if there are three two’s (O’s) in a row. If so, display a message that says O has won the game.
4. If neither X nor O has won the game, and there are no blank board positions, then display a message that says no one has won the game.
5. If neither X nor O has won the game and there are blank board positions, then display a message that says the game continues.

Here are the board scenarios you must use to test your program. You must determine the arrays necessary to represent these boards. Put the six arrays in your program and pass them one at a time to a routine that will display the board and check the X’s and O’s to see if there is a winner.
Only one execution will be required, since your program will display and check all six board scenarios when it runs.

Posted Image

It is easy to pass an array to a method. Just declare an array variable in the method definition and access the array within the method as you would any other array.


this is the code that I have so far:
 
class Threeinarow
{

public static void main(String args[]) //Input for each board.
{

int array1[][] = {{1,2,0},{2,1,0},{2,1,1}}; 
int array2[][] = {{1,2,2},{1,2,0},{2,1,1}}; 
int array3[][] = {{1,2,0},{2,1,0},{2,1,2}}; 
int array4[][] = {{2,2,1},{1,1,2},{2,1,1}}; 
int array5[][] = {{1,2,0},{2,2,0},{1,1,1}}; 
int array6[][] = {{1,2,2},{2,1,2},{1,1,2}}; 


testarray(array1); 
testarray(array2); 
testarray(array3); 
testarray(array4); 
testarray(array5); 
testarray(array6);


}
}





public void testarray (int array[][])


 

{ 
bool xwin = false; 
bool owin = false; 
bool nwin = false; 
//checks to see if X won 
for(int i = 0; i < 3; i++) 
{ 
if(array[i][0] == 1 & array[i][1] == 1 & array[i][2] == 1)xwin = true; 
if(array[0][i] == 1 & array[1][i] == 1 & array[2][i] == 1)xwin = true; 
} 
if(array[0][0] == 1 & array[1][1] == 1 & array[2][2] == 1)xwin = true; 
if(array[2][0] == 1 & array[1][1] == 1 & array[0][2] == 1)xwin = true; 

//checks to see if O won 
for(int j = 0; j < 3; j++) 
{ 
if(array[i][0] == 2 & array[i][1] == 2 & array[i][2] == 2)owin = true; 
if(array[0][i] == 2 & array[1][i] == 2 & array[2][i] == 2)owin = true; 
} 
if(array[0][0] == 2 & array[1][1] == 2 & array[2][2] == 2)owin = true; 
if(array[2][0] == 2 & array[1][1] == 2 & array[0][2] == 2)owin = true; 

//checks to see if no one won 

						  
for(int k = 0; k < 3; k++) 
{ 
if(array[i][0] == 0 || array[i][1] == 0 || array[i][2] == 0)nwin = true; 
} 

if(xwin == true)System.out.println("X has won the game."); 
if(owin == true)System.out.println("O has won the game."); 
if(nwin == true)System.out.println("No one has won the game."); 
if((xwin == owin) == (nwin == false))System.out.println("The game continues."); 
} 





Can anyone guide me in finishing this? It would be really appreciated. I keep getting the error "class,interface, or enum expected"

Thanks

This post has been edited by jlm476: 25 March 2009 - 01:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe Java

#2 Mikeyp926   User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Tic Tac Toe Java

Posted 25 March 2009 - 01:46 AM

Hmm...I'm slightly confused about your code. Is it one class or two?
If it is all supposed to be one class then you have some problems.

1.) There are two closing braces "}" at the end of your main function. There should only be one of these, unless the testarray function is in another class.

2.) Also, if both these functions are in the same class, then you will want to make the testarray method "static"
public static void testarray (int array[][])
{
...
}



3.) The bool's should really be "boolean", that is the keyword in java

4.) You also have some problems with your if statements. you use "i" as the index sometimes that you should be using "j" or "k"


Hopefully this helps. It would also help if you clarified if everything is supposed to be in one class or not. If it is, then removing that second closing brace at the end of the main function might fix that error that you mentioned.

-Michael

This post has been edited by Mikeyp926: 25 March 2009 - 01:47 AM

Was This Post Helpful? 0
  • +
  • -

#3 jlm476   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-March 09

Re: Tic Tac Toe Java

Posted 25 March 2009 - 02:03 AM

I did everything you said.. and the code compiles, but then when I try to execute it, it gives me this error:

java.lang.NoClassDefFoundError: ThreeinaRow (wrong name: Threeinarow)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
Exception in thread "main"
Process completed.


about the different classes, Im not sure if I need one or two... I assume that is what my big problem is... according to the error above. any suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 jlm476   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-March 09

Re: Tic Tac Toe Java

Posted 25 March 2009 - 02:11 AM

and I guess I also need to make the board somehow??

this is confusing.
Was This Post Helpful? 0
  • +
  • -

#5 Mikeyp926   User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Tic Tac Toe Java

Posted 25 March 2009 - 02:12 AM

Ok, since you haven't been specifically told to use 2 different classes, I would recommend just sticking with one. Also, in Java you must make sure that your class name and your file name are the exact same, including capitals, so the errors are probably due to the fact that one of the two names has a letter capitalized that the other one doesn't.

Here is how I would change your code.

class Threeinarow
{

   public static void main(String args[]) //Input for each board.
   {

int array1[][] = {{1,2,0},{2,1,0},{2,1,1}};
int array2[][] = {{1,2,2},{1,2,0},{2,1,1}};
int array3[][] = {{1,2,0},{2,1,0},{2,1,2}};
int array4[][] = {{2,2,1},{1,1,2},{2,1,1}};
int array5[][] = {{1,2,0},{2,2,0},{1,1,1}};
int array6[][] = {{1,2,2},{2,1,2},{1,1,2}};


testarray(array1);
testarray(array2);
testarray(array3);
testarray(array4);
testarray(array5);
testarray(array6);


}

public static void testarray (int array[][])
{
boolean xwin = false;
boolean owin = false;
boolean nwin = false;
//checks to see if X won
for(int i = 0; i < 3; i++)
{
if(array[i][0] == 1 & array[i][1] == 1 & array[i][2] == 1)xwin = true;
if(array[0][i] == 1 & array[1][i] == 1 & array[2][i] == 1)xwin = true;
}
if(array[0][0] == 1 & array[1][1] == 1 & array[2][2] == 1)xwin = true;
if(array[2][0] == 1 & array[1][1] == 1 & array[0][2] == 1)xwin = true;

//checks to see if O won
for(int j = 0; j < 3; j++)
{
if(array[j][0] == 2 & array[j][1] == 2 & array[j][2] == 2)owin = true;
if(array[0][j] == 2 & array[1][j] == 2 & array[2][j] == 2)owin = true;
}
if(array[0][0] == 2 & array[1][1] == 2 & array[2][2] == 2)owin = true;
if(array[2][0] == 2 & array[1][1] == 2 & array[0][2] == 2)owin = true;

//checks to see if no one won

						 
for(int k = 0; k < 3; k++)
{
if(array[k][0] == 0 || array[k][1] == 0 || array[k][2] == 0)nwin = true;
}

if(xwin == true)System.out.println("X has won the game.");
if(owin == true)System.out.println("O has won the game.");
if(nwin == true)System.out.println("No one has won the game.");
if((xwin == owin) == (nwin == false))System.out.println("The game continues.");
}
}



I'm not sure if the output is what you expect, but this compiles and runs fine for me.
If you get any more errors let me know, but this code runs fine for me.
Make very sure that your file name and your class name are the exact same.
Example. class name: "Threeinarow" file name: "Threeinarow.java"
If your names are different nothing will work at all.

Hope this helps!
-Michael

This post has been edited by Mikeyp926: 25 March 2009 - 02:13 AM

Was This Post Helpful? 1
  • +
  • -

#6 jlm476   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-March 09

Re: Tic Tac Toe Java

Posted 25 March 2009 - 03:49 AM

great!, you were right... I had them named wrong! it worked now!

but now, I need the actual board layout...

do you know I can start to configure this... I guess I would need to use the symbols | and --- (according to my assignment, that is what the symbols look like?)
Was This Post Helpful? 0
  • +
  • -

#7 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Tic Tac Toe Java

Posted 25 March 2009 - 04:14 AM

You're calling a method from class main. That method should be static. You're missing some "} {". Indents aren't just pretty, they're required if you're going to figure out what's going on.

Beyond that, there are a number of fundamental issues. I added comments.

class Threeinarow {
	//Input for each board.
	public static void main(String args[]) {
		int array1[][] = {{1,2,0},{2,1,0},{2,1,1}}; 
		int array2[][] = {{1,2,2},{1,2,0},{2,1,1}}; 
		int array3[][] = {{1,2,0},{2,1,0},{2,1,2}}; 
		int array4[][] = {{2,2,1},{1,1,2},{2,1,1}}; 
		int array5[][] = {{1,2,0},{2,2,0},{1,1,1}}; 
		int array6[][] = {{1,2,2},{2,1,2},{1,1,2}}; 
		
		testarray(array1); 
		testarray(array2); 
		testarray(array3); 
		testarray(array4); 
		testarray(array5); 
		testarray(array6);
	}

	// missing a static
	public static void testarray (int array[][]) { 
		bool xwin = false; 
		bool owin = false; 
		bool nwin = false; 
		
		// logic is wrong, you keep resetting the answer
		//checks to see if X won 
		for(int i = 0; i < 3; i++) { 
			// bitwise & is wrong, needs to be logical &&
			if(array[i][0] == 1 && array[i][1] == 1 && array[i][2] == 1)xwin = true; 
			if(array[0][i] == 1 && array[1][i] == 1 && array[2][i] == 1)xwin = true; 
		} 
		if(array[0][0] == 1 & array[1][1] == 1 & array[2][2] == 1)xwin = true; 
		if(array[2][0] == 1 & array[1][1] == 1 & array[0][2] == 1)xwin = true; 

		//checks to see if O won 
		for(int j = 0; j < 3; j++) { 
			if(array[i][0] == 2 & array[i][1] == 2 & array[i][2] == 2)owin = true; 
			if(array[0][i] == 2 & array[1][i] == 2 & array[2][i] == 2)owin = true; 
		} 
		if(array[0][0] == 2 & array[1][1] == 2 & array[2][2] == 2)owin = true; 
		if(array[2][0] == 2 & array[1][1] == 2 & array[0][2] == 2)owin = true; 

		//checks to see if no one won 
		// poor logic, not close to right
		for(int k = 0; k < 3; k++) { 
			// there is no i
			if(array[i][0] == 0 || array[i][1] == 0 || array[i][2] == 0)nwin = true; 
		} 

		// xwin == true is redundant
		if(xwin)System.out.println("X has won the game."); 
		// if xwin is true, this should never be
		if(owin == true)System.out.println("O has won the game."); 
		// if either of the above is true, this can't be
		if(nwin == true)System.out.println("No one has won the game."); 
		
		// what the hell does this mean?
		if((xwin == owin) == (nwin == false))System.out.println("The game continues."); 
	} 
}



If one condition is met, the rest won't be. With two more methods, your "testarray" could be as simple as:
public static void testarray (int array[][]) {
	if (isWin(array, 1)) {
		System.out.println("X has won the game.");
	} else if (isWin(array, 2)) {
		if(owin == true)System.out.println("O has won the game."); 
	} else if (hasMoves(array)) {
		System.out.println("The game continues."); 
	} else {
		System.out.println("No one has won the game."); 
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#8 jlm476   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-March 09

Re: Tic Tac Toe Java

Posted 26 March 2009 - 01:51 AM

thanks everyone for helping me, when i finally get the correct outcome, i will post.

also, I am having trouble making the board.

I was able to draw it manually using "X's, O's, |, and ---

does anyone have an idea on how to make this with an array to represent the boards?

This post has been edited by jlm476: 26 March 2009 - 01:52 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1