11class NycharanXOGame :
2- def __init__ (self ):
3- self .game_dim = 3
2+ def __init__ (self , dim ):
3+ self .game_dim = dim
44 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" ]
66 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
812
913 def show_game (self ):
1014 for row in self .game_space :
@@ -13,60 +17,58 @@ def show_game(self):
1317
1418 def reset (self ):
1519 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 }
1720 self .player_turn = 0
1821
1922 def switch_player (self ):
2023 self .player_turn = 1 - self .player_turn
2124
2225 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 }
2427
2528 def count_score (line ):
2629 for player in self .players :
2730 score = 0
2831 counter = 0
2932
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 :
3235 counter += 1
3336 else :
3437 if counter > 2 :
3538 score += (counter - 2 ) + (counter - 3 )
36- counter = 0
39+ counter = 0
40+ if counter > 2 :
41+ score += (counter - 2 ) + (counter - 3 )
3742 self .players_score [player ] += score
3843
3944 for i in range (self .game_dim ):
4045 count_score (self .game_space [i ])
4146 count_score ([j [i ] for j in self .game_space ])
42-
4347 for k in range (2 * self .game_dim - 1 ):
4448 count_score ([self .game_space [i ][j ] for i in range (self .game_dim ) if 0 <= (j := k - i ) < self .game_dim ])
4549 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 ])
4650
4751 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 ] == " " :
5153 self .game_space [row ][col ] = self .players [self .player_turn ]
5254 self .calc_score ()
5355 return True
54- return 'Invalid input Try again!'
56+ print ("Invalid input. Try again!" )
57+ return False
5558
5659 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+
6264 if maximize :
6365 max_score = float ('-inf' )
6466 for i in range (self .game_dim ):
6567 for j in range (self .game_dim ):
6668 if self .game_space [i ][j ] == " " :
6769 self .game_space [i ][j ] = self .players [1 ]
6870 self .calc_score ()
69- score = minimax (False , alpha , beta , depth + 1 )
71+ score = minimax (False , alpha , beta , depth - 1 )
7072 self .game_space [i ][j ] = " "
7173 self .calc_score ()
7274 max_score = max (max_score , score )
@@ -75,20 +77,20 @@ def minimax(maximize = True, alpha = float('-inf'), beta = float('inf'), depth =
7577 break
7678 return max_score
7779 else :
78- max_score = float ('inf' )
80+ min_score = float ('inf' )
7981 for i in range (self .game_dim ):
8082 for j in range (self .game_dim ):
8183 if self .game_space [i ][j ] == " " :
8284 self .game_space [i ][j ] = self .players [0 ]
8385 self .calc_score ()
84- score = minimax (True , alpha , beta , depth + 1 )
86+ score = minimax (True , alpha , beta , depth - 1 )
8587 self .game_space [i ][j ] = " "
8688 self .calc_score ()
87- max_score = min (max_score , score )
89+ min_score = min (min_score , score )
8890 beta = min (beta , score )
8991 if beta <= alpha :
9092 break
91- return max_score
93+ return min_score
9294
9395 max_score = float ('-inf' )
9496 best_move = (- 1 , - 1 )
@@ -97,27 +99,31 @@ def minimax(maximize = True, alpha = float('-inf'), beta = float('inf'), depth =
9799 if self .game_space [i ][j ] == " " :
98100 self .game_space [i ][j ] = self .players [1 ]
99101 self .calc_score ()
100- score = minimax (False , float ('-inf' ), float ('inf' ), 0 )
102+ score = minimax (False , float ('-inf' ), float ('inf' ), self . minimax_depth )
101103 self .game_space [i ][j ] = " "
102104 self .calc_score ()
103105 if score > max_score :
104106 max_score = score
105107 best_move = (i , j )
106108 return best_move
107-
109+
108110# Game
109111if __name__ == "__main__" :
110- game = NycharanXOGame ()
112+ dim = int (input ("Enter the game dimension (e.g., 3 for 3x3): " ))
113+ game = NycharanXOGame (dim )
111114
112115 while True :
113116 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' ]} " )
115118
116119 if game .player_turn == 0 :
117120 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." )
121127 else :
122128 print ("O's turn." )
123129 row , col = game .best_approach ()
@@ -128,7 +134,7 @@ def minimax(maximize = True, alpha = float('-inf'), beta = float('inf'), depth =
128134 if " " not in [cell for row in game .game_space for cell in row ]:
129135 print ("Game is over!" )
130136 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"
133139 print (f"Winner is: { winner } " )
134140 break
0 commit comments