-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
185 lines (136 loc) · 5.7 KB
/
game.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import sys
from board import Board
from player import Player
class Game:
def __init__(self):
self.my_board = Board()
self.player1=None
self.player2 = None
self.current_player = None
def start_game(self):
self.main_menu()
def switch_player(self):
if self.current_player == self.player1:
self.current_player = self.player2
elif self.current_player == self.player2:
self.current_player = self.player1
def is_draw(self):
empty_flag = False
for i in range(0, 3):
for j in range(0, 3):
if self.my_board.map[i][j] == " ":
empty_flag = True
if not empty_flag:
print("DRAW")
print(f"{self.current_player.player_name} with score = {self.current_player.player_score} \n ")
self.switch_player()
print(f"{self.current_player.player_name} with score = {self.current_player.player_score} \n ")
self.main_menu()
def winnig_action(self):
self.current_player.player_add_score()
print(f"winner is {self.current_player.player_name} with score = {self.current_player.player_score}")
self.main_menu()
def is_win(self):
"""column winner"""
for i in range(0, 2):
if self.my_board.map[0][i] == self.my_board.map[1][i] == self.my_board.map[2][i] != " ":
self.winnig_action()
"""row winner"""
for i in range(0,2):
if self.my_board.map[i][0] == self.my_board.map[i][1] == self.my_board.map[i][2] != " ":
self.winnig_action()
"""diagonal winner"""
if self.my_board.map[0][0] == self.my_board.map[1][1] == self.my_board.map[2][2] != " ":
self.winnig_action()
elif self.my_board.map[0][2] == self.my_board.map[1][1] == self.my_board.map[2][0] != " ":
self.winnig_action()
"""could be in separate class"""
def main_menu(self):
print("welcome to x_o game")
ret = False
option =None
while not ret:
option = str(input("1) enter start to start playing \n2) enter quit to leave\n"))
ret = self.check_main_menu_input(option)
if option == str("start"):
self.game()
elif option == str("quit"):
sys.exit(0)
def creat_players(self):
while True:
try:
name, sign = map(str, input("enter the name then space then sign of the first player\n").split())
self.player1 = Player(name, sign)
break
except ValueError:
# Catch the error when there aren't exactly two inputs
print("Error: Please enter both a name and a symbol (e.g., 'Alice X'). Try again.")
while True:
try:
name, sign = map(str, input("enter the name then space then sign of the second player\n").split())
self.player2 = Player(name, sign)
break
except ValueError:
# Catch the error when there aren't exactly two inputs
print("Error: Please enter both a name and a symbol (e.g., 'Alice X'). Try again.")
self.current_player = self.player1
def game(self):
self.my_board.clear_board()
self.creat_players()
self.my_board.show_board()
while True :
row=None
column = None
ret = False
while not ret:
print(f"{self.current_player.player_name} turn")
try:
# Get input and try to convert it to integers
row, column = map(int, input("Enter the row then space then column (e.g., '1 1').\n").split())
# Check if the row and column are valid within the board range
if not self.check_row_column(row, column):
print("Invalid row or column. Please try again.")
continue
# Check if the cell is already occupied
if not self.check_a_play(row, column):
print("The cell is already occupied. Please choose another.")
continue
# If both checks pass, we can proceed with the move
ret = True
except ValueError:
print("Invalid input. Please enter two integers separated by a space.")
except IndexError:
print("Out of bounds. Please ensure the row and column are within the board dimensions.")
self.my_board.modify_board(row, column,self.current_player.player_sign)
self.my_board.show_board()
self.is_win()
self.is_draw()
self.switch_player()
@staticmethod
def check_row_column(row, column) -> bool :
if row > 3 or row < 1:
print("invalid input")
print("try again")
return False
elif column > 3 or column < 1:
print("invalid input")
print("try again")
return False
else:
return True
@staticmethod
def check_main_menu_input(option : str) -> bool:
if (option != "start") and (option != "quit"):
print("invalid input")
print("try again")
return False
else:
return True
def check_a_play(self,row, column):
row=row-1
column=column-1
if self.my_board.map[row][column] != " ":
print("can't play here")
return False
else:
return True