Mini Projects
Mini Projects
Using Python
09.04.2023
─
Overview
For my mini project I have created a python code which consists of 5 mini games which can
entertain you. I have done this using replit
Goals
1. To create a mini project consisting mini games such as:
● Guess the number
● Rock, Paper, Scissors
● Wordle
● ConnectFour
● Tic Tac Toe
Case Study
Here I am going to steps involved in creating this game
1
● In this game the user chooses a option between rock,paper and scissors
● The computer also randomly chooses a option and then the winner is decided
● The code starts by asking the user for a choice.
● The code then checks to see if the input is 1, 2, or 3.
● If it is not one of those values, the code sets the choice_name variable to
‘Rock’ if choice == 1, ‘paper’ if choice == 2, and ‘scissor’ if choice == 3.
● The next part of the code asks the user for their computer turn.
● The code uses a random number generator to choose between 1, 2, and 3.
● This value is stored in comp_choice_name.
● Next, the code loops until comp_choice equals choice.
● In each loop iteration, comp_choice will be randomly chosen from 1-3 and
stored in comp_choice_name.
● Once comp_choice equals choice, this means that the computer has
chosen rock as its turn!
2
● Finally, it prints out both choices so that everyone can see what happened
(user choice is: Rock V/s paper; Computer choice is: Rock V/s scissor).
● The code will ask the user for a choice between rock, paper and scissors.
● Once the user enters their choice, the code will randomly choose one of
those options as the computer’s turn.
● The code then prints out the chosen option and the user’s choice.
● Wordle
● In this game, there is a list of words present, out of which our interpreter will choose
1 random word. The user first has to input their names and then, will be asked to
guess any alphabet. If the random word contains that alphabet, it will be shown as
the output(with correct placement) , else the program will ask you to guess another
alphabet. The user will be given 12 turns(which can be changed accordingly) to
guess the complete word.
Step 1: Guess a Word
● Connect Four
● It is a two-player connection board game, in which the players choose a color and
then take turns dropping colored discs into a seven-column, six-row vertically
suspended grid. The pieces fall straight down, occupying the lowest available space
within the column. The objective of the game is to be the first to form a horizontal,
vertical, or diagonal line of four of one’s own discs. Connect Four is a solved game.
1. Import the NumPy package as np. Then we will create a python function named
create_board( ).
2. We will is create another three functions
Winning Rules a
s follows:
Main Program
from gtn import guess_the_number
while True:
- Wordle (3)
- ConnectFour (4)
value = input(txt)
if value == "1":
guess_the_number(100)
rock_paper_scissors()
game = Wordle()
game.play()
game = ConnectFour()
game.play()
game = TicTacToe()
game.play()
break
5
def guess_the_number(x):
random_number = random.randint(0, x)
num_guesses = 0
while True:
num_guesses += 1
if guess == random_number:
break
print("Too low!")
else:
print("Too high!")
def rock_paper_scissors():
r = "rock"
6
p = "paper"
s = "scissors"
all_choices = [r,p,s]
print("Invalid choice")
return
computer = random.choice(all_choices)
if user == computer:
print("Tie")
print("You won!")
else:
print("You lost")
words = (
"awake",
"blush",
"focal",
7
"evade",
"serve",
"model",
"karma",
"grade",
"quiet",
class Wordle:
def __init__(self):
self.word = random.choice(words)
self.num_guesses = 0
self.guess_dict = {
0: [" "]*5,
1: [" "]*5,
2: [" "]*5,
3: [" "]*5,
4: [" "]*5,
5: [" "]*5,
def draw_board(self):
print(" | ".join(guess))
print("================")
def get_user_input(self):
while len(user_guess) != 5:
user_guess = user_guess.lower()
8
if char in self.word:
if char == self.word[idx]:
char = f"[green]{char}[/]"
else:
char = f"[yellow]{char}[/]"
self.guess_dict[self.num_guesses][idx] = char
self.num_guesses += 1
return user_guess
def play(self):
while True:
self.draw_board()
user_guess = self.get_user_input()
if user_guess == self.word:
self.draw_board()
break
if self.num_guesses > 5:
self.draw_board()
break
class ConnectFour:
9
self.height = height
self.width = width
def draw_board(self):
print(row)
return self.board[index]
def get_diagonals(self):
diagonals = []
diag1.append(self.board[self.height - i + j - 1][j])
diag2.append(self.board[i - j][j])
diagonals.append(diag1)
diagonals.append(diag2)
return diagonals
return False
row = self.height -1
row -= 1
self.board[row][col] = team
return True
def check_win(self):
# check rows
if self.get_row(row)[col:col+4] in four_in_row:
return True
# check cols
if self.get_column(col)[row:row+4] in four_in_row:
return True
# check diagonals
if diag[idx:idx+4] in four_in_row:
return True
return False
def play(self):
team = "0"
11
while True:
self.draw_board()
ok = self.insert_token(team, col)
print("column is full!")
ok = self.insert_token(team, col)
if self.check_win():
self.draw_board()
break
winners = ((0,1,2),(3,4,5),(6,7,8),(0,3,6),
(1,4,7),(2,5,8),(0,4,8),(2,4,6))
class TicTacToe:
def __init__(self):
def print_board(self):
12
if i == 2 or i == 5:
end = "\n-----------\n"
elif i == 8:
end = "\n"
print(val, end=end)
return True
return False
win = True
win = True
if self.board[idx] != player:
win = False
break
if win == True:
break
return win
# moved, win
if self.can_move(move):
self.board[move-1] = player
win = self.can_win(player)
def computer_move(self):
if self.can_move(move):
def play(self):
result = "Tie"
while True:
if self.board.count(player) + self.board.count(computer) ==
9:
break
self.print_board()
if not moved:
continue
if won:
break
_, won = self.computer_move()
if won:
break
self.print_board()
print(result)
OUTPUT/RESULT
15
16
17