-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
150 lines (130 loc) · 4.94 KB
/
web.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
from flask import Flask, render_template, request, redirect
from chess import Board, WebInterface
from movehistory import MoveHistory
import re
from chess import Rook, Knight, Bishop, Queen
def main():
app = Flask(__name__)
ui = WebInterface()
board = Board()
board.start()
history = MoveHistory(10)
@app.route('/')
def root():
ui.next_link = '/newgame'
return render_template('homepage.html', ui=ui)
@app.route('/newgame', methods=['GET', 'POST'])
def newgame():
ui.wname, ui.bname = request.form['wname'], request.form['bname']
board.turn = 'black'
ui.errmsg = ''
return redirect('/play')
@app.route('/play', methods=['POST', 'GET'])
def play():
ui.board = board.display()
board.next_turn()
if ui.errmsg == None:
ui.errmsg = ''
if board.turn == 'white':
ui.inputlabel = f'{ui.wname}\'s turn:'
else:
ui.inputlabel = f'{ui.bname}\'s turn:'
ui.next_link = '/validation'
return render_template('game.html', ui=ui, variables=None)
@app.route('/error', methods=['POST', 'GET'])
def error():
player = ui.wname if board.turn == "white" else ui.bname
ui.board = board.display()
ui.errmsg = f'Invalid move for {player}'
ui.next_link = '/validation'
return render_template('game.html', ui=ui, variables=None)
@app.route('/promotion', methods=['POST', 'GET'])
def promotion():
print("promotion page")
# /promote path must always have coord in GET parameter so that
# board knows where the pawn to be promoted is
# Process pawn promotion
# Player will be prompted for another input if invalid
if request.method == 'POST':
piece = request.form['move'].lower()
pattern = r"\(\d, \d\)"
temp = request.form['variables']
start, end = re.findall(pattern, temp)
if piece in 'rkbq':
if piece == 'r':
PieceClass = Rook
elif piece == 'k':
PieceClass = Knight
elif piece == 'b':
PieceClass = Bishop
elif piece == 'q':
PieceClass = Queen
board.promotepawns(PieceClass)
return redirect('/play')
else:
ui.errmsg = 'Invalid input (r, k, b, or q only). Please try again.'
return redirect(f'/promotion?start={start}&end={end}')
elif request.method == 'GET':
start, end = request.args['start'], request.args['end']
ui.board = board.display()
ui.inputlabel = f'Promote pawn at {end} to (r, k, b, q): '
ui.btnlabel = 'Promote'
ui.next_link = '/promotion'
return render_template('game.html', ui=ui, variables=(start, end))
# def promotion():
# piece = request.args.get("promote", None)
# if piece is None:
# return render_template('game.html', ui=ui)
# else:
# if piece == 'Rook':
# PieceClass = Rook
# elif piece == 'Knight':
# PieceClass = Knight
# elif piece == 'Bishop':
# PieceClass = Bishop
# elif piece == 'Queen':
# PieceClass = Queen
# board.promotepawns(PieceClass)
# ui.board = board.display()
# ui.info = game.info
# return redirect("/play")
@app.route('/validation', methods=['POST', 'GET'])
def validation():
move = request.form['move']
ui.errmsg = ''
status = board.prompt(move, ui)
if status == 'error':
return redirect('/error')
else:
start, end = status
if board.movetype(start, end) == 'promotion':
board.update(start, end)
return redirect(f'/promotion?start={start}&end={end}')
else:
board.update(start, end)
opponent_colour = 'black' if board.turn == 'white' else 'white'
if board.checkmate_checking(opponent_colour):
ui.winner=board.turn
return redirect('/winner')
move = (start,end)
history.push(move)
print(history)
return redirect('/play')
@app.route('/winner')
def winner():
ui.winner = ui.wname if ui.winner == "white" else ui.bname
return render_template('winner.html',ui=ui, variables=None)
@app.route('/undo',methods=['POST','GET'])
def undo():
move = history.pop()
if move == None:
ui.errmsg = 'No more undo move'
board.next_turn()
return redirect('/play')
else:
board.undo(move)
ui.errmsg = ''
return redirect('/play')
app.run('0.0.0.0', debug=True)
# app.run(debug=True)
main()