Hello all, been having some issues with this tic-tac-toe game as the title stated. For some reason it seems that a player cannot place an X or O in any but three of the positions. I had this working before, but then I went to code a 'checkwin' method and suddenly it no longer works. I'm completely baffled because this method is in no way required by the game to function.
Also, it seems as if the checkwin method does not want to be called in the driver class. It displays a "cannot find symbol" error even though I made sure to make the method public.
This is the driver file:
import java.util.Scanner; import javax.swing.JOptionPane; /** This program runs a TicTacToe game. It prompts the user to set positions on the board and prints out the result. */ public class TicTacToeRunner { public static void main(String[] args) { Scanner in = new Scanner(System.in); String player = "x"; int row = 0; int column = 0; String s1; String s2; int turn = 1; TicTacToe game = new TicTacToe(); boolean done = false; while (!done) { System.out.print(game.toString()); s1 = JOptionPane.showInputDialog(null, player + " Please input a row from 1 - 3 or 0 to quit"); row = Integer.parseInt(s1); if (row == 0) done = true; else{ while(row < 1 || row > 3) { if (row == 0) done = true; else{ s1 = JOptionPane.showInputDialog(null, player + " Please input a row from 1 - 3 or 0 to quit"); row = Integer.parseInt(s1); } } } s2 = JOptionPane.showInputDialog(null, player + " Please input a column from 1 - 3 or 0 to quit"); column = Integer.parseInt(s1); if (column == 0) done = true; else{ while(column < 1 || row > 3) { if (column == 0) done = true; else{ s2 = JOptionPane.showInputDialog(null, player + " Please input a column from 1 - 3 or 0 to quit"); column = Integer.parseInt(s1); } } } System.out.println("-----"); turn++; game.set(row - 1, column - 1, player); checkWin(turn); if (player.equals("x")) player = "o"; else player = "x"; } } }
This is the game file:
import javax.swing.JOptionPane; /** A 3 x 3 tic-tac-toe board. */ public class TicTacToe { private String[][] board; private static final int ROWS = 3; private static final int COLUMNS = 3; /** Constructs an empty board. */ public TicTacToe() { board = new String[ROWS][COLUMNS]; // Fill with spaces for (int i = 0; i < ROWS; i++) for (int j = 0; j < COLUMNS; j++) board[i][j] = " "; } /** Sets a field in the board. The field must be unoccupied. @param i the row index @param j the column index @param player the player ("x" or "o") */ public void set(int i, int j, String player) { if (board[i][j].equals(" ")) board[i][j] = player; } public void checkWin(int turn) { boolean win = false; String message; if (turn > 4) { if(board[1][1] == "x"){ if (board[1][2] == "x"){ if (board[1][3] == "x"){ win = true; } else{ if(board[1][1] == "o"){ if (board[1][2] == "o"){ if (board[1][3] == "o"){ win = true; } else{ if(board[2][1] == "x"){ if (board[2][2] == "x"){ if (board[2][3] == "x"){ win = true; } else{ if(board[2][2] == "o"){ if (board[2][1] == "o"){ if (board[2][3] == "o"){ win = true; } else{ if(board[3][1] == "x"){ if (board[3][2] == "x"){ if (board[3][3] == "x"){ win = true; } else{ if(board[3][3] == "o"){ if (board[3][2] == "o"){ if (board[3][1] == "o"){ win = true; } else{ if(board[1][1] == "x"){ if (board[2][1] == "x"){ if (board[3][1] == "x"){ win = true; } else{ if(board[1][1] == "o"){ if (board[2][1] == "o"){ if (board[3][1] == "o"){ win = true; } else{ if(board[1][2] == "x"){ if (board[2][2] == "x"){ if (board[3][2] == "x"){ win = true; } else{ if(board[1][2] == "o"){ if (board[2][2] == "o"){ if (board[3][2] == "o"){ win = true; } else{ if(board[1][3] == "x"){ if (board[2][3] == "x"){ if (board[3][3] == "x"){ win = true; } else{ if(board[1][3] == "o"){ if (board[2][3] == "o"){ if (board[3][3] == "o"){ win = true; } else{ if(board[1][1] == "x"){ if (board[2][2] == "x"){ if (board[3][3] == "x"){ win = true; } else{ if(board[1][1] == "o"){ if (board[2][2] == "o"){ if (board[3][3] == "o"){ win = true; } else{ if(board[1][3] == "x"){ if (board[2][2] == "x"){ if (board[3][1] == "x"){ win = true; } else{ if(board[3][1] == "o"){ if (board[2][2] == "o"){ if (board[1][3] == "o"){ win = true; } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } if(win || (!win && turn>9)) { if(win) { if(turn % 2 == 0) message = "X has won!"; else message = "O has won!"; JOptionPane.showMessageDialog(null, message); win = false; } else if(!win && turn>9) { message = "Both players have tied!\nBetter luck next time."; JOptionPane.showMessageDialog(null, message); } } } /** Creates a string representation of the board, such as |x o| | x | | o| @return the string representation */ public String toString() { String r = ""; for (int i = 0; i < ROWS; i++) { r = r + "|"; for (int j = 0; j < COLUMNS; j++) r = r + board[i][j]; r = r + "|\n"; } return r; } }
Any help would be appreciated at this point. I'm a bit of a java noob so I have a feeling these are simple errors for you but I've been stuck on this for hours.
Thanks in advance for any help.