Skip to content

Commit c6572b1

Browse files
Merge pull request steam-bell-92#1085 from basantnema31/fix/issue-888-docs
docs: Expand Documentation with Docstrings and Type Hints for Core Functions
2 parents 6bb43d9 + cbde826 commit c6572b1

3 files changed

Lines changed: 30 additions & 18 deletions

File tree

games/Tic-Tac-Toe/Tic-Tac-Toe.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ def main():
2929
clock = pygame.time.Clock()
3030

3131
# ── Fonts ─────────────────────────────────────────────────
32-
def sf(size, bold=False):
32+
from typing import Optional, Tuple, List, Any
33+
34+
def sf(size: int, bold: bool = False) -> pygame.font.Font:
35+
"""Create a sans-serif font."""
3336
for n in ["Segoe UI", "Helvetica Neue", "Arial", "DejaVu Sans"]:
34-
try:
35-
return pygame.font.SysFont(n, size, bold=bold)
36-
except pygame.error:
37-
pass
37+
try: return pygame.font.SysFont(n, size, bold=bold)
38+
except: pass
3839
return pygame.font.Font(None, size)
3940

40-
def mf(size, bold=False):
41+
def mf(size: int, bold: bool = False) -> pygame.font.Font:
42+
"""Create a monospace font."""
4143
for n in ["Consolas", "Courier New", "DejaVu Sans Mono"]:
42-
try:
43-
return pygame.font.SysFont(n, size, bold=bold)
44-
except pygame.error:
45-
pass
44+
try: return pygame.font.SysFont(n, size, bold=bold)
45+
except: pass
4646
return pygame.font.Font(None, size)
4747

4848
F_TAG = sf(14, bold=True)
@@ -67,7 +67,8 @@ def mf(size, bold=False):
6767
BX = (W - (3*CELL + 2*GAP)) // 2 # board left edge = 43
6868
BY = 270 # board top edge
6969

70-
def cell_rect(i):
70+
def cell_rect(i: int) -> pygame.Rect:
71+
"""Get the rectangle for a given cell index."""
7172
r, c = divmod(i, 3)
7273
return pygame.Rect(BX + c*(CELL+GAP), BY + r*(CELL+GAP), CELL, CELL)
7374

@@ -85,24 +86,28 @@ def cell_rect(i):
8586
BTN2 = pygame.Rect((W//2) + 12, BTN_Y, BW, BH)
8687

8788
# ── Helpers ───────────────────────────────────────────────
88-
def rrect(surf, color, rect, r=14, bw=0, bc=None):
89+
def rrect(surf: pygame.Surface, color: Tuple[int, int, int], rect: pygame.Rect, r: int = 14, bw: int = 0, bc: Optional[Tuple[int, int, int]] = None) -> None:
90+
"""Draw a rounded rectangle."""
8991
pygame.draw.rect(surf, color, rect, border_radius=r)
9092
if bw and bc:
9193
pygame.draw.rect(surf, bc, rect, bw, border_radius=r)
9294

93-
def tc(surf, txt, font, color, cx, cy):
95+
def tc(surf: pygame.Surface, txt: str, font: pygame.font.Font, color: Tuple[int, int, int], cx: int, cy: int) -> None:
96+
"""Draw centered text."""
9497
s = font.render(txt, True, color)
9598
surf.blit(s, (cx - s.get_width()//2, cy - s.get_height()//2))
9699

97-
def check_winner():
100+
def check_winner() -> Tuple[Optional[str], Optional[List[int]]]:
101+
"""Check if there is a winner and return the winner and winning combination."""
98102
for a,b,c in WINS:
99103
if board[a] and board[a]==board[b]==board[c]:
100104
return board[a], [a,b,c]
101105
if "" not in board:
102106
return "D", []
103107
return None, None
104108

105-
def play(i):
109+
def play(i: int) -> None:
110+
"""Make a move at the given index."""
106111
global current, game_over
107112
if game_over or board[i]: return
108113
board[i] = current

math/Armstrong-Number/Armstrong-Number.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from utils.validation import get_int
1010

1111
def is_armstrong_number(n: int) -> bool:
12+
"""Check if a number is an Armstrong number."""
1213
if n < 0:
1314
return False
1415
num_str = str(n)
@@ -17,6 +18,7 @@ def is_armstrong_number(n: int) -> bool:
1718
return total == n
1819

1920
def main() -> None:
21+
"""Run the Armstrong number checker CLI."""
2022
print("=" * 50)
2123
print("🔢 ARMSTRONG NUMBER CHECKER 🔢")
2224
print("=" * 50)

math/Collatz-Conjecture/Collatz-Conjecture.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,24 @@
1111
steps_cache = {1: 0}
1212

1313

14-
def collatz_next(n):
14+
from typing import List, Generator
15+
16+
def collatz_next(n: int) -> int:
17+
"""Calculate the next number in the Collatz sequence."""
1518
return n // 2 if n % 2 == 0 else 3 * n + 1
1619

1720

18-
def get_remaining_sequence(n):
21+
def get_remaining_sequence(n: int) -> List[int]:
22+
"""Calculate the remaining sequence for a given number until it reaches 1."""
1923
seq = []
2024
while n != 1:
2125
n = collatz_next(n)
2226
seq.append(n)
2327
return seq
2428

2529

26-
def collatz_sequence(start):
30+
def collatz_sequence(start: int) -> Generator[int, None, None]:
31+
"""Generate the Collatz sequence starting from the given number."""
2732
if start in steps_cache:
2833
n = start
2934
yield n

0 commit comments

Comments
 (0)