Lab 7:

NIM is played with two players and a pile of coins. The players take turns removing coins from the pile. On each turn, a player must take one, two, or three coins from the pile. The player who takes the last coin loses. You are going to write a GUI for the game and a "computer player" that will play it. Play several games of NIM with another student until you discover the best possible strategy. Hint: it all depends on the number of coins that you start with, so playing some games with 1, 2, 3, 4, 5, 6, and 7 to start with will help you understand which initial coin-counts make it possible for the first player to be sure that s/he wins and which make it possible for the second player to be sure s/he wins. The standard NIM game begins with 15 coins. This game was one of the "immunity challenges" on "Survivor Thailand".

If you cannot figure out the optimal strategy, ask the TA to tell it to you; you want to move on to writing your NIM program in no more than about 15 minutes.

Tasks 1 and 2 should be very simple; if they take more than 6 minutes, talk to the TA.

Task 1:
Write the function y = bestMove(count) which chooses a number of coins to remove from the pile if there are "count" coins left. Your program should make the choice that ensures a win for you if possible; if not (i.e., if the count is bad for you), you can just play randomly, or always take one coin, or whatever else you like. Then play a game of NIM against your program by laying out 15 coins on your desk, asking the computer how many coins it wants to take (type bestMove(15), don't actually talk to your computer), and then removing that many coins on its behalf; then it's your turn to play. If, after your turn, there are only 12 coins left, type bestMove(12) to ask the computer how many it wants to take; continue until someone wins.
Task 2:
Write the function y = randomMove(count) which chooses a number of coins to remove from the pile if there are "count" coins left. Your function should choose a random number of coins to remove (either 1, 2, or 3).
Task 3:

Write the function

function w = playGame(count,strategy1,strategy2)
which takes as input the initial number of coins, and the name of two functions. The function should return 1 if strategy 1 wins and should return 2 if strategy 2 wins. It should return the following answers:
>> playGame(1,'bestMove','bestMove')
2
>> playGame(2,'bestMove','bestMove')
1
>> playGame(3,'bestMove','bestMove')
1
>> playGame(4,'bestMove','bestMove')
1
>> playGame(5,'bestMove','bestMove')
2
>> playGame(6,'bestMove','bestMove')
1
>> playGame(6,'bestMove','randomMove')
1
>> playGame(6,'randomMove','bestMove')
1 or 2, (DEPENDS ON WHAT RANDOM CHOOSES)

In the first case, with just 1 coin, the first player must take it, so the second player wins. In the second case the first player can take just one coin and then leave the second player with just one coin (which s/he must take), so player 1 wins, and so on.

This function requires some planning on your part. You need to:

  1. keep track of how many coins are left and stop when it gets to zero (think "while")
  2. keep track of whose turn it is.
  3. use the correct strategy for that player (think "feval").
Check in with the TA if you are having trouble getting the above results
Task 4:

Write the function

function y = humanPlayer(count)

which takes in the current number of coins (count). It should then tell the user how many coins there are (you can use the "display" and "num2str" commands) for this, then ask how many coins the user wants to take (use the "input" command).

You should then be able to play against the previous strategies by running the command

>> playGame(6,'humanPlayer','bestMove')
1
and if you play correctly, get the above result. Let the TA play against the computer for your only required check off for this lab.