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,428 @@
1
+ import random
2
+ from shutil import move
3
+ import time
4
+ import os
5
+
6
+ def clear_screen():
7
+ os.system("cls" if os.name == "nt" else "clear")
8
+
9
+ # ANSI colours
10
+ GREEN = "\033[92m"
11
+ RED = "\033[91m"
12
+ YELLOW = "\033[93m"
13
+ CYAN = "\033[96m"
14
+ MAGENTA = "\033[95m"
15
+ RESET = "\033[0m"
16
+
17
+
18
+ WINNING_COMBINATIONS = [
19
+ (0, 1, 2),
20
+ (3, 4, 5),
21
+ (6, 7, 8),
22
+ (0, 3, 6),
23
+ (1, 4, 7),
24
+ (2, 5, 8),
25
+ (0, 4, 8),
26
+ (2, 4, 6),
27
+ ]
28
+
29
+
30
+ class TerminalTicTacToe:
31
+
32
+ def __init__(self):
33
+ self.reset_game_state()
34
+
35
+ def reset_game_state(self):
36
+ self.current_player = "X"
37
+ self.mode = None
38
+ self.difficulty = None
39
+ self.board = [str(i) for i in range(1, 10)]
40
+
41
+ self.last_move = None
42
+ self.winning_line = None
43
+
44
+ def format_cell(self, index):
45
+ value = self.board[index]
46
+
47
+ if self.winning_line and index in self.winning_line:
48
+ return f"{GREEN}{value}{RESET}"
49
+
50
+ if index == self.last_move:
51
+ return f"{YELLOW}{value}{RESET}"
52
+
53
+ return value
54
+
55
+
56
+ def display_board(self):
57
+
58
+ print("=" * 35)
59
+
60
+ if self.mode == "PVE":
61
+ print(
62
+ f"šŸŽ® Mode: {CYAN}PVE{RESET} "
63
+ f"šŸŽÆ Difficulty: {YELLOW}{self.difficulty}{RESET}"
64
+ )
65
+ elif self.mode == "PVP":
66
+ print(f"šŸŽ® Mode: {CYAN}PVP{RESET}")
67
+
68
+ print("=" * 35)
69
+
70
+ print()
71
+
72
+ print(
73
+ f" {self.format_cell(0)} | "
74
+ f"{self.format_cell(1)} | "
75
+ f"{self.format_cell(2)} "
76
+ )
77
+
78
+ print("---+---+---")
79
+
80
+ print(
81
+ f" {self.format_cell(3)} | "
82
+ f"{self.format_cell(4)} | "
83
+ f"{self.format_cell(5)} "
84
+ )
85
+
86
+ print("---+---+---")
87
+
88
+ print(
89
+ f" {self.format_cell(6)} | "
90
+ f"{self.format_cell(7)} | "
91
+ f"{self.format_cell(8)} "
92
+ )
93
+
94
+ print()
95
+
96
+ def post_game_menu(self):
97
+ while True:
98
+ print("\n[1] šŸ”„ Play Again")
99
+ print("[2] šŸŽ® Game Selection")
100
+ print("[q] 🚪 Exit")
101
+
102
+ choice = input("\nChoose: ").strip().lower()
103
+
104
+ if choice == "1":
105
+ return "again"
106
+
107
+ if choice == "2":
108
+ return "menu"
109
+
110
+ if choice == "q":
111
+ return "quit"
112
+
113
+ print("āŒ Invalid choice.")
114
+
115
+ def select_mode(self):
116
+ while True:
117
+ clear_screen()
118
+ print("šŸŽ® === TIC TAC TOE === šŸŽ®")
119
+ print("Choose a game mode:")
120
+
121
+ print("\n[1] šŸ§‘šŸ¤– PVE - Single Player")
122
+ print("[2] šŸ§‘šŸ§‘ PVP - Multi-Player")
123
+ print("[q] 🚪 Return to TinyArcade menu")
124
+
125
+ choice = input("\nšŸŽÆ Choose a mode: ").strip().lower()
126
+
127
+ if choice == "1":
128
+ self.mode = "PVE"
129
+
130
+ if not self.select_difficulty():
131
+ return False
132
+
133
+ return True
134
+
135
+ if choice == "2":
136
+ self.mode = "PVP"
137
+ return True
138
+
139
+ if choice == "q":
140
+ print("\n🚪 Returning to TinyArcade menu...")
141
+ return False
142
+
143
+ print("āŒ Invalid choice. Please enter 1, 2, or q.")
144
+
145
+ def select_difficulty(self):
146
+ while True:
147
+ clear_screen()
148
+ print("šŸŽÆ Choose the computer difficulty:")
149
+
150
+ print("\n[1] 🟢 Easy - Random moves")
151
+ print("[2] 🟔 Normal - Blocks and wins when possible")
152
+ print("[3] šŸ”“ Hard - Plays the best possible move")
153
+ print("[q] 🚪 Return to TinyArcade menu")
154
+
155
+ choice = input("\nšŸŽÆ Choose a difficulty: ").strip().lower()
156
+
157
+ if choice == "1":
158
+ self.difficulty = "Easy"
159
+ clear_screen()
160
+ return True
161
+
162
+ if choice == "2":
163
+ self.difficulty = "Normal"
164
+ clear_screen()
165
+ return True
166
+
167
+ if choice == "3":
168
+ self.difficulty = "Hard"
169
+ clear_screen()
170
+ return True
171
+
172
+ if choice == "q":
173
+ print("\n🚪 Returning to TinyArcade menu...")
174
+ return False
175
+
176
+ print("āŒ Invalid choice. Please enter 1, 2, 3, or q.")
177
+
178
+ def play(self):
179
+ if not self.select_mode():
180
+ return
181
+
182
+ clear_screen()
183
+
184
+ print(f"\nšŸŽ® Game Started! Mode: {self.mode}")
185
+
186
+ if self.mode == "PVE":
187
+ print(f"šŸ¤– Computer difficulty: {self.difficulty}")
188
+
189
+ time.sleep(1.5)
190
+
191
+ while True:
192
+ clear_screen()
193
+
194
+ self.display_board()
195
+ print(f"Current Turn: {self.current_player}")
196
+
197
+ if self.mode == "PVE" and self.current_player == "O":
198
+ print("šŸ¤– Computer is thinking...")
199
+ time.sleep(0.6)
200
+ move = self.get_computer_move()
201
+
202
+ self.board[move] = "O"
203
+ self.last_move = move
204
+
205
+ clear_screen()
206
+ self.display_board()
207
+
208
+ print(
209
+ f"{MAGENTA}šŸ¤– Computer played "
210
+ f"position {move + 1}{RESET}"
211
+ )
212
+
213
+ time.sleep(1.2)
214
+ if self.check_game_over():
215
+ break
216
+
217
+ self.current_player = "X"
218
+ continue
219
+
220
+ else:
221
+ move = self.get_player_move()
222
+
223
+ if move == "quit":
224
+ print("\n🚪 Returning to TinyArcade menu...")
225
+ return
226
+
227
+ self.board[move] = self.current_player
228
+ self.last_move = move
229
+
230
+ if self.check_game_over():
231
+ break
232
+
233
+ self.current_player = "O" if self.current_player == "X" else "X"
234
+
235
+ print("=" * 30)
236
+
237
+ action = self.post_game_menu()
238
+
239
+ if action == "again":
240
+
241
+ mode = self.mode
242
+ difficulty = self.difficulty
243
+
244
+ self.reset_game_state()
245
+
246
+ self.mode = mode
247
+ self.difficulty = difficulty
248
+
249
+ self.play()
250
+
251
+ elif action == "menu":
252
+
253
+ self.reset_game_state()
254
+
255
+ self.play()
256
+
257
+ return
258
+
259
+ def get_player_move(self):
260
+ while True:
261
+ try:
262
+ move_input = input(
263
+ f"Choose a position (1-9) to place "
264
+ f"'{self.current_player}' or [q] to quit: "
265
+ ).strip().lower()
266
+
267
+ if move_input == "q":
268
+ return "quit"
269
+
270
+ move = int(move_input) - 1
271
+
272
+ if move < 0 or move > 8:
273
+ print("āŒ Please enter a number between 1 and 9.")
274
+ continue
275
+
276
+ if self.board[move] in ["X", "O"]:
277
+ print("āŒ That position is already taken! Try again.")
278
+ continue
279
+
280
+ return move
281
+
282
+ except ValueError:
283
+ print("āŒ Invalid input! Please enter a valid number.")
284
+
285
+ def get_empty_cells(self):
286
+ return [
287
+ i for i in range(9)
288
+ if self.board[i] not in ["X", "O"]
289
+ ]
290
+
291
+ def get_computer_move(self):
292
+ if self.difficulty == "Easy":
293
+ return self.get_random_move()
294
+
295
+ if self.difficulty == "Normal":
296
+ return self.get_normal_move()
297
+
298
+ return self.get_best_move()
299
+
300
+ def get_random_move(self):
301
+ empty_cells = self.get_empty_cells()
302
+ return random.choice(empty_cells)
303
+
304
+ def find_winning_move(self, player):
305
+ empty_cells = self.get_empty_cells()
306
+
307
+ for cell in empty_cells:
308
+ test_board = self.board[:]
309
+ test_board[cell] = player
310
+
311
+ if self.check_winner(test_board, player):
312
+ return cell
313
+
314
+ return None
315
+
316
+ def get_normal_move(self):
317
+ winning_move = self.find_winning_move("O")
318
+
319
+ if winning_move is not None:
320
+ return winning_move
321
+
322
+ blocking_move = self.find_winning_move("X")
323
+
324
+ if blocking_move is not None:
325
+ return blocking_move
326
+
327
+ return self.get_random_move()
328
+
329
+ def get_best_move(self):
330
+ best_score = -2
331
+ best_move = None
332
+ empty_cells = self.get_empty_cells()
333
+
334
+ for cell in empty_cells:
335
+ self.board[cell] = "O"
336
+ score = self.minimax(False)
337
+ self.board[cell] = str(cell + 1)
338
+
339
+ if score > best_score:
340
+ best_score = score
341
+ best_move = cell
342
+
343
+ return best_move
344
+
345
+ def minimax(self, is_computer_turn):
346
+ if self.check_winner(self.board, "O"):
347
+ return 1
348
+
349
+ if self.check_winner(self.board, "X"):
350
+ return -1
351
+
352
+ empty_cells = self.get_empty_cells()
353
+
354
+ if not empty_cells:
355
+ return 0
356
+
357
+ if is_computer_turn:
358
+ best_score = -2
359
+
360
+ for cell in empty_cells:
361
+ self.board[cell] = "O"
362
+ score = self.minimax(False)
363
+ self.board[cell] = str(cell + 1)
364
+ best_score = max(best_score, score)
365
+
366
+ return best_score
367
+
368
+ best_score = 2
369
+
370
+ for cell in empty_cells:
371
+ self.board[cell] = "X"
372
+ score = self.minimax(True)
373
+ self.board[cell] = str(cell + 1)
374
+ best_score = min(best_score, score)
375
+
376
+ return best_score
377
+
378
+ def check_winner(self, board, player):
379
+ for combo in WINNING_COMBINATIONS:
380
+ if (
381
+ board[combo[0]] == player
382
+ and board[combo[1]] == player
383
+ and board[combo[2]] == player
384
+ ):
385
+ return True
386
+
387
+ return False
388
+
389
+ def check_game_over(self):
390
+ for combo in WINNING_COMBINATIONS:
391
+ if (
392
+ self.board[combo[0]]
393
+ == self.board[combo[1]]
394
+ == self.board[combo[2]]
395
+ ):
396
+
397
+ self.winning_line = combo
398
+
399
+ clear_screen()
400
+ self.display_board()
401
+
402
+ winner = self.board[combo[0]]
403
+
404
+ if self.mode == "PVE" and winner == "O":
405
+ print(f"{RED}šŸ¤– Game Over: Computer (O) wins!{RESET}")
406
+ else:
407
+ print(
408
+ f"{GREEN}šŸŽ‰ Game Over: Player {winner} wins!{RESET}"
409
+ )
410
+
411
+ return True
412
+
413
+ if all(cell in ["X", "O"] for cell in self.board):
414
+ clear_screen()
415
+ self.display_board()
416
+ print(f"{YELLOW}šŸ¤ Game Over: It's a Draw!{RESET}")
417
+ return True
418
+
419
+ return False
420
+
421
+
422
+ def play():
423
+ game = TerminalTicTacToe()
424
+ game.play()
425
+
426
+
427
+ if __name__ == "__main__":
428
+ play()
tinyarcade/menu.py ADDED
@@ -0,0 +1,81 @@
1
+ import os
2
+
3
+ from tinyarcade import game_num_guess
4
+ from tinyarcade import game_mastermind
5
+ from tinyarcade import game_tictactoe
6
+ from tinyarcade import game_colour_memorizing
7
+ from tinyarcade import game_rock_paper_scissors
8
+ from tinyarcade import game_hangman
9
+
10
+
11
+ def clear_screen():
12
+ os.system("cls" if os.name == "nt" else "clear")
13
+
14
+
15
+ GAMES = {
16
+ "1": ("Number Guess", game_num_guess.play),
17
+ "2": ("Mastermind", game_mastermind.play),
18
+ "3": ("Tic Tac Toe", game_tictactoe.play),
19
+ "4": ("Colour Memorizing", game_colour_memorizing.play),
20
+ "5": ("Rock, Paper, Scissors", game_rock_paper_scissors.play),
21
+ "6": ("Hangman", game_hangman.play),
22
+ }
23
+
24
+
25
+ def show_menu():
26
+ print("=" * 50)
27
+ print("šŸŽ® TINYARCADE šŸŽ®".center(50))
28
+ print("Select a Game".center(50))
29
+ print("=" * 50)
30
+ print()
31
+
32
+ for key, (name, _) in GAMES.items():
33
+ print(f" [{key}] {name}")
34
+
35
+ print(" [Q] Quit")
36
+
37
+ print()
38
+ print("=" * 50)
39
+
40
+
41
+ def show_goodbye():
42
+ clear_screen()
43
+
44
+ print("=" * 50)
45
+ print("THANK YOU FOR PLAYING".center(50))
46
+ print("TINYARCADE".center(50))
47
+ print("=" * 50)
48
+ print()
49
+ print("šŸŽ® Hope you had fun!".center(50))
50
+ print("šŸ† Keep practicing and challenge yourself!".center(50))
51
+ print("šŸ•¹ļø Thanks for supporting TinyArcade!".center(50))
52
+ print()
53
+ print("=" * 50)
54
+
55
+ input("\nPress Enter to exit...")
56
+
57
+
58
+ def run():
59
+ while True:
60
+ clear_screen()
61
+ show_menu()
62
+
63
+ choice = input("šŸ‘‰ Enter your choice: ").strip().lower()
64
+
65
+ if choice == "q":
66
+ show_goodbye()
67
+ break
68
+
69
+ if choice in GAMES:
70
+ _, game_function = GAMES[choice]
71
+
72
+ clear_screen()
73
+ game_function()
74
+
75
+ else:
76
+ print("\nāŒ Invalid choice. Please try again.")
77
+ input("\nPress Enter to continue...")
78
+
79
+
80
+ if __name__ == "__main__":
81
+ run()