Skip to content

Commit 9711cd6

Browse files
author
basantnema31
committed
docs: Expand Documentation with Docstrings and Type Hints for Core Functions (Fixes steam-bell-92#888)
1 parent ea1b8f6 commit 9711cd6

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@
2727
clock = pygame.time.Clock()
2828

2929
# ── Fonts ─────────────────────────────────────────────────
30-
def sf(size, bold=False):
30+
from typing import Optional, Tuple, List, Any
31+
32+
def sf(size: int, bold: bool = False) -> pygame.font.Font:
33+
"""Create a sans-serif font."""
3134
for n in ["Segoe UI", "Helvetica Neue", "Arial", "DejaVu Sans"]:
3235
try: return pygame.font.SysFont(n, size, bold=bold)
3336
except: pass
3437
return pygame.font.Font(None, size)
3538

36-
def mf(size, bold=False):
39+
def mf(size: int, bold: bool = False) -> pygame.font.Font:
40+
"""Create a monospace font."""
3741
for n in ["Consolas", "Courier New", "DejaVu Sans Mono"]:
3842
try: return pygame.font.SysFont(n, size, bold=bold)
3943
except: pass
@@ -61,7 +65,8 @@ def mf(size, bold=False):
6165
BX = (W - (3*CELL + 2*GAP)) // 2 # board left edge = 43
6266
BY = 270 # board top edge
6367

64-
def cell_rect(i):
68+
def cell_rect(i: int) -> pygame.Rect:
69+
"""Get the rectangle for a given cell index."""
6570
r, c = divmod(i, 3)
6671
return pygame.Rect(BX + c*(CELL+GAP), BY + r*(CELL+GAP), CELL, CELL)
6772

@@ -79,24 +84,28 @@ def cell_rect(i):
7984
BTN2 = pygame.Rect((W//2) + 12, BTN_Y, BW, BH)
8085

8186
# ── Helpers ───────────────────────────────────────────────
82-
def rrect(surf, color, rect, r=14, bw=0, bc=None):
87+
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:
88+
"""Draw a rounded rectangle."""
8389
pygame.draw.rect(surf, color, rect, border_radius=r)
8490
if bw and bc:
8591
pygame.draw.rect(surf, bc, rect, bw, border_radius=r)
8692

87-
def tc(surf, txt, font, color, cx, cy):
93+
def tc(surf: pygame.Surface, txt: str, font: pygame.font.Font, color: Tuple[int, int, int], cx: int, cy: int) -> None:
94+
"""Draw centered text."""
8895
s = font.render(txt, True, color)
8996
surf.blit(s, (cx - s.get_width()//2, cy - s.get_height()//2))
9097

91-
def check_winner():
98+
def check_winner() -> Tuple[Optional[str], Optional[List[int]]]:
99+
"""Check if there is a winner and return the winner and winning combination."""
92100
for a,b,c in WINS:
93101
if board[a] and board[a]==board[b]==board[c]:
94102
return board[a], [a,b,c]
95103
if "" not in board:
96104
return "D", []
97105
return None, None
98106

99-
def play(i):
107+
def play(i: int) -> None:
108+
"""Make a move at the given index."""
100109
global current, game_over
101110
if game_over or board[i]: return
102111
board[i] = current
@@ -108,11 +117,13 @@ def play(i):
108117
else:
109118
current = "O" if current == "X" else "X"
110119

111-
def new_game():
120+
def new_game() -> None:
121+
"""Start a new game."""
112122
global board, current, game_over
113123
board = [""] * 9; current = "X"; game_over = False
114124

115-
def reset_all():
125+
def reset_all() -> None:
126+
"""Reset the game scores and start a new game."""
116127
scores["X"] = scores["O"] = scores["D"] = 0
117128
new_game()
118129

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)