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:
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;
}
Re: Help with tic tac toe java program?
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
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?
Re: Help with tic tac toe java program?
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.
Re: Help with tic tac toe java program?
Quote:
Originally Posted by
Norm
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.
Re: Help with tic tac toe java program?
Quote:
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
Re: Help with tic tac toe java program?
Quote:
Originally Posted by
Norm
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?
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;
}
}
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.
Re: Help with tic tac toe java program?
switchPlayer and curPlay are the variables that should switch.
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);
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 ]
Re: Help with tic tac toe java program?
Quote:
Originally Posted by
Momar99
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.
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.
Re: Help with tic tac toe java program?
Quote:
Originally Posted by
Momar99
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?
Re: Help with tic tac toe java program?
Screw you guys... I'm going home.
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.
Re: Help with tic tac toe java program?
Quote:
Originally Posted by
Momar99
Screw you guys... I'm going home.
What a fine attitude. It would seem that you have an IQ on par with Cartman.
Re: Help with tic tac toe java program?
I like your username, it's cute.
Re: Help with tic tac toe java program?
Oh no!
I have another stalker.