Java Reversi/Otherlo程序2D数组翻转/获取方法?

Java Reversi/Otherlo程序2D数组翻转/获取方法?,java,methods,multidimensional-array,Java,Methods,Multidimensional Array,我希望有人能帮我理解我的代码问题。我试图用Java创建一个Reversi棋盘游戏,但我的棋盘只会粘在白色的棋子上,拒绝做任何事情。任何建议都很好 这是我必须做的部分(因此标记为要做)。老实说,他们每个人都应该有一两行,但我就是不明白。我的get()和flip()方法肯定有问题 public class Simple2DArray implements Simple2DInterface { // TO DO: Your instance variables private int[][] sim

我希望有人能帮我理解我的代码问题。我试图用Java创建一个Reversi棋盘游戏,但我的棋盘只会粘在白色的棋子上,拒绝做任何事情。任何建议都很好

这是我必须做的部分(因此标记为要做)。老实说,他们每个人都应该有一两行,但我就是不明白。我的get()和flip()方法肯定有问题

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 < simpleArray.length; i++)
        {
            for (int j = 1; j < 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 < simpleArray.length; i++)
    {
        for (int j = 1; j < 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 < simpleArray.length; i++)
    {
        for (int j = 1; j < 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 < simpleArray.length; i++)
    {
        for (int j = 1; j < 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 < simpleArray.length; i++)
    {
        for (int j = 1; j < simpleArray[i].length; j++)
        {
            row = i;
            column = j;
            value = simpleArray[row][column];
        }
    }
    return value;
}
}

}

导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.awt.geom.Ellipse2D;
导入java.awt.geom.Line2D;
导入javax.swing.JComponent;
@抑制警告(“串行”)
公共类OtherloComponent扩展JComponent实现MouseListener
{
私有Simple2DInterface网格;
私人int numRows;
私人国际专栏;
私有int leftMargin=10;
私人int rightMargin=10;
私人int topMargin=10;
私人保证金=10;
私有整数圈边缘=5;
私人双环化;
私有整数宽度;
私人内部高度;
私有int-topLeftX;
私有int-topLeftY;
私有int-bottomRightX;
私隐权;
私人双单元宽度;
私人双室高度;
私人双板宽度;
私人双板高度;
私有布尔值是白色的;
公共OTHELLO组件(Simple2DInterface a2DArray)
{
网格=a2DArray;
numRows=grid.getNumberOfRows();
numColumns=grid.getNumberOfColumns();
isWhite=false;
这个。addMouseListener(这个);
}
公共组件(图形g)
{
图形2d g2=(图形2d)g;
宽度=this.getWidth();
height=this.getHeight();
cellWidth=(双精度)(宽度-(左边距+右边距))/numColumns;
单元格高度=(双精度)(高度-(上边距+下边距))/numRows;
如果(单元宽度>单元高度)
{
单元宽度=单元高度;
}
其他的
{
单元高度=单元宽度;
}
圆圈大小=单元格宽度-(2*圆圈边缘);
boardWidth=单元格宽度*numColumns;
boardHeight=单元高度*numRows;
topLeftX=(宽度-(int)boardWidth)/2;
topLeftY=(高度-(int)boardHeight)/2;
bottomRightX=topLeftX+(int)boardWidth;
bottomRightY=topLeftY+(int)板高;
Line2D.Double line=新的Line2D.Double(0,0,0,0);
//画板
g2.设置颜色(颜色为黑色);

对于(int i=0;i查看代码时,我会更改很多与问题无关的内容,因此我只是重构了您的类:

OtherlofName.java:

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 static final int FRAME_WIDTH = 600;
  private static final int FRAME_HEIGHT = FRAME_WIDTH;

  private OthelloComponent gridComponent;
  private JButton switchPlayerButton;

  public OthelloFrame() {
    // create grid and add to frame
    Simple2DInterface grid = 
        new Simple2DArray(Simple2DArray.DEFAULT_BOARD_DIMENSION, Simple2DArray.DEFAULT_BOARD_DIMENSION);
    int halfWidth = grid.getNumberOfColumns() / 2;
    int halfHeight = grid.getNumberOfRows() / 2;
    grid.set(halfHeight - 1, halfWidth - 1, Simple2DArray.WHITE);
    grid.set(halfHeight - 1, halfWidth, Simple2DArray.BLACK);
    grid.set(halfHeight, halfWidth - 1, Simple2DArray.BLACK);
    grid.set(halfHeight, halfWidth, Simple2DArray.WHITE);
    gridComponent = new OthelloComponent(grid);
    add(gridComponent);

    // create control panel
    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new GridLayout(2,1));

    // create and add switch player button to control panel
    switchPlayerButton = new JButton("Switch Color to White");
    ActionListener sp = new SwitchButtonListener();
    switchPlayerButton.addActionListener(sp);
    controlPanel.add(switchPlayerButton);

    // create and add instructions to control panel
    JPanel msgPanel = new JPanel();
    msgPanel.setBorder(new TitledBorder("Message"));
    msgPanel.add(new JLabel("Click on an empty space to put a disk or click on a disk to change color."));
    controlPanel.add(msgPanel);

    // add control panel to frame
    add(controlPanel, BorderLayout.SOUTH);

    // final setup
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setTitle("Wanna be Othello");
    setVisible(true);
  }

  public static void main(String[] args) {
    JFrame frame = new OthelloFrame();
    frame.setVisible(true);
  }

  private class SwitchButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent arg0) {
      gridComponent.switchColor();
      switchPlayerButton.setText("Switch Color to " + (gridComponent.whiteToPlay() ? "Black" : "White"));
    }
  }
}
OtherloComponent.java:

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 static final int MARGIN = 10;

  private static final int CIRCLE_MARGIN = MARGIN / 2;
  private static final int BOTTOM_MARGIN = MARGIN;
  private static final int TOP_MARGIN = MARGIN;
  private static final int LEFT_MARGIN = MARGIN;
  private static final int RIGHT_MARGIN = MARGIN;

  private Simple2DInterface grid;
  private int numRows;
  private int numColumns;

  private double circleDiameter;
  private double topLeftX;
  private double topLeftY;
  private double cellHeight;
  private double cellWidth;
  private double boardHeight;
  private double boardWidth;

  private int turn;

  public OthelloComponent(Simple2DInterface a2DArray) {
    grid = a2DArray;
    numRows = grid.getNumberOfRows();
    numColumns = grid.getNumberOfColumns();
    turn = Simple2DArray.BLACK;

    this.addMouseListener(this);
  }

  public int getColumn(int x) {
    return (int) (Math.ceil((x - topLeftX) / cellWidth) - 1);
  }

  public int getRow(int y) {
    return (int) (Math.ceil((y - topLeftY) / cellHeight) - 1);
  }

  public void mouseClicked(MouseEvent arg0) {
    int column = getColumn(arg0.getX());
    int row = getRow(arg0.getY());

    // you can take this out if you want
    System.out.println("r: " + row + ", c: " + column);

    if (row >= 0 && column >= 0 && row < numRows && column < numColumns) {
        grid.set(row, column, turn);
    }
    repaint();
  }

  public void mouseEntered(MouseEvent arg0) {}

  public void mouseExited(MouseEvent arg0) {}

  public void mousePressed(MouseEvent arg0) {}

  public void mouseReleased(MouseEvent arg0) {}

  public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    cellHeight = ((double) getHeight() - (TOP_MARGIN + BOTTOM_MARGIN)) / numRows;
    cellWidth = ((double) getWidth() - (LEFT_MARGIN + RIGHT_MARGIN)) / numColumns;

    // make cells the largest possible squares
    if (cellWidth > cellHeight) {
      cellWidth = cellHeight;
    } else {
      cellHeight = cellWidth;
    }
    circleDiameter = cellWidth - (2 * CIRCLE_MARGIN);

    boardHeight = cellHeight * numRows;
    boardWidth = cellWidth * numColumns;

    topLeftX = (getWidth() - boardWidth) / 2;
    topLeftY = (getHeight() - boardHeight) / 2;
    double bottomRightX = topLeftX + boardWidth;
    double bottomRightY = topLeftY + boardHeight;

    Line2D.Double line = new Line2D.Double(0,0,0,0);

    // draw the lines on the board
    g2.setColor(Color.BLACK);
    for (int i = 0; i <= numRows; i++) {
      line.setLine(topLeftX, topLeftY + i * cellHeight, bottomRightX, topLeftY + i * cellHeight);
      g2.draw(line);
    }

    for (int i = 0; i <= numColumns; i++) {
      line.setLine(topLeftX + i * cellWidth, topLeftY, topLeftX + i * cellWidth, bottomRightY);
      g2.draw(line);
    }

    // draw circles
    for (int row = 0; row < numRows; row++) {
      for (int column = 0; column < numColumns; column++) {
        if  (grid.get(row, column) != Simple2DArray.NONE) {
          Color fill = grid.get(row, column) == Simple2DArray.BLACK ? Color.BLACK : Color.WHITE;
          drawCircle(row, column, fill, Color.BLACK, g2);
        }
      }
    }
  }

  public void switchColor() {
    turn = -turn;
  }

  public boolean whiteToPlay() {
    return turn == Simple2DArray.WHITE;
  }

  private void drawCircle(int row, int column, Color fill, Color border, Graphics2D g2) {
    Ellipse2D.Double circle = new Ellipse2D.Double();
    double x = topLeftX + CIRCLE_MARGIN + column * cellWidth;
    double y = topLeftY + CIRCLE_MARGIN + row * cellHeight;
    circle.setFrame(x,y,circleDiameter,circleDiameter);
    g2.setColor(fill);
    g2.fill(circle);
    g2.setColor(border);
    g2.draw(circle);
  }
}
Simple2DArray.java:

