Advertisement
321igor

ex4_nim

Nov 26th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. from computer_functions import get_computer_move, HEAPS
  2. nop = int(input("Please enter the number of human players (1 or 2):"))  #number of players
  3. if nop%2:
  4.     namea = input("Please enter your name:")
  5. else:
  6.     namea = input("Name of first player:")
  7.     nameb = input("Name of second player:")
  8. player = True                               #True indicates its the turn of player number 1
  9. active = True                               #True while the board is not empty
  10. while active:
  11.     print(sum)
  12.     for i in range(len(HEAPS)):
  13.         print(i+1, end = "")
  14.         print(":")
  15.         for K in range(HEAPS[i]):
  16.             print("*", end="")
  17.         print()
  18.  
  19.     if player:
  20.         print(namea, "it's your turn:")
  21.     elif not nop%2:
  22.         print(nameb, "it's your turn:")
  23.  
  24.     while not nop%2 or player:              #User move input
  25.         move = [int(input("Row?"))]
  26.         move[0] -= 1
  27.         if not move[0] in range(len(HEAPS)):
  28.             continue
  29.         if HEAPS[move[0]] == 0:
  30.             print("That row is empty")
  31.             continue
  32.         break
  33.     move.append(int(0))
  34.     while not nop%2 or player:
  35.         move[1] = (int(input("How many?")))
  36.         if move[1] <= HEAPS[move[0]] and move[1]:
  37.             break
  38.  
  39.     if nop%2 and not player:
  40.         move = list(get_computer_move(list(HEAPS)))
  41.         print("Computer takes ", move[1], "from row ", move[0]+1)
  42.  
  43.     HEAPS = list(HEAPS)
  44.     HEAPS[move[0]] = HEAPS[move[0]] - move[1]
  45.     HEAPS = tuple(HEAPS)
  46.     sum = 0
  47.     for i in range(len(HEAPS)):
  48.         sum += HEAPS[i]
  49.         print(sum)
  50.     if sum == 0:
  51.         active = False
  52.  
  53.     player = not player                 #Gives the move to the next player
  54. if nop%2:
  55.     if not player:
  56.         print("You win")
  57.     else:
  58.         print("Computer wins")
  59. else:
  60.     if not player:
  61.         print(namea, "wins")
  62.     else:
  63.         print(nameb, "wins")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement