-2

Hi guys this is my program ive been using java for about 3 weeks. I have achieved to do this simple game, has taken me long, but what i would like to do is how to include a code that randomly chooses who goes first in the game, some advice would be great, (bare in mind im very amateur if not less)

This isnt homework, or an assignment or work... just something im learning next week in class thought id learn it earlier and be ahead... (slighly a neek haha)

Thanks to anyone who helps

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");
   }
}   
  • 5
    Before adding more features, fix your current errors. Read this: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jlordo Apr 12 '13 at 13:07
  • whats the problem exactly? – LethalWeaponnn Apr 12 '13 at 13:08
  • 2
    Code format and style matter much more than you think. Readability is key to understanding. Pay more attention to developing a consistent style. Yours is poor. – duffymo Apr 12 '13 at 13:11
  • 2
    ok let me re format change few thanks... thanks for help guys. Please bare in mind im 3 weeks old in java. But thanks – LethalWeaponnn Apr 12 '13 at 13:13
  • That should be "bear in mind". It's good to learn early. Start by reading and taking to heart the Sun Java coding conventions: http://www.oracle.com/technetwork/java/codeconv-138413.html. Once you have that, think hard about brace placement, consistent indentation, using whitespace well, and naming objects, methods, and variables in a self-documenting fashion. Oh, and write less code. – duffymo Apr 12 '13 at 13:24
  • Hi guys ive updated it, hope this helps>? @jlordo – LethalWeaponnn Apr 12 '13 at 13:44

5 Answers5

2

new Random().nextInt(n) gives you a random number between 0 and n-1, so you can do

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

to randomly choose one.

If you plan to pick multiple players consider reusing Random instance.

Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
2

You could do something like:

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

But please learn something about Java in general before.

Adrian
  • 2,233
  • 1
  • 22
  • 33
2

Inside your welcome method add the lines:

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

Have a look at the documentation for Random class.

halex
  • 16,253
  • 5
  • 58
  • 67
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
}
Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
looper
  • 1,929
  • 23
  • 42
1

Add this line to your libraries

import java.util.Random;

Then this line adds a new random number generator, kind of like Scanner.

Random generator = new Random();

Use this resource for more information.

Xiam
  • 345
  • 4
  • 8
  • 16