gpt4 book ai didi

Java - 初始化游戏后出错 - 扫雷

转载 作者:行者123 更新时间:2023-12-01 10:04:32 26 4
gpt4 key购买 nike

我遇到了这个奇怪的问题,但我似乎无法弄清楚。我有一个按钮,上面写着“新游戏”。在我玩一次游戏后,然后按“新游戏”返回到 initGame() ,我遇到一个问题,因为我无法右键单击前一个游戏的地雷单元格。我可以毫无问题地单击左键,因此我迷路了。

(另外,如果您想建议我如何缩短/简化我的代码,也请随意这样做!)

请提出建议,这是我的代码的一部分(其他一切正常):

private void initGame() {
minesFlagged=10;
tfFlag.setText(minesFlagged + "");
CellMouseListener listener = new CellMouseListener();
// Reset cells, mines, and flags
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
// Set all cells to un-revealed
btnCells[row][col].setEnabled(true); // enable button
btnCells[row][col].setForeground(FGCOLOR_NOT_REVEALED);
btnCells[row][col].setBackground(BGCOLOR_NOT_REVEALED);
btnCells[row][col].setFont(FONT_NUMBERS);
btnCells[row][col].setText(""); // display blank
mines[row][col] = false; // clear all the mines
flags[row][col] = false; // clear all the flags
zeroesVisit[row][col] = false;
btnCells[row][col].setIcon(null);
btnCells[row][col].addMouseListener(listener);
}
}

for (int row = 0; row <= ROWS; row++) {
for (int col = 0; col <= COLS; col++) {
sol[row][col]=-1;
}
}

// Set the number of mines and the mines' location
numMines = 10; //rmb the array starts from zero and ends at index 9
for(int i=0;i<numMines;i++){
Random rand = new Random();
//get random position for next mine
int row = rand.nextInt(ROWS);
int col = rand.nextInt(COLS);
while(mines[row][col]||mines[0][0]){//if this position is a mine
//we get new position
row = rand.nextInt(ROWS);
col = rand.nextInt(COLS);
}
mines[row][col]=true;
}
}
private void findSol(int rowSelected,int colSelected){
for (int rr = rowSelected - 1; rr<= rowSelected + 1; rr++){//finding how many surrounding mines
for (int cc = colSelected - 1; cc <= colSelected + 1; cc++){
if(rr>=0 && cc>=0 && rr<ROWS && cc<COLS){
if (mines[rr][cc]){
sol[rowSelected][colSelected]++;
}
}
}
}
}
private class CellMouseListener extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
// Determine the (row, col) of the JButton that triggered the event
int rowSelected = -1,colSelected = -1;

// Get the source object that fired the Event
JButton source = (JButton)e.getSource();
// Scan all rows and columns, and match with the source object
boolean found = false;
boolean done = true;
for (int row = 0; row < ROWS && !found; ++row) {
for (int col = 0; col < COLS && !found; ++col) {
if (source == btnCells[row][col]) {
rowSelected = row;
colSelected = col;
found = true; // break both inner/outer loops
}
}
}

// Left-click to reveal a cell; Right-click to plant/remove the flag.
if (e.getButton() == MouseEvent.BUTTON1 && (!flags[rowSelected][colSelected])) { // Left-button clicked
//If you hit a mine, game over - OKAY
//Otherwise, reveal the cell and display the number of surrounding mines - okay
if (mines[rowSelected][colSelected]){
btnCells[rowSelected][colSelected].setBackground(BGCOLOR_REVEALED);
imgMines = new ImageIcon(getClass().getResource("Bombicon.png"));
btnCells[rowSelected][colSelected].setIcon(imgMines);
JOptionPane.showMessageDialog(null, "Game Over! Try to complete the game~");
}else{
btnCells[rowSelected][colSelected].setBackground(FGCOLOR_REVEALED); //though foreground is text color
btnCells[rowSelected][colSelected].setIcon(null);
sol[rowSelected][colSelected]=0;
findSol(rowSelected,colSelected);

if(sol[rowSelected][colSelected]>0){
btnCells[rowSelected][colSelected].setText(""+sol[rowSelected][colSelected]);
btnCells[rowSelected][colSelected].removeMouseListener(this);
colorCell(rowSelected,colSelected);
}
else if(sol[rowSelected][colSelected]==0){
do{
done = true;
for(int r=0;r<ROWS;r++){//scanning through whole board
for(int c=0;c<COLS;c++){
if(sol[r][c]==0 && !zeroesVisit[r][c]){
zeroesVisit[r][c]=true;
for(int row=r-1;row<=r+1;row++){//open the 8 surrounding cells
for(int col=c-1;col<=c+1;col++){
if(row>=0 && col>=0 && row<ROWS && col<COLS){//make sure not out of bounds
btnCells[row][col].setBackground(FGCOLOR_REVEALED);
btnCells[row][col].setIcon(null);
sol[row][col]=0;
findSol(row,col);
btnCells[row][col].removeMouseListener(this);
if(sol[row][col]>0){
btnCells[row][col].setText(""+sol[row][col]);
colorCell(row,col);
}
else if(sol[row][col]==0){
btnCells[row][col].setText("");
done=false;//to prompt looping again
}
}
}
}
}
}
}
}while(!done);//scan again to find all/any the zero cells
}
}

}else if (e.getButton() == MouseEvent.BUTTON3) { // right-button clicked
//If the location is flagged, remove the flag
// Otherwise, plant a flag. - COMPLETED
if(flags[rowSelected][colSelected]){
flags[rowSelected][colSelected]=false;
btnCells[rowSelected][colSelected].setBackground(BGCOLOR_NOT_REVEALED);
btnCells[rowSelected][colSelected].setIcon(null);
minesFlagged++;
tfFlag.setText(minesFlagged + "");
}else{
flags[rowSelected][colSelected]=true;
btnCells[rowSelected][colSelected].setBackground(FGCOLOR_NOT_REVEALED);
imgFlags = new ImageIcon(getClass().getResource("whiteflag.png"));
btnCells[rowSelected][colSelected].setIcon(imgFlags);
minesFlagged--;
tfFlag.setText(minesFlagged + "");
}
}

//Check if the player has won, after revealing this cell - COMPLETED
int completeCount=0,flagCount=0;
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
if(!mines[row][col] && btnCells[row][col].getBackground()==FGCOLOR_REVEALED){
completeCount++;
if((ROWS*COLS-numMines) == completeCount){
JOptionPane.showMessageDialog(null, "CONGRATULATIONS!");
}
}
}
}
}
}

最佳答案

看来您每次游戏初始化时都会调用 addMouseListener 。这不会替换以前游戏中现有的 MouseListener,因此您现在在未调用removeMouseListener 的按钮上有一个额外的监听器。如果某些按钮没有触发事件,我会首先调查这一点。

最好只在首次创建框架时添加监听器一次。然后向 mouseClicked 事件方法添加逻辑以忽略对“已停用”按钮的点击,而不是一遍又一遍地删除和添加监听器。

如果您必须采用这种方法,我建议在 initGame 方法中添加监听器之前,从任何具有 MouseListener 的按钮中删除所有 MouseListener(可以使用 getMouseListeners 方法找到它们)。

正如 ArielB 所说,最佳实践是将模型(地雷的位置和图 block 的状态等)分离到远离 UI 代码的另一个类中。

关于Java - 初始化游戏后出错 - 扫雷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36551554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com