In this article, we will develop a game called 21 strategy game with the necessary algorithm and code.
Outline
The user and Computer will choose a number from 1 to 3. Whichever player reaches the total of 21 first, loses the game.
Algorithm
- Import the random module.
- Write a function to specify who wants to play the game first.
- If the user starts first get a number between 1-3 from the user and add it to the total. Now, change the current player to a computer.
- If the computer starts first get a random number between 1-3 and add it to the total. Now, change the current player to the user.
- Repeat steps 3 and 4 till the total becomes 21.
- Once we cross the total of 21 check who the current player is and announce the winner accordingly.
Program with explanation in comments
import random
def startPlayer(uName):
"""Randomly selects who wants to play first"""
player = random.randint(1,2)
if player == 1:
player = 'Ms. Computer'
print('I will go first.')
else:
player = uName
print('You will go first.')
return player
def rules():
"""Displays the rules"""
print('Welcome to the 21 Strategy Game! \nFirst, user and computer will choose a number from 1 to 3. Whichever player reaches the total 21 first, loses the game.')
def msComputer():
"""Generates a random number between 1 and 3 for computer's choice"""
compNum = random.randint(1,3)
return compNum
def user():
"""Gets the user input"""
userNum = int(input('Choose a number from 1 to 3. '))
if userNum<1 or userNum>3:
print("Enter a valid number")
return user()
else:
return userNum
def winning(otherP, uName):
"""This function is used to print the winner"""
if otherP == 'Ms. Computer':
print('Ms. Computer won! Better luck next time!')
else:
print(uName,'won! Good job!')
def gameStarts(uName):
"""This function is used to get the input from the user and computer till the number reaches 21 and returns the winner name"""
total = 0 #initialize the total variable to 0
rules()#display the rules
firstP = startPlayer(uName) #randomly initialise the first player
while total < 21: #repeat the game untill the total becomes 21
if firstP == 'Ms. Computer':#if the current player was computer then we can call the function to get random number
num = msComputer()
total += num #increment the total by the taken number
print('Ms. Computer (aka me) has chosen:',num)
firstP = uName #change the current player to user
else:#if the current player was user then we can call the function to get users input
num = user()
total += num#increment the total by the taken number
print(uName,'(aka you) has chosen:',num)
firstP = 'Ms. Computer' #change the current player to computer
print('The total so far is:',total)
return firstP #after coming out of the while loop we can return current player
#gets the player name
userName = input('What is your name? ')
player = gameStarts(userName) #call the functionNumOutput and returns the player who won the game.
winning(player, userName)#calls the methodfor displaying who won the game.
Please note:
- Recursion is the process of calling the same function inside the function definition. To know about recursion, recursion.