Results 1 to 6 of 6

Thread: Extra bracket

  1. #1
    Join Date
    May 2008
    Posts
    9
    Rep Power
    0

    Default Extra bracket

    Im getting an error that bluej is expecting a bracket to be at the beginning of a class, however all brackets are in place and accounted for....IM a bit confused?

  2. #2
    Join Date
    May 2008
    Posts
    9
    Rep Power
    0

    Default

    Heres the code I currently have laid out

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Tic Tac Toe  {
    
    public static void main(String[] args)     {
    
    Scanner keyboard=new Scanner(System.in);
    String[][] board= {{" "," "," "},{" "," "," "},{" "," "," "}};
    int x=0;
    int y=0;
    int option=0;
    
    String player1;
    String player2;
    
    System.out.println("Press 1 for 2 players, or press 7 to exit");
    
    option=keyboard.nextInt();
    
    if (option==1)  {
    
    System.out.println("Player 1 enter your name");
    
    player1=keyboard.next();
    
    System.out.println("Player 2 enter your name");
    
    player2=keyboard.next();
    
    boolean w=winner(board);
    
    while (w==true||w==false)   {
    
    System.out.println(player1+"Enter your choice");
    x=keyboard.nextInt();
    y=keyboard.nextInt();
    
    if (board[x][y]=="X")  {
    System.out.println("That space is already taken");
    continue;
                           }
    
                           
    if (board[x][y]=="Y"){
    
    System.out.println("That space is already taken");
    continue;
                        }
    board[x][y]="X";
    displayBoard(board);
    do   {
    
    System.out.println(player2+" enter your choice");
    x=keyboard.nextInt();
    y=keyboard.nextInt();
    
    if (board[x][y]=="X")   {
    
    System.out.println("That space is already taken");
    continue;
    
                            }
    
    
    if (board[x][y]=="O") {
    
    System.out.println("That space is already taken");
    continue;
    
                         }
    
                         
    board[x][y]="O";
    displayBoard(board);
    break;
        }
    
    while (true);
                            }
    
    
                       }
    
                    }
                    
                    
    public static void displayBoard (String z[][])   {
    
    
    System.out.println(" "+z[0][0]+" | "+z[0][1]+" | "+z[0][2]);
    System.out.println(" ---------");
    System.out.println(" "+z[1][0]+" | "+z[1][1]+" | "+z[1][2]);
    System.out.println(" ---------");
    System.out.println(" "+z[2][0]+" | "+z[2][1]+" | "+z[2][2]);
    
                                                       }
    
     public static boolean winner (String z[][])
    
         {
     
          if ( ( z[0][0]=="X" && z[0][1]=="X" && z[0][2]=="X" )
     
          || ( z[1][0]=="X" && z[1][1]=="X" && z[1][2]=="X" )
     
          || ( z[2][0]=="X" && z[2][1]=="X" && z[2][2]=="X" )
     
          || ( z[0][0]=="X" && z[1][0]=="X" && z[2][0]=="X" )
     
          || ( z[0][1]=="X" && z[1][1]=="X" && z[2][1]=="X" )
     
          || ( z[0][0]=="X" && z[1][1]=="X" && z[2][2]=="X" )
     
          || ( z[0][2]=="X" && z[1][1]=="X" && z[2][0]=="X" ) )
     
        {
          return true;
        
        }
    }
    
    }

  3. #3
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Your Program still has lots of Buggs. Like the game doesnt end. Try using Swing =)

    The winner function doesnt have a return statement if the 'if' function is not satisfied. Remember to put default values if the 'if' condition is not satisfied. =)
    Java Code:
    public static boolean winner (String z[][]){
     
          if ( ( z[0][0].equalsIgnoreCase("X") && z[0][1].equalsIgnoreCase("X") && z[0][2].equalsIgnoreCase("X") )
     
          || ( z[1][0].equalsIgnoreCase("X") && z[1][1].equalsIgnoreCase("X") && z[1][2].equalsIgnoreCase("X") )
     
          || ( z[2][0].equalsIgnoreCase("X") && z[2][1].equalsIgnoreCase("X") && z[2][2].equalsIgnoreCase("X") )
     
          || ( z[0][0].equalsIgnoreCase("X")&& z[1][0].equalsIgnoreCase("X")&& z[2][0].equalsIgnoreCase("X") )
     
          || ( z[0][1].equalsIgnoreCase("X") && z[1][1].equalsIgnoreCase("X")&& z[2][1].equalsIgnoreCase("X") )
     
          || ( z[0][0].equalsIgnoreCase("X") && z[1][1].equalsIgnoreCase("X") && z[2][2].equalsIgnoreCase("X") )
     
          || ( z[0][2].equalsIgnoreCase("X") && z[1][1].equalsIgnoreCase("X") && z[2][0].equalsIgnoreCase("X") ) )
     
          {return true;}
          else {return false;}
        }
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  4. #4
    Join Date
    May 2008
    Posts
    9
    Rep Power
    0

    Default

    Ive been taking different approaches and what not, I changed the code a bit so Im like a couple of steps ahead:)

  5. #5
    Join Date
    May 2008
    Posts
    9
    Rep Power
    0

    Default

    Alright so I made the necessary corrections, but I still have the bracket errror

    Java Code:
    public class Tic Tac Toe  {
    
    public static void main(String[] args)     {
    
    Scanner keyboard=new Scanner(System.in);
    String[][] board= {{" "," "," "},{" "," "," "},{" "," "," "}};
    int x=0;
    int y=0;
    int option=0;
    
    String player1;
    String player2;
    
    System.out.println("Press 1 for 2 players, or press 7 to exit");
    
    option=keyboard.nextInt();
    
    if (option==1)  {
    
    System.out.println("Player 1 enter your name");
    
    player1=keyboard.next();
    
    System.out.println("Player 2 enter your name");
    
    player2=keyboard.next();
    
    boolean w=winner(board);
    
    while (w==true||w==false)   {
    
    System.out.println(player1+"Enter your choice");
    x=keyboard.nextInt();
    y=keyboard.nextInt();
    
    if (board[x][y]=="X")  {
    System.out.println("That space is already taken");
    continue;
                           }
    
                           
    if (board[x][y]=="Y"){
    
    System.out.println("That space is already taken");
    continue;
                        }
    board[x][y]="X";
    displayBoard(board);
    do   {
    
    System.out.println(player2+" enter your choice");
    x=keyboard.nextInt();
    y=keyboard.nextInt();
    
    if (board[x][y]=="X")   {
    
    System.out.println("That space is already taken");
    continue;
    
                            }
    
    
    if (board[x][y]=="O") {
    
    System.out.println("That space is already taken");
    continue;
    
                         }
    
                         
    board[x][y]="O";
    displayBoard(board);
    break;
        }
    
    while (true);
                            }
    
    
                       }
    
                    }
                    
                    
    public static void displayBoard (String z[][])   {
    
    
    System.out.println(" "+z[0][0]+" | "+z[0][1]+" | "+z[0][2]);
    System.out.println(" ---------");
    System.out.println(" "+z[1][0]+" | "+z[1][1]+" | "+z[1][2]);
    System.out.println(" ---------");
    System.out.println(" "+z[2][0]+" | "+z[2][1]+" | "+z[2][2]);
    
                                                       }
    
     public static boolean winner (String z[][])
    
         {
     
          if ( ( z[0][0]=="X" && z[0][1]=="X" && z[0][2]=="X" )
     
          || ( z[1][0]=="X" && z[1][1]=="X" && z[1][2]=="X" )
     
          || ( z[2][0]=="X" && z[2][1]=="X" && z[2][2]=="X" )
     
          || ( z[0][0]=="X" && z[1][0]=="X" && z[2][0]=="X" )
     
          || ( z[0][1]=="X" && z[1][1]=="X" && z[2][1]=="X" )
     
          || ( z[0][0]=="X" && z[1][1]=="X" && z[2][2]=="X" )
     
          || ( z[0][2]=="X" && z[1][1]=="X" && z[2][0]=="X" ) )
     
        {
          return true; }
          else {return false;}
        
        }
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Class definition is wrong. You can't leave space in class name. It should be like this,

    Java Code:
    public class TicTacToe  {
       // body of the class
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •