-2

大家好,这是我使用 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");
   }
}   
4

5 回答 5

2

new Random().nextInt(n)给你一个介于 0 和 n-1 之间的随机数,所以你可以

Player[] players = ...
playerToStart = players[new Random().nextInt(players.length)];

随机选择一个。

如果您打算选择多个玩家,请考虑重用 Random 实例。

于 2013-04-12T13:10:42.953 回答
2

您可以执行以下操作:

public void start() {
  Scanner input = new Scanner(System.in);
  player = Math.random() < 0.5 ? "A" :"B";
  do {

但是请先了解一些有关 Java 的知识。

于 2013-04-12T13:13:23.800 回答
2

在您的welcome方法中添加以下行:

if(new java.util.Random().nextBoolean())
    player = "A";
else
    player = "B";

查看Random class的文档。

于 2013-04-12T13:14:18.183 回答
1
Random rand = new Random();
int result = rand.nextInt(2); // a random number, either 0 or 1, see http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextInt(int)
if (result == 0) {
    // Player 1 goes first
}
else {
    // Player 2 goes first
}
于 2013-04-12T13:10:06.947 回答
1

将此行添加到您的库中

import java.util.Random;

然后这一行添加了一个新的随机数生成器,有点像 Scanner。

Random generator = new Random();

使用资源了解更多信息。

于 2013-04-12T13:12:25.360 回答