As a way to start experimenting with Python, I created simple functions to reproduce some games for children as The Hangman and Rock-Paper-Scissor.
The fun part of a similar exercise is how some games are scalable according to the acquired knowledge: what if the virtual opponent can learn the user's behavioral pattern through machine learning? or what if the dictionary used to chose the words from comes from web scraping?
In the future, I'm planning to grow this list of games adding more games and more "Artificial Intelligence" to each of them.
As usual, all the code is available on my GitHub profile.
The Hangman:
alphabet = [' ','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
to_guess = "vegetarian pizza"
to_guess = to_guess.lower()
chances = int(len(to_guess))
total_chances = chances
hidden_word = list("_"*len(to_guess))
already_asked=[]
to_guess_copy = list(to_guess)
plural=""
to_guess = "vegetarian pizza"
to_guess = to_guess.lower()
chances = int(len(to_guess))
total_chances = chances
hidden_word = list("_"*len(to_guess))
already_asked=[]
to_guess_copy = list(to_guess)
plural=""
def hanger(total_chances, chances):
print(f"\nYou have:{chances} chances left!\n")
if chances/total_chances <= .06:
print("""
_________
|/ |
| (_)
| \|/
| |
| /
|
_|___""")
elif chances/total_chances <= .12:
print("""
_________
|/ |
| (_)
| \|/
| |
|
|
_|___""")
elif chances/total_chances <= .18:
print("""
_________
|/ |
| (_)
| \|/
|
|
|
_|___""")
elif chances/total_chances <= .24:
print("""
_________
|/ |
| (_)
| \|
|
|
|
_|___""")
elif chances/total_chances <= .30:
print("""
_________
|/ |
| (_)
| |
|
|
|
_|___""")
elif chances/total_chances <= .36:
print("""
_________
|/ |
| (_)
|
|
|
|
_|___""")
elif chances/total_chances <= .42:
print("""
_________
|/ |
|
|
|
|
|
_|___""")
elif chances/total_chances <= .48:
print("""
_________
|/
|
|
|
|
|
_|___""")
elif chances/total_chances <= .54:
print("""
______
|/
|
|
|
|
|
_|___""")
elif chances/total_chances <= .60:
print("""
___
|/
|
|
|
|
|
_|___""")
elif chances/total_chances <= .66:
print("""
|/
|
|
|
|
|
_|___""")
elif chances/total_chances <= .72:
print("""
|
|
|
|
_|___""")
elif chances/total_chances <= .78:
print("""
|
|
|
_|___""")
elif chances/total_chances <= .84:
print("""
|
|
_|___""")
elif chances/total_chances <= .90:
print("""
|
_|___""")
elif chances/total_chances <= .96:
print("""
_|___""")
print(f"\nYou have:{chances} chances left!\n")
if chances/total_chances <= .06:
print("""
_________
|/ |
| (_)
| \|/
| |
| /
|
_|___""")
elif chances/total_chances <= .12:
print("""
_________
|/ |
| (_)
| \|/
| |
|
|
_|___""")
elif chances/total_chances <= .18:
print("""
_________
|/ |
| (_)
| \|/
|
|
|
_|___""")
elif chances/total_chances <= .24:
print("""
_________
|/ |
| (_)
| \|
|
|
|
_|___""")
elif chances/total_chances <= .30:
print("""
_________
|/ |
| (_)
| |
|
|
|
_|___""")
elif chances/total_chances <= .36:
print("""
_________
|/ |
| (_)
|
|
|
|
_|___""")
elif chances/total_chances <= .42:
print("""
_________
|/ |
|
|
|
|
|
_|___""")
elif chances/total_chances <= .48:
print("""
_________
|/
|
|
|
|
|
_|___""")
elif chances/total_chances <= .54:
print("""
______
|/
|
|
|
|
|
_|___""")
elif chances/total_chances <= .60:
print("""
___
|/
|
|
|
|
|
_|___""")
elif chances/total_chances <= .66:
print("""
|/
|
|
|
|
|
_|___""")
elif chances/total_chances <= .72:
print("""
|
|
|
|
_|___""")
elif chances/total_chances <= .78:
print("""
|
|
|
_|___""")
elif chances/total_chances <= .84:
print("""
|
|
_|___""")
elif chances/total_chances <= .90:
print("""
|
_|___""")
elif chances/total_chances <= .96:
print("""
_|___""")
if " " in to_guess:
how_many_spaces = to_guess.count(" ")
print(f"Actually they are {how_many_spaces + 1} words")
plural = "s"
for x in range (how_many_spaces):
pos = to_guess_copy.index(" ")
hidden_word[pos] = " "
to_guess_copy[pos] = "."
how_many_spaces = to_guess.count(" ")
print(f"Actually they are {how_many_spaces + 1} words")
plural = "s"
for x in range (how_many_spaces):
pos = to_guess_copy.index(" ")
hidden_word[pos] = " "
to_guess_copy[pos] = "."
while chances>0:
print("""
""")
print(" ".join(hidden_word))
print("""
Tell me a letter or your final guess!
""")
letter = input(prompt=" >").lower()
print("""
""")
print(" ".join(hidden_word))
print("""
Tell me a letter or your final guess!
""")
letter = input(prompt=" >").lower()
if letter.lower() == to_guess:
print(f"Great job! the password is {to_guess}")
hidden_word = list(to_guess)
break
elif len(letter)>1:
print(f"{letter} is not the correct password")
chances -=1
hanger(total_chances, chances)
print(f"Great job! the password is {to_guess}")
hidden_word = list(to_guess)
break
elif len(letter)>1:
print(f"{letter} is not the correct password")
chances -=1
hanger(total_chances, chances)
elif letter.lower() not in alphabet:
print("Only real letters please!")
chances -=1
hanger(total_chances, chances)
elif letter.lower() in already_asked:
print(f"You already asked for {letter}!")
chances -=1
hanger(total_chances, chances)
elif letter.lower() in to_guess:
how_many = to_guess.count(letter)
print(f"Good choice: you have {how_many} {letter} in the word{plural}")
already_asked.append(letter)
for x in range (how_many):
pos = to_guess_copy.index(letter)
hidden_word[pos] = letter
to_guess_copy[pos] = "."
else:
print(f"Sorry! no {letter} in the word{plural}!")
chances -=1
hanger(total_chances, chances)
if "_" not in hidden_word:
print(f"Great job! the password is {to_guess}")
break
print("Only real letters please!")
chances -=1
hanger(total_chances, chances)
elif letter.lower() in already_asked:
print(f"You already asked for {letter}!")
chances -=1
hanger(total_chances, chances)
elif letter.lower() in to_guess:
how_many = to_guess.count(letter)
print(f"Good choice: you have {how_many} {letter} in the word{plural}")
already_asked.append(letter)
for x in range (how_many):
pos = to_guess_copy.index(letter)
hidden_word[pos] = letter
to_guess_copy[pos] = "."
else:
print(f"Sorry! no {letter} in the word{plural}!")
chances -=1
hanger(total_chances, chances)
if "_" not in hidden_word:
print(f"Great job! the password is {to_guess}")
break
if "_" in hidden_word:
print(" ".join(hidden_word))
print("You have no more chances to choose letters. Tell me the password or you'll die!")
final_guess = input(prompt = " >").lower()
if final_guess.lower() == to_guess:
print(f"Great job! the password is {to_guess}")
else:
print("You failed")
print("""
_________
|/ |
| (_)
| \|/
| |
| / \
|
_|___""")
print(" ".join(hidden_word))
print("You have no more chances to choose letters. Tell me the password or you'll die!")
final_guess = input(prompt = " >").lower()
if final_guess.lower() == to_guess:
print(f"Great job! the password is {to_guess}")
else:
print("You failed")
print("""
_________
|/ |
| (_)
| \|/
| |
| / \
|
_|___""")
Rock-Paper-Scissor:
import random as rand
import time
import time
def rockpaperscissor():
play_score = 0
adv_score = 0
target=3
choice = ("rock","paper","scissor")
while play_score < target and adv_score < target:
print("""
""")
play_choice = input('rock, paper, or scissor? ')
adv_choice = rand.choice(choice)
if play_choice not in choice:
print("make a proper choice!")
continue
time.sleep(1)
print("""
""")
print(f"""you say {play_choice.lower()}, your adversary says {adv_choice.lower()}""")
print("""
""")
if play_choice.lower() == adv_choice:
print("This round is even!")
elif play_choice.lower() == "rock" and adv_choice == "paper":
print("you lose this round")
adv_score += 1
elif play_choice.lower() == "paper" and adv_choice == "scissor":
print("you lose this round")
adv_score += 1
elif play_choice.lower() == "scissor" and adv_choice == "rock":
print("you lose this round")
adv_score += 1
else:
print("you win this round")
play_score += 1
time.sleep(2)
print("""
""")
print(f"""the score is:
you: {play_score}
adversary: {adv_score}""")
time.sleep(1)
print("""
""")
if play_score > adv_score:
print("you win!!")
else:
print("you lose!!")
print("Want to try again?")
try_again = input("Y/N?")
if try_again.lower() in ("y","yes"):
rockpaperscissor()
else:
print("Goodbye!")
rockpaperscissor()
play_score = 0
adv_score = 0
target=3
choice = ("rock","paper","scissor")
while play_score < target and adv_score < target:
print("""
""")
play_choice = input('rock, paper, or scissor? ')
adv_choice = rand.choice(choice)
if play_choice not in choice:
print("make a proper choice!")
continue
time.sleep(1)
print("""
""")
print(f"""you say {play_choice.lower()}, your adversary says {adv_choice.lower()}""")
print("""
""")
if play_choice.lower() == adv_choice:
print("This round is even!")
elif play_choice.lower() == "rock" and adv_choice == "paper":
print("you lose this round")
adv_score += 1
elif play_choice.lower() == "paper" and adv_choice == "scissor":
print("you lose this round")
adv_score += 1
elif play_choice.lower() == "scissor" and adv_choice == "rock":
print("you lose this round")
adv_score += 1
else:
print("you win this round")
play_score += 1
time.sleep(2)
print("""
""")
print(f"""the score is:
you: {play_score}
adversary: {adv_score}""")
time.sleep(1)
print("""
""")
if play_score > adv_score:
print("you win!!")
else:
print("you lose!!")
print("Want to try again?")
try_again = input("Y/N?")
if try_again.lower() in ("y","yes"):
rockpaperscissor()
else:
print("Goodbye!")
rockpaperscissor()