在提供的多米诺骨牌记忆游戏代码中,存在两个主要问题导致游戏功能不完整:
为了使 Domino 对象能够基于其内容(即 top 和 bottom 值)进行比较,我们需要在 Domino 类中重写 equals() 方法。同时,根据 Java 规范,如果重写了 equals() 方法,也必须重写 hashCode() 方法,以维护它们之间的契约:如果两个对象通过 equals() 方法比较是相等的,那么它们的 hashCode() 方法必须产生相同的整数结果。
以下是 Domino 类中 equals() 和 hashCode() 方法的正确实现:
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() { return revealed; // 简化了原有的 if-else 结构 } public void setRevealed(boolean revealed) { this.revealed = revealed; } @Override public int hashCode() { int hash = 7; hash = 59 * hash + this.getTop(); hash = 59 * hash + this.getBottom(); return hash; } @Override public boolean equals(Object obj) { if (this == obj) { // 检查是否是同一个对象实例 return true; } if (obj == null || getClass() != obj.getClass()) { // 检查是否为null或类型不匹配 return false; } final Domino other = (Domino) obj; // 类型转换 if (this.getTop() != other.getTop()) { // 比较 top 值 return false; } if (this.getBottom() != other.getBottom()) { // 比较 bottom 值 return false; } return true; // 如果 top 和 bottom 都相等,则认为对象相等 } }
说明:
在 MemoryLane 类的 guess 方法中,当两个多米诺骨牌被正确匹配时,需要调用它们的 setRevealed(true) 方法,将它们的状态设置为已揭示。
立即学习“Java免费学习笔记(深入)”;
以下是 MemoryLane 类中 guess 方法的修正:
import java.util.Arrays; 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) { // 使用重写后的 equals 方法进行内容比较 if (board[i].equals(board[k])) { board[i].setRevealed(true); // 设置第一个多米诺骨牌为已揭示 board[k].setRevealed(true); // 设置第二个多米诺骨牌为已揭示 return true; } return false; } public String peek(int a, int b) { String text = ""; // 使用空字符串初始化,避免 String 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 debug() { String text = ""; for (int i = 0; i < board.length; i++) { text += ("[" + board[i].getTop() + "] "); } text += ('\n'); for (int i = 0; i < board.length; i++) { text += ("[" + board[i].getBottom() + "] "); } return text; } @Override public String toString() { String text = ""; 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; } }
说明:
通过以上修正,您的多米诺骨牌记忆游戏将能够正确地识别匹配、揭示多米诺骨牌,并最终判断游戏胜利,提供完整的游戏体验。
以上就是Java 多米诺骨牌记忆游戏:揭示机制与游戏结束逻辑修复教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号