public class Simple2DArray implements Simple2DInterface {

  public static final int DEFAULT_BOARD_DIMENSION = 8;

  static final int BLACK = -1;
  static final int NONE = 0;
  static final int WHITE = 1;

  private int[][] simpleArray;

  /** Creates a blank Simple2DArray with the specified number of rows and columns. */
  public Simple2DArray(int rows, int columns) {
    simpleArray = new int[rows][columns];
  }

  /** Reset every element to 0 */
  public void clear() {
    simpleArray = new int[getNumberOfRows()][getNumberOfColumns()];
  }

  /** Gets the value at row and column. */ 
  public int get(int row, int column) {
    return simpleArray[row][column];
  }

  /** Gets the number of columns of this Simple 2D Array. */
  public int getNumberOfColumns() {
    return simpleArray.length != 0 ? simpleArray[0].length : 0;
  }

  /** Gets the number of rows of this Simple 2D Array. */
  public int getNumberOfRows() {
    return simpleArray.length;
  }

  /** Sets the value at location row and column to a certain color. */
  public void set(int row, int column, int color) {
    simpleArray[row][column] = color;
  }
}

接下来的步骤:收紧绑定检查,发出游戏结束的信号,清除按钮,在每次移动中自动更改所有对手的硬币,移动验证。

