4 Replies - 5634 Views - Last Post: 16 January 2012 - 05:55 PM Rate Topic: -----

#1 Sylentwolf8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-January 12

Creating simple tic-tac-toe game, but having issues

Posted 16 January 2012 - 01:09 PM

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.



The driver class:
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";    
         
      }
   }
}
      





The game class is here:

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating simple tic-tac-toe game, but having issues

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Creating simple tic-tac-toe game, but having issues

Posted 16 January 2012 - 04:56 PM

Your problem is here:
row = Integer.parseInt(s1);

s2 = JOptionPane.showInputDialog(null, player + " Please input a column from 1 - 3 or 0 to quit");
...
column = Integer.parseInt(s1);



column should be parsing s2, not s1.

Now, aside from that, I have a few comments:
  • You should always compare Strings with the equals() method, not the == operator
  • Fix your indenting! Having 20+ "}" in a row makes for a horrible setup. Try to move those around and restructure if statements/loops
  • Try to avoid copy/paste code. That's where your problem originated.

Was This Post Helpful? 1
  • +
  • -

#3 Sylentwolf8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-January 12

Re: Creating simple tic-tac-toe game, but having issues

Posted 16 January 2012 - 05:30 PM

Thank you very much, you're right that specific error most likely occurred from my copying and pasting that first block of code to save time, and in the end it wasted more time than it saved.

Do you have any ideas for the error when calling the checkWin method?
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Creating simple tic-tac-toe game, but having issues

Posted 16 January 2012 - 05:35 PM

Yes. Start over. I hate to sound so pessimistic, but your braces are all over the place, and you have like 40 nested for loops.

I have written a full fledged GUI-based TicTacToe game here:
http://www.dreaminco...-1-tic-tac-toe/

You can look in that tutorial to see my checkWin() method:
Spoiler


I hope this helps!
Was This Post Helpful? 0
  • +
  • -

#5 Sylentwolf8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-January 12

Re: Creating simple tic-tac-toe game, but having issues

Posted 16 January 2012 - 05:55 PM

OK i've now tried redoing a large portion of the game using the checkwin you provided as help, however when I still go to call the checkWin in the driver file it still says "cannot find symbol." Also i'm trying to use the variable 'player' in order to display who won the game under the checkwin method but this is displaying the same error. It's as if the two files are for some reason not communicating. Have you ever had this error?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1