-
Notifications
You must be signed in to change notification settings - Fork 0
/
afds
90 lines (66 loc) · 4.06 KB
/
afds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
| Tic | Tac | Toe |
|--|--|--|
| [![Empty](/img/blank.png)](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%200) | [![Empty](/img/blank.png)](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%201) | [![Empty](/img/blank.png)](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%202) |
| [![Empty](/img/blank.png)](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%203) | [![Empty](/img/blank.png)](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%204) | [![Empty](/img/blank.png)](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%205) |
| [![Empty](/img/blank.png)](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%206) | [![Empty](/img/blank.png)](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%207) | [![Empty](/img/blank.png)](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%208) |
import re
def update_board(board, move, player):
if board[move] == 'X' or board[move] == 'O':
return False
board[move] = player
return True
def check_winner(board):
winning_combinations = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], # Rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], # Columns
[0, 4, 8], [2, 4, 6] # Diagonals
]
for combo in winning_combinations:
if board[combo[0]] == board[combo[1]] == board[combo[2]] != ' ':
return board[combo[0]]
if ' ' not in board:
return 'Tie'
return None
def update_readme(board, status):
with open('README.md', 'r') as file:
content = file.read()
cell_image = lambda cell: '/img/blank.png' if cell == ' ' else '/img/o.png' if cell == 'O' else '/img/x.png'
cell_type = lambda cell: 'X' if cell == 'X' else 'O' if cell == 'O' else 'Empty' if cell == ' ' else cell
board_str = f"""| Tic | Tac | Toe |
|--|--|--|
| [![{cell_type(board[0])}]({cell_image(board[0])})](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%201) |
[![{cell_type(board[1])}]({cell_image(board[1])})](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%202) |
[![{cell_type(board[2])}]({cell_image(board[2])})](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%203) |
| [![{cell_type(board[3])}]({cell_image(board[3])})](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%204) |
[![{cell_type(board[4])}]({cell_image(board[4])})](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%205) |
[![{cell_type(board[5])}]({cell_image(board[5])})](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%206) |
| [![{cell_type(board[6])}]({cell_image(board[6])})](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%207) |
[![{cell_type(board[7])}]({cell_image(board[7])})](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%208) |
[![{cell_type(board[8])}]({cell_image(board[8])})](https://github.com/Coding4Hours/tic_tac_toe/issues/new?title=move%209) |
"""
new_content = re.sub(r'## Current Board\n\n.*?\n\n', f'## Current Board\n\n{board_str}\n\n', content, flags=re.DOTALL)
new_content = re.sub(r'## Game Status\n\n.*', f'## Game Status\n\n{status}', new_content)
with open('README.md', 'w') as file:
file.write(new_content)
def main(move):
with open('README.md', 'r') as file:
content = file.read()
board = re.findall(r'\| (.) \| (.) \| (.) \|', content)
board = [item for sublist in board for item in sublist]
current_player = 'X' if content.endswith("It's X's turn to play.") else 'O'
if update_board(board, move, current_player):
winner = check_winner(board)
if winner:
status = f'{winner} wins!' if winner != 'Tie' else "It's a tie!"
else:
next_player = 'O' if current_player == 'X' else 'X'
status = f"It's {next_player}'s turn to play."
update_readme(board, status)
return True
else:
return False
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
move = int(sys.argv[1]) - 1
main(move)