我无法让我的游戏正确翻转瓷砖。我已经被这个问题困扰了无数个小时,似乎无法理解。我已经检查了其他的Reversi代码示例,不幸的是仍然没有骰子。我注意到,每当我在gui中点击一个被占用的BoardPiece时,程序都不会感觉到它被占用了。鼓励任何批评/帮助!提前谢谢。
类索引{
int row;
int column;
public Index(int row, int column) {
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
public boolean equals(Index other){
if(this.row == other.row && this.column == other.column){
return true;
}
return false;
}
}
类游戏板扩展JPanel{
static ArrayList<BoardSquare> possibleCaptures = new ArrayList<>();
public BoardSquare BoardSquares[][] = new BoardSquare[8][8];
BoardSquare bs;
Index index;
GameBoard() {
setLayout(new GridLayout(8, 8));
for (int row = 0; row < 8; row++) {
for (int column = 0; column < 8; column++) {
index = new Index(row, column);
BoardSquares[row][column] = new BoardSquare(index, Color.white);
add(BoardSquares[row][column]);
}
}
}
public BoardSquare getBoardSquare(Index index) {
return BoardSquares[index.row][index.column];
}
public void StartingPieces(HumanPlayer p1, aiPlayer p2) {
BoardSquares[3][3].setColor(p1);
BoardSquares[3][4].setColor(p2);
BoardSquares[4][4].setColor(p1);
BoardSquares[4][3].setColor(p2);
}
class BoardSquare extends JLabel {
private Index index;
private Color c = Color.white;
private Border b = BorderFactory.createLineBorder(Color.BLACK, 2);
BoardSquare(Index index, final Color c) {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 1) {
if (c == Color.white) {
checkCaptures();
System.out.println("Checking");
} else {
System.out.println("This square is already occupied.");
}
}
}
});
this.index = index;
this.c = c;
setBorder(b);
}
public Index getIndex(){
return index;
}
public void setColor(Player p) {
if (p instanceof HumanPlayer) {
c = Color.blue;
}
if (p instanceof aiPlayer) {
c = Color.red;
}
}
public Color getColor() {
return c;
}
private void checkCaptures() {
Direction[] directions = Direction.values();
for (Direction direction : directions) {
// get next piece's index along the direction
Index nextIndex = direction.next(this.index);
if (isValid(nextIndex)) { // if the index is not valid (i.e. edge of the board) ignore it
// get next piece in the same direction
BoardSquare bs = getBoardSquare(nextIndex);
// find all pieces that should be captured in this direction
ArrayList<BoardSquare> squaresToCapture = new ArrayList<>();
bs.findCaptures(squaresToCapture, this.c, direction);
for (BoardSquare candidate : squaresToCapture) {
// flip the color (WHITE to BLACK and vice-versa)
candidate.capture();
}
}
}
}
public void findCaptures(ArrayList<BoardSquare> possibleCaptures, Color col, Direction d) {
Index next = d.next(this.index);
if (this.c == col) {
// This piece has the same color with the first one.
// No need to search further for this direction. All pieces collected in the list
// between the first one and this one, have opposite color and should be captured.
} else if (this.c == Color.white) {
// found a blank piece. Stop the search and clear any captured pieces found so far
possibleCaptures.clear();
} else {
// this piece has the opposite color of the first
if (isValid(next)) {
// this is not the last piece in this direction.
// Since it has a flipped color it is a candidate for capturing
possibleCaptures.add(this);
// ask the next piece recursively to also check itself
BoardSquare bs = getBoardSquare(next);
bs.findCaptures(possibleCaptures, col, d);
} else {
// next index is not valid i.e. we have reached board edge.
// Stop the search and clear any captured pieces found so far
possibleCaptures.clear();
}
}
}
public void capture() {
if (c == Color.red) {
c = Color.blue;
}
if (c == Color.blue) {
c = Color.red;
}
}
public String colorStatus() {
if (c == Color.white) {
return "white";
}
if (c == Color.red) {
return "red";
} else {
return "blue";
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
setText(colorStatus());
}
public boolean isValid(Index index) {
if (index.getRow() <= 7 && index.getRow() >= 0) {
if (index.getColumn() <= 7 && index.getColumn() >= 0) {
return true;
}
}
return false;
}
}
}
您有两个同名的不同变量。
class BoardSquare extends JLabel {
private Index index;
private Color c = Color.white;
private Border b = BorderFactory.createLineBorder(Color.BLACK, 2);
BoardSquare(Index index, final Color c) {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 1) {
if (c == Color.white) {
你在这里指的是哪个c
?final Color c
还是private Color c
?我认为你的意思是private Color c
,但你使用的是final Color c
。
我建议:
class BoardSquare extends JLabel {
private Index index;
private Color myColor = Color.white;
private Border b = BorderFactory.createLineBorder(Color.BLACK, 2);
BoardSquare(Index index, final Color initialColor) {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 1) {
if (myColor.equals(Color.white)) {
(未测试)
更好的做法是将mouseListener添加到更高的一层。(您在构造函数中泄漏了一个this
,这很糟糕)
for (int column = 0; column < 8; column++) {
index = new Index(row, column);
BoardSquares[row][column] = new BoardSquare(index, Color.white);
BoardSquares[row][column].addMouseListener( .. );