我目前正在制作一个程序,有4名玩家轮流投掷3个硬币。第一位获得16分的球员获胜。玩家每次掷硬币时都会获得积分。他赚取的积分数等于他投掷的头数。如果他抛出没有头,那么他将失去他的下一个回合。如果他翻转3个头,那么他会获得额外的转弯并再次掷硬币。如果他投掷少于3个头,则轮到下一个玩家。一名球员必须获得16分才能获胜。如果一名球员得到14分并投掷2个头,那么他就赢了,但是如果他投掷了n个头且超过了16分,那么他就失去了一半的分数并且也失去了他的回合。他必须得到16分赢了。
如何让玩家在下一回合被跳过?每个玩家目前都按顺序排列。防爆。汤姆,汉克,汉娜,蒂娜。如果汉克掷出0个头或者超过16个点,那么他应该输掉他的下一个顺序Tom,Hannah,Tina,Tom,Hank,Hannah,Tina。我发布了我的代码,我需要弄清楚如何编辑nextPlayer()方法以满足我的需求。我相信我在computerState()方法中所做的逻辑和代码对于LOSE_TURN是正确的。任何帮助,将不胜感激。
游戏类
import java.util.Random;
public class Game
{
private Random randomizer;
private final int n_players;
private final int m_coins;
private final int p_points;
private int player_index;
private boolean game_over;
public Game()
{
n_players = 4;
m_coins = 3;
p_points = 16;
game_over = false;
randomizer = new Random();
player_index = randomizer.nextInt(n_players);
}
public Game(int new_m_coins, int new_n_players, int new_p_points)
{
n_players = new_n_players;
m_coins = new_m_coins;
p_points = new_p_points;
game_over = false;
randomizer = new Random();
player_index = randomizer.nextInt(n_players);
}
public int getPlayerIndex()
{
return player_index;
}
public void setPlayerIndex()
{
player_index = randomizer.nextInt(n_players);
}
public boolean gameOver()
{
return game_over;
}
public int nextPlayer(Player[] players)
{
//player_index = (player_index + 1) % n_players;
if(players[player_index].getState() == State.EXTRA_TURN)
{
players[player_index].setState(State.NORMAL);
}
else if(players[player_index].getState() == State.LOSE_TURN)
{
player_index = (player_index + 1) % n_players;
}
else
{
player_index = (player_index + 1) % n_players;
}
/*while(players[player_index].getState() != State.NORMAL)
{
players[player_index].setState(State.NORMAL);
player_index = (player_index + 1) % n_players;
}*/
return player_index;
}
public void computeState(Player player, int m_heads, int oldPoints, int newPoints)
{
int player_points = player.getPoints();
if(player_points == p_points)
game_over = true;
else if(player_points > p_points)
{
player.setPoints(player_points / 2);
player.setState(State.LOSE_TURN);
}
else if(m_heads == 0)
{
player.setState(State.LOSE_TURN);
}
else if(m_heads == 3)
{
player.setState(State.EXTRA_TURN);
}
else if(m_heads == 3 && player_points > p_points)
{
player.setState(State.NORMAL);
}
else
player.setState(State.NORMAL);
}
}
TestCoinGame
public class testcoingame
{
public static void main(String[] args)
{
try
{
int m_coins = 3;
int n_players = 4;
int p_points = 16;
String [] names = {"Hank", "Tina", "Hannah", "Tom"};
Player [] players = new Player[n_players];
for(int index = 0; index < players.length; index++)
players[index] = new Player(names[index]);
Coins coins = new Coins();
Game game = new Game();
int player_index;
do
{
player_index = game.nextPlayer(players);
System.out.printf("It is %s's turn\n", players[player_index].getName());
System.out.printf("%s has %d points\n", players[player_index].getName(),
players[player_index].getPoints());
coins.tossCoins();
int n_heads = coins.getNHeads();
System.out.printf("%s tossed %d heads\n",
players[player_index].getName(), n_heads);
int old_points = players[player_index].getPoints();
int new_points = old_points + n_heads;
players[player_index].setPoints(new_points);
game.computeState(players[player_index], n_heads, old_points, new_points);
System.out.printf("%s has %d points\n", players[player_index].getName(),players[player_index].getPoints());
}
while(!game.gameOver());
System.out.printf("%s wins!\n", players[player_index].getName());
}
catch(Exception ex)
{
}
}
}
答案 0 :(得分:2)
你可以在Player Class
中有一个布尔实例变量来跟踪玩家是否在下一回合跳过她。当你到达检查该值是真还是假并且评估为真的时候(即是,她跳过她的转弯),然后跳过你通常会做的转弯,但是在移动之前将布尔值设置为false到下一个Player
或Turn
或者你有什么。
编辑(基于用户响应):
伪代码:
我假设您通过数组或列表或类似的东西跟踪它的转向。我将把它称为playerList。例如,如果我们有三个玩家,那么我们会继续循环playerList[0]
,playerList[1]
,playerList[2]
。
while (gameNotOver())
{
if (!playerList[currentPlayer].skipNextTurn())
{
//do what you would normally do
}
else
{
playerList[currentPlayer].setSkipNextTurn(false);
}
}