Nim Game with AI Program Project

User Generated

chcergl

Programming

Description

Write a program that plays the subtraction game (Nim). We have written a simplied version before....as a reminder, The game is such that there are initially 21 stones. Each player takes turns removing 1-3 stones on their turn (their choice). Play alternates back and forth. The person who takes the last stone is the loser... Rather than having another player have a computer AI remove 1-3 stones randomly (to a degree, when 4 stones or less hard-coded choices). Create at least two functions, aiMove, and userMove...these functions should return the amount of stones to take from the pot.


code:

import random

def main():

stones = 21;

#TODO:

# - Randomly determine starting player

# - Alternate between AI and User, calling their methods

# - When a player takes the last stone, end the game and state that they lost.

def userTurn(sPile):

"""

A function that asks the user how many stones to take, and returns that amount. It also checks to make sure that

the user enters a valid choice (1-3, or less if the pile of stones doesn't have 3).

:param sPile the number of stones in the pile

:return the valid number of stones to take ---0 is place holder

"""

#TODO: complete the method to ask the user for a number of stones to take (keep asking until valid).

return(0)

def aiTurn(sPile):

"""

A function that randomly generates a number of stones to take...1-3, or use if-statements when 4 or less stones remaining

:param sPile the number of stones in the pile

:return the valid number of stones to take ---0 is place holder

"""

#TODO: complete the method to randomly generate the number of stones to take.

return(0)

main()


Unformatted Attachment Preview

Explanation & Answer:
1 Code
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Here is the code along with a...


Anonymous
Awesome! Made my life easier.

Studypool
4.7
Indeed
4.5
Sitejabber
4.4

Related Tags