Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment 1 The Game of Nim Class design, The game of \"Nim\" is played with tw

ID: 3813037 • Letter: A

Question

Assignment 1 The Game of Nim Class design, The game of "Nim" is played with two players. A game begins with 15 sticks laid out in a row like this Players take turns removing sticks. On your turn you must remove either one, two, or three sticks The player who removes the last stick is the loser. There is a winning strategy for Nim; at the end of your turn you want the number of sticks left, minus 1, to be divisible by 4. Implement a second mode for computer move that follows the winning strategy whenever possible If a strategic move is not possible, the program should make a random move. You have to design and implement appropriate classes to play a series of Nim against the user Your code must report the winner of each game and keep score throughout the match (number of user wins and number of program wins Design: Design a class (or classes) to support the operations necessary to play Nim. o Design a MM!Game class. Services provided Create a new NimGame in particular mode (winning or random computer Play a game Find out the winner of a game. You may want some private methods to do individual moves. o Design a Nim Match class. Services provided Create a new match Ask for the mode of the match human computer, or human -winning computer Ask who will make first move in the match (alternate in the successive games. Play a game in the match. Ask for another game. Retrieve the number of wins by user and by program.

Explanation / Answer

/** * @fileName NimGame.java * @author * @since 29/3/17 */ package nimgame.nimgame1; import java.util.Random; public class NimGame { int[] board; /** * This is parametrized constructor which will initialize the object * * @param initialStick */ public NimGame(int[] initialStick) { this.board = initialStick; } /** * @param r * @return */ public int getRow(int r) { return board[r - 1]; } /** * * @param r * @param s */ public void play(int r, int s) { if (r >= 1 && r =1 && s = s) { board[r - 1] -= s; } else { throw new NoEnoughSticksException("No Enough Sticks"); } } else { throw new IllegalSticksException("Stick Should be between 1 and 3"); } } else { throw new NoSuchRowException("No such row"); } } public boolean isOver() { for (int x : board) { if (x > 0) { return false; } } return true; } public void AIMove() { boolean validMove = false; while (!validMove) { // generating random numbers int s = new Random().nextInt(3) + 1; int r = new Random().nextInt(3); if (s >=1 && s = s) { board[r] -= s; validMove = true; System.out.println("Computer move:" + (r+1) + " " + s); } } } } }