Below is the Hangman Game in Python, which is my second favorite project:
-----
import random
secret_word = random.choice(["apple", 'banana', 'orange', 'strawberry', 'blueberry', 'watermelon', 'melon', 'mouse', 'delicious', 'python'])
trials = 15
win = False
# two lists
wordlist = list(secret_word)
blanks = []
i = len(secret_word)
while i > 0:
blanks.append('-')
i -= 1
# guessing starts
while trials > 0:
if win:
break
else:
user_guess = input("Guess: ")
trials -= 1
times = 0 #which letter
for i in wordlist:
if user_guess == i:
blanks[times] = wordlist[times]
print('Yes! "' + user_guess + '" is in the word!')
times += 1
if user_guess not in wordlist:
print('Sorry, "' + user_guess + '" is not in the word! You have ' + str(trials) + " trials left.")
for j in blanks:
print(j, end='')
print("\n")
if '-' not in blanks:
print('Congrats! You guessed the word! It is "' + secret_word + '"!')
win = 1
if trials == 0:
print('Sorry, you already tried for 15 times. The word is "' + secret_word + '", please play the game again!')
-----
This is still considered as an easy one (actually it is extremely easy. Lol I spent 1 hour on this).
Another game, my favorite, is the Tic Tac Toe Game:
-----
class Board:def __init__(self):
self.spots = []
for i in range(9):
self.spots.append(None)
def checkwin(self):
for i in range (0, 9, 3):
if (self.spots[i]==self.spots[i+1] == self.spots[i+2]) and self.spots[i] != None:
return True
for i in range(3):
if (self.spots[i] == self.spots[i+3] == self.spots[i+6]) and self.spots[i] != None:
return True
if (self.spots[0] == self.spots[4] == self.spots[8]) and self.spots[0] != None:
return True
if (self.spots[2] == self.spots[4] == self.spots[6]) and self.spots[2] != None:
return True
return False
def printSpot(self, spotNum: int):
if self.spots[spotNum] == None:
return spotNum
else:
return self.spots[spotNum]
def print_board(self):
print(" ", self.printSpot(0), " | ", self.printSpot(1), " | ", self.printSpot(2))
print("-----|-----|-----")
print(" ", self.printSpot(3), " | ", self.printSpot(4), " | ", self.printSpot(5))
print("-----|-----|-----")
print(" ", self.printSpot(6), " | ", self.printSpot(7), " | ", self.printSpot(8))
def placesign(self, playerplace):
if current_player == player1:
self.spots[int(playerplace)] = player1sign
elif current_player == player2:
self.spots[int(playerplace)] = player2sign
# Start
my_board = Board()
player1 = input("Player 1, please type your name: ")
while True:
player1sign = input(player1 + ", please type your mark (O or X): ")
if str(player1sign) == "O" or str(player1sign) == "X":
break
else:
print("Sorry, that's not an 'O' or 'X'!")
print("Hello, " + player1 + "!")
player2 = input("Player 2, please type your name: ")
if player1sign == "O":
player2sign = "X"
else:
player2sign = "O"
print("Hello, " + player2 + "!")
current_player = player1
trials = 0
win = False
chosed = []
print("This is the board:")
my_board.print_board()
# Loop Starts
while trials < 9:
if current_player == player1:
while True:
playerplace = input(player1 + ", please make your move (1 - 9): ")
if playerplace in chosed:
print("Sorry, the place was already taken!")
elif playerplace not in chosed:
chosed.append(playerplace)
break
my_board.placesign(playerplace)
my_board.print_board()
else:
while True:
playerplace = input(player2 + ", please make your move (1 - 9): ")
if playerplace in chosed:
print("Sorry, the place was already taken!")
elif playerplace not in chosed:
chosed.append(playerplace)
break
my_board.placesign(playerplace)
my_board.print_board()
if my_board.checkwin() == True:
win = True
break
print(current_player)
if current_player == player1:
current_player = player2
else:
current_player = player1
trials += 1
if win == True:
print("Congratulations! " + current_player.title() + " wins!")
elif win == False:
print("This is a draw! Play the game again!")
This program is much longer and has included the use of class, loops, and methods. I actually felt so proud of myself as a beginner after seeing this works, with no errors when I play it with my mum. (Mum was like: "Is this what you learned for the whole week? There's nothing special about it!", thinking: "There are much better ones on the web!") Dad (Programming as his first job after his graduation) was kind of surprised for what I have done (and confused how I could make so many errors). Anyway, I like these games a lot and if you want to have a try, please try here:
Hangman:
Tic Tac Toe:
Despite these, we also have lots of happy memories at the id tech camp!