Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a python program in PyCharm that will play a game called Nim. One particul

ID: 3673865 • Letter: W

Question

Write a python program in PyCharm that will play a game called Nim. One particularly interesting version plays like this: "Assume you have a large pile of marbles. Two players alternate taking marbles from the pile. In each move, a player is allowed to take between 1 and half of the total marbles. So, for instance, if there are 64 marbles in the pile, any number between and including 1 and 32 is a legal move. Whichever player takes the last marble loses." Write a program called Nim in which a human player plays against the computer. The program should first ask for how many marbles are in the starting pile. Then the program should ask for a character for who will start the game (‘p’ for player, ‘c’ for computer). The game will then alternate between the computer and the player, asking the player for how many marbles they want to take (making sure they take a positive integer between 1 and half the pile and making them pick another number if they do it wrong). The computer should always take enough marbles to make the size of the pile a power of 2 minus 1 - such as 3, 7, 15, 31, 63, etc. If that’s not possible, the computer will take one marble. You should find that the computer, if it gets to go first, will always win. Similarly, if you go first and know the strategy, you’ll always win!

An example run might be:

The Game of Nim Number of marbles are in the pile: 100

Who will start? (player or computer): computer

The pile has 100 marbles in it.

The computer takes 37 marbles.

The pile has 63 marbles in it.

How many marbles do you want to take? (1-31): 4

The pile has 59 marbles in it.

The computer takes 28 marbles.

The pile has 31 marbles in it.

How many marbles do you want to take? (1-15): 13

The pile has 18 marbles in it.

The computer takes 3 marbles.

The pile has 15 marbles in it.

How many marbles do you want to take? (1-7): 6

The pile has 9 marbles in it.

The computer takes 2 marbles.

The pile has 7 marbles in it.

How many marbles do you want to take? (1-3): 2

The pile has 5 marbles in it.

The computer takes 2 marbles.

The pile has 3 marbles in it.

How many marbles do you want to take? (1-1): 1

The pile has 2 marbles in it.

The computer takes 1 marbles.

The pile has 1 marbles in it.

How many marbles do you want to take? (1-1): 1

The computer wins!

Explanation / Answer

import random
import re
def ifEmpty(heap):
for i in heap:
if i != 0:
return False
return True
def com_Action(heap):
for i in range(len(heap)):
if heap[i] != 0:
take = heap[i]
index = i + 1
break
print ('Player computer took' ,take, 'objects from heap' ,index )
return (take, index)
def hum_Action(heap):
s = input('Player human enter the number of objects (Y) to take from what heap (X)- in order: Y X ')
while(ifValid(s,heap) == False):
print('Player human that is an invalid move, try again')
s = input('Player human enter the number of objects (Y) to take from what heap (X)- in order: Y X ')
l = s.split()
take = l[0]
index = l[1]
return (int(take), int(index))
def ifValid(s,heap):
if not re.match('[0-9]+s[0-9]$',str(s)):
return False
else:
l = s.split()
take = l[0]
index = l[1]
if int(index) > len(heap) or int(index) <= 0:
return False
elif int(take) > heap[int(index)-1] or int(take) <= 0:
return False
else:
return True
def after_Taking(heap,take,index):
heap[index-1] -= take
return heap
def hPrint(heap):
for x in heap:
print ('',x,end='')
print ('')
def nim():
heapLen = random.choice((3,5,7))
heap = list()
for i in range(0,heapLen):
heap.append( random.choice((9,11,13)) )
print ('Created' , heapLen , 'heaps of sizes',end='')
hPrint(heap)
firstPlayer = random.choice(('human','computer'))
print ('Player ' + firstPlayer + ' goes first')
if firstPlayer == 'computer':
(take,index) = com_Action(heap)
heap = after_Taking(heap,take,index)
hPrint(heap)
while( ifEmpty(heap) == False ):
(take,index) = hum_Action(heap)
heap = after_Taking(heap,take,index)
hPrint(heap)
if ifEmpty(heap) == True:
print ('Player human has won')
break
(take,index) = com_Action(heap)
heap = after_Taking(heap,take,index)
hPrint(heap)
if ifEmpty(heap) == True:
print ('Player computer has won')
break
if __name__ == "__main__":
nim()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote