-
Notifications
You must be signed in to change notification settings - Fork 0
/
getmove.py
67 lines (58 loc) · 2.19 KB
/
getmove.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
#!/usr/bin/env python
#
# Copyright (C) 2010-2012 John Driscoll <johnoliverdriscoll@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# QTPy Move server
import web
class GetMoveHandler:
""" getmove request handler """
def POST(self):
""" Handle request to get a bot's move """
# Make sure responses aren't cached by client
web.header('Content-Type', 'text/plain')
web.header('Cache-Control', 'no-cache, no-store, no-transform')
web.header('Expires', '0')
web.header('Pragma', 'no-cache')
if web.input()['state']:
from state import State
from move import COLLAPSE
# Load the state of the game
game = State(web.input()['state'])
last_move = game.moves[-1]
response = ""
from bot import Bot
# Is it the bot's turn?
if not game.outcome and last_move and \
last_move.player == 1 and \
last_move.type != COLLAPSE:
# if response = self.debug(game): return response
response = Bot.play_move(game, int(web.input()['difficulty']))
# Is the game over?
if game.outcome: Bot.learn(game)
return response
def debug(self, game):
""" Plays moves in a predetermined game for testing purposes """
#import logging
#from state import State
#logging.debug(State.get_valid_moves(game))
#s = "15/59/59/05/36/36/03/78/78"
#s = "37/37/07/14"
s = "15/59/19"
s = s.split('/')
if len(game.moves) >= len(s): return False
m = s[len(game.moves)]
if m[0] == '0' and len(s) >= len(game.moves) + 2:
m += '/' + s[len(game.moves)+1]
return m