Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Illegal start of expression?

$
0
0
Hi everyone,

For some reason, I keep on getting the compiling error of "illegal start of expression." Does anyone know why/how can I prevent this?

Thank you
import java.awt.*; 
import java.util.*; 
public class Connect4 extends JComponentWithEvents{
        private Color[][] checkers; 
        private boolean isRedTurn; 
        private boolean isOver; 
public void keyPressed(char key){
  if(key == 's'){
    char relaunch = 3;
    while (relaunch == 3) {
   {   
        
     public void start(){
          checkers = new Color[6][7];
          for(int r = 0; r < checkers.length; r++){
            for(int c = 0; c < checkers[r].length; c++){
              checkers[r][c] = Color.white; 
            }
          }
          isRedTurn = true; 
          isOver = false; 
     }
        public void paint(Graphics2D page){
          int colWidth = getWidth() / 7; 
          int rowHeight = getHeight() / 6; 
          
          page.setColor(Color.blue); 
          page.fillRect(0, 0, getWidth(), getHeight()); 
          for(int r = 0; r < checkers.length; r++){
            for(int c = 0; c < checkers[r].length; c++){
              page.setColor(checkers[r][c]); 
              page.fillOval(colWidth * c + 5, rowHeight * r + 5, colWidth - 10, rowHeight - 10); 
            }
          }
        }
        
        
        public void mousePressed(int x, int y){
          int col = 7 * x / getWidth(); 
          int newRow = -1; 
          if(isRedTurn){
            for(int r = checkers.length - 1; r >= 0; r--){
              if(checkers[r][col].equals(Color.white)){
                checkers[r][col] = Color.red; 
                newRow = r; 
                break; 
              }
            }
          }
          else{
            for(int r = checkers.length - 1; r >= 0; r--){
              if(checkers[r][col].equals(Color.white)){
                checkers[r][col] = Color.black; 
                newRow = r; 
                break; 
              }
            }
          }
          if(newRow != -1){
            isRedTurn = !isRedTurn; 
            checkWin(newRow, col);
          }
        }
        //                   row index, col index
        public void checkWin(int row, int col)
        {
          //just added a checker at row, col
          //what color was it?
          int countMatches=0;
          //counting the number of matching checkers below the new checker
          for (int i= row + 1; i < 6; i++){//starting on the row below
            if (checkers[i][col]==checkers[row][col]){
              countMatches++;
            }
            else{
              break; 
            }
          }
          if (countMatches >= 3){
            System.out.println("Congratulations, you are the winner");
          }
          
          countMatches = 0; 
          //count right
          for (int i=col + 1; i < 7; i++){
            if (checkers[row][i]==checkers[row][col]){
              countMatches++;
            }
            else{
              break; 
            }
          }
          //count left
          for (int i=col-1; i>=0; i--){
            if (checkers[row][i]==checkers[row][col]){
              countMatches++;
            }
            else{
              break; 
            }
          }
          if (countMatches >= 3){
            System.out.println("Congratulations, you are the winner");
          }
          
          countMatches = 0; 
          //counting down-right
          for (int i=1; row + i < 6 && col + i < 7; i++){
            if (checkers[row + i][col + i]==checkers[row][col]){
              countMatches++;
            }
            else{
              break; 
            }
          }
          //counting up-left
          for (int i=1; row - i >= 0 && col - i >= 0; i++){
            if (checkers[row - i][col - i]==checkers[row][col]){
              countMatches++;
            }
            else{
              break; 
            }
          }
          if (countMatches >= 3){
            System.out.println("Congratulations, you are the winner");
          }
          
          countMatches = 0; 
          //up-right
          for (int i=1; row - i >= 0 && col + i < 7; i++){
            if (checkers[row - i][col + i]==checkers[row][col]){
              countMatches++;
            }
            else{
              break; 
            }
          }
          //down-left
          for (int i=1; row + i < 6 && col - i >= 0; i++){
            if (checkers[row + i][col - i]==checkers[row][col]){
              countMatches++;
            }
            else{
              break; 
            }
          }
          if (countMatches >= 3){
            System.out.println("Congratulations, you are the winner");
          }
          public static void main(String[] args)
          {
            launch(700, 600); 
          }
        }
        
      }
    }
    
  }
  
}
}




Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>