Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default Java Code Won't Compile

    For some reason, the code below won't compile. It says that I have an illegal start of expression error on line 136.

    Any suggestions?

    Java Code:
    import java.util.*;
    /**
     * The class board defines a "playing board" for the user to use in playing Tic Tac Toe against a computer.
     * 
     * @author
     * @version 7/28/10
     */
    public class Board
    {
       char [ ] [ ] gameBoard; //Creates the character array for the board.
       
       public Board( ) //Sets the default values for the board.
       //Initializes the values of the array to white space.
       {
           gameBoard = new char[3][3];
           for(int row = 0; row < 3; row++) //Initializes the rows to ' '
        {
            for(int col = 0; col < 3; col++) //Initializes the coloums to ' '
            {
                gameBoard[row][col] = ' '; //Initializes each space on the board square-by-square.
            }
        }
       }
        public boolean checkForWin(char marker) //The constructor method for checkForWin if only a character is used for the method parameter. Same code as the previous method.
       {
           boolean isAWinner = false;
           int consecutive;
           int row;
           int col;
           
           consecutive = 0;
           //Checks rows for a three-in-a-row match.
           for( row = 0; row < 3 && consecutive != 3; row++)
           {
             consecutive = 0;
             for(col = 0; col < 3; col ++)
             {
               if(gameBoard[row][col] == marker)
               {
                  consecutive++;
               }
             }
           }
           
           if (consecutive == 3)
           {
             isAWinner = true;
           }
           //Executes if isAWinner is false.
           //Checks coloumns for a three-in-a-row match.
           if(!isAWinner)
              for(col = 0; col < 3 && consecutive != 3; col++)
               {
                 consecutive = 0;
                 for(row = 0; row < 3; row ++)
                   if(gameBoard[row][col] == marker)
                   {
                     consecutive++;
                   }
               }
            
           if(consecutive == 3)
           {
              isAWinner = true;
           }
           //Executes if isAWinner is false.
           //Checks the diagonals for three-in-a-row markers.
           if(!isAWinner)
           {
               if((gameBoard[0][0] == marker && gameBoard[1][1] == marker && gameBoard [2][2] == marker) || (gameBoard[2][0] == marker && gameBoard[1][1] == marker && gameBoard[0][2] == marker))
               {
                   isAWinner = true;
                }
            }
           return isAWinner;
        } 
        
       
       //Method that checks to see whether or not the user or computer won a game on the board.
       public boolean checkForWin (char marker, char[ ][ ] board) //The constructor method for checkForWin if both a character marker and character array (place on the board) are used
       //as parameters.
       {
           boolean isAWinner = false; //By default, there is no winner.
           int consecutive; //Variable for determining whether or not there is a match for a marker in each column. Each time there is a marker in a column,
           //the variable consecutive is incremented. When it equals 3, the game is over and there is a winner.
           
           consecutive = 0;
           for(int row = 0; row < 3 && consecutive != 3; row++) //Goes through the row until it reaches the end and while consecutive is not equal to 3 (There is no winner).
           //Checks the rows for three-in-a-row markers.
           {
             consecutive = 0; //Sets consecutive to 0 within the loop.
             
             for(int col = 0; col < 3; col++) //Goes through the coloumns and the rows (for loop within a for loop).
             
              {
                  if(gameBoard[row][col] == marker)
                  {
                      consecutive++; //If there is a marker instead of whitespace on a "block" on the board, consecutive is incremented.
                  }
           }
           if (consecutive == 3)
           {
             isAWinner = true;
            }
           //Executes if isAWinner is false. Checks the coloumns for three-in-a-row markers.
           if(!isAWinner)
           {
              for(int col = 0; col < 3 && consecutive != 3; col++)
               {
                 consecutive = 0;
                 
                 for(int row = 0; row < 3; row ++)
                 {
                 if(gameBoard[row][col] == marker)
                    consecutive++;
                }
               }
               
               if(consecutive == 3)
                  isAWinner = true;
            }
           //Executes if isAWinner is false. Checks the diagonals for three-in-a-row markers.
           if(!isAWinner)
           {
               if((gameBoard[0][0] == marker && gameBoard[1][1] == marker && gameBoard [2][2] == marker) || (gameBoard[2][0] == marker && gameBoard[1][1] == marker && gameBoard[0][2] == marker))
               {
                   isAWinner = true;
                }
            }
               
           return isAWinner;
        } 
        
     
        //The method for determining the computer's move in the game.
        public void computerMove(char marker)
        {
            boolean canComputerWin = false;
            char[ ][ ] copyBoard; //Copies the player board.
            int row;
            int col;
            int tryRow;
            int tryCol;
            row = 0;
            col = 0;
            
           //Checks for a win in each row.    
           for(row = 0; row < 3 && !canComputerWin; row++)
           {
               for(col = 0; col < 3 && !canComputerWin; col++)
               {
                   if(gameBoard[row][col] == ' ') //If there is whitespace is the current block being analyzed.
                   {
                     copyBoard = copyTheBoard( ); //Copies the player board.
                     copyBoard[row][col] = marker; //Checks each row and column of the copied board for a marker.
                     canComputerWin = checkForWin(marker, copyBoard); //Sets canComputerWin equal to true or false.
                   }
               }
            }
            //Checks for a win in each column.
            if(!canComputerWin)
            {
            for(col = 0; col < 3 && !canComputerWin; col++)
            {
             
             for(row = 0; row < 3&& !canComputerWin; row++)
              {
                  if(gameBoard[row][col] == ' ')
                  {
                    copyBoard = copyTheBoard( );
                    copyBoard[row][col] = marker;
                    canComputerWin = checkForWin(marker, copyBoard); //Set canComputerWin equal to true or false.
                  }
              }
             }
            }
            
            if(canComputerWin) //If canComputerWin is true.
            {
               gameBoard[row-1][col-1] = marker; //Set a marker down in one less than the current row and current coloumn being analyzed (next to another marker).
            }
            else //If the computer can not win.
            {
                boolean placed = false; //Sets placed to fale.
                Random rand = new Random(); //Creates a new random object.
                
                do
                {
                  tryRow = rand.nextInt(3); //Creates a random number for the row.
                  tryCol = rand.nextInt(3); //Creates a random number for the colomn.
                  
                  if(gameBoard[tryRow][tryCol] == ' ') //If there is a blank space.
                  {
                   gameBoard[tryRow][tryCol] = marker; //Set the space equal to the marker.
                   placed = true; //Set placed equal to true.
                   }
                }while(!placed); //Do these steps while placed is not true.
                System.out.println(canComputerWin); //Prints whether or not the computer can or can not win.
             }
          }
        
          
          public boolean humanMoveOK(int row, int col) //Checks to see if the human move is a valid move.
        {
          {
              
              return row <= 3 && col <= 3 && gameBoard[row-1][col-1] == ' ';
          }
          
          public void makeMove(int row, int col, char marker)
          {
              gameBoard[row-1][col-1] = marker;
          }
        }
              
             
            
                  
                       
         private char[ ][ ] copyTheBoard( ) //The method to copy the board.
         {
             char [ ][ ] temp = new char[3][3]; //Creates a new character array named temp.
             for(int row = 0; row < 3; row++)
             {
              for(int col = 0; col < 3; col++)
              {
                temp[row][col] = gameBoard[row][col]; //Sets the rows and coloumns of temp equal to the rows and coloumns of gameBoard.
              }
             }
             return temp;
         }
         
         public boolean humanMove(int row, int col, char marker) //Returns placed. If there is a blank space, makes it equal to marker. Placed is true. Else, placed is false.
         {
             boolean placed;
             if(gameBoard[row-1][col-1] == ' ')
              {
                  gameBoard[row-1][col-1] = marker;
                  placed = true;
              }
              else
              {
                  placed = false;
              }
              return placed;
            }     
                  
               
         //Creates a visible board for the user.
         public String toString( )
         {
             String result = "";
             int row;
             int col;
             result += "    1   2   3\n";
             
             for(row = 0; row < 3; row++)
             {
                 result += "\n" + (row+1);
                 for(col = 0; col < 3; col++)
                 { 
                  
                   result += "  " + gameBoard[row][col] + " ";
                  
                 }
               
             }
             return result;
         }     
                 
             
             
              
       
       
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Where is the line with the error? I'm willing to count to 10, but 100+ is too far.
    Or could you add comments in the code and tell us what to search for to find the error?

    Please copy full text of error message and paste it here. Here is a sample:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

  3. #3
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default

    Java Code:
     public void computerMove(char marker)
    The computerMove(char marker) method. Line 136, there are line counters in IDE's like BlueJ that appear on the side of each line. The error message is that it's an illegal start of expression or such, and thus won't compile.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Please copy full text of error message and paste it here.

    illegal start of expression
    Check for matching {} and ()

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    As Norm says, check your brackets.

    Format your code properly, with correct indentation (using spaces not tabs), and don't skip {} on single line ifs and loops...that'll help you see straight away if there's a bracket problem, or if you've declared something in a silly place. I have no idea if BlueJ has an auto formatter, but you could always use that if it does.

Similar Threads

  1. Code does not Compile
    By ustar in forum New To Java
    Replies: 13
    Last Post: 03-06-2010, 04:54 PM
  2. New to JAVA and code cant compile
    By Gayethiri_86 in forum New To Java
    Replies: 2
    Last Post: 03-05-2010, 06:43 AM
  3. Code will not compile
    By ShotGunRockets in forum New To Java
    Replies: 17
    Last Post: 05-10-2009, 03:31 AM
  4. Replies: 6
    Last Post: 02-06-2009, 08:05 PM
  5. Compile/Execute code in Java app
    By Doctor Cactus in forum New To Java
    Replies: 5
    Last Post: 12-16-2008, 09:58 AM

Posting Permissions

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