我目前正在制作一个程序,有4名玩家轮流投掷n枚硬币。第一个获得16分的玩家获胜。玩家每次投掷硬币都能获得分数。他得到的分数等于他掷出正面的次数。如果他没有掷出正面,那么他将失去下一回合。如果他投掷3次正面,那么他将获得一次额外的机会,并再次投掷硬币。如果他掷出少于3次正面,则该轮到下一个玩家。玩家必须获得16分才能获胜。如果一个玩家得到14分,投掷2次正面,那么他就赢了,但如果他投掷n次正面,超过16分,那么他就失去了一半的分数,也失去了轮到他的机会。他必须得16分才能赢。
我的问题在于游戏类。如果玩家连续掷出3个正面,我似乎无法让他再次进入游戏。
<<p> 硬币类/strong>import java.util.Random;
public class Coins
{
//constants
private static final int HEADS = 0;
private static final int TAILS = 1;
//constants
private final int n_coins;
//instance variables
private int n_heads;
private int n_tails;
private Random randomizer;
//constructors
public Coins()
{
n_tails = 0;
n_heads = 0;
n_coins = 3;
randomizer = new Random();
}
public Coins(int new_n_coins) throws Exception
{
if(new_n_coins < 1)
throw new Exception("Coins constructor number of coins is less than 1");
n_coins = new_n_coins;
n_tails = 0;
n_heads = 0;
randomizer = new Random();
}
//custom method
public void tossCoins()
{
n_tails = 0;
n_heads = 0;
for(int toss_counter = 1; toss_counter <= n_coins; toss_counter++)
{
int coin_face = randomizer.nextInt(TAILS + 1);
if(coin_face == HEADS)
n_heads++;
else
n_tails++;
}
}
//accessors
public int getNCoins()
{
return n_coins;
}
public int getNHeads()
{
return n_heads;
}
public int getNTails()
{
return n_tails;
}
}
Player Class
public class Player
{
private String name;
private int state;
private int points;
public Player()
{
state = State.NORMAL;
points = 0;
name = "no name";
}
public Player(String new_name) throws Exception
{
state = State.NORMAL;
points = 0;
setName(new_name);
}
//accessors
public int getState()
{
return state;
}
public int getPoints()
{
return points;
}
public String getName()
{
return name;
}
//mutators
public void setState(int new_state)
{
state = new_state;
}
public void setPoints(int new_points)
{
points = new_points;
}
public void setName(String new_name) throws Exception
{
if(new_name.length() == 0)
throw new Exception("setName error - empty name");
name = new_name;
}
}
<<p> 状态类/strong> public class State
{
public static final int NORMAL = 0;
public static final int EXTRA_TURN = 1;
public static final int LOSE_TURN = 2;
}
游戏类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;
}
//write mutators
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
{
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(player_points == oldPoints + m_heads)
{
player.setState(State.EXTRA_TURN);
}
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 turnn", players[player_index].getName());
System.out.printf("%s has %d pointsn", players[player_index].getName(),
players[player_index].getPoints());
coins.tossCoins();
int n_heads = coins.getNHeads();
System.out.printf("%s tossed %d headsn",
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 pointsn", players[player_index].getName(),players[player_index].getPoints());
}
while(!game.gameOver());
System.out.printf("%s wins!n", players[player_index].getName());
}
catch(Exception ex)
{
}
}
}
编辑
只删除第一行player_index = (player_index + 1) % n_players;
in game.nextPlayer()
我将给您一个一般的提示,因为您有相当多的代码,并且将我的解决方案塞进将是危险的。
考虑"运行"的概念-如果一个玩家有一个正面,那么他们的运行是1。如果他们连续两次正面向上,那么他们连续2次正面向上。如果他们已经连续三次,那么他们的运行计数重置,并且他们的状态被设置为EXTRA_TURN
。
像这样:
private int runCount = 0;
// should only *ever* be called after a heads
public void onRun(boolean hadHeads) {
if(hadHeads) {
runCount++;
if(runCount == 3) {
runCount = 0;
setState(EXTRA_TURN);
System.out.println("Three heads in a row - you get an extra turn!");
} else {
runCount = 0;
}
}