-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.py
116 lines (93 loc) · 2.82 KB
/
tictactoe.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
Tic tac toe
"""
import os
class Game:
"""
Class game with initial parameters
"""
def __init__(self):
self.board = {1: " ", 2: " ", 3: " ",
4: " ", 5: " ", 6: " ",
7: " ", 8: " ", 9: " "}
self.mark = "X"
self.turns = 9
def __str__(self):
return "game is active"
@staticmethod
def end_game():
"""
Example method
"""
print(f"Game over")
exit(0)
def check_winner(current_mark, lboard):
"""
Check for the winner
"""
return ((lboard[1] == lboard[2] == lboard[3] == current_mark) or
(lboard[4] == lboard[5] == lboard[6] == current_mark) or
(lboard[7] == lboard[8] == lboard[9] == current_mark) or
(lboard[1] == lboard[4] == lboard[7] == current_mark) or
(lboard[2] == lboard[5] == lboard[8] == current_mark) or
(lboard[3] == lboard[6] == lboard[9] == current_mark) or
(lboard[1] == lboard[5] == lboard[9] == current_mark) or
(lboard[3] == lboard[5] == lboard[7] == current_mark))
def print_board(lboard):
"""
Print board
"""
os.system('clear')
print("hit 'q' to quit")
print(lboard[1] + "|" + lboard[2] + "|" + lboard[3])
print(lboard[4] + "|" + lboard[5] + "|" + lboard[6])
print(lboard[7] + "|" + lboard[8] + "|" + lboard[9])
def new_game(game):
"""
create new game
"""
game.turns = 9
del game.board
game.board = {1: " ", 2: " ", 3: " ",
4: " ", 5: " ", 6: " ",
7: " ", 8: " ", 9: " "}
def main():
"""
main function
"""
game = Game()
print_board(game.board)
while True:
if game.turns == 0:
print("There is no winner")
play_again = input("Play again? (y/n)")
if play_again.lower() == "n":
break
else:
new_game(game)
print_board(game.board)
position = input()
if position.lower() == "q":
break
try:
position = int(position)
except ValueError:
pass
if position in game.board and game.board[position] == " ":
game.turns -= 1
if game.mark == "X":
game.mark = "O"
else:
game.mark = "X"
game.board[position] = game.mark
print_board(game.board)
if check_winner(game.mark, game.board):
print(f"{game.mark} is the winner")
play_again = input("Play again? (y/n)")
if play_again.lower() == "n" or play_again.lower() == "q":
break
else:
new_game(game)
print_board(game.board)
if __name__ == '__main__':
main()