该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
跪求大佬们帮忙,这游戏大概是这样的 这是个双人游戏,首先你先输入名字。 然后选择好多局你要耍。 有五堆,每一堆有5个以下随机数量的石头,每次可以拿五个以下的石头,玩家1拿一次,玩家2拿一次,哪个最先拿完就赢了。 我这个问题就是 如果我选择两局以上,第二局开始那五堆显示的石头量还是0。 我就不晓得该怎么改。十分感谢
import java.util.Scanner;
public class pilegame {
public static int player = 1;
public static void main(String[] args)
{
Scanner myObj = new Scanner(System.in);
String Player1name;
System.out.println("Enter your name, player1");
Player1name = myObj.nextLine();
String Player2name;
System.out.println("Enter your name, player2");
Player2name = myObj.nextLine();
int rounds;
System.out.println("Enter how many rounds of pilegame");
rounds = Integer.parseInt(myObj.nextLine());
int pile;
int stones;
int game = 1;
Scanner input = new Scanner(System.in);
pilegame myAssistant = new pilegame();
do {
myAssistant.displayBoard();
if (player == 1)
player = 2;
System.out.print(Player1name + " Please enter the pile you wish" + " to remove from. (1, 2, 3, 4, 5)\n" + "Game " + game + ".\n");
pile = input.nextInt();
System.out.print(Player1name + " Please enter the amount of" + " stones you wish to remove\n");
stones = input.nextInt();
myAssistant.playerMove(pile, stones);
myAssistant.displayBoard();
if (player == 2)
player = 1;
System.out.print(Player2name + " Please enter the pile you wish" + " to remove from. (1, 2, 3, 4, 5)\n" + "Game " + game + ".\n");
pile = input.nextInt();
System.out.print(Player2name + " Please enter the amount of" + " stones you wish to remove\n");
stones = input.nextInt();
myAssistant.playerMove(pile, stones);
myAssistant.displayBoard();
myAssistant.determineWinner();
if (myAssistant.determineWinner() != -1)
game++;
}
while (game <= rounds);
}
private int [] _board;
public pilegame()
{
_board = new int [5];
_board[0] = (int) (Math.random() * 5 + 1);
_board[1] = (int) (Math.random() * 5 + 1);
_board[2] = (int) (Math.random() * 5 + 1);
_board[3] = (int) (Math.random() * 5 + 1);
_board[4] = (int) (Math.random() * 5 + 1);
System.out.println("Welcome to the Game of Nim\n" + "You'll need to first pick a pile\n" + "Then select how many stones you wish to remove\n");
}
public void displayBoard()
{
for(int row = 0; row < 5; row++)
{
if(_board[row] == 0)
System.out.print(" 0 ");
else
System.out.print(" " + _board[row] + " ");
}
System.out.println("\n Pile 1 Pile 2 Pile 3 Pile 4 Pile 5\n");
}
public boolean playerMove (int pile, int stones)
{
if ((pile < 1) || (pile > _board.length))
{
return false;
}
if (_board[pile - 1] >= stones)
{
_board[pile - 1] -= stones;
return true;
}
else
return false;
}
public int determineWinner()
{
int winner = -1;
if((_board[0] == 0) && (_board[1] == 0) && ( _board[2]==0) && ( _board[3]==0) && ( _board[4]==0))
winner = pilegame.player;
if(winner == player)
{
System.out.println("You've Won!");
winner = 0;
}
return winner;
}
}