Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance docstrings in the Chess Game project and Update the Readme file #276

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions Caesar_Cipher/Caesar_cipher.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
class main:
class main:
"""
The Main class provides functionality for encrypting and decrypting strings using a simple substitution cipher.

Attributes:
key (dict): A dictionary mapping each letter of the alphabet to its corresponding encrypted letter.
blank_string (str): The string input by the user to be encrypted.
decrypted_string (str): The string that results from the encryption of the blank_string.
"""
def __init__(self,key:dict) -> None:
"""
Initializes the Main class with the given key for the substitution cipher.

Parameters:
key (dict): A dictionary mapping each letter of the alphabet to its corresponding encrypted letter.
"""
self.key = key

def get_input(self) -> None:
def get_input(self) -> None:
"""
Prompts the user to enter a string to be encrypted, validates the input, and converts it to lowercase.
Only alphabetical input is accepted, if the input is invalid, the user is prompted to try again.
"""
while True:
blank_string = str(input("Enter string to decrypt: "))
if blank_string.isalpha():
Expand All @@ -14,6 +32,12 @@ def get_input(self) -> None:
continue

def encrypt_string(self) -> str:
"""
Encrypts the user-provided string using the substitution cipher defined by the key.

Returns:
str:The encrypted string.
"""
output = ""
for c in self.blank_string:
for k,v in self.key.items():
Expand All @@ -25,6 +49,15 @@ def encrypt_string(self) -> str:
return(output)

def decrypt_string(self, string: str) -> str:
"""
Decrypts a given string using the substitution cipher defined by the key.

Parameters:
string (str): The string to be decrypted.

Returns:
str: The decrypted string, or the original blank_string if the input string is empty.
"""
output = ""
string = string.lower()
string = string.strip()
Expand Down
113 changes: 0 additions & 113 deletions Caterpillar_Game/Caterpillar.py

This file was deleted.

Binary file not shown.
88 changes: 88 additions & 0 deletions Caterpillar_Game/caterpillarGame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import turtle as t
from gameDesign import GameDesign

class CaterpillarGame:
def __init__(self):
self.design = GameDesign()
self.game_started = False
self.score = 0
self.caterpillar_speed = 2
self.caterpillar_length = 3

self.design.write_text('Press Space to start', (0, 0), ('Arial', 18, 'bold'))
self.bind_keys()

def bind_keys(self):
t.onkey(self.start_game, 'space')
t.onkey(self.move_up, 'Up')
t.onkey(self.move_right, 'Right')
t.onkey(self.move_down, 'Down')
t.onkey(self.move_left, 'Left')
t.listen()

def start_game(self):
if self.game_started:
return
self.game_started = True

self.score = 0
self.design.text_turtle.clear()

self.caterpillar_speed = 2
self.caterpillar_length = 3
self.design.caterpillar.shapesize(1, self.caterpillar_length, 1)
self.design.caterpillar.showturtle()

self.design.display_score(self.score)
self.design.place_leaf()

self.run_game_loop()

def run_game_loop(self):
while True:
self.design.caterpillar.forward(self.caterpillar_speed)
if self.design.caterpillar.distance(self.design.leaf) < 20:
self.design.place_leaf()
self.caterpillar_length += 1
self.design.caterpillar.shapesize(1, self.caterpillar_length, 1)
self.caterpillar_speed += 1
self.score += 10
self.design.display_score(self.score)
if self.outside_window():
self.game_over()
break

def outside_window(self):
left_wall = -t.window_width() / 2
right_wall = t.window_width() / 2
top_wall = t.window_height() / 2
bottom_wall = -t.window_height() / 2
(x, y) = self.design.caterpillar.pos()
outside = x < left_wall or x > right_wall or y > top_wall or y < bottom_wall
return outside

def game_over(self):
self.design.caterpillar.color('yellow')
self.design.leaf.color('yellow')
t.penup()
t.hideturtle()
t.write('Game Over!', align='center', font=('Arial', 30, 'normal'))
t.onkey(self.start_game, 'space')

def move_up(self):
self.design.caterpillar.setheading(90)

def move_down(self):
self.design.caterpillar.setheading(270)

def move_left(self):
self.design.caterpillar.setheading(180)

def move_right(self):
self.design.caterpillar.setheading(0)

if __name__ == '__main__':
game = CaterpillarGame()
t.mainloop()


34 changes: 34 additions & 0 deletions Caterpillar_Game/caterpillar_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import unittest
from caterpillarGame import CaterpillarGame


class TestCaterpillarGame(unittest.TestCase):

def setUp(self):
self.game = CaterpillarGame()

def test_initial_state(self):
self.assertFalse(self.game.game_started)
self.assertEqual(self.game.score, 0)
self.assertEqual(self.game.caterpillar_speed, 2)
self.assertEqual(self.game.caterpillar_length, 3)

def test_move_up(self):
self.game.move_up()
self.assertEqual(self.game.design.caterpillar.heading(), 90)

def test_move_down(self):
self.game.move_down()
self.assertEqual(self.game.design.caterpillar.heading(), 270)

def test_move_left(self):
self.game.move_left()
self.assertEqual(self.game.design.caterpillar.heading(), 180)

def test_move_right(self):
self.game.move_right()
self.assertEqual(self.game.design.caterpillar.heading(), 0)


if __name__ == '__main__':
unittest.main()
60 changes: 60 additions & 0 deletions Caterpillar_Game/gameDesign.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import turtle as t
import random as rd

class GameDesign:
def __init__(self):
self.setup_screen()
self.setup_caterpillar()
self.setup_leaf()
self.setup_text_turtle()
self.setup_score_turtle()

def setup_screen(self):
t.bgcolor('yellow')

def setup_caterpillar(self):
self.caterpillar = t.Turtle()
self.caterpillar.shape('square')
self.caterpillar.speed(0)
self.caterpillar.penup()
self.caterpillar.hideturtle()

def setup_leaf(self):
self.leaf = t.Turtle()
leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14))
t.register_shape('leaf', leaf_shape)
self.leaf.shape('leaf')
self.leaf.color('green')
self.leaf.penup()
self.leaf.hideturtle()
self.leaf.speed(0)

def setup_text_turtle(self):
self.text_turtle = t.Turtle()
self.text_turtle.hideturtle()

def setup_score_turtle(self):
self.score_turtle = t.Turtle()
self.score_turtle.hideturtle()
self.score_turtle.speed(0)

def write_text(self, message, position, font):
self.text_turtle.clear()
self.text_turtle.penup()
self.text_turtle.goto(position)
self.text_turtle.write(message, align='center', font=font)

def display_score(self, score):
self.score_turtle.clear()
self.score_turtle.penup()
x = (t.window_width() / 2) - 70
y = (t.window_height() / 2) - 70
self.score_turtle.setpos(x, y)
self.score_turtle.write(str(score), align='right', font=('Arial', 40, 'bold'))

def place_leaf(self):
self.leaf.hideturtle()
self.leaf.setx(rd.randint(-200, 200))
self.leaf.sety(rd.randint(-200, 200))
self.leaf.showturtle()

Loading