gpt4 book ai didi

java - 努力保持算法以通过掷骰子整齐地排序玩家

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:43:49 25 4
gpt4 key购买 nike

我正在编写一个 Java 算法,该算法让游戏中的六个玩家中的每一个都掷骰子,掷出最高的人先轮到,依此类推。我已经编写了以 <String playerName, Player player> 形式接收玩家 map 的掷骰子方法。 ,并让每个玩家掷骰子,其中玩家是一个存储游戏所需的许多玩家属性的类。

我在对玩家排序时遇到的问题是,如果两个玩家掷出相同的数字,他们会再次掷出以查看谁先于另一个。这是一个示例场景:

位置:球员(掷出的数字)

1:汤姆(5)

2:杰瑞 (4)

=3: jack (3)

=3:吉尔(3)

5:哈利(2)

6:罗恩 (1)

于是 jack 和吉尔再次翻滚。 jack 掷出 6,吉尔掷出 3。 jack 现在排在第 3 位,吉尔排在第 4 位。

我开始编写的任何策略都会很快变得过于复杂、非常凌乱且难以阅读。这是因为必须检查是否有任何数量的重复卷,同时以正确的顺序存储每个卷,如果有重复的卷,则允许两个或更多位置。谁能想出一个可以确定和存储此顺序的简洁结构?

Player 的每个实例, 有一个 nextPlayer指向他们之后位置的玩家的变量。最好也存储 numberRolled在类里面也是。任何掷出相同数字的玩家都可以存储在新 map 中,然后传递到 rollDice 中。再次方法。

编辑

感谢 Andy Turner,这是我的解决方案:

private Player[] playerOrder = new Player[ModelConstants.NUM_PLAYERS_PLUS_NEUTRALS];

playerOrder = getPlayerOrder();

Player[] getPlayerOrder() {
Player[] players = ModelConstants.PLAYERS.values().toArray(new Player[ModelConstants.PLAYERS.size()]);
String[] playerNames = ModelConstants.PLAYERS.keySet().toArray(new String[ModelConstants.PLAYERS.size()]);
getPlayerOrder(playerNames, players, 0, players.length);
return players;
}

void getPlayerOrder(String[] playerNames, Player[] players, int start, int end) {
// Get all players between players[start] (inclusive) and
// players[end] (exclusive) to re-roll the dice.
for (int i = start; i < end; ++i) {
players[i].setDiceNumberRolled(rollDice(playerNames[i], players[i]));
}

// Sort this portion of the array according to the number rolled.
Arrays.sort(players, start, end, new Comparator<Player>() {
@Override public int compare(Player a, Player b) {
return Integer.compare(a.getDiceNumberRolled(), b.getDiceNumberRolled());
}
});

for (int i = 0; i < playerNames.length; i++) {
playerNames[i] = HashMapUtilities.getKeyFromValue(ModelConstants.PLAYERS, players[i]);
}

// Look for players who rolled the same number.
int i = start;
while (i < end) {
// Try to find a "run" of players with the same number.
int runStart = i;
int diceNumberRolled = players[runStart].getDiceNumberRolled();
i++;
while (i < end && players[i].getDiceNumberRolled() == diceNumberRolled) {
i++;
}

if (i - runStart > 1) {
// We have found more than one player with the same dice number.
// Get all of the players with that dice number to roll again.
addMessageToLog(MessageType.INFO, "There has been a tie." , 2000);
tiedPlayers = true;
getPlayerOrder(playerNames, players, runStart, i);
tiedPlayers = false;
}
}
}

private int rollDice(String playerName, Player player) {
int numberRolled = 0;
if (player.getPlayerType().equals(PlayerType.HUMAN)) {
boolean diceRolled = false;
while (!diceRolled) {
String message = ", roll the dice";
if (tiedPlayers == true) {
message += " again.";
}
else {
message += ".";
}
String userInput = getCommand(playerName + message, "Invlaid command. Type \"Roll Dice\" or something similar.", 2000);
if (userInput.equalsIgnoreCase("Roll Dice") || userInput.equalsIgnoreCase("roll the dice") || userInput.equalsIgnoreCase("Roll")) {
numberRolled = dice.rollDice();
diceRolled = true;
}
else {
addMessageToLog(MessageType.ERROR, "Invlaid command. Type \"Roll Dice\" or something similar.", 0);
}
}
}
else {
String message = " is now rolling the dice";
if (tiedPlayers == true) {
message += " again...";
}
else {
message += "...";
}
addMessageToLog(MessageType.INFO, playerName + message, 2000);
numberRolled = dice.rollDice();
}
player.setDiceNumberRolled(numberRolled);
addMessageToLog(MessageType.SUCCESS, playerName + " rolled a " + numberRolled, 1000);
addDicePanel(numberRolled);
return numberRolled;
}

private void setPlayerOrder() {
for (int i = 0; i < playerOrder.length; i++) {
if (i == (playerOrder.length - 1)) {
playerOrder[i].setNextPlayer(playerOrder[0]);
}
else {
playerOrder[i].setNextPlayer(playerOrder[i + 1]);
}
}
activePlayer = playerOrder[0];
}

private void changePlayer() {
activePlayer = activePlayer.getNextPlayer();
}

最佳答案

有两种方法可以解决这个问题。

1) 简单的事情就是忘记“掷骰子”。为每个玩家生成 32 个随机(一个整数)位并将其用于排序。如果他们碰巧匹配,请选择您想要的任何球员。它将非常罕见以至于它并不重要(40 亿分之一的 2 个数字相同)。

2) 如果您想坚持掷骰子。创建一个函数,获取玩家列表,在内部掷骰子并返回正确排序的一个。每当你有相等的点名时,创建一个较小的列表,其中包含相等的玩家,并递归调用该函数来为你提供这些玩家的顺序。当它返回时将其复制到结果中并继续。您可以从数学上证明这极不可能(读作不可能)导致无限循环。

关于java - 努力保持算法以通过掷骰子整齐地排序玩家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35767949/

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