I am trying to make a tic tac toe game and am new to java. I am getting outofbounds at the first if statement within the method isHorizontalWin and I am assuming it would happen within isVerticalWin as well. Correct me if I am wrong but I believe it is happening because part of gameboard has no value so the if statement is outofbounds however I am unsure in how to fix this. A side note: the formatting got messed up a little bit when pasting the code so I had to make some of the JOptionPanes go onto multiple lines to fit. Thanks!
import javax.swing.JOptionPane;
public class TicTacToe
{
public static void main(String[] args)
{
char gameboard[][] = new char[3][3];
printCurrentState(gameboard);
int Turn = 0;
int gameon=0;
while(gameon<9)
{
if(Turn==0)
{
JOptionPane.showMessageDialog(null, "Player 1's turn aka X");
}
else
{
JOptionPane.showMessageDialog(null, "Player 2's turn aka O");
}
boolean proceed=true;
String yo =JOptionPane.showInputDialog("Enter the row and column"+ "one"+
"after another. Example-for row 1 and comlumn 2 type \"12\"");
if(yo.length()!=2)
{
JOptionPane.showMessageDialog(null, "You did not put the row"+ "and column one after another- Try again");
proceed=false;
}
if(proceed==true) {
String aa= yo.charAt(0)+"";
int x = Integer.parseInt(aa)-1;
String ba= yo.charAt(1)+"";
int y = Integer.parseInt(ba)-1;
if((x < 0 || x > 2)||(y<0||y>2))
{
JOptionPane.showMessageDialog(null, "Please enter a row and"+
"column that both at least 1 and no bigger than 3. Try again!");
yo =JOptionPane.showInputDialog("Enter the row and column one"+
"after another. Example-for row 1 and comlumn 2 type \"12\"");
aa= yo.charAt(0)+"";
x = Integer.parseInt(aa)-1;
ba= yo.charAt(1)+"";
y = Integer.parseInt(ba)-1;
}
if(gameboard[x][y] == 'X' || gameboard[x][y] == 'O')
{
JOptionPane.showMessageDialog(null, "That is already taken"+
"by an "+ gameboard[x][y]+". Go again!");
yo =JOptionPane.showInputDialog("Enter the row and column"+
"one after another. Example-for row 1 and comlumn 2 type \"12\"");
aa= yo.charAt(0)+"";
x = Integer.parseInt(aa)-1;
ba= yo.charAt(1)+"";
y = Integer.parseInt(ba)-1;
}
if(Turn== 0)
{
gameboard[x][y] = 'X';
printCurrentState(gameboard);
isHorizontalWin(gameboard);
isVerticalWin(gameboard);
isDiagnolWin(gameboard);
Turn++;
}
else if(Turn == 1)
{
gameboard[x][y] = 'O';
printCurrentState(gameboard);
isHorizontalWin(gameboard);
isVerticalWin(gameboard);
isDiagnolWin(gameboard);
Turn--;
}
gameon++;
}
}
if(isHorizontalWin(gameboard)==false&&isVerticalWin(gameboard)==false&&isDiagnolWin(gameboard)==false)
{
JOptionPane.showMessageDialog(null, "There was no winner this game ");
}
}
public static void printCurrentState(char gameboard[][])
{
System.out.println(" COLUMN");
System.out.println(" 1 2 3");
System.out.print(gameboard[0][0] + " | " + gameboard[0][1] + " | " + gameboard[0][2] +" 1 R"+ "\n"
+ "--|---|--\n" +
gameboard[1][0] + " | " + gameboard[1][1] + " | " + gameboard[1][2] +" 2 O"+ "\n"
+ "--|---|--\n" +
gameboard[2][0] + " | " + gameboard[2][1] + " | " + gameboard[2][2] +" 3 W "+"\n");
System.out.println();
}
public static boolean isHorizontalWin(char[][] gameboard)
{
int tally[][]= new int[3][3];
for(int r = 0; r < 3; r++)
{
for(int c = 0; c < 3; r++)
{
if(gameboard[r][c]=='O')
{
tally[r][c]=10;
}
else if(gameboard[r][c]=='X')
{
tally[r][c]=1;
}
else
{
tally[r][c]=0;
}
}
}
int c=0;
for(int m = 0; m < 3; m++)
{
c++;
int cool = 0;
for(int n = 0; n < 3; n++)
{
cool += tally[m][n];
if(cool == 30)
{
JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a horizontal win in row " +c);
System.exit(0);
return true;
}
else if(cool==3)
{
JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a horizontal win in row " + c);
System.exit(0);
return true;
}
}
}
return false;
}
public static boolean isVerticalWin(char[][] gameboard)
{
int tally[][]= new int[3][3];
for(int r = 0; r < 3; r++)
{
for(int c = 0; c < 3; r++)
{
if(gameboard[r][c]=='O')
{
tally[r][c]=10;
}
else if(gameboard[r][c]=='X')
{
tally[r][c]=1;
}
else
{
tally[r][c]=0;
}
}
}
int r=0;
for(int m = 0; m < 3; m++)
{
r++;
int cool = 0;
for(int n = 0; n < 3; n++)
{
cool += tally[n][m];
if(cool == 30)
{
JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a vertical win in column " +r);
System.exit(0);
return true;
}
else if(cool==3)
{
JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a vertical win in column " + r);
System.exit(0);
return true;
}
}
}
return false;
}
public static boolean isDiagnolWin(char[][] gameboard)
{
if((gameboard[0][0]=='O'&&gameboard[1][1]=='O'&&gameboard[2][2]=='O')||(gameboard[0][2]=='O'&&gameboard[1][1]=='O'&&gameboard[3][1]=='O'))
{
JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a diagonal win" );
System.exit(0);
return true;
}
if((gameboard[0][0]=='X'&&gameboard[1][1]=='X'&&gameboard[2][2]=='X')||(gameboard[0][2]=='X'&&gameboard[1][1]=='X'&&gameboard[2][0]=='X'))
{
JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a diagonal win" );
System.exit(0);
return true;
}
return false;
}
}