【发布时间】:2021-01-12 19:54:50
【问题描述】:
我是 CS 一年级的学生,我正在用 Java 制作一个名为 Reversi/othello 的简单棋盘游戏作为学校项目。 为了打印电路板,我使用了 HashMap 并打印出: board
每个点或 X 或 O 都是哈希图中的键,例如键 A1 的值为 .键 D4 的值为 O。 游戏由两个用户玩,每个用户根据玩家输入 X 或 O。 其他两个玩家之间的每一块石头(或一系列石头)水平,垂直或对角线都需要改变为它周围的石头,玩家只有在可以抓住另一个的情况下才能给出输入玩家的石头,否则他跳过一个回合。例如: Board2
如果玩家 O 在 b2 放置一颗子,c2 处的 X 也需要更改为 O,因为它位于另一玩家的 2 个石头之间: Board3
我需要创建一个算法,每轮检查玩家是否可以放置有效的石头,并检查需要转动哪些石头。它需要在各个方向上都这样做,但我不知道如何创建这样的算法。我知道的唯一方法是在 if 语句中硬编码每个解决方案,但这不是很有效。喜欢:if(board.board.get("c2") == board.BLACK && board.board.get("d2") == board.WHITE && board.board.get("b2") == board.WHITE ) { board.board.replace("c2", board.WHITE); board.printBoard(); }
代码如下:
private boolean p1Start = false;
private boolean p2Start = false;
public final Player P1 = new Player();
public final Player P2 = new Player();
public void play(int amountofstones) {
ConsoleInteractor io = new ConsoleInteractor();
// create players
P1.setName1();
P2.setName2();
//welcome message
System.out.println("");
System.out.println("Welcome to a game of Reversi " + P1.getName().toUpperCase() + " and "
+ P2.getName().toUpperCase() + "!");
System.out.println("");
// create and print board
Board board = new Board();
board.printBoard();
// play a game of Reversi
//create a boolean that returns true when a stone has been succesfuly placed
boolean succes;
for (int i = 1; i < amountofstones; i = i + 2) { // eruit
if(p1Start) {
succes = false;
while (!succes) {
System.out.println(P1.getName().toUpperCase() + ", please enter your move:");
String move1 = io.readInput();
// check if the hashmap contains the key of move
if (board.board.containsKey(move1)) {
// check if the value of key move is equal to empty
if (board.board.get(move1) == board.EMPTY) {
//place a stone
board.board.replace(move1, board.BLACK);
succes = true;
board.printBoard();
} else {
//return error message when users tries to place a stone on a place thats already taken
System.out.println("that spot is already taken");
}
} else {
//return error message when user wants to put stone outside of board
System.out.println("That spot doesnt exist");
}
}
p2Start = true;
}
//same for player 2
succes = false;
if(p2Start) {
while (!succes) {
System.out.println(P2.getName().toUpperCase() + ", please enter your move:");
String move2 = io.readInput();
// check if the hashmap contains the key of move
if (board.board.containsKey(move2)) {
// check if the value of key move is equal to empty
if (board.board.get(move2) == board.EMPTY) {
board.board.replace(move2, board.WHITE);
succes = true;
board.printBoard();
} else {
//return error message when users tries to place a stone on a place thats already taken
System.out.println("that spot is already taken");
}
} else {
//return error message when user wants to put stone outside of board
System.out.println("That spot doesnt exist");
}
}
p1Start = true;
}
//if board is full or no white or black stones are left end game and print winner
if(!board.board.containsValue(board.EMPTY) || !board.board.containsValue(board.BLACK) || !board.board.containsValue(board.WHITE)) {
System.out.println("Game over");
int white = Collections.frequency( board.board.values(), board.WHITE);
int black = Collections.frequency( board.board.values(), board.BLACK);
int numOfWinsWhite = 0;
int numOfWinsBlack = 0;
if(white > black) {
numOfWinsWhite++;
System.out.println("The winner is: " + P2.getName());
}else if(black > white) {
System.out.println("The winner is: " + P1.getName());
numOfWinsBlack++;
}else {
System.out.println("Its a tie");
}
System.out.println(P1.getName() + ": " + numOfWinsBlack + " - " + P2.getName() + ": " + numOfWinsWhite);
break;
}
【问题讨论】:
-
问题是什么?你能解释什么是行不通的,什么是你不明白的吗?你做了什么调试?这些都是您应该问自己并在帖子中解释的问题。请访问help center,使用tour,尤其是阅读How to Ask。
-
问题是如何创建帖子中描述的这种算法。
-
查看meta.stackoverflow.com/questions/334822/… -- 并再次阅读@JimGarrison 提供的链接