Skip to content

Commit 54fe477

Browse files
committed
delete 1 file and update 1 file
1 parent 60bd05a commit 54fe477

File tree

4 files changed

+143
-143
lines changed

4 files changed

+143
-143
lines changed

MiniMax/TicTacTok.py

Lines changed: 0 additions & 141 deletions
This file was deleted.

MiniMax/server.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from source import TicTacToe
2+
from flask import Flask, render_template, request, jsonify
3+
4+
# Initialize game
5+
game = TicTacToe()
6+
7+
app = Flask(__name__)
8+
9+
@app.route('/')
10+
@app.route('/')
11+
def index():
12+
# Prepare the board with indices
13+
board_with_indices = [
14+
[{"value": cell, "row": i, "col": j} for j, cell in enumerate(row)]
15+
for i, row in enumerate(game.board)
16+
]
17+
return render_template(
18+
'index.html',
19+
board=board_with_indices,
20+
player_turn=game.players[game.player_turn]
21+
)
22+
23+
@app.route('/move', methods=['POST'])
24+
def move():
25+
data = request.get_json()
26+
row, col = data['row'], data['col']
27+
28+
try:
29+
game.make_move(row, col)
30+
winner = game.check_winner()
31+
if not winner:
32+
game.switch_player()
33+
if game.player_turn == 1: # AI's turn
34+
ai_move = game.find_best_move()
35+
game.make_move(ai_move[0], ai_move[1])
36+
winner = game.check_winner()
37+
if not winner:
38+
game.switch_player()
39+
40+
return jsonify({
41+
'board': game.board,
42+
'winner': winner,
43+
'player_turn': game.players[game.player_turn]
44+
})
45+
except ValueError as e:
46+
return jsonify({'error': str(e)}), 400
47+
48+
@app.route('/restart', methods=['POST'])
49+
def restart():
50+
# Reset the game board and state
51+
global game
52+
game.reset() # Assuming there's a reset method in your game class
53+
return '', 204 # Respond with no content
54+
55+
if __name__ == '__main__':
56+
app.run(host='0.0.0.0', port=5000, debug=True, threaded=True)

MiniMax/source.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
class NycharanXOGame:
2+
def __init__(self):
3+
self.game_dim = 3
4+
self.game_space = [[None for _ in range(self.game_dim)] for _ in range(self.game_dim)]
5+
self.players= [0, 1]
6+
self.player_turn = 0
7+
8+
def reset(self):
9+
self.game_space = [[None for _ in range(self.game_dim)] for _ in range(self.game_dim)]
10+
self.player_turn = 0
11+
12+
# turn this into calc_score function coz in 'NycharanXOGame' winner gets the more score than other player
13+
14+
def check_winner(self):
15+
for i in range(self.game_dim):
16+
if all(self.game_space[i][j] == self.game_space[i][0] for j in range(1,self.game_dim)) and self.game_space[i][0] != None:
17+
return self.game_space[i][0]
18+
if all(self.game_space[j][i] == self.game_space[0][i] for j in range(1,self.game_dim)) and self.game_space[0][i] != None:
19+
return self.game_space[0][i]
20+
21+
if all(self.game_space[j][j] == self.game_space[0][0] for j in range(1,self.game_dim)) and self.game_space[0][0] != None:
22+
return self.game_space[0][0]
23+
if all(self.game_space[j][i] if i+j == (self.game_dim - 1) else -1 == self.game_space[0][(self.game_dim - 1)] for j in range(1,self.game_dim)) and self.game_space[0][(self.game_dim - 1)] != None:
24+
return self.game_space[0][(self.game_dim - 1)]
25+
26+
for row in self.game_space:
27+
if None in row:
28+
return None
29+
30+
return "Tie"
31+
32+
def apply_move(self, row, col):
33+
if self.game_space[row][col] != None:
34+
raise ValueError("square is taken before make another move!")
35+
36+
self.game_space[row][col] = self.players[self.player_turn]
37+
38+
def switch_player(self):
39+
self.player_turn = 1 - self.player_turn
40+
41+
def best_approach(self):
42+
def minimax(game_space, maximize):
43+
winner = self.check_winner()
44+
if winner == self.players[1]:
45+
return 1
46+
elif winner == self.players[0]:
47+
return -1
48+
elif winner == "Tie":
49+
return 0
50+
51+
if maximize:
52+
max_score = -1
53+
for i in range(self.game_dim):
54+
for j in range(self.game_dim):
55+
if game_space[i][j] == None:
56+
game_space[i][j] = self.players[1]
57+
score = minimax(game_space, False)
58+
game_space[i][j] = None
59+
max_score = max(max_score, score)
60+
return max_score
61+
else:
62+
max_score = -1
63+
for i in range(self.game_dim):
64+
for j in range(self.game_dim):
65+
if game_space[i][j] == None:
66+
game_space[i][j] = self.players[0]
67+
score = minimax(game_space, True)
68+
game_space[i][j] = None
69+
max_score = min(max_score, score)
70+
return max_score
71+
72+
max_score = -1
73+
max_move = (-1, -1)
74+
for i in range(self.game_dim):
75+
for j in range(self.game_dim):
76+
if self.game_space[i][j] == None:
77+
self.game_space[i][j] = self.players[1]
78+
score = minimax(self.game_space, False)
79+
self.game_space[i][j] = None
80+
if score > max_score:
81+
max_score = score
82+
max_move = (i, j)
83+
return max_move
84+
85+
game = NycharanXOGame()

MiniMax/templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ <h1>Tic Tac Toe</h1>
7070
{% endfor %}
7171
</div>
7272
<div class="message" id="message">
73-
Player {{ current_player }}'s turn.
73+
Player {{ player_turn }}'s turn.
7474
</div>
7575
<button id="restart">Restart</button>
7676
</div>
@@ -101,7 +101,7 @@ <h1>Tic Tac Toe</h1>
101101
message.textContent = data.winner === 'Draw' ? "It's a draw!" : `Player ${data.winner} wins!`;
102102
board.classList.add('disabled');
103103
} else {
104-
message.textContent = `Player ${data.current_player}'s turn.`;
104+
message.textContent = `Player ${data.player_turn}'s turn.`;
105105
}
106106
}
107107
});

0 commit comments

Comments
 (0)