大家好,这是我使用 java 大约 3 周的程序。我已经完成了这个简单的游戏,花了我很长时间,但我想做的是如何包含一个随机选择谁先进入游戏的代码,一些建议会很棒,(记住我很业余如果不是更少)
这不是家庭作业,也不是作业或工作……只是我下周在课堂上学到的东西,我想早点学习并取得领先……(有点笨拙哈哈)
感谢任何帮助的人
import java.util.Scanner;
/**
* The simple NIM game.
* There are 21 matches in the pile. On each move, each user take turns picking
* up 1, 2, or 3 matches until there are no more matches left.
* The one who picks up the last match loses.
*/
public class SimpleNIM {
private int matchesLeft = 21;
private String player = "A";
/**
* Starts and runs the game
*/
public void start() {
Scanner input= new Scanner(System.in);
while (true) {
int pickmatches = 0;
do {
System.out.print(
"Player " + player + ": How many matches do want to pick? (1, 2, or 3) ");
pickmatches = input.nextInt();
if (validMove(pickmatches)) {
break;
}
System.out.println(
matchesLeft - pickmatches < 0
? "You can't pick "
+ pickmatches
+ " matches as only "
+ matchesLeft
+ " matches left"
: "That's an illegal move. "
+ "Choose 1, 2, or 3 matches.");
}
while (true);
updateMatchesLeft(pickmatches);
System.out.println(
"Player "
+ player
+ " takes "
+ pickmatches
+ ( (pickmatches == 1) ? " match, " : " matches, ")
+ "leaving "
+ matchesLeft
+ '\n');
player = otherPlayer(player);
if (gameOver()) {
System.out.println("Player " + player + " wins the game!");
break;
}
}
}
/**
* Update the number of matches left in pile.
* pickmatches No. of matches picked
*/
private void updateMatchesLeft(int pickmatches) {
matchesLeft = matchLeft - pickmatches;
}
/**
* Game Over?
* true if game is over.
*/
private boolean gameOver() {
}
/**
* Returns the other player
* The current player ("B" or "A")
* The other player ("A" or "B")
*/
private String otherPlayer(String p) {
// YOUR CODE GOES HERE
}
/**
* Valid move?
* numMatches The number of matches picked
* true if there are enough matches left and numMatches is between 1 and 3
*/
private boolean validMove(int numMatches) {
}
/**
* Plays the game
* args ignored
*/
public static void main(String[] args) {
SimpleNIM pickUpMatches = new SimpleNIM();
welcome();
pickUpMatches.start();
}
/**
* Displays the startup information banner.
*/
private static void welcome() {
System.out.println("WELCOME TO THE SIMPLE NIM GAME: 21 MATCHES IN PILE");
}
}