Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22
  1. #21
    java157 is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    I took your suggestion:

    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]);
    
    	}
    
    }
    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 10:11 PM.

  2. #22
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 10:01 PM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. basic snake game
    By Ruuhkis in forum New To Java
    Replies: 1
    Last Post: 03-05-2011, 06:40 PM
  2. Java game dev, Basic AI
    By Kerrai in forum New To Java
    Replies: 1
    Last Post: 12-07-2010, 11:49 AM
  3. Basic Football management game
    By Wakesta in forum Java Gaming
    Replies: 1
    Last Post: 07-05-2010, 11:50 PM
  4. Basic counter timer for a game?
    By Laura in forum New To Java
    Replies: 1
    Last Post: 04-18-2010, 02:46 AM
  5. Beginner at very basic game development
    By codermom in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 05:22 AM

Posting Permissions

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