0% found this document useful (0 votes)
3 views24 pages

Python Project

Project using python on topic mimi games. Can be used in university entrance and school project
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views24 pages

Python Project

Project using python on topic mimi games. Can be used in university entrance and school project
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ST JUDE PUBLIC SCHOOL

KUTTANELLUR

CLASS 12
COMPUTER SCIENCE
PROJECT

Submitted by:
Agnus Elsa Binu
MINI GAMES
COMPUTER SCIENCE AGNUS ELSA BINU

24627403
2023-2024

COMPUTER SCIENCE

AGNUS ELSA BINU 24627403


25 - 10 - 2023
Index
SERIAL NO. CONTENTS PAGE NO.

1 Aim 5

2 Acknowledgement 6

3 Introduction 7

4 Platforms used 8

5 Input 9

6 Result
16

7 Bibliography 22
AIM

To create mini games ( hang-man


and
tic-tac-toe) using python
ACKNOWLEDGEMENT
In the accomplishment of this project
successfully, many people have bestowed upon
me their blessings and the heart-pledged
support this time. I am utilizing this time to
thank all the people who have been concerned
with the project.
Primarily I would thank god for being able to
complete this project with success. Then I
would like to thank my principal Sr. Philomina
Daisy and our computer science teacher Mrs.
Suja, whose valuable guidance has been the one
that helped me patch this project and make it
full success. Their suggestion and instruction
has served as the major contribution towards
completion of project.
Then I would like to thank my parents and
friends who have helped me with their valuable
suggestion and guidance, which has been
helpful in various phases of completion of this
project.
INTRODUCTION
In this project, we learn how to create two classic and
evergreen games – Hangman and Tic-Tac-Toe – in the
Python programming language. The two classic games
that many people enjoy will come into life using
programming. Developing these games will help us to
apply such fundamental Python constructs as
variables, loops, conditionals, and functions. We will
also develop an understanding of creating user-
friendly software interfaces and the logical complexity
needed to implement good gameplay.

Hangman is a word guessing game in which one player


attempts to guess the letters of the word that the
opponent has in mind. In the beginning, three
incorrect guesses are plotted as parts of the stick figure
in order to depict the condition of the game. When the
stick figure has no more parts, it means that the player
has lost, while the game can be won if the word is
revealed before all of the body parts are drawn.

The game Tic-Tac-Toe is played on the paper which is


divided into squares. Each square of the paper can be
filled by X or O. X and O take turns filling the 3 by 3
squares in this simple game which can turn to be
strategic given that the only aim is to get three same
marks in a row, in a column or diagonally.
PLATFORMS USED

Idle python (3.12)


Microsoft Notepad
INPUT

Game 1 : Hangman
import random

def get_word():
try:
with open('hangman.txt', 'r') as file:
words = file.read().splitlines()
return random.choice(words).upper()
except FileNotFoundError:
print("The file hangman.txt was not found.")
return None

def display_hangman(tries):
stages = [
"""
-----
| |
| O
| \\|/
| |
| / \\
-
""",
"""
-----
| |
| O
| \\|/
| |
| /
-
""",
"""
-----
| |
| O
| \\|/
| |
|
-
""",
"""
-----
| |
| O
| \\|
| |
|
-
""",
"""
-----
| |
| O
| |
| |
|
-
""",
"""
-----
| |
| O
|
|
|
-
""",
"""
-----
| |
|
|
|
|
-
"""
]
return stages[tries]

def play():
word = get_word()
if word is None:
return
word_completion = '_' * len(word)
guessed = False
guessed_letters = []
guessed_words = []
tries = 6

print("Let's play Hangman!")


print(display_hangman(tries))
print(word_completion)
print("\n")

while not guessed and tries > 0:


