|
I've been trying to code the Nim game in java for weeks now but it is so complicated. There are 3 classes. 1) InteractiveApp class package nim;
import java.util.*; public class InteractiveApp {
/** * The main method creates a new NimGame using the setInitialPosition * method and then alternately lets the user and the computer make moves * until the game is finished and a winner is announced. If input from the * user stops then the computer will continue playing until the game is * finished. * * @param args The command line arguments are not used. */ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter initial position"); NimGame game = getInitialPosition(input); while (!game.isFinished()) { System.out.println(game); userMove(input, game); if (game.isFinished()) { System.out.println("You win!"); break; } System.out.println(game); computerMove(game); if (game.isFinished()) { System.out.println("I win!"); } } }
/** * Creates a new NimGame as determined by the first line of input read from * the given scanner. Input should consist of integers greater than 0. If * the line begins with more than one integer then a NimGame is created * using each integer to represent the number of counters in a separate * pile. If only one number is given then a NimGame is created with that * number of piles. If no numbers are given a NimGame is created * containing 4 piles. If a non-number is encountered then no more numbers * will be read. Any numbers with a value of less than 1 will be ignored * but input will continue to be read. * * @param input the source of input to initialise a new NimGame from. * @return a NimGame which has been initialised according to the input read * in. */ private static NimGame getInitialPosition(Scanner input) { ??? }
/** * If the game is not finished then a move is generated, printed out * in the specified form, and performed. * @param game used to generate and perform the move. */ private static void computerMove(NimGame game) { ??? }
/** * Repeatedly read input from the given scanner one line at a time and * perform one of two possible operations until a successful move has been * performed or there is no more input. The operations are * <ul><br/> * <li><b>h</b> - use generateMove to calculate a possible move and then * print it out in this form * <pre> * </pre></li> * <li><b>r <i>counters</i> <i>pile-number</i></b> - remove the given * number of counters from the given pile. * </ul><br/> * If a valid move is performed or there is no more input then exit this * method. For any input which doesn't match a valid move and isn't a * request for a hint then print <b>Illegal move</b>. * * @param input * @param game used to generate and perform the move */ private static void userMove(Scanner input, NimGame game) { ???? }
/** * Returns a string description of the given move in the form * <pre> * (remove <i>counters</i> counters from pile <i>pile-number</i>) * </pre> * Where the pile numbers start from 1 not 0. * * @return A 'wordy' string representation of the given move where the pile * numbers start from 1 not 0. */ public static String asWords(Move move) { return "(remove " + move.getCounters() + " counter" + (move.getCounters() > 1 ? "s" : "") + // add s if > 1 counter " from pile " + (move.getPileNumber() + 1) + ")"; }
}
2) Move class looks like : package nim;
/** * This class represents a move which can be made in a game of Nim. A move * consists of a pile number and a number of counters public class Move {
/** The pile number of this move. */ private int pileNumber;
/** The number of counters in this move. */ private int counters;
/** * Creates a new Move object with the given pile number and number of * counters. * * @param pileNumber the pile number of this move. * @param counters the number of counters in this move. */ public Move(int pileNumber, int counters) { this.pileNumber = pileNumber; this.counters = counters; }
/** * Gets the pile number of this move. * * @return The pile number of this move. */ public int getPileNumber() { return pileNumber; }
/** * Gets the number of counters in this move. * * @return The number of counters in this move. */ public int getCounters() { return counters; }
/** * Creates a string representation of this move in the form * <pre>[counters, pilenumber]</pre> * * @return a string representation of this move. */ public String toString() { return "[" + counters + ", " + pileNumber + "]"; }
} and the 3) NimGame.java i havent even started yet.. any suggestions?
|