我目前正在对游戏拾取棒进行任务,其中代码提示用户输入游戏中的棒数,然后玩家1和玩家3选择在(1-3)棒之间选择捆绑和任何选择最后一根棍子的人赢得比赛。我目前停留在我的getWinner()和我的pickUpSticks()方法中。我的想法是尝试让玩家1为偶数而玩家2为奇数,因此通过将当前玩家增加1并使用if语句来查看是否为%2来决定玩家是什么,这将很容易在两者之间切换转过来吧。我遇到的问题是代码不能正常工作,而不是让赔率或平均值确定它是否为1或2,它只是增加了玩家,所以它变为0,1,2,3,4,5 ..我遇到的第二个问题是我不确定把声明放在哪里numOfSticks< 0,我现在拥有它的当前位置它允许numOfSticks变为负数但是一旦你尝试输入一个新值它会抛出异常,我希望它在它之前抛出异常允许它变为负数。下面我发布了代码,任何帮助将不胜感激。
import java.util.Scanner;
/**
* Plays a game of Sticks over and over until the players wish to quit.
*
*
*/
public class SticksGameApp
{
int intialNumOfSticks;
int numOfSticks;
int currentPlayer = 0;
public SticksGameApp () {
intialNumOfSticks = 20;
numOfSticks = 20;
}
public SticksGameApp(int startSticks) {
intialNumOfSticks = startSticks;
numOfSticks = startSticks;
}
public int getNumOfSticks() {
return numOfSticks;
}
public int getCurrentPlayer() {
return currentPlayer;
}
public String getPile() {
String pile = "";
for(int i=0;i<numOfSticks;i++){
String c = "|";
pile += c;
}
return pile;
}
public boolean isOver() {
if (numOfSticks == 0) return true;
return false;
}
public int getWinner() {
// player 1 is even, player 2 is odd
int player1 = 1;
int player2 = 2;
if (currentPlayer % 2 == 0) {
return player1;
}
else if (currentPlayer % 2 == 1) {
return player2;
}
else {
return 0;
}
}
public void reset() {
currentPlayer = 0;
numOfSticks = intialNumOfSticks;
}
public void pickUpSticks(int someInt)throws InvalidOptionException {
if (someInt < 1 || someInt > 3 || numOfSticks < 0)throw new InvalidOptionException();
currentPlayer++;
numOfSticks = numOfSticks - someInt;
}
/**
* Plays the game of Sticks over and over until the players quit.
*
* @param args not used.
*/
public static void main(String[] args)
{
int numSticks;
//Initialize a Scanner object to read from the console input stream.
Scanner consuleInput = new Scanner(System.in);
//Ask the user for the number sticks to play with.
System.out.println("It's time for an exciting game of Sticks!");
System.out.println("How many sticks would you like to start with?");
int intialNumOfSticks = consuleInput.nextInt();
//Create a sticks game with the inputed number of sticks.
SticksGameApp myGame = new SticksGameApp(intialNumOfSticks);
//Keep playing games until the players want to quit.
String playAgain = "Y";
while (playAgain.toUpperCase().startsWith("Y"))
{
//Start with a fresh game.
myGame.reset();
//Keep playing until the game is over.
while (!myGame.isOver())
{
//Get the player's selection for the number of sticks to pick up.
System.out.println("\n\nPlayer " + myGame.getCurrentPlayer() + " there are " + myGame.getNumOfSticks() + " stick(s) left.");
System.out.println(myGame.getPile());
System.out.print("How many do you want to take (1-3): ");
numSticks = consuleInput.nextInt();
//Try picking up that many sticks. If there is an exception, have the player reenter.
try
{
myGame.pickUpSticks(numSticks);
}
catch(InvalidOptionException e)
{
System.out.println("That is an invalid number of sticks!!! Try again.");
}
}
//Once the game is over, display who won.
System.out.println("Player " + myGame.getWinner() + " wins!");
//As the user to play again.
System.out.println("\n\nThat was exciting, would you like to play again?");
playAgain = consuleInput.next();
}
//Close the scanner object.
consuleInput.close();
}
}
Player 0 there are 5 stick(s) left.
|||||
How many do you want to take (1-3): 2
Player 1 there are 3 stick(s) left.
|||
How many do you want to take (1-3): 1
Player 2 there are 2 stick(s) left.
||
How many do you want to take (1-3): 3
Player 3 there are -1 stick(s) left.
How many do you want to take (1-3): 2
That is an invalid number of sticks!!! Try again.
Player 3 there are -1 stick(s) left.
How many do you want to take (1-3): 1
That is an invalid number of sticks!!! Try again.
Player 3 there are -1 stick(s) left.
How many do you want to take (1-3): 3
That is an invalid number of sticks!!! Try again.
答案 0 :(得分:0)
...而不是赔率或平均值确定它的1号或2号球员 只是递增玩家所以它变为0,1,2,3,4,5 ...
当然可以。每次转弯后你都会++currentPlayer
将currentPlayer
设置为下一个玩家,那么为什么会做但从0到1,到2,......等等。即便如此,你对代表球员的赔率/均匀的想法仍然有效。唯一的问题是你需要测试currentPlayer
奇数/偶数到处你想知道哪个玩家被代表,而不仅仅是在宣布获胜者的时候。
最好选择特定的值currentPlayer
来代表两个玩家中的每一个,然后确保currentPlayer
只能设置currentPlayer
这些价值中的一个或另一个。然后你可以随时查看currentPlayer
并立即知道哪个玩家的意思。这里有一个方便的技巧:如果让0和1代表两个玩家,你可以方便地在两个玩家之间切换currentPlayer = 1 - currentPlayer;
:
numOfSticks < 0
我不知道把声明放在哪里numOfSticks&lt; 0
不要把它放在任何地方!它太少,太晚了。如果if (someInt < 1 || // can't take less than one stick...
someInt > 3 || // ... nor move then three ...
someInt > numOfSticks) // ... nor more than are actually in the pile
throw new InvalidOptionException();
为真,则意味着之前的播放器被错误地允许使用比实际堆叠更多的棒,并且我们现在才发现它是目前的球员正试图移动。好的,很好,你知道发生了一件坏事,但是你无法做任何事情。太少,太晚了。
你必须首先阻止误入歧途的玩家拿太多的棍子,如下:
paramsFor()