我无法让我的游戏正确翻转瓷砖。我被困在这无数个小时,似乎无法解决这个问题。我已经检查了其他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;
}
}
类GameBoard扩展了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;
}
}
}
答案 0 :(得分:0)
你有不同的变量同名。
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
,即bad)
for (int column = 0; column < 8; column++) {
index = new Index(row, column);
BoardSquares[row][column] = new BoardSquare(index, Color.white);
BoardSquares[row][column].addMouseListener( .. );