Can anyone help me figure out what's wrong with this program? I wrote the code but when i run it, no matter what number I input, it just says "Invalid marker" and then exits.
import javax.swing.*;
import java.util.*;
public class tictactoe1
{
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;
}
}