Swing java国际象棋游戏设计中OOP的误区
我在编写用于练习java技能的代码时遇到了一个问题。 我想写一盘棋。无论如何,在我的项目中,我有一个名为ChessGUI的类,它包含GUI和我需要玩的方法(多人游戏)。我愿意稍后将代码更改为MVC模式,以便更易于阅读/编辑/理解。 我有一个名为Figure的抽象类,我将其扩展为12个类(每种颜色6个图形): 但是,在我的ChessGui类中,我有一个名为CheckAvailableSquares()的方法,每当选择了棋盘上的一个棋子(通过鼠标单击)时,我都会调用该方法,以便在棋盘上向我显示可以移动所选棋子的可用方块Swing java国际象棋游戏设计中OOP的误区,swing,oop,chess,Swing,Oop,Chess,我在编写用于练习java技能的代码时遇到了一个问题。 我想写一盘棋。无论如何,在我的项目中,我有一个名为ChessGUI的类,它包含GUI和我需要玩的方法(多人游戏)。我愿意稍后将代码更改为MVC模式,以便更易于阅读/编辑/理解。 我有一个名为Figure的抽象类,我将其扩展为12个类(每种颜色6个图形): 但是,在我的ChessGui类中,我有一个名为CheckAvailableSquares()的方法,每当选择了棋盘上的一个棋子(通过鼠标单击)时,我都会调用该方法,以便在棋盘上向我显示可以
private void CheckAvailableSquares(Figure figure){
for (int row = 0; row < 8; row++){
for (int col = 0; col < 8; col++){
if (row == figure.getRowPos() && col == figure.getColPos()) //bypass the actual square where my piece stands
continue;
if (figure.isValidMove(board,figure.getRowPos(), figure.getColPos(),row,col)){
board[row][col].setBackground(Color.GREEN);
}
}
}
}
问题是:
每当我调用CheckAvailableSquares()方法来检查方块的可用性时,firstMovePlayed布尔值将设置为true
我真的很抱歉占用了你太长时间,但我真的搞不懂:/
下面是ChessGui类的其余相关代码:
public void actionPerformed(ActionEvent e){
//正在查找触发ActionListener的方块:
对于(int i=0;i<8;i++){
对于(int j=0;j<8;j++){
if(board[i][j]==e.getSource()){
processClick(i,j);
}
}
}
}
私有void processClick(int i,int j){
如果(bufRow==99&&bufCol==99){//选择要移动的地物并等待下一次单击
if(board[i][j].isEmpty(){//如果正方形上没有点击的图形
System.out.println(“这个正方形上没有数字”);
返回;
}
否则{//将所选的方块(按钮)保存在缓冲区中,以便我们以后可以移动它。
//决定轮到谁了:
if(board[i][j].getFigure().getColor().equals(Color.WHITE)&&!whiteToPlay)
System.out.println(“轮到布莱克玩了!”);
else if(board[i][j].getFigure().getColor().equals(Color.BLACK)&!blackToPlay)
System.out.println(“轮到怀特玩了!”);
如果((线路板[i][j].getFigure().getColor().equals(Color.WHITE)和&whiteToPlay)| |线路板[i][j].getFigure().getColor().equals(Color.BLACK)和&blackToPlay){
board[i][j].SetHighLeightDicon(board[i][j].getFigure().GetHilightDicon());//更改所选方块的图标
bufRow=i;//保存所选图形以便以后移动
bufCol=j;
System.out.println(“原始数据上的选定图形:+bufRow+”列:+bufCol”);
开关匝数();
CheckAvailableSquares(board[bufRow][bufCol].getFigure());
}
}
}
其他的
图(i,j);
}
私有void移动图(int wantedRow,int wantedCol){
if(board[bufRow][bufCol].getFigure().isValidMove(board,bufRow,bufCol,wantedRow,wantedCol)){//检查移动是否对图形有效
//if(board[wantedRow][wantedCol].isEmpty()){//如果想要的方块是空的(不是捕获)
//从旧正方形中删除图形后,将其移动到想要的正方形
board[wantedRow][wantedCol].setFigure(board[bufRow][bufCol].getFigure());
board[wantedRow][wantedCol].setSquareIcon(board[bufRow][bufCol].getFigure().getIcon());
板[bufRow][bufCol].emptySquare();
//更新工件的位置
线路板[wantedRow][wantedCol].getFigure().setNewPos(wantedRow,wantedCol);
System.out.println(“移动[“+bufRow+”,“+bufCol+”]到:[“+WANTEROW+”,“+WANTECOL+”);
//重置缓冲区
bufRow=99;
bufCol=99;
resetSquaresColors();
/*}
否则如果(!board[wantedRow][wantedCol].isEmpty()){//如果是捕获
如果(board[wantedRow][wantedCol].getFigure().getColor()!=board[bufRow][bufCol].getFigure().getColor()){//无法捕获自己的片段!
board[wantedRow][wantedCol].setFigure(board[bufRow][bufCol].getFigure());
线路板[wantedRow][wantedCol].setSquareIcon(线路板[wantedRow][wantedCol].getFigure().getIcon());
板[bufRow][bufCol].emptySquare();
//更新工件的位置
线路板[wantedRow][wantedCol].getFigure().setRowPos(wantedRow);
线路板[wantedRow][wantedCol].getFigure().setColPos(wantedCol);
System.out.println(“移动[“+bufRow+”,“+bufCol+”]到:[“+WANTEROW+”,“+WANTECOL+”);
//重置缓冲区
bufRow=99;
bufCol=99;
}
}*/
}否则{//如果这不是有效的移动
线路板[bufRow][bufCol].setIcon(线路板[bufRow][bufCol].getFigure().getIcon());
//重置缓冲区
bufRow=99;
bufCol=99;
resetSquaresColors();
System.out.println(“无效移动”);
开关匝数();
}
}
私人作废支票可用方格(图){
用于(int行=0;行<8;行++){
for(int col=0;col<8;col++){
if(row==figure.getRowPos()&&col==figure.getColPos())//绕过我的作品所在的实际正方形
继续;
if(图.isValidMove(板,图.getRowPos(),图.getColPos(),行,列)){
方形sqr=板[行][列];
if(sqr.isEmpty())//如果正方形为空
sqr.setAvailableIcon(新图像图标(“Icons/available_square.png”);
否则//如果广场上有一块
sqr.setAvailableIcon(sqr.getFigure().getAvailableIcon());
}
}
}
}
谢谢你,再见
public class WhitePawn extends Figure {
private boolean firstMovePlayed = false;
public WhitePawn(){
super.setColor(Color.WHITE);
}
@Override
public boolean isValidMove(Square[][] board, int currentRow, int currentCol, int wantedRow, int wantedCol){
int rowDelta = currentRow - wantedRow; //no Math.abs because a pawn can't move backwards.
int colDelta = Math.abs(wantedCol - currentCol);
//if we have an own piece on the wanted square
if (!board[wantedRow][wantedCol].isEmpty() && board[wantedRow][wantedCol].getFigure().getColor().equals(Color.WHITE)) {
return false;
}
//on the first move a pawn can move 2 square at a time, and when moving 2 square he can't capture. A pawn can't move two square if there is another piece in the way. TODO: en passant ??????
if (rowDelta == 2 && colDelta == 0 && board[wantedRow][wantedCol].isEmpty() && board[wantedRow + 1][wantedCol].isEmpty() && !firstMovePlayed) {
firstMovePlayed = true;
return true;
}
else if (rowDelta == 1 && colDelta == 1 && !board[wantedRow][wantedCol].isEmpty()) { //capture
if (board[wantedRow][wantedCol].getFigure().getColor() != board[currentRow][currentCol].getFigure().getColor())
firstMovePlayed = true;
return true;
}
else if (rowDelta == 1 && colDelta == 0 && board[wantedRow][wantedCol].isEmpty()) { //moving one square forward without a capture
firstMovePlayed = true;
return true;
}
//those were the only possibilities to move a pawn
return false;
}}
public void actionPerformed(ActionEvent e) {
//looking for the square which fired the ActionListener:
for (int i = 0; i < 8; i++){
for (int j = 0; j < 8; j++){
if(board[i][j] == e.getSource()){
processClick(i, j);
}
}
}
}
private void processClick(int i, int j){
if(bufRow == 99 && bufCol == 99){ //select a figure to move and wait for the next click
if (board[i][j].isEmpty()) { //if there is no figure on the square that has benn clicked
System.out.println("no Figures on this square");
return;
}
else {//save the chosen square (button) in buffers so that we can move it later.
//decide who's turn it is:
if (board[i][j].getFigure().getColor().equals(Color.WHITE) && !whiteToPlay)
System.out.println("It's black's turn to play!");
else if (board[i][j].getFigure().getColor().equals(Color.BLACK) && !blackToPlay)
System.out.println("It's white's turn to play!");
if ((board[i][j].getFigure().getColor().equals(Color.WHITE) && whiteToPlay) || board[i][j].getFigure().getColor().equals(Color.BLACK) && blackToPlay) {
board[i][j].setHighleightedIcon(board[i][j].getFigure().getHilightedIcon()); //change the icon of the chosen square
bufRow = i; //save the chosen figure to move it later
bufCol = j;
System.out.println("Selectd figure on Raw: " + bufRow + " Column: " + bufCol);
switchTurns();
CheckAvailableSquares(board[bufRow][bufCol].getFigure());
}
}
}
else
moveFigure(i, j);
}
private void moveFigure(int wantedRow, int wantedCol) {
if (board[bufRow][bufCol].getFigure().isValidMove(board, bufRow, bufCol, wantedRow, wantedCol)) { // check if the move is valid for the figure
// if (board[wantedRow][wantedCol].isEmpty()) { //if the wanted square is empty (not a capture)
//move the figure to the wanted square after deleting it from the old square
board[wantedRow][wantedCol].setFigure(board[bufRow][bufCol].getFigure());
board[wantedRow][wantedCol].setSquareIcon(board[bufRow][bufCol].getFigure().getIcon());
board[bufRow][bufCol].emptySquare();
//update the location of the piece
board[wantedRow][wantedCol].getFigure().setNewPos(wantedRow,wantedCol);
System.out.println("Moved [" + bufRow + ", " + bufCol + "] To: [" + wantedRow + ", " + wantedCol + "].");
//reset the buffers
bufRow = 99;
bufCol = 99;
resetSquaresColors();
/*}
else if (!board[wantedRow][wantedCol].isEmpty()){ //if it's a capture
if(board[wantedRow][wantedCol].getFigure().getColor() != board[bufRow][bufCol].getFigure().getColor()) { //can't capture own pieces!
board[wantedRow][wantedCol].setFigure(board[bufRow][bufCol].getFigure());
board[wantedRow][wantedCol].setSquareIcon(board[wantedRow][wantedCol].getFigure().getIcon());
board[bufRow][bufCol].emptySquare();
//update the location of the piece
board[wantedRow][wantedCol].getFigure().setRowPos(wantedRow);
board[wantedRow][wantedCol].getFigure().setColPos(wantedCol);
System.out.println("Moved [" + bufRow + ", " + bufCol + "] To: [" + wantedRow + ", " + wantedCol + "].");
//reset the buffers
bufRow = 99;
bufCol = 99;
}
}*/
} else { //if it's not a valid move
board[bufRow][bufCol].setIcon(board[bufRow][bufCol].getFigure().getIcon());
//reset the buffers
bufRow = 99;
bufCol = 99;
resetSquaresColors();
System.out.println("Not a valid move");
switchTurns();
}
}
private void CheckAvailableSquares(Figure figure){
for (int row = 0; row < 8; row++){
for (int col = 0; col < 8; col++){
if (row == figure.getRowPos() && col == figure.getColPos()) //bypass the actual square where my piece stands
continue;
if (figure.isValidMove(board,figure.getRowPos(), figure.getColPos(),row,col)){
Square sqr = board[row][col];
if (sqr.isEmpty()) //if the square is empty
sqr.setAvailableIcon(new ImageIcon("Icons/available_square.png"));
else //if there is a piece on the square
sqr.setAvailableIcon(sqr.getFigure().getAvailableIcon());
}
}
}
}