多米诺记忆游戏无法保留正确翻牌状态及无法结束问题求助
多米诺记忆游戏问题修复
问题描述
开发的多米诺记忆游戏存在两个核心问题:
- 猜对匹配的牌后,无法保持翻面状态
- 游戏永远无法触发结束条件
已知约束:
MemoryLaneDriver类不可修改- 禁止使用
StringBuilder - 曾尝试调用
Domino.setRevealed()但遇到报错(根源在于equals方法错误)
原代码实现
Domino类
public class Domino { private int top, bottom; private boolean revealed; public Domino(int x, int y) { if (x > y) { top = y; bottom = x; } else { top = x; bottom = y; } } public int getTop() { return top; } public int getBottom() { return bottom; } public boolean isRevealed() { if (revealed) return true; return false; } public void setRevealed(boolean revealed) { this.revealed = revealed; } public boolean equals(Domino other) { if (top == bottom) return true; return false; } }
MemoryLane类
import java.util.Random; public class MemoryLane { private Domino[] board; public MemoryLane(int max) { board = new Domino[(max * max) + max]; int i = 0; for(int top = 1; top <= max; top++) for(int bot = 1; bot <= max; bot++) { if(top <= bot) { board[i] = new Domino(top, bot); i++; board[i] = new Domino(top, bot); i++; } } shuffle(); } private void shuffle() { int index; Random random = new Random(); for (int i = board.length - 1; i > 0; i--) { index = random.nextInt(i + 1); if (index != i) { Domino temp = board[index]; board[index] = board[i]; board[i] = temp; } } } public boolean guess(int i, int k) { if(board[i] == board[k]) { return true; } return false; } public String peek(int a, int b) { String text = new String(); text += ("[" + board[a].getTop()+ "] [" + board[b].getTop()+ "]\n"); text += ("[" + board[a].getBottom()+ "] [" + board[b].getBottom()+ "]\n"); return text; } public boolean gameOver() { int count = 0; for(int i=0; i< board.length; i++) { if(board[i].isRevealed()) count ++; } return (count == board.length); } public String toString() { String text = new String(); for(int i=0; i< board.length; i++) { if(board[i].isRevealed()) text += ("[" + board[i].getTop()+ "] "); else text += ("[ ] "); } text += ('\n'); for(int i=0; i< board.length; i++) { if(board[i].isRevealed()) text += ("[" + board[i].getBottom()+ "] "); else text += ("[ ] "); } return text; } }
MemoryLaneDriver类(不可修改)
import java.util.Scanner; public class MemoryLaneDriver { public static void main(String[] args) { String message = "Welcome to Memory Lane!" + "\n" + "Choose two indexes to reveal the corresponding dominoes." + "\n" + "If the dominoes match, they stay revealed." + "\n" + "Reveal all the dominoes to win the game!" + "\n"; System.out.println(message); Scanner input = new Scanner(System.in); MemoryLane game = new MemoryLane(2); long start = System.currentTimeMillis(); while(!game.gameOver()) { System.out.println(game); System.out.print("First: "); int first = input.nextInt(); System.out.print("Second: "); int second = input.nextInt(); game.guess(first, second); System.out.println(game.peek(first, second) + "\n"); } long stop = System.currentTimeMillis(); long elapsed = (stop - start) / 1000; System.out.println(game); System.out.println("\nYou win!"); System.out.println("Total time: " + elapsed + "s"); } }
当前运行异常示例
Welcome to Memory Lane! Choose two indexes to reveal the corresponding dominoes. If the dominoes match, they stay revealed. Reveal all the dominoes to win the game! [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] First: 1 Second: 3 [2] [2] [2] [2] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] First:
问题根源与修复代码
1. Domino类equals方法逻辑完全错误
原equals方法仅判断当前多米诺的上下点数是否相等,完全无法实现“两张多米诺匹配”的判断。必须修改为比较两张多米诺的上下点数是否分别一致:
public boolean equals(Domino other) { if (other == null) return false; return this.top == other.top && this.bottom == other.bottom; }
2. MemoryLane类guess方法双重错误
- 用
==比较对象引用,而非调用equals方法比较内容 - 猜对后未调用
setRevealed(true)标记牌为已翻面状态
修复后的guess方法:
public boolean guess(int i, int k) { // 边界检查:避免索引越界或选择同一张牌 if (i < 0 || i >= board.length || k < 0 || k >= board.length || i == k) { return false; } Domino domino1 = board[i]; Domino domino2 = board[k]; if (domino1.equals(domino2)) { domino1.setRevealed(true); domino2.setRevealed(true); return true; } return false; }
修复后效果
修复后,猜对的牌会保持翻面状态,当所有牌都被成功匹配翻面后,gameOver()方法会返回true,游戏正常结束并显示胜利信息。
内容的提问来源于stack exchange,提问作者RipTehHomie
相关产品推荐
相关产品推荐

