1
1
class NycharanXOGame :
2
- def __init__ (self ):
3
- self .game_dim = 3
2
+ def __init__ (self , dim ):
3
+ self .game_dim = dim
4
4
self .game_space = [[" " for _ in range (self .game_dim )] for _ in range (self .game_dim )]
5
- self .players = ["X" , "O" ]
5
+ self .players = ["X" , "O" ]
6
6
self .player_turn = 0
7
- self .players_score = {self .players [0 ] : 0 , self .players [1 ] : 0 }
7
+ self .players_score = {self .players [0 ]: 0 , self .players [1 ]: 0 }
8
+ self .minimax_depth = 4
9
+
10
+ # depth comments
11
+ # depth = 3 lost in dim = 6
8
12
9
13
def show_game (self ):
10
14
for row in self .game_space :
@@ -13,60 +17,58 @@ def show_game(self):
13
17
14
18
def reset (self ):
15
19
self .game_space = [[" " for _ in range (self .game_dim )] for _ in range (self .game_dim )]
16
- self .players_score = {self .players [0 ] : 0 , self .players [1 ] : 0 }
17
20
self .player_turn = 0
18
21
19
22
def switch_player (self ):
20
23
self .player_turn = 1 - self .player_turn
21
24
22
25
def calc_score (self ):
23
- self .players_score = {self .players [0 ] : 0 , self .players [1 ] : 0 }
26
+ self .players_score = {self .players [0 ]: 0 , self .players [1 ]: 0 }
24
27
25
28
def count_score (line ):
26
29
for player in self .players :
27
30
score = 0
28
31
counter = 0
29
32
30
- for i in range ( len ( line ) - 1 ) :
31
- if line [ i ] == line [ i + 1 ] == player :
33
+ for cell in line :
34
+ if cell == player :
32
35
counter += 1
33
36
else :
34
37
if counter > 2 :
35
38
score += (counter - 2 ) + (counter - 3 )
36
- counter = 0
39
+ counter = 0
40
+ if counter > 2 :
41
+ score += (counter - 2 ) + (counter - 3 )
37
42
self .players_score [player ] += score
38
43
39
44
for i in range (self .game_dim ):
40
45
count_score (self .game_space [i ])
41
46
count_score ([j [i ] for j in self .game_space ])
42
-
43
47
for k in range (2 * self .game_dim - 1 ):
44
48
count_score ([self .game_space [i ][j ] for i in range (self .game_dim ) if 0 <= (j := k - i ) < self .game_dim ])
45
49
count_score ([self .game_space [i ][j ] for i in range (self .game_dim ) if 0 <= (j := i + k - self .game_dim + 1 ) < self .game_dim ])
46
50
47
51
def make_move (self , row , col ):
48
- def check_validation ():
49
- return 0 <= row < self .game_dim and 0 <= col < self .game_dim and self .game_space [row ][col ] == " "
50
- if check_validation ():
52
+ if 0 <= row < self .game_dim and 0 <= col < self .game_dim and self .game_space [row ][col ] == " " :
51
53
self .game_space [row ][col ] = self .players [self .player_turn ]
52
54
self .calc_score ()
53
55
return True
54
- return 'Invalid input Try again!'
56
+ print ("Invalid input. Try again!" )
57
+ return False
55
58
56
59
def best_approach (self ):
57
- def minimax (maximize = True , alpha = float ('-inf' ), beta = float ('inf' ), depth = 6 ):
58
- for row in self .game_space :
59
- if " " not in row :
60
- return self .players_score [self .players [0 ]] - self .players_score [self .players [1 ]]
61
-
60
+ def minimax (maximize , alpha , beta , depth ):
61
+ if " " not in [cell for row in self .game_space for cell in row ] or depth == 0 :
62
+ return self .players_score [self .players [1 ]] - self .players_score [self .players [0 ]]
63
+
62
64
if maximize :
63
65
max_score = float ('-inf' )
64
66
for i in range (self .game_dim ):
65
67
for j in range (self .game_dim ):
66
68
if self .game_space [i ][j ] == " " :
67
69
self .game_space [i ][j ] = self .players [1 ]
68
70
self .calc_score ()
69
- score = minimax (False , alpha , beta , depth + 1 )
71
+ score = minimax (False , alpha , beta , depth - 1 )
70
72
self .game_space [i ][j ] = " "
71
73
self .calc_score ()
72
74
max_score = max (max_score , score )
@@ -75,20 +77,20 @@ def minimax(maximize = True, alpha = float('-inf'), beta = float('inf'), depth =
75
77
break
76
78
return max_score
77
79
else :
78
- max_score = float ('inf' )
80
+ min_score = float ('inf' )
79
81
for i in range (self .game_dim ):
80
82
for j in range (self .game_dim ):
81
83
if self .game_space [i ][j ] == " " :
82
84
self .game_space [i ][j ] = self .players [0 ]
83
85
self .calc_score ()
84
- score = minimax (True , alpha , beta , depth + 1 )
86
+ score = minimax (True , alpha , beta , depth - 1 )
85
87
self .game_space [i ][j ] = " "
86
88
self .calc_score ()
87
- max_score = min (max_score , score )
89
+ min_score = min (min_score , score )
88
90
beta = min (beta , score )
89
91
if beta <= alpha :
90
92
break
91
- return max_score
93
+ return min_score
92
94
93
95
max_score = float ('-inf' )
94
96
best_move = (- 1 , - 1 )
@@ -97,27 +99,31 @@ def minimax(maximize = True, alpha = float('-inf'), beta = float('inf'), depth =
97
99
if self .game_space [i ][j ] == " " :
98
100
self .game_space [i ][j ] = self .players [1 ]
99
101
self .calc_score ()
100
- score = minimax (False , float ('-inf' ), float ('inf' ), 0 )
102
+ score = minimax (False , float ('-inf' ), float ('inf' ), self . minimax_depth )
101
103
self .game_space [i ][j ] = " "
102
104
self .calc_score ()
103
105
if score > max_score :
104
106
max_score = score
105
107
best_move = (i , j )
106
108
return best_move
107
-
109
+
108
110
# Game
109
111
if __name__ == "__main__" :
110
- game = NycharanXOGame ()
112
+ dim = int (input ("Enter the game dimension (e.g., 3 for 3x3): " ))
113
+ game = NycharanXOGame (dim )
111
114
112
115
while True :
113
116
game .show_game ()
114
- print (f"Scores: X = { game .players_score ["X" ]} , O = { game .players_score ["O" ]} " )
117
+ print (f"Scores: X = { game .players_score ['X' ]} , O = { game .players_score ['O' ]} " )
115
118
116
119
if game .player_turn == 0 :
117
120
print ("X's turn." )
118
- row , col = map (int , input (f"Enter row and column (0-{ game .game_dim - 1 } , space-separated): " ).split ())
119
- game .make_move (row , col )
120
- game .switch_player ()
121
+ try :
122
+ row , col = map (int , input (f"Enter row and column (0-{ game .game_dim - 1 } , space-separated): " ).split ())
123
+ if game .make_move (row , col ):
124
+ game .switch_player ()
125
+ except ValueError :
126
+ print ("Invalid input. Please enter two integers." )
121
127
else :
122
128
print ("O's turn." )
123
129
row , col = game .best_approach ()
@@ -128,7 +134,7 @@ def minimax(maximize = True, alpha = float('-inf'), beta = float('inf'), depth =
128
134
if " " not in [cell for row in game .game_space for cell in row ]:
129
135
print ("Game is over!" )
130
136
game .show_game ()
131
- print (f"Final Scores: X = { game .players_score ["X" ]} , O = { game .players_score ["O" ]} " )
132
- winner = "Tie" if game .players_score ["X" ] == game .players_score ["O" ] else "X" if game .players_score ["X" ] > game .players_score ["O" ] else "O"
137
+ print (f"Final Scores: X = { game .players_score ['X' ]} , O = { game .players_score ['O' ]} " )
138
+ winner = "Tie" if game .players_score ['X' ] == game .players_score ['O' ] else "X" if game .players_score ['X' ] > game .players_score ['O' ] else "O"
133
139
print (f"Winner is: { winner } " )
134
140
break
0 commit comments