Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Tic-Tac-Toe program

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Tic-Tac-Toe program *editted*

    I've been working on this but still haven't gotten much progress. I haven't done much changes compared to what it was before this. Basically I just put the if/else control statements into subroutines to help the main routine look a bit cleaner but I still haven't found a way to shorten the code up (although that can wait until I actually get the program to actually work). I still haven't found a way to enact the winner/loser subroutines, I was thinking about putting them into an if/else and break the program if one of them become true.

    Something like this:
    if (nummoves == 9)
        {
             System.out.println("Cat's game!");
             break;
         }
    else if (winner(a) = true)
        {
            System.out.println("Congratulations! ......");
            break;
         }
    else if // Same thing with the loser(a) statement

    Anyways, I also still haven't figured out a way to program the loop to loop again if the space is taken.

    Updated Code:
    import java.io.*;
     
    public class HW7
    {
     
    	public static void main(String args[]) throws IOException
    	{
     
    	BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));	
     
    import java.io.*;
     
    public class Test 
    {
     
    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)
    	{
    		for (int i = 0; i <= 8; i++)
    		{	
     
    	System.out.println("Your Turn");		
    	System.out.println("Enter: ");
    	int x = Integer.parseInt(keybd.readLine());
     
    	SpaceTaken(a,x);
    	User(a,x);
    	drawBoard(a);
     
    	System.out.println();
    	System.out.println("Computer's Turn:");
     
    	AI(a,x);
    	drawBoard(a);
    	System.out.println();
    	}
    	break;
    	}
    }
     
    public static void drawBoard(int[] a)
    {
     
       String[] s = new String[9];   // since moves were tracked with int array a
       for (int i=0; i<=8; i++)      // instead of String array a, we must now
       {                             // create extra array with X's and O's
          if (a[i]==0)               // instead of 1's and 2's, for nicer output.
             s[i]=" ";               // (Instead, you could have tracked moves
          else if (a[i]==1)          // with String array a, instead of int array a.
             s[i]="X";               // But, that would make the XWins and OWins
          else                       // routines much more complex.)
             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]);
     
    }
     
    public static void User(int[] a, int x)
    {
     
    	if (x == 0 && a[0] == 0)
    		{
    			a[0] = 1;
    		}
    	else if (x == 1 && a[1] == 0)
    		{
    			a[1] = 1;
    		}
    	else if (x == 2 && a[2] == 0)
    		{
    			a[2] = 1;
    		}
    	else if (x == 3 && a[3] == 0)
    		{
    			a[3] = 1;
    		}
    	else if (x == 4 && a[4] == 0)
    		{
    			a[4] = 1;
    		}
    	else if (x == 5 && a[5] == 0)
    		{
    			a[5] = 1;
    		}
    	else if (x == 6 && a[6] == 0)
    		{
    			a[6] = 1;
    		}
    	else if (x == 7 && a[7] == 0)
    		{
    			a[7] = 1;
    		}
    	else if (x == 8 && a[8] == 0)
    		{	
    			a[8] = 1;
    		}	
    }
     
    public static void AI(int[] a, int x)
    {
    	int r = (int) (Math.random() * 9);
     
    	if (r == 0 && a[0] == 0)
    		{
    			a[0] = 2;
    		}
    	else if (r == 1 && a[1] == 0)
    		{
    			a[1] = 2;
    		}
    	else if (r == 2 && a[2] == 0)
    		{
    			a[2] = 2;
    		}
    	else if (r == 3 && a[3] == 0)
    		{
    			a[3] = 2;
    		}
    	else if (r == 4 && a[4] == 0)
    		{
    			a[4] = 2;
    		}
    	else if (r == 5 && a[5] == 0)
    		{
    			a[5] = 2;
    		}
    	else if (r == 6 && a[6] == 0)
    		{
    			a[6] = 2;
    		}
    	else if (r == 7 && a[7] == 0)
    		{
    			a[7] = 2;
    		}
    	else if (r == 8 && a[8] == 0)
    		{	
    			a[8] = 2;
    		}
    }
     
    public static boolean Winner(int[] a)
    {
    	if (a[0] == 1 && a[1] == 1 && a[2] == 1)
    		{
    		return true;
    		}
    	else if (a[3] == 1 && a[4] == 1 && a[5] == 1)
    		{
    		return true;			
    		}
    	else if (a[6] == 1 && a[7] == 1 && a[8] == 1)
    		{
    		return true;		
    		}
    	else if (a[0] == 1 && a[3] == 1 && a[6] == 1)
    		{
    		return true;			
    		}
    	else if (a[1] == 1 && a[4] == 1 && a[7] == 1)
    		{
    		return true;			
    		}
    	else if (a[2] == 1 && a[5] == 1 && a[8] == 1)
    		{
    		return true;			
    		}
    	else if (a[0] == 1 && a[4] == 1 && a[8] == 1)
    		{
    		return true;
    		}
    	else if (a[2] == 1 && a[4] == 1 && a[6] == 1)
    		{
    		return true;
    		}
    	else
    	{
    	return false;
    	}
    }
     
    public static boolean Loser(int[] a)
    {
    	if (a[0] == 2 && a[1] == 2 && a[2] == 2)
    		{
    		return true;
    		}
    	else if (a[3] == 2 && a[4] == 2 && a[5] == 2)
    		{
    		return true;
    		}
    	else if (a[6] == 2 && a[7] == 2 && a[8] == 2)
    		{
    		return true;
    		}
    	else if (a[0] == 2 && a[3] == 2 && a[6] == 2)
    		{
    		return true;
    		}
    	else if (a[1] == 2 && a[4] == 2 && a[7] == 2)
    		{
    		return true;
    		}
    	else if (a[2] == 2 && a[5] == 2 && a[8] == 2)
    		{
    		return true;
    		}
    	else if (a[0] == 2 && a[4] == 2 && a[8] == 2)
    		{
    		return true;
    		}
    	else if (a[2] == 2 && a[4] == 2 && a[6] == 2)
    		{
    		return true;
    		}
    	else
    	{
    	return false;
    	}
    }
     
    public static void SpaceTaken(int[] a, int x)
    {
    	if (x == 0 && (a[0] == 1 || a[0] == 2))
    	{
    		System.out.println("Sorry, space is taken!");
    		return;
    	}
     
    	else if (x == 1 && (a[1] == 1 || a[1] == 2))
    	{
    		System.out.println("Sorry, space is taken!");
    		return;
    	}
     
    	else if (x == 2 && (a[2] == 1 || a[2] == 2))
    	{
    		System.out.println("Sorry, space is taken!");
    		return;
    	}
     
    	else if (x == 3 && (a[3] == 1 || a[3] == 2))
    	{
    		System.out.println("Sorry, space is taken!");
    		return;
    	}
    	else if (x == 4 && (a[4] == 1 || a[4] == 2))
    	{
    		System.out.println("Sorry, space is taken!");
    		return;
    	}
    	else if (x == 5 && (a[5] == 1 || a[5] == 2))
    	{
    		System.out.println("Sorry, space is taken!");
    		return;
    	}
    	else if (x == 6 && (a[6] == 1 || a[6] == 2))
    	{
    		System.out.println("Sorry, space is taken!");
    		return;
    	}
    	else if (x == 7 && (a[7] == 1 || a[7] == 2))
    	{
    		System.out.println("Sorry, space is taken!");
    		return;
    	}
    	else if (x == 8 && a[8] != 0)
    	{
    		System.out.println("Sorry, space is taken!");
    		return;
    	}
    }
     
    }

    At this point in the program, it prints out the following (with overlaps, since I haven't put in any code for it yet. Also the subroutines aren't working as I wanted but I think it may be because I haven't done anything telling the program to break once it turns true.)

    Hello! Welcome to a one-player version of Tic-Tac-Toe!
    Please enter your name: k
       |   |  
    ---+---+---
       |   |  
    ---+---+---
       |   |  
    Enter: 
    0
     X |   |  
    ---+---+---
       | O |  
    ---+---+---
       |   |  
    Enter: 
    1
     X | X |  
    ---+---+---
       | O |  
    ---+---+---
     O |   |  
    Enter: 
    2
     X | X | X
    ---+---+---
       | O |  
    ---+---+---
     O | O |  
    Enter: 
    3
     X | X | X
    ---+---+---
     X | O |  
    ---+---+---
     O | O |  
    Enter: 
    4
    Cat's game!
    Last edited by Actinistia; April 28th, 2011 at 03:08 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tic-Tac-Toe program

    I'm not sure if this helps because my logic will probably be different considering I built my tic tac toe game in GUI not console. I find my logic could also be more efficient but it was for an assignment so I just needed it to work.

    I have the (buttons)board in a 2-D Array and all I do is check the text of the buttons and compare them to all the possible win combinations, if its true, i disable the board.
    Ive put a few examples to save room.

    the boolean "win" variables are only used to keep score in another part of my program so you may not need those.

     
    if (b[0][0].getText().equals("o") && b[0][1].getText().equals("o")
                    && b[0][2].getText().equals("o")) {
     
                oWin = true;
     
                for (int i = 0; i <= 2; i++) {
     
                    for (int j = 0; j <= 2; j++) {
     
                        b[i][j].setEnabled(false);
     
                    }
                }
     
                //*******************
                //START DIAGONAL WINS
                //*******************
     
            } else if (b[0][0].getText().equals("o") && b[1][1].getText().equals("o")
                    && b[2][2].getText().equals("o")) {
     
     
                oWin = true;
     
                for (int i = 0; i <= 2; i++) {
     
                    for (int j = 0; j <= 2; j++) {
     
                        b[i][j].setEnabled(false);
     
                    }
                }
     
            } else if (b[0][0].getText().equals("x") && b[1][1].getText().equals("x")
                    && b[2][2].getText().equals("x")) {
     
     
                xWin = true;
     
                for (int i = 0; i <= 2; i++) {
     
                    for (int j = 0; j <= 2; j++) {
     
                        b[i][j].setEnabled(false);
     
                    }
                }
     
            }

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Tic-Tac-Toe program

    Thanks dego89 for the response. I think based on your logic, my win/lose combinations should be alright but I'm not sure what the for loops did but I'm guessing it's for "disabling" your board as you stated. (Sorry, I'm still relatively new to java, so code like that confuses me a bit).
    Last edited by Actinistia; April 29th, 2011 at 01:04 PM.

Similar Threads

  1. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM