CSC 160 - Computer Programming for Non-majors

Dr. R.M. Siegfried

Computer Science Assignment #12 - A simple game of Nim

Due Friday, May 8, 2009

Artificial intelligence is the subdiscipline of Computer Science that focuses on how to get the computer to do this that we consider intelligent. This includes learning, understanding human language, emulating human expertise and playing games of strategy.

Nim is a simple example of such a game. There are seven sticks on the table. A player can pick up either 1, 2 or 3 at a time. whoever picks up the last stick loses. Therefore, the goal is to leave exactly one stick for your opponent.

You will find below a program written in Java to play Nim. Enter the program, and run it. Try to find the one strategy that will let you beat the computer. You will turn in a listing of Nim and how you beat it. If you couldn't beat it, give the deatils of how you played and why you lost.


import java.util.Scanner;

public class Nim2 {
  // Play the game Nim against the computer
  public static void main(String[] args) {
    Scanner keyb = new Scanner(System.in);
    int sticksLeft = 7, pickUp = 0, reply = 0;
    boolean winner = false, move;
    char answer = ' ';
    String answerString = new String();

    printInstructions();
    // Find out if the user wants to go first or
    // second
    while (answer != 'f' && answer != 'F'
            && answer != 's' && answer != 'S') {
      System.out.println
          ("Do you wish to go (f)irst or"
                  + " (s)econd ?");
      answerString = keyb.next();
      answer = answerString.charAt(0);
    }

    // If the user goes second, have the computer
    // take two sticks.
    if (answer == 's' || answer == 'S') {
      reply = 2;
      sticksLeft = sticksLeft - reply;
      System.out.println("The computer took "
          + reply + " sticks leaving "
          + sticksLeft + " on the table.");
    }

    // If the user goes first, tell him how many
    // sticks are on the table
    else
      System.out.println("There are " + sticksLeft
                         + " on the table.");

    // As long as there is no winner, keep playing
    while (!winner) {
      pickUp = getMove(keyb, sticksLeft);
      // Take the sticks off the table
      sticksLeft = sticksLeft - pickUp;
      //See if the user won
      if (sticksLeft == 1) {
        System.out.println
            ("Congratulations! You won!");
        winner = true;
      }

      // See if the user lost
      else if (sticksLeft == 0) {
        System.out.println
             ("Sorry, the computer has "
                    + "won - you have lost...");
        winner = true;
      }
      else
        reply = planMove(sticksLeft);
    
      if (!winner)
        sticksLeft
            = updateSticks(sticksLeft, reply);
    }
  }


  // printInstructions() - Print instructions for
  //                       the player
  public static void printInstructions() {
    // Print the instructions
    System.out.println
            ("There are seven (7) sticks on the table.");
    System.out.println
            ("Each player can pick up one, two , or"
                        + " three sticks");
    System.out.println
            ("in a given turn. A player cannot pick "
                        + "up more than");
    System.out.println
            ("three stick nor can a player pass.\n");
  }

  // getMove() - Get the player's next move,
  //             testing to ensure that it is legal
  //             and that there are enough sticks
  //             on the table.
  public static int getMove(Scanner keyb,
                              int sticksLeft) {
    int pickUp = 0;
    boolean move = false;
    // How many sticks is the user taking
    while (!move) {
      System.out.println("How many sticks do you "
                        + " wish to pick up ?");
      pickUp = keyb.nextInt();

      // Make sure its 1, 2 or 3
      if (pickUp < 1 || pickUp > 3)
        System.out.println(pickUp
               + " is not a legal number of sticks");
      //Make sure that there are enough sticks on
      // the table
      else if (pickUp > sticksLeft)
        System.out.println("There are not "
                  + pickUp + " sticks on the table");
      else
        move = true;
    }
    return pickUp;
  }


  // planMove() - Plan the computer's next move
  public static int planMove(int sticksLeft) {
    int reply = 0;
    // Plan the computer's next move
    if (sticksLeft == 6 || sticksLeft == 5
                              || sticksLeft ==2)
      reply = 1;
    else if (sticksLeft == 4)
      reply = 3;
    else if (sticksLeft == 3)
      reply = 2;
    return reply;
}


  // updateSticks() - Update the count of sticks
  //                  left on the table and
  //                  determine if either the
  //                  player or the computer has
  //                  won.
  public static int updateSticks(int sticksLeft,
                                       int reply) {
    // If neither player won, get ready for the
    // next move
    sticksLeft = sticksLeft - reply;
    System.out.println("The computer picked up "
                             + reply + " sticks.");
    System.out.println("There are now "
                         + sticksLeft
                         + " sticks left on the table.\n\n\n");
    return sticksLeft;
  }
}

[Back to the Assignments Index]