title: Java Reversi Game Flipping Tiles
body: <p>I am creating a reversi (othello) game on Java. Included below are my functions for when tiles need to flip to the left, right, below, or above the selected piece. For some reason, whenever a vertical flip is initiated, it ALWAYS works correctly. But, when a horizontal flip is initiated, sometimes it does nothing (when it should do something), or flips over blank tiles that are not supposed to be flipped. The gameboard is an 8 by 8 matrix using an extended JButton called BlankPiece. I will post that in addition to the functions. Can anyone suggest a more efficient / better way to run this game without bugs? Any help is greatly appreciated! If you need clarification, just ask.</p>
  
  <pre><code>   public void checkFlipDown(BlankPiece temp)
      {
          for(int j = temp.getRow() - 1; j&gt;=0; j--){
              // j equals the square right above the clicked one, and it keeps going up until it hits the top of the board
          if ((gameboard[j][temp.getCol()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getCol()].pieceType() != temp.pieceType())){
              //if the spot right above the clicked one is not black AND it is the other player's color,
              continue;
              // keep going with the block statement
          }
          if ((gameboard[j][temp.getCol()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getCol()].pieceType() == temp.pieceType())){
          // if the spot right above is not black AND it is the same player's color,
              for(int i = temp.getRow(); i &gt;= j; i--)
                  // i equals the row of the clicked spot, it goes until it finds J, where the other green / purple end piece is
                  {
                  // change the colors of all colors in the clicked color's column and rows in between the other border piece
                      gameboard[i][temp.getCol()].change(temp.pieceType());
                  }
              }
          else
          {
              // if no other border pieces exist, go out of the loop
              break;
          }
          }
      }
      public void checkFlipUp(BlankPiece temp)
      {
          for(int j = temp.getRow() + 1; j&lt;=7; j++){
              // j equals the square right below the clicked one, and it keeps going down until it hits the bottom of the board
          if ((gameboard[j][temp.getCol()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getCol()].pieceType() != temp.pieceType())){
              //if the spot right above the clicked one is not black AND it is the other player's color,
              continue;
              // keep going with the block statement            2,4 jj = 3,4,5,6,7
          }
          if ((gameboard[j][temp.getCol()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getCol()].pieceType() == temp.pieceType())){
          // if the spot right above is not black AND it is the same player's color,
              for(int i = temp.getRow(); i &lt;= j; i++)
                  // i equals the row of the clicked spot, it goes until it finds J, where the other green / purple end piece is
                  {
                  // change the colors of all colors in the clicked color's column and rows in between the other border piece
                      gameboard[i][temp.getCol()].change(temp.pieceType());
                  }
              }
          else
          {
              break;
          }
          }
      }
      public void checkFlipLeft(BlankPiece temp)
      {
          for(int j = temp.getCol() + 1; j&lt;=7; j++){
              // j equals the square right below the clicked one, and it keeps going down until it hits the bottom of the board
          if ((gameboard[j][temp.getRow()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getRow()].pieceType() != temp.pieceType())){
              //if the spot right above the clicked one is not black AND it is the other player's color,
              continue;
              // keep going with the block statement            2,4 jj = 3,4,5,6,7
          }
          if ((gameboard[j][temp.getRow()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getRow()].pieceType() == temp.pieceType())){
          // if the spot right above is not black AND it is the same player's color,
              for(int i = temp.getCol(); i &lt;= j; i++)
                  // i equals the row of the clicked spot, it goes until it finds J, where the other green / purple end piece is
                  {
                  // change the colors of all colors in the clicked color's column and rows in between the other border piece
                      gameboard[temp.getRow()][i].change(temp.pieceType());
                  }
              }
          else
          {
              break;
          }
          }
      }
      public void checkFlipRight(BlankPiece temp)
      {
          for(int j = temp.getCol() - 1; j&gt;=0; j--){
              // j equals the square to the right of the clicked one, and it keeps going down until it hits the bottom of the board
          if ((gameboard[j][temp.getRow()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getRow()].pieceType() != temp.pieceType())){
              //if the spot right above the clicked one is not black AND it is the other player's color,
              continue;
              // keep going with the block statement            2,4 jj = 3,4,5,6,7
          }
          if ((gameboard[j][temp.getRow()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getRow()].pieceType() == temp.pieceType())){
          // if the spot right above is not black AND it is the same player's color,
              for(int i = temp.getCol(); i &gt;= j; i--)
                  // i equals the row of the clicked spot, it goes until it finds J, where the other green / purple end piece is
                  {
                  // change the colors of all colors in the clicked color's column and rows in between the other border piece
                      gameboard[temp.getRow()][i].change(temp.pieceType());
                  }
              }
          else if (gameboard[j][temp.getRow()].pieceType() == 0)
          {
              break;
          }
          }
      }
  </code></pre>
  
  <p>So those are my four functions (one for west, east, north, and south). BlankPiece is below:</p>
  
  <pre><code>import java.awt.Color;
  
  import javax.swing.*;
  
  
  public class BlankPiece extends JButton
  {
      private final ImageIcon bbutton = new ImageIcon("bbutton.png");
      private final ImageIcon gbutton = new ImageIcon("gbutton.jpg");
      private final ImageIcon pbutton = new ImageIcon("pbutton.png");
      private int x;
      private int y;
  
      BlankPiece(int x, int y)
      {
        this.setBackground(Color.BLACK);
        this.setIcon(bbutton);
        this.x = x;
        this.y = y;
      }
      public void setGreen()
      {
            this.setBackground(Color.BLACK);
            this.setIcon(gbutton);
      }
      public void setPurple()
      {
            this.setBackground(Color.BLACK);
            this.setIcon(pbutton);
      }
      public int getRow() {
          // TODO Auto-generated method stub
          return x;
      }
      public int getCol() {
          return y;
      }
      public void resetImage(int count)
      {
          if (count % 2 == 0)
          {
                this.setIcon(gbutton);
          }
          else
          {
              this.setIcon(pbutton);
          }
      }
  
      public boolean isSet()
      {
  
          String image = "" + this.getIcon();
  
          if((image.equals("pbutton.png")) || (image.equals("gbutton.jpg")))
          {
          }
          return false;
      }
      public int pieceType()
      {
          if(getIcon().equals(pbutton)) // purple
          {
              return -1;
          }
          else if(getIcon().equals(bbutton)) // blank
          {
              return 0;
          }
          else // green
          {
              return 1;
          }
      }
      public void change(int i) {
          if (i == -1)
          {
              setIcon(pbutton);
          }
          else if(i == 1)
          {
              setIcon(gbutton);
          }
          else
          {
              setIcon(bbutton);
          }
  
      }
  
  }
  </code></pre>
  
  <p>The problem is not that any checkflip isn't called (they are all called with the same correct parameters), but something in checkFlipRight or CheckFlipLeft functions...</p>
  
  <p>EDIT: full working example by request.</p>
  
  <p>Make a folder with TWO files</p>
  
  <p>One is called Reversi.java
  and the other is BlankPiece.java</p>
  
  <p>REVERSI.JAVA:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class Reversi extends JPanel {
      BlankPiece [][] gameboard = new BlankPiece[8][8];
      JPanel toparea = new JPanel();
      JPanel gamearea = new JPanel();
      JPanel greenarea = new JPanel();
      JPanel purplearea = new JPanel();
      JPanel logo = new JPanel();
      JLabel logoarea = new JLabel();
      JLabel greenlogo = new JLabel();
      JLabel purplelogo = new JLabel();
      ImageIcon blackicon = new ImageIcon("bbutton.png");
      ImageIcon icon = new ImageIcon("reversilogo.png");
      ImageIcon dgreen = new ImageIcon("DarkGreen.png");
      ImageIcon lgreen = new ImageIcon("LightGreen.png");
      ImageIcon dpurple = new ImageIcon("DarkPurple.png");
      ImageIcon lpurple = new ImageIcon("LightPurple.png");
      int count = 0;
      int jplus = 0;
      public Reversi()
      {  
          gamearea.setLayout(new GridLayout(8,8));
          for (int row = 0; row &lt; gameboard.length; row++)
          {
              for(int col = 0; col &lt; gameboard.length; col++)
              {
                  gameboard[row][col] = new BlankPiece(row, col);
                  gamearea.add(gameboard[row][col]);
                  gameboard[row][col].addActionListener(new ButtonListener());
  
              }
          }
          logoarea.setPreferredSize(new Dimension(304,73));
          toparea.setLayout(new BorderLayout());
          this.setLayout(new BorderLayout());
          toparea.setBackground(new Color(97,203,242));
          gamearea.setBackground(new Color(83,35,215));
          logo.setBackground(new Color(97,203,242));
          logoarea.setIcon(icon);
          greenlogo.setIcon(dgreen);
          purplelogo.setIcon(lpurple);
          toparea.add(greenlogo, BorderLayout.WEST);
          toparea.add(purplelogo, BorderLayout.EAST);
          toparea.add(logo, BorderLayout.CENTER);
          logo.add(logoarea);
          this.add(toparea, BorderLayout.NORTH);
          this.add(gamearea, BorderLayout.CENTER);
          gameboard[3][3].setGreen();
          gameboard[3][4].setPurple();
          gameboard[4][3].setPurple();
          gameboard[4][4].setGreen();
      }
      private class ButtonListener implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
          {
              BlankPiece temp = (BlankPiece)e.getSource();  // #1
              int row = temp.getRow();  // #2
              System.out.println(row);
              int col = temp.getCol();
              System.out.println(col);
  
              temp.isSet();
  
              // col and row are the column and row of blankpiece that was clicked
              String image = "" + temp.getIcon();
              if((image.equals("pbutton.png")) || (image.equals("gbutton.jpg")))
              {
                  JOptionPane.showMessageDialog(null, "This spot is already occupied by " +
                          "a piece. Please pick an empty tile.");
              }
              else 
              {
                  temp.resetImage(count++);
                  System.out.println("About to check");
                  checkFlipDown(gameboard[row][col]);
                  checkFlipUp(gameboard[row][col]);
                  checkFlipLeft(gameboard[row][col]);
                  checkFlipRight(gameboard[row][col]);
              }
              if (count % 2 == 0)
              {
                  greenlogo.setIcon(dgreen);
                  purplelogo.setIcon(lpurple);
              }
              else
              {
                  greenlogo.setIcon(lgreen);
                  purplelogo.setIcon(dpurple);
              }
          } 
      } 
      public void checkFlipDown(BlankPiece temp)
      {
          for(int j = temp.getRow() - 1; j&gt;=0; j--){
              // j equals the square right above the clicked one, and it keeps going up until it hits the top of the board
          if ((gameboard[j][temp.getCol()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getCol()].pieceType() != temp.pieceType())){
              //if the spot right above the clicked one is not black AND it is the other player's color,
              continue;
              // keep going with the block statement
          }
          if ((gameboard[j][temp.getCol()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getCol()].pieceType() == temp.pieceType())){
          // if the spot right above is not black AND it is the same player's color,
              for(int i = temp.getRow(); i &gt;= j; i--)
                  // i equals the row of the clicked spot, it goes until it finds J, where the other green / purple end piece is
                  {
                  // change the colors of all colors in the clicked color's column and rows in between the other border piece
                      gameboard[i][temp.getCol()].change(temp.pieceType());
                  }
              }
          else
          {
              // if no other border pieces exist, go out of the loop
              break;
          }
          }
      }
      public void checkFlipUp(BlankPiece temp)
      {
          for(int j = temp.getRow() + 1; j&lt;=7; j++){
              // j equals the square right below the clicked one, and it keeps going down until it hits the bottom of the board
          if ((gameboard[j][temp.getCol()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getCol()].pieceType() != temp.pieceType())){
              //if the spot right above the clicked one is not black AND it is the other player's color,
              continue;
              // keep going with the block statement            2,4 jj = 3,4,5,6,7
          }
          if ((gameboard[j][temp.getCol()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getCol()].pieceType() == temp.pieceType())){
          // if the spot right above is not black AND it is the same player's color,
              for(int i = temp.getRow(); i &lt;= j; i++)
                  // i equals the row of the clicked spot, it goes until it finds J, where the other green / purple end piece is
                  {
                  // change the colors of all colors in the clicked color's column and rows in between the other border piece
                      gameboard[i][temp.getCol()].change(temp.pieceType());
                  }
              }
          else
          {
              break;
          }
          }
      }
      public void checkFlipLeft(BlankPiece temp)
      {
          for(int j = temp.getCol() + 1; j&lt;=7; j++){
              // j equals the square right below the clicked one, and it keeps going down until it hits the bottom of the board
          if ((gameboard[j][temp.getRow()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getRow()].pieceType() != temp.pieceType())){
              //if the spot right above the clicked one is not black AND it is the other player's color,
              continue;
              // keep going with the block statement            2,4 jj = 3,4,5,6,7
          }
          if ((gameboard[j][temp.getRow()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getRow()].pieceType() == temp.pieceType())){
          // if the spot right above is not black AND it is the same player's color,
              for(int i = temp.getCol(); i &lt;= j; i++)
                  // i equals the row of the clicked spot, it goes until it finds J, where the other green / purple end piece is
                  {
                  // change the colors of all colors in the clicked color's column and rows in between the other border piece
                      gameboard[temp.getRow()][i].change(temp.pieceType());
                  }
              }
          else
          {
              break;
          }
          }
      }
      public void checkFlipRight(BlankPiece temp)
      {
          for(int j = temp.getCol() - 1; j&gt;=0; j--){
              // j equals the square to the right of the clicked one, and it keeps going down until it hits the bottom of the board
          if ((gameboard[j][temp.getRow()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getRow()].pieceType() != temp.pieceType())){
              //if the spot right above the clicked one is not black AND it is the other player's color,
              continue;
              // keep going with the block statement            2,4 jj = 3,4,5,6,7
          }
          if ((gameboard[j][temp.getRow()].pieceType() != 0) &amp;&amp; (gameboard[j][temp.getRow()].pieceType() == temp.pieceType())){
          // if the spot right above is not black AND it is the same player's color,
              for(int i = temp.getCol(); i &gt;= j; i--)
                  // i equals the row of the clicked spot, it goes until it finds J, where the other green / purple end piece is
                  {
                  // change the colors of all colors in the clicked color's column and rows in between the other border piece
                      gameboard[temp.getRow()][i].change(temp.pieceType());
                  }
              }
          else if (gameboard[j][temp.getRow()].pieceType() == 0)
          {
              break;
          }
          }
      }
  
  }
  </code></pre>
  
  <p>BLANKPIECE.JAVA</p>
  
  <pre><code>import java.awt.Color;
  
  import javax.swing.*;
  
  
  public class BlankPiece extends JButton
  {
      private final ImageIcon bbutton = new ImageIcon("bbutton.png");
      private final ImageIcon gbutton = new ImageIcon("gbutton.jpg");
      private final ImageIcon pbutton = new ImageIcon("pbutton.png");
      private int x;
      private int y;
  
      BlankPiece(int x, int y)
      {
        this.setBackground(Color.BLACK);
        this.setIcon(bbutton);
        this.x = x;
        this.y = y;
      }
      public void setGreen()
      {
            this.setBackground(Color.BLACK);
            this.setIcon(gbutton);
      }
      public void setPurple()
      {
            this.setBackground(Color.BLACK);
            this.setIcon(pbutton);
      }
      public int getRow() {
          // TODO Auto-generated method stub
          return x;
      }
      public int getCol() {
          return y;
      }
      public void resetImage(int count)
      {
          if (count % 2 == 0)
          {
                this.setIcon(gbutton);
          }
          else
          {
              this.setIcon(pbutton);
          }
      }
  
      public boolean isSet()
      {
  
          String image = "" + this.getIcon();
  
          if((image.equals("pbutton.png")) || (image.equals("gbutton.jpg")))
          {
          }
          return false;
      }
      public int pieceType()
      {
          if(getIcon().equals(pbutton)) // purple
          {
              return -1;
          }
          else if(getIcon().equals(bbutton)) // blank
          {
              return 0;
          }
          else // green
          {
              return 1;
          }
      }
      public void change(int i) {
          if (i == -1)
          {
              setIcon(pbutton);
          }
          else if(i == 1)
          {
              setIcon(gbutton);
          }
          else
          {
              setIcon(bbutton);
          }
  
      }
  
  }
  </code></pre>