CPS 6: Lab #3

Conditionals and Loops

6 points

In lab 1 you should have created a directory called cps6. Change into that directory using the "cd" command and create another directory called "lab3" using the "mkdir" command. Change into the "lab3" directory. If you did this correctly, when you type "pwd" you should see a long path name that ends with "/cps6/lab3".

In order to do this lab, you need to copy some files using the following "cp" command (don't forget the trailing period, or dot):

cp  ~ola/cps6/lab3/*  .

This command will copy all the files in the directory ~ola/cps6/lab3 into your directory for you to use. If you type "ls" you should see the following files: Makefile, oldmac.cc, and nim.cc.

For each of the programming problems that follow, you should use the style rules discussed in class, which includes meaningful variable names, indentation, and comments at the top of the file and for each function.

To compile these programs, you will NOT use the g++ command explicitly, but rather implicitly. The file Makefile will make it easier to use the g++ command. To compile the program called nim.cc, type "make nim"

This will automatically use the g++ command and link to libraries that you'll need for this assignment. If your program compiles correctly, the executable file called "nim" will be created. Then you can type "nim" to run your program.

Problems

  1. The file oldmac.cc contains the old macdonald program we discussed in class. First, compile and run the program.

    Now let's make it easier on the user. Modify the main function of oldmac.cc to do the following. If the user enters one of the animals listed below, instead of asking the user to type in the sound, use the sound listed below corresponding to that animal. If the user enters an animal that is not listed below, then ask the user for the sound the animal makes. In either case, print the corresponding verse of the old macdonald song.

    Animal Sound
    cow moo
    horse neigh
    goat baa
    hen cluck
    pig oink

    Here is a sample run:

    > oldmac
    Enter an animal: cow
    
    Old MacDonald had a farm, Ee-igh, ee-igh, oh!
    And on his farm he had a cow, Ee-igh, ee-igh, oh!
    With a moo moo here
    And a moo moo there
    Here a moo, there a moo, everywhere a moo moo
    Old MacDonald had a farm, Ee-igh, ee-igh, oh!
    >
    

  2. Nim

    In the game of Nim, you start with a pile of sticks. The players take turns removing between 1 and 6 sticks from the pile. The player who takes the last stick wins.

    For example, suppose the game begins with 13 sticks and the players are you and the computer. In the first round, you take 3 sticks, and the computer takes 6 sticks. In the second round, you take 4 sticks and the game is over. You win!

    The program nim.cc does not yet play a complete game of Nim, but rather just one round of Nim between a user and the computer. In this assignment, you'll modify nim.cc to play a complete game. Currently, in nim.cc, the computer always takes a single stick. Compile nim by typing "make nim" and run this program a few times. What happens if you enter 40? What happens if you enter 3?

    Now let's make changes to nim.cc. First, modify the function "UserMove" to ensure that the player is removing a legal number of sticks. That is, if the player tries taking a negative number, zero, more than six sticks, or more than the current number of sticks, ask the player again. To do this, the functions "InRange" and "Min" might be useful. They are already written, but you will have to call them correctly. Compile and test your program again. Can you enter illegal numbers now?

    Now make the computer's moves smarter. First, use the Dice class to generate a random number of sticks, from 1 to 6. Make sure the computer doesn't try to take more sticks than are available. Compile and run this new version. Is it doing what you'd expect?

    This still isn't very interesting, though. Change the program by adding a loop so that it will play an entire game. Remember, the game ends when one or the other players win by taking the last stick. Compile nim again and make sure it is working correctly. Does it always stop when it should?

    You should now be able to play a complete game of Nim. However, the computer is still pretty easy to beat. Make the computer smarter by changing "CompMove" again. One way is to check if there are six or less sticks, then take exactly that many. If you can think of other ways to improve the computer's play, add those, too. Compile the program again and run it. Can you still beat it?

To submit programs

When your programs for problems 1 and 2 compile and produce the correct output, create a README file. This file should include your name, how long you worked on these programs, and anyone you received significant help from. You can submit your programs by typing (where N is your section number: 1, 2, 3, 4, or 5):

submit6 lab3secN oldmac.cc nim.cc README

You should receive a message telling you that the programs were submitted correctly.