fork download
  1. # nims.py
  2. # Exercise 2.6 – The game of Nims/Stones
  3. # http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2011/assignments/MIT6_189IAP11_hw2.pdf
  4. # Written for Python 3.2
  5. # Glenn Richard
  6. # July 3, 2013
  7.  
  8.  
  9. def play_nims(pile, max_stones):
  10. '''
  11. An interactive two-person game; also known as Stones.
  12. @param pile: the number of stones in the pile to start
  13. @param max_stones: the maximum number of stones you can take on one turn
  14. '''
  15.  
  16. ## Basic structure of program (feel free to alter as you please):
  17.  
  18. # while [pile is not empty]:
  19. # while [player 1's answer is not valid]:
  20. # [ask player 1]
  21. # [execute player 1's move]
  22. #
  23. # while [player 2's answer is not valid]:
  24. # [ask player 2]
  25. # [execute player 2's move]
  26. #
  27. # print "Game over"
  28.  
  29. player = 1
  30. print(str(pile) + " stones, initally")
  31. while pile > 0:
  32. taken = 0
  33.  
  34. while not 1 <= taken <= max_stones:
  35. try:
  36. inp = input("\nPlayer " + str(player) + " > Number of stones? (1 - " + str(max_stones) + "): ")
  37. print("\nInput was: " + str(inp))
  38. taken = int(inp)
  39. except:
  40. taken = 0
  41.  
  42. pile -= taken
  43. print("\n" + str(taken) + " stones taken; " + str(pile) + " remaining")
  44.  
  45. max_stones = min(pile, max_stones)
  46. # Switch players
  47. player = player % 2 + 1
  48. print("\nGame over")
  49. print("\nPlayer " + str(player % 2 + 1) + " wins!")
  50.  
  51. play_nims(100, 5)
  52.  
Success #stdin #stdout 0.06s 9568KB
stdin
4
3
3
5
three
1
7
2
4
1
999999
5
4
2
3
3
y
4
2
2
4
0
3
3
1
5
2
4
5
1
1
5
gazillion to the gazillionth power
3
3
4
2
1
5
stdout
100 stones, initally

Player 1 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 96 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 93 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 90 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 85 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: three

Player 1 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 84 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 7

Player 2 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 82 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 78 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 77 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 999999

Player 1 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 72 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 68 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 66 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 63 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 60 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: y

Player 2 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 56 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 54 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 52 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 48 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 0

Player 2 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 45 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 42 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 41 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 36 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 34 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 30 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 25 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 24 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 23 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 18 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: gazillion to the gazillionth power

Player 2 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 15 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 12 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 8 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 6 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 5 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 0 remaining

Game over

Player 1 wins!