Results 1 to 20 of 20
  1. #1
    Momar99 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Help with tic tac toe java program?

    Hey guys, i'm in an intro to java class and i'm supposed to make a tic tac toe game and i have most of the code done but i can't figure out the problem because the program starts but says "Invalid marker" if you put in any number. any help is greatly appreciated. Here's the code:
    Java Code:
    import javax.swing.*;
    import java.util.*;
    
    public class tictactoe
    {
    	static String a,b,c,d,e,f,g,h,i;
    	static int curPlay = 1;
    	static int over = 0;
    	public static void main(String[] args)
    	
    	{
    		
    		while (over != 1)
    		{
    			String square = displayBoard();
    			int ava = checkSquare(square);
    			if (ava == 1)
    			{
    				convertnumber(square);
    			}
    			checkwin(curPlay);
    			switchPlayer(curPlay);
    			rollComp();
    			checkwin(curPlay);
    			switchPlayer(curPlay);
    		}
    	}
    
    	public static String displayBoard()
    	{
    		String number;
    		a = "1";
    		b = "2";
    		c = "3";
    		d = "4";
    		e = "5";
    		f = "6";
    		g = "7";
    		h = "8";
    		i = "9";
    		number = JOptionPane.showInputDialog(null, a + "  " + b + "  " + c + "\n" + d + "  " + e + "  " + f +"\n" + g + "  " + h + "  " + i + "\nEnter position that you would like to play");
    		return number;
    	}
    
    		public static int checkSquare(String gameMarker)
    		{
    System.out.println(a);
    
    			int intMarker = Integer.parseInt(gameMarker);
    			int ava = 0;
    			String somenumber = " ";
    			if (intMarker == 1)
    			{
    			somenumber = a;
    			}
    				else if(intMarker == 2)
    			{
    			somenumber = b;
    			}
    				else if(intMarker == 3)
    			{
    			somenumber = c;
    			}
    				else if(intMarker == 4)
    			{
    			somenumber = d;
    			}
    				else if(intMarker == 5)
    			{
    			somenumber = e;
    			}
    				else if(intMarker == 6)
    			{
    			somenumber = f;
    			}
    				else if(intMarker == 7)
    			{
    			somenumber = g;
    			}
    				else if(intMarker == 8)
    			{
    			somenumber = h;
    			}
    				else if(intMarker == 9)
    			{
    			somenumber = i;
    			}
    
    			if (somenumber.equals(intMarker))
    			{
    			ava = 1;
    			}
    			else
    			{
    			JOptionPane.showMessageDialog(null, "Invalid marker, try again");
    			}
    			return ava;
    
    		}
    
    		public static void convertnumber(String selection)
    		{
    			String str = " ";
    			//String selection; //
    			if(curPlay == 1)
    			str = "X";
    			else if(curPlay == 2)
    			str = "O";
    if (selection.equals(a))
    a = str;
    else if (selection.equals(b))
    b = str;
    else if (selection.equals(c))
    c = str;
    else if (selection.equals(d))
    d = str;
    else if (selection.equals(e))
    e = str;
    else if (selection.equals(f))
    f = str;
    else if (selection.equals(g))
    g = str;
    else if (selection.equals(h))
    h = str;
    else if (selection.equals(i))
    	i = str;
    else { }
    		}
    
    
    
    
    
    
    	public static void checkwin(int cPlayer)
    {
    	if((a.equals(b) && b.equals(c)) || (d.equals(e) && e.equals(f)) || (g.equals(h) && h.equals(i)) || (a.equals(d) && d.equals(g)) || (b.equals(e) && e.equals(h)) || (c.equals(f) && f.equals(i)) || (a.equals(e) && e.equals(i)) || (c.equals(e) && e.equals(g)))
    	{
    		JOptionPane.showMessageDialog(null, " Player" + cPlayer + " wins! ");	
    		over = 1;
    	}
    	else if((!(a.equals(1))) && (!(b.equals(2))) && (!(c.equals(3))) && (!(d.equals(4))) && (!(e.equals(5))) && (!(f.equals(6))) && (!(g.equals(7))) && (!(h.equals(8))) && (!(i.equals(9))))
    			{
    		JOptionPane.showMessageDialog(null, "players have tied" + "\nbetter luck next time.");
    		over = 1;
    			}
    }
    
    
    
    
    
    		public static int rollComp() //computer generates its choice
    	{
    		//String compWeapon = "";
    		Random generator = new Random();
    		double weaponNum = generator.nextDouble();
    		int num = 0;
    		while (num == 0)
    		{
    
    			if (weaponNum < (1/9))
    			{
    				a = "O";
    				num = 1;
    			}
    			else if (weaponNum < (2 / 9))
    			{
    				b = "o";
    				num = 2;
    			}
    			else if (weaponNum < (3 / 9))
    			{
    				c = "o";
    				num = 3;
    			}
    			else if (weaponNum < (4 / 9))
    			{
    				d = "o";
    				num = 4;
    			}
    			else if (weaponNum < (5 / 9))
    			{
    				e = "o";
    				num = 5;
    			}
    			else if (weaponNum < (6 / 9))
    			{
    				f = "o";
    				num = 6;
    			}
    			else if (weaponNum < (7 / 9))
    			{
    				g = "o";
    				num = 7;
    			}
    			else if (weaponNum < (8 / 9))
    			{
    				h = "o";
    				num = 8;
    			}
    			else if (weaponNum < (9 / 9))
    			{
    				i = "o";
    				num = 9;
    			}
    			else { }
    		}
    		
    				return num;
    		}
    
    
    
    		public static int switchPlayer(int gamenumber)
    	{
    		if (gamenumber == 0)
    		{
    			return 1;
    		}
    		else if (gamenumber == 1)
    		{
    			return 0;
    		}
    		else
    			return 999;
    	}
    Last edited by Norm; 12-07-2011 at 02:18 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Help with tic tac toe java program?

    Java Code:
    if (somenumber.equals(intMarker))
    Add a println statement just before this statement and print out the values of both the variables used in that statement to see what your program is doing and why the values are not equal.

    Look at what this statement prints out:

    System.out.println("1".equals(1)); // false
    Last edited by Norm; 12-07-2011 at 02:23 AM.

  3. #3
    Momar99 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Help with tic tac toe java program?

    I put in that line and it still says invalid marker and then prints "1 false"... any help please?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with tic tac toe java program?


  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Help with tic tac toe java program?

    You are comparing a String value to a char value.
    The statement I posted shows that they are not equal.
    You need to change one of the things you are comparing to be the same data type as the other if you want to compare them.

  6. #6
    Momar99 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Help with tic tac toe java program?

    Quote Originally Posted by Norm View Post
    You are comparing a String value to a char value.
    The statement I posted shows that they are not equal.
    You need to change one of the things you are comparing to be the same data type as the other if you want to compare them.
    Could you be more specific? I never used a char variable in the program...
    A direct example of where the problem is would be great. Thanks.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Help with tic tac toe java program?

    I never used a char variable
    I meant to say to an int value. The code I posted used an int. The 1 without the "s is an int value:
    System.out.println("1".equals(1)); // false

  8. #8
    Momar99 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Help with tic tac toe java program?

    Quote Originally Posted by Norm View Post
    I meant to say to an int value. The code I posted used an int. The 1 without the "s is an int value:
    System.out.println("1".equals(1)); // false
    Ok i've fixed the problem and made a few updates to the program. Now the only problem left is that the program won't switch between the player and computer. any ideas?
    Java Code:
    import javax.swing.*;
    import java.util.*;
    
    public class tictactoe
    {
    	static String 	a = "1";
    	static String 		b = "2";
    	static String 		c = "3";
    	static String 		d = "4";
    	static String 		e = "5";
    	static String 		f = "6";
    	static String 		g = "7";
    	static String 		h = "8";
    	static String 		i = "9";
    	static int curPlay = 1;
    	static int over = 0;
    	static String number;
    	public static void main(String[] args)
    	
    	{
    
    		while (over != 1)
    		{
    			String square = displayBoard();
    			int ava = checkSquare(square);
    			System.out.println(ava);
    			if (ava == 1)
    			{
    				convertnumber(square);
    				checkwin(curPlay);
    			}
    			if (over != 1)
    			{
    				switchPlayer(curPlay);
    				rollComp();
    				System.out.println(over + curPlay);
    				checkwin(curPlay);
    				switchPlayer(curPlay);
    			}
    		}
    	}
    
    	public static String displayBoard()
    	{
    	
    		number = JOptionPane.showInputDialog(null, a + "  " + b + "  " + c + "\n" + d + "  " + e + "  " + f +"\n" + g + "  " + h + "  " + i + "\nEnter position that you would like to play");
    		return number;
    	}
    
    		public static int checkSquare(String gameMarker)
    		{
    
    			int intMarker = Integer.parseInt(gameMarker);
    			int ava = 0;
    			String somenumber = " ";
    			if (intMarker == 1)
    			{
    			somenumber = a;
    			}
    				else if(intMarker == 2)
    			{
    			somenumber = b;
    			}
    				else if(intMarker == 3)
    			{
    			somenumber = c;
    			}
    				else if(intMarker == 4)
    			{
    			somenumber = d;
    			}
    				else if(intMarker == 5)
    			{
    			somenumber = e;
    			}
    				else if(intMarker == 6)
    			{
    			somenumber = f;
    			}
    				else if(intMarker == 7)
    			{
    			somenumber = g;
    			}
    				else if(intMarker == 8)
    			{
    			somenumber = h;
    			}
    				else if(intMarker == 9)
    			{
    			somenumber = i;
    			}
    			gameMarker = Integer.toString(intMarker);
    			if (somenumber.equals(gameMarker))
    			{
    			ava = 1;
    			}
    			else
    			{
    			JOptionPane.showMessageDialog(null, "Invalid marker, try again");
    			}
    			return ava;
    
    		}
    
    		public static void convertnumber(String selection)
    		{
    			String str = " ";
    			//String selection; //
    			if(curPlay == 1)
    			str = "X";
    			else if(curPlay == 2)
    			str = "O";
    if (selection.equals(a))
    a = "X";
    else if (selection.equals(b))
    b = str;
    else if (selection.equals(c))
    c = str;
    else if (selection.equals(d))
    d = str;
    else if (selection.equals(e))
    e = str;
    else if (selection.equals(f))
    f = str;
    else if (selection.equals(g))
    g = str;
    else if (selection.equals(h))
    h = str;
    else if (selection.equals(i))
    	i = str;
    else { }
    System.out.println(a);
    		}
    
    
    
    
    
    
    	public static void checkwin(int cPlayer)
    	{
    		if ((a.equals(b) && b.equals(c)) || (d.equals(e) && e.equals(f)) || (g.equals(h) && h.equals(i)) || (a.equals(d) && d.equals(g)) || (b.equals(e) && e.equals(h)) || (c.equals(f) && f.equals(i)) || (a.equals(e) && e.equals(i)) || (c.equals(e) && e.equals(g)))
    		{
    			JOptionPane.showMessageDialog(null, " Player" + cPlayer + " wins! ");
    			over = 1;
    		}
    		else if ((!(a.equals("1"))) && (!(b.equals("2"))) && (!(c.equals("3"))) && (!(d.equals("4"))) && (!(e.equals("5"))) && (!(f.equals("6"))) && (!(g.equals("7"))) && (!(h.equals("8"))) && (!(i.equals("9"))))
    		{
    			JOptionPane.showMessageDialog(null, "players have tied" + "\nbetter luck next time.");
    			over = 1;
    		}
    	}
    
    
    
    
    
    		public static int rollComp() //computer generates its choice
    	{
    		//String compWeapon = "";
    		Random generator = new Random();
    		double weaponNum = generator.nextDouble();
    		int num = 0;
    		while (num == 0)
    		{
    
    			if (weaponNum < (1/9))
    			{
    				a = "O";
    				num = 1;
    			}
    			else if (weaponNum < (2 / 9))
    			{
    				b = "o";
    				num = 2;
    			}
    			else if (weaponNum < (3 / 9))
    			{
    				c = "o";
    				num = 3;
    			}
    			else if (weaponNum < (4 / 9))
    			{
    				d = "o";
    				num = 4;
    			}
    			else if (weaponNum < (5 / 9))
    			{
    				e = "o";
    				num = 5;
    			}
    			else if (weaponNum < (6 / 9))
    			{
    				f = "o";
    				num = 6;
    			}
    			else if (weaponNum < (7 / 9))
    			{
    				g = "o";
    				num = 7;
    			}
    			else if (weaponNum < (8 / 9))
    			{
    				h = "o";
    				num = 8;
    			}
    			else if (weaponNum < (9 / 9))
    			{
    				i = "o";
    				num = 9;
    			}
    			else { }
    		}
    		
    				return num;
    		}
    
    
    
    		public static int switchPlayer(int gamenumber)
    	{
    		if (gamenumber == 0)
    		{
    			return 1;
    		}
    		else if (gamenumber == 1)
    		{
    			return 0;
    		}
    		else
    			return 999;
    	}
    
    
    }
    Last edited by Norm; 12-07-2011 at 09:25 PM. Reason: added code tags

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Help with tic tac toe java program?

    What variable(s) control the switching between players?
    Print out their values as they are changed to see why they are not being set to the values the code is expecting.

  10. #10
    Momar99 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Help with tic tac toe java program?

    switchPlayer and curPlay are the variables that should switch.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Help with tic tac toe java program?

    print out their values as the program executes and see where they are not getting the correct values.

    Be sure to add id Strings to the printlns. Just printing a number with out an id String makes it hard to know what was printed and where it was printed.
    For example:

    System.out.println("ava=" + ava);

  12. #12
    Momar99 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Help with tic tac toe java program?

    Hey Junky, maybe instead of saying that it's a duplicate post even though it isn't because it's on another website, you could have helped me. [ deleted, no need to curse - jos ]
    Last edited by JosAH; 12-08-2011 at 09:22 PM.

  13. #13
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Help with tic tac toe java program?

    Quote Originally Posted by Momar99 View Post
    Hey Junky, maybe instead of saying that it's a duplicate post even though it isn't because it's on another website, you could have helped me. Dick.
    It is a cross post, which you should have informed both forums of.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Help with tic tac toe java program?

    Junky is trying to keep the rest of us from wasting our time if the problem has been solved on another forum.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with tic tac toe java program?

    Quote Originally Posted by Momar99 View Post
    Hey Junky, maybe instead of saying that it's a duplicate post even though it isn't because it's on another website, you could have helped me. Dick.
    What Norm said.

    I don't waste my time helping people who post the same question on multiple fora. Many others don't either thus you actually reduce your chances of getting help instead of increasing it. I also don't help those who make obnoxious insulting post such as yours. Did you really think that attitude would encourage people to help you?

  16. #16
    Momar99 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Help with tic tac toe java program?

    Screw you guys... I'm going home.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Help with tic tac toe java program?

    You might as well give up if you're not going to make any efforts to work on your problem.

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with tic tac toe java program?

    Quote Originally Posted by Momar99 View Post
    Screw you guys... I'm going home.
    What a fine attitude. It would seem that you have an IQ on par with Cartman.

  19. #19
    Momar99 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Help with tic tac toe java program?

    I like your username, it's cute.

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with tic tac toe java program?

    Oh no!

    I have another stalker.

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 04:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 12:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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