tinyarcade 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,333 @@
1
+ import os
2
+ import random
3
+ import string
4
+ import time
5
+
6
+
7
+ def clear_screen():
8
+ os.system("cls" if os.name == "nt" else "clear")
9
+
10
+
11
+ EASY_WORDS = [
12
+ "cat",
13
+ "dog",
14
+ "sun",
15
+ "book",
16
+ "apple",
17
+ "house",
18
+ "water",
19
+ "happy",
20
+ "green",
21
+ "music",
22
+ ]
23
+
24
+ NORMAL_WORDS = [
25
+ "python",
26
+ "github",
27
+ "package",
28
+ "terminal",
29
+ "keyboard",
30
+ "computer",
31
+ "hangman",
32
+ "library",
33
+ "cityu",
34
+ "student",
35
+ ]
36
+
37
+ HARD_WORDS = [
38
+ "algorithm",
39
+ "repository",
40
+ "debugging",
41
+ "encryption",
42
+ "framework",
43
+ "interface",
44
+ "javascript",
45
+ "database",
46
+ "variables",
47
+ "exception",
48
+ ]
49
+
50
+ DIFFICULTIES = {
51
+ "1": ("Easy", EASY_WORDS, 5),
52
+ "2": ("Normal", NORMAL_WORDS, 3),
53
+ "3": ("Hard", HARD_WORDS, 0),
54
+ }
55
+
56
+ MAX_WRONG_GUESSES = 6
57
+
58
+ HANGMAN_STAGES = [
59
+ """
60
+ +---+
61
+ | |
62
+ |
63
+ |
64
+ |
65
+ |
66
+ =========
67
+ """,
68
+ """
69
+ +---+
70
+ | |
71
+ O |
72
+ |
73
+ |
74
+ |
75
+ =========
76
+ """,
77
+ """
78
+ +---+
79
+ | |
80
+ O |
81
+ | |
82
+ |
83
+ |
84
+ =========
85
+ """,
86
+ """
87
+ +---+
88
+ | |
89
+ O |
90
+ /| |
91
+ |
92
+ |
93
+ =========
94
+ """,
95
+ """
96
+ +---+
97
+ | |
98
+ O |
99
+ /|\\ |
100
+ |
101
+ |
102
+ =========
103
+ """,
104
+ """
105
+ +---+
106
+ | |
107
+ O |
108
+ /|\\ |
109
+ / |
110
+ |
111
+ =========
112
+ """,
113
+ """
114
+ +---+
115
+ | |
116
+ O |
117
+ /|\\ |
118
+ / \\ |
119
+ |
120
+ =========
121
+ """,
122
+ ]
123
+
124
+
125
+ def choose_difficulty():
126
+ while True:
127
+ clear_screen()
128
+ print("🎮 === HANGMAN === 🎮")
129
+ print("Choose your difficulty:")
130
+
131
+ print("\n[1] 🟢 Easy - 5 hints")
132
+ print("[2] 🟡 Normal - 3 hints")
133
+ print("[3] 🔴 Hard - 0 hints")
134
+ print("[q] 🚪 Return to TinyArcade menu")
135
+
136
+ choice = input("\n🎯 Choose a difficulty: ").strip().lower()
137
+
138
+ if choice == "q":
139
+ return None
140
+
141
+ if choice in DIFFICULTIES:
142
+ clear_screen()
143
+ return DIFFICULTIES[choice]
144
+
145
+ print("❌ Invalid choice. Please enter 1, 2, 3, or q.")
146
+
147
+
148
+ def choose_word(words):
149
+ return random.choice(words)
150
+
151
+
152
+ def display_word(secret_word, guessed_letters):
153
+ return " ".join(
154
+ letter if letter in guessed_letters else "_"
155
+ for letter in secret_word
156
+ )
157
+
158
+
159
+ def show_hangman(wrong_guesses):
160
+ print(HANGMAN_STAGES[wrong_guesses])
161
+
162
+
163
+ def get_hint_letter(secret_word, guessed_letters):
164
+ alphabet = set(string.ascii_lowercase)
165
+ letters_not_in_word = alphabet - set(secret_word)
166
+ available_hints = letters_not_in_word - guessed_letters
167
+
168
+ if not available_hints:
169
+ return None
170
+
171
+ return random.choice(sorted(available_hints))
172
+
173
+
174
+ def ask_for_hint(hints_left, secret_word, guessed_letters):
175
+ if hints_left <= 0:
176
+ print("🚫 You have no hints left.")
177
+ return None
178
+
179
+ hint_letter = get_hint_letter(secret_word, guessed_letters)
180
+
181
+ if hint_letter is None:
182
+ print("🤔 No useful hint is available.")
183
+ return None
184
+
185
+ print(f"💡 HINT: The letter '{hint_letter}' is NOT in the word.")
186
+ return hint_letter
187
+
188
+
189
+ def play_round(difficulty_name, words, max_hints):
190
+ secret_word = choose_word(words)
191
+ guessed_letters = set()
192
+ wrong_guesses = 0
193
+ hints_left = max_hints
194
+
195
+ print(f"🎮 === HANGMAN: {difficulty_name.upper()} === 🎮")
196
+ print("🔤 Guess the secret word one letter at a time.")
197
+ print(f"❤️ You can make {MAX_WRONG_GUESSES} wrong guesses.")
198
+ print(f"💡 You have {max_hints} hint(s).")
199
+ print("💬 Type 'hint' to reveal one letter that is not in the word.")
200
+ print("🚪 Type 'quit' to return to the Hangman menu.")
201
+ input("\n⏎ Press ENTER to start the game...")
202
+
203
+ while wrong_guesses < MAX_WRONG_GUESSES:
204
+ clear_screen()
205
+ print(f"🔤 Word: {display_word(secret_word, guessed_letters)}")
206
+ print(f"❌ Wrong guesses: {wrong_guesses}/{MAX_WRONG_GUESSES}")
207
+ print(f"💡 Hints left: {hints_left}")
208
+ print(
209
+ "🔤 Guessed letters: "
210
+ + (", ".join(sorted(guessed_letters)) if guessed_letters else "None")
211
+ )
212
+
213
+ show_hangman(wrong_guesses)
214
+
215
+ guess = input(
216
+ "\n🎯 Enter a letter, 'hint', or 'quit': "
217
+ ).strip().lower()
218
+
219
+ if guess == "quit":
220
+ clear_screen()
221
+
222
+ print("🚪 QUIT GAME")
223
+ print("-----------------------------")
224
+ print(f"🤫 The word was: {secret_word.upper()}")
225
+
226
+ input("\n⏎ Press ENTER to continue...")
227
+
228
+ return
229
+
230
+ clear_screen()
231
+
232
+ if guess == "hint":
233
+ hint_letter = ask_for_hint(
234
+ hints_left,
235
+ secret_word,
236
+ guessed_letters
237
+ )
238
+
239
+ if hint_letter is not None:
240
+ hints_left -= 1
241
+ guessed_letters.add(hint_letter)
242
+
243
+ input("\n⏎ Press ENTER to continue...")
244
+ continue
245
+
246
+ if len(guess) != 1 or not guess.isalpha():
247
+ print(
248
+ "⚠️ Please enter one letter, 'hint', or 'quit'."
249
+ )
250
+ input("\n⏎ Press ENTER to continue...")
251
+ continue
252
+
253
+ if guess in guessed_letters:
254
+ print("🔁 You already guessed that letter.")
255
+ input("\n⏎ Press ENTER to continue...")
256
+ continue
257
+
258
+ guessed_letters.add(guess)
259
+
260
+ if guess in secret_word:
261
+ print("🎯 GUESS RESULT")
262
+ print("-----------------------------")
263
+ print(f"✅ '{guess}' is in the word!")
264
+
265
+ input("\n⏎ Press ENTER to continue...")
266
+ else:
267
+ wrong_guesses += 1
268
+
269
+ print("🎯 GUESS RESULT")
270
+ print("-----------------------------")
271
+ print(f"❌ '{guess}' is NOT in the word.")
272
+
273
+ print("\n🎨 Drawing the hangman...")
274
+ time.sleep(0.7)
275
+
276
+ show_hangman(wrong_guesses)
277
+ time.sleep(0.7)
278
+
279
+ input("\n⏎ Press ENTER to continue...")
280
+
281
+
282
+ if all(letter in guessed_letters for letter in secret_word):
283
+ clear_screen()
284
+ print("🎉🎉🎉 CONGRATULATIONS! 🎉🎉🎉")
285
+ print(f"🏆 You guessed the word: {secret_word.upper()}")
286
+ input("\n⏎ Press ENTER to continue...")
287
+ clear_screen()
288
+ return
289
+
290
+ clear_screen()
291
+ print("💀 GAME OVER 💀")
292
+ print(f"😢 The word was: {secret_word.upper()}")
293
+ show_hangman(MAX_WRONG_GUESSES)
294
+ input("\n⏎ Press ENTER to continue...")
295
+ clear_screen()
296
+
297
+
298
+ def ask_play_again():
299
+ while True:
300
+ print("🎮 What would you like to do?")
301
+ print("[1] 🔁 Play Hangman again")
302
+ print("[2] 🚪 Return to TinyArcade menu")
303
+
304
+ choice = input("\n🎯 Choose an option: ").strip()
305
+
306
+ if choice == "1":
307
+ return True
308
+
309
+ if choice == "2":
310
+ return False
311
+
312
+ print("❌ Invalid choice. Please enter 1 or 2.")
313
+
314
+
315
+ def play():
316
+ while True:
317
+ difficulty = choose_difficulty()
318
+
319
+ if difficulty is None:
320
+ break
321
+
322
+ difficulty_name, words, max_hints = difficulty
323
+
324
+ play_round(difficulty_name, words, max_hints)
325
+
326
+ clear_screen()
327
+
328
+ if not ask_play_again():
329
+ break
330
+
331
+
332
+ if __name__ == "__main__":
333
+ play()
@@ -0,0 +1,354 @@
1
+ import random
2
+ import os
3
+
4
+
5
+ # ANSI Colors
6
+ RED = "\033[91m"
7
+ YELLOW = "\033[93m"
8
+ GREEN = "\033[92m"
9
+ RESET = "\033[0m"
10
+
11
+
12
+ def clear_screen():
13
+ os.system("cls" if os.name == "nt" else "clear")
14
+
15
+
16
+ def show_rules():
17
+ print("=" * 40)
18
+ print("🎮 CODE BREAKER")
19
+ print("=" * 40)
20
+
21
+ print("\nRULES")
22
+ print("- Guess the secret code.")
23
+ print("- Digits = correct digits in the code.")
24
+ print("- Positions = correct digits in the correct position.")
25
+ print("- Type 'hint' to use a hint.")
26
+ print("- Type 'q' during a game to quit the round.")
27
+ print()
28
+
29
+ print("DIFFICULTY")
30
+ print("1. Easy (3 digits, 0 hints)")
31
+ print("2. Normal (4 digits, 1 hint)")
32
+ print("3. Hard (6 digits, 2 hints)")
33
+ print()
34
+
35
+ print("Type 'q' to quit.")
36
+ print()
37
+
38
+
39
+ def choose_difficulty():
40
+ while True:
41
+ clear_screen()
42
+ show_rules()
43
+
44
+ choice = input("Choice: ").strip().lower()
45
+
46
+ if choice == "1":
47
+ return 3, 0
48
+
49
+ if choice == "2":
50
+ return 4, 1
51
+
52
+ if choice == "3":
53
+ return 6, 2
54
+
55
+ if choice == "q":
56
+ return None
57
+
58
+ input("❌ Invalid choice. Press Enter...")
59
+
60
+
61
+ def analyze_guess(guess, secret):
62
+ positions = 0
63
+
64
+ for i in range(len(secret)):
65
+ if guess[i] == secret[i]:
66
+ positions += 1
67
+
68
+ digits = 0
69
+ temp_secret = list(secret)
70
+
71
+ for digit in guess:
72
+ if digit in temp_secret:
73
+ digits += 1
74
+ temp_secret.remove(digit)
75
+
76
+ return digits, positions
77
+
78
+
79
+ def display_history(history):
80
+ print("📜 Guess History\n")
81
+
82
+ if not history:
83
+ print("No guesses yet.")
84
+ return
85
+
86
+ print(
87
+ f"{'No.':<5}"
88
+ f"{'Guess':<10}"
89
+ f"{'Digits':<10}"
90
+ f"{'Positions'}"
91
+ )
92
+
93
+ print("-" * 35)
94
+
95
+ for i, item in enumerate(history, start=1):
96
+ print(
97
+ f"{i:<5}"
98
+ f"{item['guess']:<10}"
99
+ f"{item['digits']:<10}"
100
+ f"{item['positions']}"
101
+ )
102
+
103
+
104
+ def display_game(
105
+ code_length,
106
+ tries,
107
+ hints_left,
108
+ hint_messages,
109
+ history,
110
+ ):
111
+ print("🎮 CODE BREAKER\n")
112
+
113
+ print(f"Digits : {code_length}")
114
+ print(f"Tries : {tries}")
115
+ print(f"Hints Left : {hints_left}")
116
+
117
+ print()
118
+
119
+ if hint_messages:
120
+ print("💡 Hints")
121
+
122
+ for message in hint_messages:
123
+ print(message)
124
+
125
+ print()
126
+
127
+ display_history(history)
128
+
129
+ print()
130
+
131
+
132
+ def color_guess(guess, secret):
133
+ """
134
+ Used ONLY on final results screen.
135
+ Handles duplicate numbers correctly.
136
+ """
137
+ colors = [""] * len(guess)
138
+
139
+ secret_remaining = []
140
+ guess_remaining = []
141
+
142
+ # Pass 1 - exact matches
143
+ for i in range(len(guess)):
144
+ if guess[i] == secret[i]:
145
+ colors[i] = GREEN
146
+ else:
147
+ secret_remaining.append(secret[i])
148
+ guess_remaining.append((i, guess[i]))
149
+
150
+ # Pass 2 - wrong position matches
151
+ for index, digit in guess_remaining:
152
+ if digit in secret_remaining:
153
+ colors[index] = YELLOW
154
+ secret_remaining.remove(digit)
155
+ else:
156
+ colors[index] = RED
157
+
158
+ result = ""
159
+
160
+ for i in range(len(guess)):
161
+ result += colors[i] + guess[i] + RESET
162
+
163
+ return result
164
+
165
+
166
+ def show_final_history(history, secret):
167
+ clear_screen()
168
+
169
+ print("📜 FINAL GUESS HISTORY\n")
170
+
171
+ print("Legend:")
172
+ print(f"{GREEN}■{RESET} Correct digit and position")
173
+ print(f"{YELLOW}■{RESET} Correct digit, wrong position")
174
+ print(f"{RED}■{RESET} Not in code")
175
+
176
+ print()
177
+
178
+ print(
179
+ f"{'No.':<5}"
180
+ f"{'Guess':<12}"
181
+ f"{'Digits':<10}"
182
+ f"{'Positions'}"
183
+ )
184
+
185
+ print("-" * 40)
186
+
187
+ for i, item in enumerate(history, start=1):
188
+ coloured_guess = color_guess(
189
+ item["guess"],
190
+ secret,
191
+ )
192
+
193
+ # ANSI colours break width calculation,
194
+ # so manually add spacing.
195
+ spacing = max(
196
+ 1,
197
+ 12 - len(item["guess"]),
198
+ )
199
+
200
+ print(
201
+ f"{i:<5}"
202
+ f"{coloured_guess}"
203
+ f"{' ' * spacing}"
204
+ f"{item['digits']:<10}"
205
+ f"{item['positions']}"
206
+ )
207
+
208
+ print()
209
+
210
+ while True:
211
+ print("\nWhat would you like to do?")
212
+ print("[1] Play Code Breaker again")
213
+ print("[2] Return to Menu")
214
+
215
+ choice = input("\nChoose an option: ").strip()
216
+
217
+ if choice == "1":
218
+ return True
219
+
220
+ if choice == "2":
221
+ return False
222
+
223
+ print("❌ Invalid choice. Please enter 1 or 2.")
224
+
225
+
226
+ def play_round():
227
+ difficulty = choose_difficulty()
228
+
229
+ if difficulty is None:
230
+ return False
231
+
232
+ code_length, max_hints = difficulty
233
+
234
+ secret_code = "".join(
235
+ str(random.randint(0, 9))
236
+ for _ in range(code_length)
237
+ )
238
+
239
+ tries = 0
240
+ hints_left = max_hints
241
+
242
+ history = []
243
+ hint_messages = []
244
+
245
+ while True:
246
+ clear_screen()
247
+
248
+ display_game(
249
+ code_length,
250
+ tries,
251
+ hints_left,
252
+ hint_messages,
253
+ history,
254
+ )
255
+
256
+ guess = input(
257
+ "Enter guess ('hint' or 'q'): "
258
+ ).strip().lower()
259
+
260
+ # Quit current round
261
+ if guess == "q":
262
+ return True
263
+
264
+ # Hint
265
+ if guess == "hint":
266
+ if hints_left <= 0:
267
+ clear_screen()
268
+ print("❌ No hints remaining.")
269
+
270
+ input(
271
+ "\nPress Enter to continue..."
272
+ )
273
+
274
+ continue
275
+
276
+ position = random.randint(
277
+ 0,
278
+ code_length - 1,
279
+ )
280
+
281
+ hint_message = (
282
+ f"Digit #{position + 1} "
283
+ f"is {secret_code[position]}"
284
+ )
285
+
286
+ hint_messages.append(hint_message)
287
+ hints_left -= 1
288
+
289
+ continue
290
+
291
+ # Input validation
292
+ if (
293
+ len(guess) != code_length
294
+ or not guess.isdigit()
295
+ ):
296
+ clear_screen()
297
+
298
+ print(
299
+ f"❌ Please enter exactly "
300
+ f"{code_length} digits."
301
+ )
302
+
303
+ input(
304
+ "\nPress Enter to continue..."
305
+ )
306
+
307
+ continue
308
+
309
+ tries += 1
310
+
311
+ digits, positions = analyze_guess(
312
+ guess,
313
+ secret_code,
314
+ )
315
+
316
+ history.append(
317
+ {
318
+ "guess": guess,
319
+ "digits": digits,
320
+ "positions": positions,
321
+ }
322
+ )
323
+
324
+ # Win
325
+ if guess == secret_code:
326
+ clear_screen()
327
+
328
+ print("🎉 CONGRATULATIONS!\n")
329
+ print(f"✅ Secret Code : {secret_code}")
330
+ print(f"🔢 Total Tries : {tries}")
331
+
332
+ input(
333
+ "\nPress Enter to view results..."
334
+ )
335
+
336
+ return show_final_history(
337
+ history,
338
+ secret_code,
339
+ )
340
+
341
+
342
+ def play():
343
+ while True:
344
+ result = play_round()
345
+
346
+ if result is False:
347
+ break
348
+
349
+ clear_screen()
350
+ print("Returning to Menu...")
351
+
352
+
353
+ if __name__ == "__main__":
354
+ play()