lundi 28 mars 2016

How do I Check Winner In connect 4 Diagonally?

So i am having problem checking the winner diagonally in a game called connect 4. I didn't have any problem checking vertically and horizontally. So.. Can u please help me check diagonally?

Code so far:

private Color playerColor = Color.red;
  private class clikMeOneButtonListener implements ActionListener{
     public void actionPerformed(ActionEvent event) {


      if (event.getSource() == clickMeOne 
        || event.getSource() == clickMeTwo
        || event.getSource() == clickMeThree
        || event.getSource() == clickMeFour
        || event.getSource() == clickMeFive
        || event.getSource() == clickMeSix
        || event.getSource() == clickMeSeven
        ) {

          JButton b = (JButton)event.getSource();

          int column = 0;
          switch ( b.getName() ) {

           case "clickMeOne" : column = 0; break;
           case "clickMeTwo" : column = 1; break;
           case "clickMeThree" : column = 2; break;
           case "clickMeFour" : column = 3; break;
           case "clickMeFive" : column = 4; break;
           case "clickMeSix" : column = 5; break;
           case "clickMeSeven" : column = 6; break;
          }

          int lastEmptyIdx = -1;

          for ( int i = 0; i < slots[column].length; i++ ) {

           if ( slots[column][i].getBackground() != Color.green ) {

            break;
           }
           else {

            lastEmptyIdx = i;
           }
          }

          if ( lastEmptyIdx != -1 ) {

              slots[column][lastEmptyIdx].setBackground(playerColor);

              if ( IsWin(column, lastEmptyIdx) ) {

               String message = playerColor == Color.red ? " Player One Won!" : " Player Two Won!";
               JOptionPane.showMessageDialog(null, message, " Results ", JOptionPane.INFORMATION_MESSAGE);
               System.exit(0);

              } else {               
               playerColor = playerColor == Color.red ? Color.yellow : Color.red;  
              }      
          }
      } 
     }

     public boolean IsWin(int column, int row) {   

      boolean result = false;

      // horizontal
      boolean found = false;

      int counter = 0;

      for ( int i = 0; i < slots.length; i++ ) {

       if ( slots[i][row].getBackground().equals(playerColor)) { 

     counter++;

        if ( found == true ) {

         if ( counter == 4 ) { // win \o/
          result = true;          
          break;
         }         
        }        
        else {

         found = true;
        }
       }
       else {

        if ( found == true ) { // reset counter

         counter = 0;
        }

        found = false;
       }
      }

      // vertical 
      for ( int i = 0; i < slots.length; i++ ) {

          if ( slots[column][i].getBackground().equals(playerColor)) { 

        counter++;

           if ( found == true ) {

            if ( counter == 4 ) { // win \o/
             result = true;          
             break;
            }         
           }        
           else {

            found = true;
           }
          }
          else {

           if ( found == true ) { // reset counter

            counter = 0;
           }

           found = false;
          }
         }

      **// diagonal not working. This is what I have so far diagonal. **
      **// TODO**

      for ( int i = 0; i < slots.length; i++ ) {

          if ( slots[column][i].getBackground().equals(playerColor)) { 

        counter++;

           if ( found == true ) {

            if ( counter == 4 ) { // win \o/
             result = true;          
             break;
            }         
           }        
           else {

            found = true;
           }
          }
          else {

           if ( found == true ) { // reset counter

            counter = 0;
           }

           found = false;
          }
         }

      return result;
     }
  }
}

Aucun commentaire:

Enregistrer un commentaire