你应该使用增强for循环,因为你现在有1个索引循环。
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 > 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 <= numRows; i++)
    {
        line.setLine(topLeftX, topLeftY + (i * cellHeight), bottomRightX, topLeftY + (i * cellHeight));
        g2.draw(line);
    }

    for(int i = 0; i <= 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 <= numRows; row++)
    {
        for(int column = 1; column <= 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 && column != 0)
    {
        if(grid.get(row, column) == -1)
        {
            if(isWhite)
            {
                grid.setToOne(row, column);
            }
            else
            {
                grid.setToZero(row, column);
            }
        }
        else
        {
            if(isWhite && grid.get(row, column) == 0)
            {
                grid.flip(row, column);
            }
            else if(!isWhite && 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 <= numColumns; column++)
    {
        if(x > topLeftX + ((column - 1) * cellWidth) && x < topLeftX + (column * cellWidth))
        {
            result = column;
            break;
        }
    }

    return result;
}

public int getRow(int y)
{
    int result = 0;

    for(int row = 1; row <= numRows; row++)
    {
        if(y > topLeftY + ((row - 1) * cellHeight) && y < 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)
{
}
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 static final int FRAME_WIDTH = 600;
  private static final int FRAME_HEIGHT = FRAME_WIDTH;

  private OthelloComponent gridComponent;
  private JButton switchPlayerButton;

  public OthelloFrame() {
    // create grid and add to frame
    Simple2DInterface grid = 
        new Simple2DArray(Simple2DArray.DEFAULT_BOARD_DIMENSION, Simple2DArray.DEFAULT_BOARD_DIMENSION);
    int halfWidth = grid.getNumberOfColumns() / 2;
    int halfHeight = grid.getNumberOfRows() / 2;
    grid.set(halfHeight - 1, halfWidth - 1, Simple2DArray.WHITE);
    grid.set(halfHeight - 1, halfWidth, Simple2DArray.BLACK);
    grid.set(halfHeight, halfWidth - 1, Simple2DArray.BLACK);
    grid.set(halfHeight, halfWidth, Simple2DArray.WHITE);
    gridComponent = new OthelloComponent(grid);
    add(gridComponent);

    // create control panel
    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new GridLayout(2,1));

    // create and add switch player button to control panel
    switchPlayerButton = new JButton("Switch Color to White");
    ActionListener sp = new SwitchButtonListener();
    switchPlayerButton.addActionListener(sp);
    controlPanel.add(switchPlayerButton);

    // create and add instructions to control panel
    JPanel msgPanel = new JPanel();
    msgPanel.setBorder(new TitledBorder("Message"));
    msgPanel.add(new JLabel("Click on an empty space to put a disk or click on a disk to change color."));
    controlPanel.add(msgPanel);

    // add control panel to frame
    add(controlPanel, BorderLayout.SOUTH);

    // final setup
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setTitle("Wanna be Othello");
    setVisible(true);
  }

  public static void main(String[] args) {
    JFrame frame = new OthelloFrame();
    frame.setVisible(true);
  }

  private class SwitchButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent arg0) {
      gridComponent.switchColor();
      switchPlayerButton.setText("Switch Color to " + (gridComponent.whiteToPlay() ? "Black" : "White"));
    }
  }
}
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 static final int MARGIN = 10;

  private static final int CIRCLE_MARGIN = MARGIN / 2;
  private static final int BOTTOM_MARGIN = MARGIN;
  private static final int TOP_MARGIN = MARGIN;
  private static final int LEFT_MARGIN = MARGIN;
  private static final int RIGHT_MARGIN = MARGIN;

  private Simple2DInterface grid;
  private int numRows;
  private int numColumns;

  private double circleDiameter;
  private double topLeftX;
  private double topLeftY;
  private double cellHeight;
  private double cellWidth;
  private double boardHeight;
  private double boardWidth;

  private int turn;

  public OthelloComponent(Simple2DInterface a2DArray) {
    grid = a2DArray;
    numRows = grid.getNumberOfRows();
    numColumns = grid.getNumberOfColumns();
    turn = Simple2DArray.BLACK;

    this.addMouseListener(this);
  }

  public int getColumn(int x) {
    return (int) (Math.ceil((x - topLeftX) / cellWidth) - 1);
  }

  public int getRow(int y) {
    return (int) (Math.ceil((y - topLeftY) / cellHeight) - 1);
  }

  public void mouseClicked(MouseEvent arg0) {
    int column = getColumn(arg0.getX());
    int row = getRow(arg0.getY());

    // you can take this out if you want
    System.out.println("r: " + row + ", c: " + column);

    if (row >= 0 && column >= 0 && row < numRows && column < numColumns) {
        grid.set(row, column, turn);
    }
    repaint();
  }

  public void mouseEntered(MouseEvent arg0) {}

  public void mouseExited(MouseEvent arg0) {}

  public void mousePressed(MouseEvent arg0) {}

  public void mouseReleased(MouseEvent arg0) {}

  public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    cellHeight = ((double) getHeight() - (TOP_MARGIN + BOTTOM_MARGIN)) / numRows;
    cellWidth = ((double) getWidth() - (LEFT_MARGIN + RIGHT_MARGIN)) / numColumns;

    // make cells the largest possible squares
    if (cellWidth > cellHeight) {
      cellWidth = cellHeight;
    } else {
      cellHeight = cellWidth;
    }
    circleDiameter = cellWidth - (2 * CIRCLE_MARGIN);

    boardHeight = cellHeight * numRows;
    boardWidth = cellWidth * numColumns;

    topLeftX = (getWidth() - boardWidth) / 2;
    topLeftY = (getHeight() - boardHeight) / 2;
    double bottomRightX = topLeftX + boardWidth;
    double bottomRightY = topLeftY + boardHeight;

    Line2D.Double line = new Line2D.Double(0,0,0,0);

    // draw the lines on the board
    g2.setColor(Color.BLACK);
    for (int i = 0; i <= numRows; i++) {
      line.setLine(topLeftX, topLeftY + i * cellHeight, bottomRightX, topLeftY + i * cellHeight);
      g2.draw(line);
    }

    for (int i = 0; i <= numColumns; i++) {
      line.setLine(topLeftX + i * cellWidth, topLeftY, topLeftX + i * cellWidth, bottomRightY);
      g2.draw(line);
    }

    // draw circles
    for (int row = 0; row < numRows; row++) {
      for (int column = 0; column < numColumns; column++) {
        if  (grid.get(row, column) != Simple2DArray.NONE) {
          Color fill = grid.get(row, column) == Simple2DArray.BLACK ? Color.BLACK : Color.WHITE;
          drawCircle(row, column, fill, Color.BLACK, g2);
        }
      }
    }
  }

  public void switchColor() {
    turn = -turn;
  }

  public boolean whiteToPlay() {
    return turn == Simple2DArray.WHITE;
  }

  private void drawCircle(int row, int column, Color fill, Color border, Graphics2D g2) {
    Ellipse2D.Double circle = new Ellipse2D.Double();
    double x = topLeftX + CIRCLE_MARGIN + column * cellWidth;
    double y = topLeftY + CIRCLE_MARGIN + row * cellHeight;
    circle.setFrame(x,y,circleDiameter,circleDiameter);
    g2.setColor(fill);
    g2.fill(circle);
    g2.setColor(border);
    g2.draw(circle);
  }
}
public interface Simple2DInterface {

