title: Java Reversi/Othello Program 2D array flip/get methods?
body: <p>I'm hoping someone could help me understand my code probs. I'm trying to create a Reversi board game in Java but my game board will only stick to white pieces and refuse to do anything. Any suggestions would be great.</p>
  
  <p>This is the part I have to do (hence marked TO DO). Honestly it should be 1 or 2 lines for each of them but I'm just not catching on. I'm definitely having trouble with my get() and flip() methods. </p>
  
  <pre><code>public class Simple2DArray implements Simple2DInterface
  {
  // TO DO: Your instance variables
  private int[][] simpleArray = new int[8][8]; 
  private int row = simpleArray.length;
  private int column = simpleArray[0].length;
  private int none = -1, white = 1, black = 0, value;
  
  /**
   * Constructor: Once a two dimensional array is constructed
   * set all elements in the array to -1.
   * @param aRow the number of rows of this Simple2DArray.
   * @param aColumn the number of columns of this Simple2DArray.
   */
  public Simple2DArray(int aRow, int aColumn)
  {
      // TO DO: Constructor
      this.row = aRow;
      this.column = aColumn;
          for (int i = 1; i &lt; simpleArray.length; i++)
          {
              for (int j = 1; j &lt; simpleArray[i].length; j++)
              {
                  simpleArray[i][j] = none;
              }
          }
  }
  
  /**
   * Gets the number of rows of this Simple 2D Array.
   * @return the number of rows of this Simple 2D array.
   */
  public int getNumberOfRows()
  {
      // TO DO
      return simpleArray.length;
  
  }
  
  /**
   * Gets the number of columns of this Simple 2D Array.
   * @return the number of columns of this Simple 2D array.
   */
  public int getNumberOfColumns()
  {
      // TO DO
      return simpleArray[1].length;
  }
  
  /**
   * Reset every element to -1
   */
  public void clear()
  {
      // TO DO
      for (int i = 1; i &lt; simpleArray.length; i++)
      {
          for (int j = 1; j &lt; simpleArray[i].length; j++)
          {
              simpleArray[i][j] = none;
          }
      }
  }
  
  /**
   * Sets the value at location row and column to 1.
   * @param row the row number (start at 1).
   * @param column the column number (start at 1).
   */
  public void setToOne(int row, int column)
  {
      // TO DO
      for (int i = 1; i &lt; simpleArray.length; i++)
      {
          for (int j = 1; j &lt; simpleArray[i].length; j++)
          {
              simpleArray[i][j] = white;
          }
      }
  
  }
  
  /**
   * Sets the value at location row and column to 0.
   * @param row the row number (start at 1).
   * @param column the column number (start at 1).
   */
  public void setToZero(int row, int column)
  {
      // TO DO
      for (int i = 1; i &lt; simpleArray.length; i++)
      {
          for (int j = 1; j &lt; simpleArray[i].length; j++)
          {
              simpleArray[i][j] = black;
          }
      }
  
  }
  
  /**
   * Reverse the value at row and column. If the value
   * at row and column is 1, reverse it to 0. If the value
   * at row and column is 0, reverse it to 1. If the value
   * at row and column is -1, do nothing.
   * @param row the row number (start at 1).
   * @param column the column number (start at 1).
   */
  public void flip(int row, int column)
  {
      // TO DO
      value = simpleArray[row][column];
  
          if (value == white)
          {
              value = black;
          }
          if(value == black)
          {
              value = white;
          }
  
  }
  
  /**
   * Gets the value at row and column.
   * @param row the row number (start at 1).
   * @param column the column number (start at 1).
   * @return the value at row and column.
   */ 
  public int get(int row, int column)
  {
      // TO DO
      for (int i = 1; i &lt; simpleArray.length; i++)
      {
          for (int j = 1; j &lt; simpleArray[i].length; j++)
          {
              row = i;
              column = j;
              value = simpleArray[row][column];
          }
      }
      return value;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And the rest of my files/interfaces/etc.:</p>
  
  <pre><code>public interface Simple2DInterface
  {
  /**
   * Gets the number of rows of this Simple 2D Array.
   * @return the number of rows of this Simple 2D array.
   */
  public int getNumberOfRows();
  
  /**
   * Gets the number of columns of this Simple 2D Array.
   * @return the number of columns of this Simple 2D array.
   */
  public int getNumberOfColumns();
  
  /**
   * Reset every element to -1
   */
  public void clear();
  
  /**
   * Sets the value at location row and column to 1.
   * @param row the row number (start at 1).
   * @param column the column number (start at 1).
   */
  public void setToOne(int row, int column);
  
  /**
   * Sets the value at location row and column to 0.
   * @param row the row number (start at 1).
   * @param column the column number (start at 1).
   */ 
  public void setToZero(int row, int column);
  
  /**
   * Reverse the value at row and column. If the value
   * at row and column is 1, reverse it to 0. If the value
   * at row and column is 0, reverse it to 1. If the value
   * at row and column is -1, do nothing.
   * @param row the row number (start at 1).
   * @param column the column number (start at 1).
   */
  public void flip(int row, int column);
  
  /**
   * Gets the value at row and column.
   * @param row the row number (start at 1).
   * @param column the column number (start at 1).
   * @return the value at row and column.
   */ 
  public int get(int row, int column);
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public class Simple2DArrayTester
  {
  public static void main(String[] args)
  {
      int point = 0;
      int numRows = 10;
      int numColumns = 15;
      boolean notEqualMinusOne = false;
  
      Simple2DInterface s2d1 = new Simple2DArray(numRows,numColumns);
  
      // Check that all locations are -1.
  
      System.out.print("Testing that all locations must be -1: ");
  
      for(int i = 1; i &lt;= numRows; i++)
      {
          for(int j = 1; j &lt;= numColumns; j++)
          {
              if(s2d1.get(i, j) != -1)
              {
                  notEqualMinusOne = true;
              }
          }
      }
  
      if(notEqualMinusOne)
      {
          System.out.println("FAIL");
          System.out.println("Not all locations contain -1.\n");
      }
      else
      {
          System.out.println("PASS");
          point++;
      }
      System.out.println("Your current point is " + point + ".\n");
  
      // Testing the method getNumberOfRows()
  
      System.out.print("Testing the method getNumberOfRows: ");
      if(s2d1.getNumberOfRows() != numRows)
      {
          System.out.println("FAIL");
          System.out.println("The number of from your method getNumberOfRows should be " + numRows + ".");
          System.out.println("But your method getNumberOfRows returns " + s2d1.getNumberOfRows() + ".\n");
      }
      else
      {
          System.out.println("PASS");
          point++;
      }
      System.out.println("Your current point is " + point + ".\n");
  
      // Testing the method getNumberOfColumns()
  
      System.out.print("Testing the method getNumberOfColumns: ");
      if(s2d1.getNumberOfColumns() != numColumns)
      {
          System.out.println("FAIL");
          System.out.println("The number of from your method getNumberOfColumns should be " + numColumns + ".");
          System.out.println("But your method getNumberOfColumns returns " + s2d1.getNumberOfColumns() + ".\n");
      }
      else
      {
          System.out.println("PASS");
          point++;
      }
      System.out.println("Your current point is " + point + ".\n");
  
      // Testing the method setToOne()
  
      System.out.print("Testing the method setToOne(): ");
  
      s2d1.setToOne(5, 9);
      if(s2d1.get(5, 9) != 1)
      {
          System.out.println("FAIL");
          System.out.println("After calling the method setToOne(5,9) the value at row 5 column 9 should be 1.");
          System.out.println("But your method get(5,9) returns " + s2d1.get(5,9) + ".\n");
      }
      else
      {
          System.out.println("PASS");
          point++;
      }
      System.out.println("Your current point is " + point + ".\n");
  
      // setToZero()
  
      System.out.print("Testing the method setToZero(): ");
  
      s2d1.setToZero(9, 5);
      if(s2d1.get(9, 5) != 0)
      {
          System.out.println("FAIL");
          System.out.println("After calling the method setToZero(9,5) the value at row 9 column 5 should be 0.");
          System.out.println("But your method get(9,5) returns " + s2d1.get(9,5) + ".\n");
      }
      else
      {
          System.out.println("PASS");
          point++;
      }
      System.out.println("Your current point is " + point + ".\n");
  
      // flip()
  
      System.out.print("Testing flip from one to zero: ");
      s2d1.flip(5, 9);
      if(s2d1.get(5, 9) != 0)
      {
          System.out.println("FAIL");
          System.out.println("After flipping by calling flip(5,9) the value at row 5 column 9 should be 0.");
          System.out.println("But your method get(5,9) returns " + s2d1.get(5,9) + "\n.");
      }
      else
      {
          System.out.println("PASS");
          point++;
      }
      System.out.println("Your current point is " + point + ".\n");
  
      System.out.print("Testing flip from zero to one: ");
      s2d1.flip(9, 5);
      if(s2d1.get(9, 5) != 1)
      {
          System.out.println("FAIL");
          System.out.println("After flipping by calling flip(9,5) the value at row 9 column 5 should be 1.");
          System.out.println("But your method get(9,5) returns " + s2d1.get(9,5) + "\n.");
      }
      else
      {
          System.out.println("PASS");
          point++;
      }
      System.out.println("Your current point is " + point + ".\n");
  
      System.out.print("Testing flip -1: ");
      s2d1.flip(1, 1);
      if(s2d1.get(1, 1) != -1)
      {
          System.out.println("FAIL");
          System.out.println("After flipping by calling flip(1,1), the value at row 1 column 1 should be -1.");
          System.out.println("But your method get(1,1) returns " + s2d1.get(1,1) + ".\n");
      }
      else
      {
          System.out.println("PASS");
          point++;
      }
      System.out.println("Your current point is " + point + ".\n");
  
      // clear()
  
      System.out.print("Testing the method clear(): ");
      s2d1.clear();
  
      notEqualMinusOne = false;
  
      for(int i = 1; i &lt;= numRows; i++)
      {
          for(int j = 1; j &lt;= numColumns; j++)
          {
              if(s2d1.get(i, j) != -1)
              {
                  notEqualMinusOne = true;
              }
          }
      }
  
      if(notEqualMinusOne)
      {
          System.out.println("FAIL");
          System.out.println("After calling the method clear. Not all locations contain -1.\n");
      }
      else
      {
          System.out.println("PASS");
          point++;
      }
      System.out.println("Your current point is " + point + ".\n");
  
      if(point == 9)
      {
          System.out.println("Everything looks good one extra point :)");
          point++;
      }
  
      System.out.println("Your final point is " + point + ".");
  
      if(point == 10)
      {
          System.out.println("Contratulation! Your class Simple2DArray works perfectly (I guess).");
          System.out.println("You can run OthelloFrame to see how Simple2DArray can be used in a program.");
      }
      else
      {
          System.out.println("There is one or more errors in your class.");
          System.out.println("Fix your bugs to get more points.");
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.GridLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.border.TitledBorder;
  
  
  @SuppressWarnings("serial")
  public class OthelloFrame extends JFrame
  {
  private int frameWidth = 600;
  private int frameHeight = 600;
  
  private JLabel msg;
  private JButton switchPlayer;
  private JPanel msgPanel;
  private JPanel controlPanel;
  
  private Simple2DInterface sa;
  private OthelloComponent oc;
  private boolean isWhite = false;
  
  public static void main(String[] args)
  {
      JFrame frame = new OthelloFrame();
      frame.setVisible(true);
  }
  
  public OthelloFrame()
  {
      sa = new Simple2DArray(8,8);
  
      sa.setToOne(4, 4);
      sa.setToZero(4, 5);
      sa.setToZero(5, 4);
      sa.setToOne(5, 5);
  
      msg = new JLabel("Click on an empty space to put a disk or click on a disk to change color.");
  
      oc = new OthelloComponent(sa);
  
      controlPanel = new JPanel();
      controlPanel.setLayout(new GridLayout(2,1));
  
      switchPlayer = new JButton("Switch Color to White");
  
      class SwitchButtonListener implements ActionListener
      {
          public void actionPerformed(ActionEvent arg0)
          {
              oc.switchColor();
              isWhite = !isWhite;
              if(isWhite)
              {
                  switchPlayer.setText("Switch Color to Black");
              }
              else
              {
                  switchPlayer.setText("Switch Color to White");
              }
          }
      }
  
      ActionListener sp = new SwitchButtonListener();
      switchPlayer.addActionListener(sp);
  
      controlPanel.add(switchPlayer);
  
      msgPanel = new JPanel();
      msgPanel.setBorder(new TitledBorder("Message"));
      msgPanel.add(msg);
  
      controlPanel.add(msgPanel);
  
      setSize(frameWidth, frameHeight);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setTitle("Wanna be Othello");
      add(oc);
      add(controlPanel, BorderLayout.SOUTH);
      setVisible(true);
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.awt.geom.Ellipse2D;
  import java.awt.geom.Line2D;
  import javax.swing.JComponent;
  
  @SuppressWarnings("serial")
  public class OthelloComponent extends JComponent implements MouseListener
  {
  private Simple2DInterface grid;
  private int numRows;
  private int numColumns;
  private int leftMargin = 10;
  private int rightMargin = 10;
  private int topMargin = 10;
  private int bottomMargin = 10;
  private int circleMargin = 5;
  private double circleSize;
  private int width;
  private int height;
  private int topLeftX;
  private int topLeftY;
  private int bottomRightX;
  private int bottomRightY;
  private double cellWidth;
  private double cellHeight;
  private double boardWidth;
  private double boardHeight;
  private boolean isWhite;
  
  public OthelloComponent(Simple2DInterface a2DArray)
  {
      grid = a2DArray;
      numRows = grid.getNumberOfRows();
      numColumns = grid.getNumberOfColumns();
      isWhite = false;
  
      this.addMouseListener(this);
  }
  
  public void paintComponent(Graphics g)
  {
      Graphics2D g2 = (Graphics2D) g;
  
      width = this.getWidth();
      height = this.getHeight();
  
      cellWidth = (double) (width - (leftMargin + rightMargin)) / numColumns;
      cellHeight = (double) (height - (topMargin + bottomMargin)) / numRows;
  
      if(cellWidth &gt; cellHeight)
      {
          cellWidth = cellHeight;
      }
      else
      {
          cellHeight = cellWidth;
      }
      circleSize = cellWidth - (2 * circleMargin);
  
      boardWidth = cellWidth * numColumns;
      boardHeight = cellHeight * numRows;
  
      topLeftX = (width - (int) boardWidth) / 2;
      topLeftY = (height - (int) boardHeight) / 2;
      bottomRightX = topLeftX + (int) boardWidth;
      bottomRightY = topLeftY + (int) boardHeight;
  
      Line2D.Double line = new Line2D.Double(0,0,0,0);
  
      // Draw the Board
  
      g2.setColor(Color.BLACK);
  
      for(int i = 0; i &lt;= numRows; i++)
      {
          line.setLine(topLeftX, topLeftY + (i * cellHeight), bottomRightX, topLeftY + (i * cellHeight));
          g2.draw(line);
      }
  
      for(int i = 0; i &lt;= numColumns; i++)
      {
          line.setLine(topLeftX + (i * cellWidth), topLeftY, topLeftX + (i * cellWidth), bottomRightY);
          g2.draw(line);
      }
  
      // Draw circles
  
      Ellipse2D.Double circle = new Ellipse2D.Double();
  
      for(int row = 1; row &lt;= numRows; row++)
      {
          for(int column = 1; column &lt;= numColumns; column++)
          {
              if(grid.get(row, column) == 0)
              {
                  g2.setColor(Color.BLACK);
                  int x = topLeftX + circleMargin + (int) ((column - 1) * cellWidth);
                  int y = topLeftY + circleMargin + (int) ((row - 1) * cellHeight);
                  circle.setFrame(x,y,circleSize,circleSize);
                  g2.fill(circle);
              }
              else if(grid.get(row, column) == 1)
              {
                  int x = topLeftX + circleMargin + (int) ((column - 1) * cellWidth);
                  int y = topLeftY + circleMargin + (int) ((row - 1) * cellHeight);
                  circle.setFrame(x,y,circleSize,circleSize);
                  g2.setColor(Color.WHITE);
                  g2.fill(circle);
                  g2.setColor(Color.BLACK);
                  g2.draw(circle);
              }
          }
      }
  }
  
  public void mouseClicked(MouseEvent arg0)
  {
      int column = getColumn(arg0.getX());
      int row = getRow(arg0.getY());
  
      if(row != 0 &amp;&amp; column != 0)
      {
          if(grid.get(row, column) == -1)
          {
              if(isWhite)
              {
                  grid.setToOne(row, column);
              }
              else
              {
                  grid.setToZero(row, column);
              }
          }
          else
          {
              if(isWhite &amp;&amp; grid.get(row, column) == 0)
              {
                  grid.flip(row, column);
              }
              else if(!isWhite &amp;&amp; grid.get(row, column) == 1)
              {
                  grid.flip(row, column);
              }
          }
      }
  
      repaint();
  }
  
  public void switchColor()
  {
      isWhite = !isWhite;
  }
  
  public int getColumn(int x)
  {
      int result = 0;
  
      for(int column = 1; column &lt;= numColumns; column++)
      {
          if(x &gt; topLeftX + ((column - 1) * cellWidth) &amp;&amp; x &lt; topLeftX + (column * cellWidth))
          {
              result = column;
              break;
          }
      }
  
      return result;
  }
  
  public int getRow(int y)
  {
      int result = 0;
  
      for(int row = 1; row &lt;= numRows; row++)
      {
          if(y &gt; topLeftY + ((row - 1) * cellHeight) &amp;&amp; y &lt; topLeftY + (row * cellHeight))
          {
              result = row;
              break;
          }
      }
  
      return result;
  }
  
  public void mouseEntered(MouseEvent arg0)
  {
  }
  
  public void mouseExited(MouseEvent arg0)
  {
  }
  
  public void mousePressed(MouseEvent arg0)
  {
  }
  
  public void mouseReleased(MouseEvent arg0)
  {
  }
  </code></pre>
  
  <p>}</p>