我希望有人可以帮助我理解我的代码问题。我正在尝试用Java创建一个Reversi棋盘游戏,但我的游戏棋盘只会坚持白色棋子并且拒绝做任何事情。任何建议都会很棒。
这是我必须做的部分(因此标记为TO DO)。老实说,每个人应该是1或2行,但我只是没有抓住。我的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;
}
}
我的其余文件/接口/等:
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);
}
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 <= numRows; i++)
{
for(int j = 1; j <= 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 <= numRows; i++)
{
for(int j = 1; j <= 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.");
}
}
}
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);
}
}
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)
{
}
}
答案 0 :(得分:0)
在查看代码时,我会更改与问题无关的很多内容,因此我只重构了您的类:
OthelloFrame.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"));
}
}
}
OthelloComponent.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);
}
}
Simple2DInterface.java:
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);
}
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;
}
}
接下来的步骤:稍微收紧绑定检查,发出游戏结束信号,清除按钮,每次移动时自动更改所有对手硬币,移动验证。