  /** Reset every element to 0. */
  public void clear();

  /** Gets the value at row and column. */ 
  public int get(int row, int column);

  /** Gets the number of columns of this Simple2DInterface. */
  public int getNumberOfColumns();

  /** Gets the number of rows of this Simple2DInterface. */
  public int getNumberOfRows();

  /** Sets the value at location row and column to a certain color. */
  public void set(int row, int column, int color);
}
public class Simple2DArray implements Simple2DInterface {

  public static final int DEFAULT_BOARD_DIMENSION = 8;

  static final int BLACK = -1;
  static final int NONE = 0;
  static final int WHITE = 1;

  private int[][] simpleArray;

  /** Creates a blank Simple2DArray with the specified number of rows and columns. */
  public Simple2DArray(int rows, int columns) {
    simpleArray = new int[rows][columns];
  }

  /** Reset every element to 0 */
  public void clear() {
    simpleArray = new int[getNumberOfRows()][getNumberOfColumns()];
  }

  /** Gets the value at row and column. */ 
  public int get(int row, int column) {
    return simpleArray[row][column];
  }

  /** Gets the number of columns of this Simple 2D Array. */
  public int getNumberOfColumns() {
    return simpleArray.length != 0 ? simpleArray[0].length : 0;
  }

  /** Gets the number of rows of this Simple 2D Array. */
  public int getNumberOfRows() {
    return simpleArray.length;
  }

  /** Sets the value at location row and column to a certain color. */
  public void set(int row, int column, int color) {
    simpleArray[row][column] = color;
  }
}