Skip to content

Commit cc470f8

Browse files
committed
Add multiple Tic-Tac-Toe game implementations and a clock difference calculator
- Implemented a Tic-Tac-Toe game with AI using Minimax algorithm (tic-tac-toe3.py). - Created a random two-player Tic-Tac-Toe game using NumPy (tic-tac-toe4.py). - Developed a two-player Tic-Tac-Toe game with full type hints and doctests (tic-tac-toe5.py). - Added a series of Tic-Tac-Toe games with score tracking (tic-tac-toe6.py). - Removed outdated Tic-Tac-Toe implementations (TicTacToe.py, tic-tac-toe.py, tic_tak_toe.py). - Introduced a Tkinter-based clock difference calculator with doctests (Timetable_Operations.py). - Enhanced leap year checker with doctests and improved input handling (to check leap year.py).
1 parent d3caf5d commit cc470f8

File tree

13 files changed

+754
-681
lines changed

13 files changed

+754
-681
lines changed

AI Game/Tic-Tac-Toe-AI/tictactoe.py

Lines changed: 0 additions & 126 deletions
This file was deleted.

TIC_TAC_TOE/index.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

Tic-Tac-Toe Games/tic-tac-toe1.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
Text-based Tic-Tac-Toe (2 players).
3+
4+
>>> check_winner([['X','X','X'],[' ',' ',' '],[' ',' ',' ']], 'X')
5+
True
6+
>>> check_winner([['X','O','X'],['O','O','O'],['X',' ',' ']], 'O')
7+
True
8+
>>> check_winner([['X','O','X'],['O','X','O'],['O','X','O']], 'X')
9+
False
10+
>>> is_full([['X','O','X'],['O','X','O'],['O','X','O']])
11+
True
12+
>>> is_full([['X',' ','X'],['O','X','O'],['O','X','O']])
13+
False
14+
"""
15+
16+
from typing import List
17+
18+
Board = List[List[str]]
19+
20+
21+
def print_board(board: Board) -> None:
22+
"""Print the Tic-Tac-Toe board."""
23+
for row in board:
24+
print(" | ".join(row))
25+
print("-" * 9)
26+
27+
28+
def check_winner(board: Board, player: str) -> bool:
29+
"""Return True if `player` has won."""
30+
for i in range(3):
31+
if all(board[i][j] == player for j in range(3)) or \
32+
all(board[j][i] == player for j in range(3)):
33+
return True
34+
if all(board[i][i] == player for i in range(3)) or \
35+
all(board[i][2 - i] == player for i in range(3)):
36+
return True
37+
return False
38+
39+
40+
def is_full(board: Board) -> bool:
41+
"""Return True if the board is full."""
42+
return all(cell != " " for row in board for cell in row)
43+
44+
45+
def get_valid_input(prompt: str) -> int:
46+
"""Get a valid integer input between 0 and 2."""
47+
while True:
48+
try:
49+
value = int(input(prompt))
50+
if 0 <= value < 3:
51+
return value
52+
print("Invalid input: Enter a number between 0 and 2.")
53+
except ValueError:
54+
print("Invalid input: Please enter an integer.")
55+
56+
57+
def main() -> None:
58+
"""Run the text-based Tic-Tac-Toe game."""
59+
board: Board = [[" " for _ in range(3)] for _ in range(3)]
60+
player = "X"
61+
62+
while True:
63+
print_board(board)
64+
print(f"Player {player}'s turn:")
65+
66+
row = get_valid_input("Enter row (0-2): ")
67+
col = get_valid_input("Enter col (0-2): ")
68+
69+
if board[row][col] == " ":
70+
board[row][col] = player
71+
72+
if check_winner(board, player):
73+
print_board(board)
74+
print(f"Player {player} wins!")
75+
break
76+
77+
if is_full(board):
78+
print_board(board)
79+
print("It's a draw!")
80+
break
81+
82+
player = "O" if player == "X" else "X"
83+
else:
84+
print("Invalid move: Spot taken. Try again.")
85+
86+
87+
if __name__ == "__main__":
88+
import doctest
89+
doctest.testmod()
90+
main()

0 commit comments

Comments
 (0)