import java.util.Random; import java.util.Scanner; /** * This file contains starter code for the Nim game. We copied * over method names from the TwoPlayerGame interface and provided * just enough implementation so that the compiler can compile them * without producing any errors. These implementations are not * correct, so you should replace them with real implementations. * * We also added an empty constructor, which you should implement. * * A good place to start is by writing a main method... */ public class Nim implements TwoPlayerGame { int[] board; int currentPlayer; Scanner s; /** * Initializes a new Nim board game. * * @param numPiles The number of matchstick piles. * @param minMatches The smallest number of matches per pile. * @param maxMatches The largest number of matches per pile. */ public Nim(int numPiles, int minMatches, int maxMatches) { // init scanner s = new Scanner(System.in); // create board board = new int[numPiles]; // init rng Random r = new Random(); for (int i = 0; i < board.length; i++) { // randomly set # of matches board[i] = r.nextInt(maxMatches - minMatches + 1) + minMatches; } currentPlayer = 0; } /** * Returns the current value for a given resource. * * @param resource Describes the game element. * @returns The current value of the resource. */ public int getResource(int resource) { // TODO replace return -1; } /** * Sets the game state. Should not check whether the given * parameters are valid. isValidMove should be called before * calling this method to ensure that a move is legal. * * @param resource Which resource to alter (e.g., position, gamepiece, pile of matches, etc.) * @param updatedValue The updated value of the resource (e.g., how many matches remain, the updated position of a piece, etc.) */ public void setResource(int resource, int updatedValue) { // TODO } /** * Returns the number representing the current player. * * @returns The current player. */ public int getPlayer() { // TODO replace return 0; } /** * Changes the current player to the given player. * * @param player A player number. */ public void setPlayer(int player) { // TODO } /** * Returns true if the specification of a move describes a legal move * given all the rules of the game. Note: this does not check whether the * move is *good* move, only whether it is legal. * * @param resource Which resource to alter (e.g., position, gamepiece, pile of matches, etc.) * @param updatedValue The updated value of the resource (e.g., how many matches remain, the updated position of a piece, etc.) * @return True iff the move is valid. */ public boolean isValidMove(int resource, int updatedValue) { // TODO replace return false; } /** * Returns true if the game is over. * @returns True if the game is over, false otherwise. */ public boolean isGameOver() { // TODO replace return false; } /** * Displays the board on screen. */ public void displayBoard() { // TODO } public void takeATurn() { // this isn't ultimately what we want, but it lets // us experiment with Scanner System.out.println(board[0]); System.out.print("Please enter a number: "); try { board[0] = s.nextInt(); System.out.println(board[0]); } catch (Exception e) { System.out.print("Not an integer! Try again!"); // This program has a bug, right? // It keeps failing repeatedly, forever. // This is because the Scanner is 'stuck' reading // the bad input. // We need to 'clear' that input somehow. // See Example1.java to see how we might handle this. // Example2.java shows another approach. takeATurn(); } } /** * The entry point to the program. It currently ignores * the contents of the args array. * @param args */ public static void main(String[] args) { // System.out.println("This program does nothing."); // create a new Nim board Nim n = new Nim(5, 1, 7); // take turns until game is over while(!n.isGameOver()) { n.takeATurn(); } } }