# Thread: basic tic tac toe game help

1. Member
Join Date
Mar 2011
Posts
27
Rep Power
0
Originally Posted by Fubarable
My suggestion to you is to first fix your methods so that they return boolean.

Next, you should do try to do each recommendation of your instructor one at a time. Test and modify the resulting code until it works, and don't work on the next instruction until the one you're working on is done and bullet proof. Don't try to do the whole thing at once. Then if you have a problem, you can ask about that specific instruction, your code and your problem, and we'll all have an easier time with this.

Java Code:
```import java.io.*;
public class HW7 {
public static void main(String args[]) throws IOException
{

int[] a = new int[9];

int nummoves=0;

while (true)
{

drawBoard(a);

System.out.print("Enter a number to place an X on the board above: ");

for (int i=0; i<=8; i++)
{
if (n==0 || n==1 || n==2 || n==3 || n== 4 || n==5 || n==6 || n==7 || n==8)
{
a[i]=1;
}
}

nummoves +=1;

XWins(a);

if (nummoves==9)
{
System.out.println ("CAT'S GAME!");
drawBoard(a);
return;
}

int r = (int) (Math.random() * 9);

for (int i=0; i<=8; i++)
{
if (r==0 || r==1 || r==2 || r==3 || r== 4 || r==5 || r==6 || r==7 || r==8)
{
a[i]=2;
}
}

nummoves +=1;

OWins(a);

}
}

public static boolean XWins(int[] a)
{

if (a[0]==1 && a[1]==1 && a[2]==1)
{
System.out.println ("X WINS!");
}

else if (a[3]==1 && a[4]==1 && a[5]==1)
{
System.out.println ("X WINS!");
}

else if (a[6]==1 && a[7]==1 && a[8]==1)
{
System.out.println ("X WINS!");
}

else if (a[0]==1 && a[3]==1 && a[6]==1)
{
System.out.println ("X WINS!");
}

else if (a[1]==1 && a[4]==1 && a[7]==1)
{
System.out.println ("X WINS!");
}

else if (a[2]==1 && a[5]==1 && a[8]==1)
{
System.out.println ("X WINS!");
}

else if (a[0]==1 && a[4]==1 && a[8]==1)
{
System.out.println ("X WINS!");
}

else if (a[2]==1 && a[4]==1 && a[6]==1)
{
System.out.println ("X WINS!");
}
return true;
}

public static boolean OWins(int[] a)
{

if (a[0]==2 && a[1]==2 && a[2]==2)
{
System.out.println ("O WINS!");
}

else if (a[3]==2 && a[4]==2 && a[5]==2)
{
System.out.println ("O WINS!");
}

else if (a[6]==2 && a[7]==2 && a[8]==2)
{
System.out.println ("O WINS!");
}

else if (a[0]==2 && a[3]==2 && a[6]==2)
{
System.out.println ("O WINS!");
}

else if (a[1]==2 && a[4]==2 && a[7]==2)
{
System.out.println ("O WINS!");
}

else if (a[2]==2 && a[5]==2 && a[8]==2)
{
System.out.println ("O WINS!");
}

else if (a[0]==2 && a[4]==2 && a[8]==2)
{
System.out.println ("O WINS!");
}

else if (a[2]==2 && a[4]==2 && a[6]==2)
{
System.out.println ("O WINS!");
}
return true;

}

public static void drawBoard(int[] a)
{

String[] s = new String[9];
for (int i=0; i<=8; i++)
{
if (a[i]==0)
s[i]=" ";
else if (a[i]==1)
s[i]="X";
else
s[i]="O";
}

System.out.println(" "+s[0]+" | "+s[1]+" | "+s[2]);
System.out.println("---+---+---");
System.out.println(" "+s[3]+" | "+s[4]+" | "+s[5]);
System.out.println("---+---+---");
System.out.println(" "+s[6]+" | "+s[7]+" | "+s[8]);

}

}```
here's the output:

B | B | B // The B's stand for blank (it's actually blank on the code's output but when I post here the alignment of the board is messed up unless it's filled)
---+--+---
B | B | B
---+--+---
B | B | B

Enter a number to place an X on the board above: 1
X WINS!
O WINS!
O | O | O
---+--+---
O | O | O
---+--+---
O | O | O
Enter a number to place an X on the board above:
Last edited by java157; 04-23-2011 at 09:11 PM.

2. OK, let's look at this method:

Java Code:
`	public static boolean OWins(int[] a)`

It's definition, the line I posted above suggests that it will return a boolean value, true or false. This value should be true if O in fact wins, and false if O in fact doesn't win. Your method body as shown below returns true always regardless of if O wins or loses. Your method also has needless System.out.println calls:

Java Code:
```	public static boolean OWins(int[] a)
{

if (a[0]==2 && a[1]==2 && a[2]==2)
{
System.out.println ("O WINS!");
}

else if (a[3]==2 && a[4]==2 && a[5]==2)
{
System.out.println ("O WINS!");
}

else if (a[6]==2 && a[7]==2 && a[8]==2)
{
System.out.println ("O WINS!");
}

else if (a[0]==2 && a[3]==2 && a[6]==2)
{
System.out.println ("O WINS!");
}

else if (a[1]==2 && a[4]==2 && a[7]==2)
{
System.out.println ("O WINS!");
}

else if (a[2]==2 && a[5]==2 && a[8]==2)
{
System.out.println ("O WINS!");
}

else if (a[0]==2 && a[4]==2 && a[8]==2)
{
System.out.println ("O WINS!");
}

else if (a[2]==2 && a[4]==2 && a[6]==2)
{
System.out.println ("O WINS!");
}
return true;

}```

I suggest you get rid of all System.out.println calls from this method and have it do one thing and one thing alone -- return true if O wins and false if it doesn't (return false; can be the last line of the method as a default value if none of the return true; lines are ever reached).

The code that calls this method from within the main method can then call a println statement depending on the boolean returned by calling this method.

Edit 1:

For example:

Java Code:
```import java.util.Random;

public class BooleanMethodEg {
private static Random random = new Random();

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {

// here I'll call the method

// here I'll do a println depending on the results.
} else {
System.out.println("Coin flip is Tails!");
}
}
}

}
}```
Last edited by Fubarable; 04-23-2011 at 09:01 PM.

Page 2 of 2 First 12

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•