guess = input("Please guess a letter or word: ").upper()
if len(guess) == 1 and guess.isalpha():
if guess in guessed_letters:
print("You already guessed the letter", guess)
elif guess not in word:
print(guess, "is not in the word.")
tries -= 1
guessed_letters.append(guess)
else:
print("Good job,", guess, "is in the word!")
guessed_letters.append(guess)
word_as_list = list(word_completion)
indices = [i for i, letter in enumerate(word) if letter ==
guess]
for index in indices:
word_as_list[index] = guess
word_completion = ''.join(word_as_list)
if '_' not in word_completion:
guessed = True
elif len(guess) == len(word) and guess.isalpha():
if guess in guessed_words:
print("You already guessed the word", guess)
elif guess != word:
print(guess, "is not the word.")
tries -= 1
guessed_words.append(guess)
else:
guessed = True
word_completion = word
else:
print("Not a valid guess.")
print(display_hangman(tries))
print(word_completion)
print("\n")
if guessed:
print("Congrats, you guessed the word! You win!")
else:
print("Sorry, you ran out of tries. The word was " + word + ". Maybe
next time!")

if __name__ == "__main__":
play()
def print_board(board):
for i, row in enumerate(board):
print(" | ".join(row))
if i < 2:
print("-----------")

def check_win(board, player):


win_conditions = [
[board[0][0], board[0][1], board[0][2]],
[board[1][0], board[1][1], board[1][2]],
[board[2][0], board[2][1], board[2][2]],
[board[0][0], board[1][0], board[2][0]],
[board[0][1], board[1][1], board[2][1]],
[board[0][2], board[1][2], board[2][2]],
[board[0][0], board[1][1], board[2][2]],
[board[2][0], board[1][1], board[0][2]],
]
return [player, player, player] in win_conditions

def check_tie(board):
return all(cell != ' ' for row in board for cell in row)

def get_move(board, player):


while True:
try:
move = int(input(f"Player {player}, enter your move (1-9): "))
if move < 1 or move > 9:
raise ValueError
row = (move - 1) // 3
col = (move - 1) % 3
if board[row][col] == ' ':
return row, col
else:
print("Cell already taken. Choose another.")
except ValueError:
print("Invalid input. Enter a number between 1 and 9.")

def tic_tac_toe():
board = [[' ' for _ in range(3)] for _ in range(3)]
current_player = 'X'

for _ in range(9):
print_board(board)
row, col = get_move(board, current_player)
board[row][col] = current_player
if check_win(board, current_player):
print_board(board)
print(f"Player {current_player} wins!")
return
current_player = 'O' if current_player == 'X' else 'X'
if check_tie(board):
print_board(board)
print("It's a tie!")
return
if __name__ == "__main__":
tic_tac_toe()
RESULT

Game 1 : Hangman
Text file named 'hangman.txt' is
created
and text 'Rosslers' is added
Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

= RESTART: C:/Users/AJEL LORANCE/OneDrive/Desktop/hangman/hangman attempt 2.py


Let's play Hangman!

-----
| |
|
|
|
|
-

________

Please guess a letter or word: e


Good job, E is in the word!

-----
| |
|
|
|
|
-

_____E__

Please guess a letter or word: i


I is not in the word.

-----
| |
| O
|
|
|
-

_____E__

Please guess a letter or word: t


T is not in the word.

-----
| |
| O
| |
| |
|
-

_____E__

Please guess a letter or word: r


Good job, R is in the word!

-----
| |
| O
| |
| |
|
-

R____ER_

Please guess a letter or word: o


Good job, O is in the word!

-----
| |
| O
| |
| |
|
-

RO___ER_

Please guess a letter or word: k


K is not in the word.

-----
| |
| O
| \|
| |
|
-

RO___ER_

Please guess a letter or word: k


You already guessed the letter K

-----
| |
| O
| \|
| |
|
-

RO___ER_

Please guess a letter or word: s


Good job, S is in the word!

-----
| |
| O
| \|
| |
|
-

ROSS_ERS

Please guess a letter or word: p


P is not in the word.

-----
| |
| O
| \|/
| |
|
-

ROSS_ERS

Please guess a letter or word: l


Good job, L is in the word!

-----
| |
| O
| \|/
| |
|
-

ROSSLERS

Congrats, you guessed the word! You win!


Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

= RESTART: C:/Users/AJEL LORANCE/OneDrive/Desktop/hangman/tictactoe real.py


| |
-----------
| |
-----------
| |
Player X, enter your move (1-9): 3
| | X
-----------
| |
-----------
| |
Player O, enter your move (1-9): 2
| O | X
-----------
| |
-----------
| |
Player X, enter your move (1-9): 4
| O | X
-----------
X | |
-----------
| |
Player O, enter your move (1-9): 5
| O | X
-----------
X | O |
-----------
| |
Player X, enter your move (1-9): 8
| O | X
-----------
X | O |
-----------
| X |
Player O, enter your move (1-9): 1
O | O | X
-----------
X | O |
-----------
| X |
Player X, enter your move (1-9): 7
O | O | X
-----------
X | O |
-----------
X | X |
Player O, enter your move (1-9): 6
O | O | X
-----------
X | O | O
-----------
X | X |
Player X, enter your move (1-9): 9
O | O | X
-----------
X | O | O
-----------
X | X | X
Player X wins!

You might also like