Exercise 2.6 – The game of Nims/Stones Here's a solution, but it departs from the "basic outline" prescribed in the 6.189 Homework 2 handout by handling both players in a single loop. When I tried to use two separate loops, it got messy because there was a lot of duplication of code, including watching for the end game condition, ready to terminate execution from either loop after all the stones had been taken. Does anyone have a solution that uses two loops without a lot of repetition of code? http://ideone.com/kLQ5Zf The solution is written for Python 3..
I think that if you are going to use a loop for each player, you are going to repeat code. However, I think that it can still be easy to read (though I know I should have commented my solution). You can view my Python 2.7 solution here: http://pastebin.com/sW0SrkdL. I had another version where I used a function to handle the players' turns, but I can't find it.
Thanks. If you find your other version, please post that one here as well. It would be interesting to look at the variety of solutions that people have developed.
Join our real-time social learning platform and learn together with your friends!