-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
54 lines (46 loc) · 2.18 KB
/
app.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
from flask import Flask, render_template, request
from tictactoe import check_win, empty_cells, minimax
app = Flask(__name__)
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
current_player = -1 # Human starts
human_wins = 0
computer_wins = 0
@app.route('/')
def index():
return render_template('index.html', board=board, current_player=current_player,
human_wins=human_wins, computer_wins=computer_wins)
@app.route('/move/<int:x>/<int:y>', methods=['POST'])
def move(x, y):
global board, current_player, human_wins, computer_wins
if board[x][y] == 0:
board[x][y] = current_player
if check_win(current_player, board):
if current_player == -1:
human_wins += 1
message = "Human wins!"
else:
computer_wins += 1
message = "Computer wins!"
return render_template('index.html', board=board, current_player=current_player,
human_wins=human_wins, computer_wins=computer_wins, message=message)
current_player = -current_player
if current_player == 1:
best_move = minimax(board, current_player, len(empty_cells(board)))
board[best_move[0]][best_move[1]] = current_player
if check_win(current_player, board):
computer_wins += 1
message = "Computer wins!"
return render_template('index.html', board=board, current_player=current_player,
human_wins=human_wins, computer_wins=computer_wins, message=message)
current_player = -current_player
return render_template('index.html', board=board, current_player=current_player,
human_wins=human_wins, computer_wins=computer_wins)
@app.route('/reset', methods=['POST'])
def reset():
global board, current_player
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
current_player = -1
return render_template('index.html', board=board, current_player=current_player,
human_wins=human_wins, computer_wins=computer_wins)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000, debug=True)