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.
Write the function
>> 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:
Write the function
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') 1and if you play correctly, get the above result. Let the TA play against the computer for your only required check off for this lab.