Results 21 to 22 of 22
Thread: basic tic tac toe game help
- 04-23-2011, 08:49 PM #21
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
I took your suggestion:
here's the output:Java Code:import java.io.*; public class HW7 { public static void main(String args[]) throws IOException { BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in)); 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: "); int n = Integer.parseInt(keybd.readLine()); 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]); } }
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.
-
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 boolean coinFlipHeads = coinFlipIsHeads(); // here I'll do a println depending on the results. if (coinFlipHeads) { System.out.println("Coin flip is Heads!"); } else { System.out.println("Coin flip is Tails!"); } } } public static boolean coinFlipIsHeads() { boolean heads = random.nextBoolean(); return heads; } }Last edited by Fubarable; 04-23-2011 at 09:01 PM.
Similar Threads
-
basic snake game
By Ruuhkis in forum New To JavaReplies: 1Last Post: 03-05-2011, 05:40 PM -
Java game dev, Basic AI
By Kerrai in forum New To JavaReplies: 1Last Post: 12-07-2010, 10:49 AM -
Basic Football management game
By Wakesta in forum Java GamingReplies: 1Last Post: 07-05-2010, 10:50 PM -
Basic counter timer for a game?
By Laura in forum New To JavaReplies: 1Last Post: 04-18-2010, 01:46 AM -
Beginner at very basic game development
By codermom in forum New To JavaReplies: 4Last Post: 01-20-2009, 04:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks