+
+ return moves
+
+ def select_best(self, colour):
+
+ self.depth += 1
+ all_moves = {}
+ for p in self.board.pieces[colour]:
+ self.choice = p # Temporarily pick that piece
+ m = self.prioritise_moves(p)
+ if len(m) > 0:
+ all_moves.update({p : m[0]})
+
+ if len(all_moves.items()) <= 0:
+ return None
+
+
+ opts = all_moves.items()
+ opts.sort(key = lambda e : e[1][1], reverse = True)
+
+ if self.depth >= self.max_depth:
+ self.depth -= 1
+ return list(opts[0])
+
+ if self.recurse_for >= 0:
+ opts = opts[0:self.recurse_for]
+ #sys.stderr.write(sys.argv[0] + " : Before recurse, options are " + str(opts) + "\n")
+
+ # Take the best few moves, and recurse
+ for choice in opts[0:self.recurse_for]:
+ [xx,yy] = [choice[0].x, choice[0].y] # Remember position
+ [nx,ny] = choice[1][0] # Target
+ [choice[0].x, choice[0].y] = [nx, ny] # Set position
+ target = self.board.grid[nx][ny] # Remember piece in spot
+ self.board.grid[xx][yy] = None # Remove piece
+ self.board.grid[nx][ny] = choice[0] # Replace with moving piece
+
+ # Recurse
+ best_enemy_move = self.select_best(opponent(choice[0].colour))
+ choice[1][1] -= best_enemy_move[1][1] / float(self.depth + 1.0)
+
+ [choice[0].x, choice[0].y] = [xx, yy] # Restore position
+ self.board.grid[nx][ny] = target # Restore taken piece
+ self.board.grid[xx][yy] = choice[0] # Restore moved piece
+
+
+
+ opts.sort(key = lambda e : e[1][1], reverse = True)
+ #sys.stderr.write(sys.argv[0] + " : After recurse, options are " + str(opts) + "\n")
+
+ self.depth -= 1
+ return list(opts[0])
+
+
+
+ # Returns [x,y] of selected piece
+ def select(self):
+ #sys.stderr.write("Getting choice...")
+ self.choice = self.select_best(self.colour)[0]
+
+ #sys.stderr.write(" Done " + str(self.choice)+"\n")
+ return [self.choice.x, self.choice.y]
+
+ # Returns [x,y] of square to move selected piece into
+ def get_move(self):
+ #sys.stderr.write("Choice is " + str(self.choice) + "\n")
+ self.choice.selected_moves = self.choice.last_moves
+ moves = self.prioritise_moves(self.choice)
+ if len(moves) > 0:
+ return moves[0][0]
+ else:
+ return AgentRandom.get_move(self)
+
+# --- agent_bishop.py --- #
+import multiprocessing
+
+# Hacky alternative to using select for timing out players
+
+# WARNING: Do not wrap around HumanPlayer or things breakify
+# WARNING: Do not use in general or things breakify
+
+class Sleeper(multiprocessing.Process):
+ def __init__(self, timeout):
+ multiprocessing.Process.__init__(self)
+ self.timeout = timeout
+
+ def run(self):
+ time.sleep(self.timeout)
+
+
+class Worker(multiprocessing.Process):
+ def __init__(self, function, args, q):
+ multiprocessing.Process.__init__(self)
+ self.function = function
+ self.args = args
+ self.q = q
+
+ def run(self):
+ #print str(self) + " runs " + str(self.function) + " with args " + str(self.args)
+ #try:
+ self.q.put(self.function(*self.args))
+ #except IOError:
+ # pass
+
+
+
+def TimeoutFunction(function, args, timeout):
+ q = multiprocessing.Queue()
+ w = Worker(function, args, q)
+ s = Sleeper(timeout)
+ w.start()
+ s.start()
+ while True: # Busy loop of crappyness
+ if not w.is_alive():
+ s.terminate()
+ result = q.get()
+ w.join()
+ #print "TimeoutFunction gets " + str(result)
+ return result
+ elif not s.is_alive():
+ w.terminate()
+ s.join()
+ raise Exception("TIMEOUT")
+ time.sleep(0.1)
+
+
+
+# A player that wraps another player and times out its moves
+# Uses threads
+# A (crappy) alternative to the use of select()
+class TimeoutPlayer(Player):
+ def __init__(self, base_player, timeout):
+ Player.__init__(self, base_player.name, base_player.colour)
+ self.base_player = base_player
+ self.timeout = timeout
+
+ def select(self):
+ return TimeoutFunction(self.base_player.select, [], self.timeout)
+
+
+ def get_move(self):
+ return TimeoutFunction(self.base_player.get_move, [], self.timeout)
+
+ def update(self, result):
+ return TimeoutFunction(self.base_player.update, [result], self.timeout)
+
+ def quit(self, final_result):
+ return TimeoutFunction(self.base_player.quit, [final_result], self.timeout)
+# --- timeout_player.py --- #
+import socket
+import select
+
+network_timeout_start = -1.0 # Timeout in seconds to wait for the start of a message
+network_timeout_delay = 1.0 # Maximum time between two characters being received
+
+class NetworkPlayer(Player):
+ def __init__(self, colour, network, player):
+ Player.__init__(self, "@network:"+str(network.address), colour)
+ self.player = player
+ self.network = network
+
+ def __str__(self):
+ return "NetworkPlayer<"+str(self.colour)+","+str(self.player)+">"
+
+ def select(self):
+ #debug(str(self) + " select called")
+ if self.player != None:
+ s = self.player.select()
+ self.send_message(str(s[0]) + " " + str(s[1]))
+ else:
+ s = map(int, self.get_response().split(" "))
+ for p in game.players:
+ if p != self and isinstance(p, NetworkPlayer) and p.player == None:
+ p.network.send_message(str(s[0]) + " " + str(s[1]))
+ if s == [-1,-1]:
+ game.final_result = "network terminate"
+ game.stop()
+ return s
+
+ def send_message(self, message):
+ #debug(str(self) + " send_message(\""+str(message)+"\") called")
+ self.network.send_message(message)
+
+ def get_response(self):
+ #debug(str(self) + " get_response() called")
+ s = self.network.get_response()
+ #debug(str(self) + " get_response() returns \""+str(s)+"\"")
+ return s
+
+
+ def get_move(self):
+ #debug(str(self) + " get_move called")
+ if self.player != None:
+ s = self.player.get_move()
+ self.send_message(str(s[0]) + " " + str(s[1]))
+ else:
+ s = map(int, self.get_response().split(" "))
+ for p in game.players:
+ if p != self and isinstance(p, NetworkPlayer) and p.player == None:
+ p.network.send_message(str(s[0]) + " " + str(s[1]))
+
+ if s == [-1,-1]:
+ game.final_result = "network terminate"
+ game.stop()
+ return s
+
+ def update(self, result):
+ #debug(str(self) + " update(\""+str(result)+"\") called")
+ if self.network.server == True:
+ if self.player == None:
+ self.send_message(result)
+ elif self.player != None:
+ result = self.get_response()
+ if result == "-1 -1":
+ game.final_result = "network terminate"
+ game.stop()
+ return "-1 -1"
+ self.board.update(result, deselect=False)
+
+
+
+ if self.player != None:
+ result = self.player.update(result)
+
+ return result
+
+
+
+ def base_player(self):
+ if self.player == None:
+ return self
+ else:
+ return self.player.base_player()
+
+ def quit(self, result):
+ try:
+ self.send_message("-1 -1")
+ except:
+ pass
+
+class Network():
+ def __init__(self, address = (None,4562)):
+ self.socket = socket.socket()
+ self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ #self.socket.setblocking(0)
+ self.address = address
+ self.server = (address[0] == None)
+
+
+ self.connected = False
+
+ def connect(self):
+ #debug(str(self) + "Tries to connect")
+ self.connected = True
+ if self.address[0] == None:
+ self.host = "0.0.0.0" #socket.gethostname() # Breaks things???
+ self.socket.bind((self.host, self.address[1]))
+ self.socket.listen(5)
+
+ self.src, self.actual_address = self.socket.accept()
+
+ self.src.send("ok\n")
+ s = self.get_response()
+ if s == "QUIT":
+ self.src.close()
+ return
+ elif s != "ok":
+ self.src.close()
+ self.__init__(colour, (self.address[0], int(s)), baseplayer)
+ return
+
+ else:
+ time.sleep(0.3)
+ self.socket.connect(self.address)
+ self.src = self.socket
+ self.src.send("ok\n")
+ s = self.get_response()
+ if s == "QUIT":
+ self.src.close()
+ return
+ elif s != "ok":
+ self.src.close()
+ self.__init__(colour, (self.address[0], int(s)), baseplayer)
+ return
+
+
+
+ def __str__(self):
+ return "@network:"+str(self.address)
+
+ def get_response(self):
+
+ # Timeout the start of the message (first character)
+ if network_timeout_start > 0.0:
+ ready = select.select([self.src], [], [], network_timeout_start)[0]
+ else:
+ ready = [self.src]
+ if self.src in ready:
+ s = self.src.recv(1)
+ else:
+ raise Exception("UNRESPONSIVE")
+
+
+ while s[len(s)-1] != '\n':
+ # Timeout on each character in the message
+ if network_timeout_delay > 0.0:
+ ready = select.select([self.src], [], [], network_timeout_delay)[0]
+ else:
+ ready = [self.src]
+ if self.src in ready:
+ s += self.src.recv(1)
+ else:
+ raise Exception("UNRESPONSIVE")
+
+
+ return s.strip(" \r\n")
+
+ def send_message(self,s):
+ if network_timeout_start > 0.0:
+ ready = select.select([], [self.src], [], network_timeout_start)[1]
+ else:
+ ready = [self.src]
+
+ if self.src in ready:
+ self.src.send(s + "\n")
+ else:
+ raise Exception("UNRESPONSIVE")
+
+
+
+ def close(self):
+ self.src.shutdown()
+ self.src.close()
+# --- network.py --- #
+import threading
+
+# A thread that can be stopped!
+# Except it can only be stopped if it checks self.stopped() periodically
+# So it can sort of be stopped
+class StoppableThread(threading.Thread):
+ def __init__(self):
+ threading.Thread.__init__(self)
+ self._stop = threading.Event()
+
+ def stop(self):
+ self._stop.set()
+
+ def stopped(self):
+ return self._stop.isSet()# --- thread_util.py --- #
+log_files = []
+import datetime
+import urllib2
+
+class LogFile():
+ def __init__(self, log, name):
+ self.name = name
+ self.log = log
+ self.logged = []
+ self.log.write("# Log starts " + str(datetime.datetime.now()) + "\n")
+
+ def write(self, s):
+ now = datetime.datetime.now()
+ self.log.write(str(now) + " : " + s + "\n")
+ self.logged.append((now, s))
+
+ def setup(self, board, players):
+
+ for p in players:
+ self.log.write("# " + str(p.colour) + " : " + str(p.name) + "\n")
+
+ self.log.write("# Initial board\n")
+ for x in range(0, w):
+ for y in range(0, h):
+ if board.grid[x][y] != None:
+ self.log.write(str(board.grid[x][y]) + "\n")
+
+ self.log.write("# Start game\n")
+
+ def close(self):
+ self.log.write("# EOF\n")
+ if self.log != sys.stdout:
+ self.log.close()
+
+class ShortLog(LogFile):
+ def __init__(self, file_name):
+ if file_name == "":
+ self.log = sys.stdout
+ else:
+ self.log = open(file_name, "w", 0)
+ LogFile.__init__(self, self.log, "@"+file_name)
+ self.file_name = file_name
+ self.phase = 0
+
+ def write(self, s):
+ now = datetime.datetime.now()
+ self.logged.append((now, s))
+
+ if self.phase == 0:
+ if self.log != sys.stdout:
+ self.log.close()
+ self.log = open(self.file_name, "w", 0)
+ self.log.write("# Short log updated " + str(datetime.datetime.now()) + "\n")
+ LogFile.setup(self, game.board, game.players)
+
+ elif self.phase == 1:
+ for message in self.logged[len(self.logged)-2:]:
+ self.log.write(str(message[0]) + " : " + message[1] + "\n")
+
+ self.phase = (self.phase + 1) % 2
+
+ def close(self):
+ if self.phase == 1:
+ ending = self.logged[len(self.logged)-1]
+ self.log.write(str(ending[0]) + " : " + ending[1] + "\n")
+ self.log.write("# EOF\n")
+ if self.log != sys.stdout:
+ self.log.close()
+
+
+class HeadRequest(urllib2.Request):
+ def get_method(self):
+ return "HEAD"
+
+class HttpGetter(StoppableThread):
+ def __init__(self, address):
+ StoppableThread.__init__(self)
+ self.address = address
+ self.log = urllib2.urlopen(address)
+ self.lines = []
+ self.lock = threading.RLock() #lock for access of self.state
+ self.cond = threading.Condition() # conditional
+
+ def run(self):
+ while not self.stopped():
+ line = self.log.readline()
+ if line == "":
+ date_mod = datetime.datetime.strptime(self.log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
+ self.log.close()
+
+ next_log = urllib2.urlopen(HeadRequest(self.address))
+ date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
+ while date_new <= date_mod and not self.stopped():
+ next_log = urllib2.urlopen(HeadRequest(self.address))
+ date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
+ if self.stopped():
+ break
+
+ self.log = urllib2.urlopen(self.address)
+ line = self.log.readline()
+
+ self.cond.acquire()
+ self.lines.append(line)
+ self.cond.notifyAll()
+ self.cond.release()
+
+ #sys.stderr.write(" HttpGetter got \'" + str(line) + "\'\n")
+
+ self.log.close()
+
+
+
+
+
+class HttpReplay():
+ def __init__(self, address):
+ self.getter = HttpGetter(address)
+ self.getter.start()
+
+ def readline(self):
+ self.getter.cond.acquire()
+ while len(self.getter.lines) == 0:
+ self.getter.cond.wait()
+
+ result = self.getter.lines[0]
+ self.getter.lines = self.getter.lines[1:]
+ self.getter.cond.release()
+
+ return result
+
+
+ def close(self):
+ self.getter.stop()
+
+class FileReplay():
+ def __init__(self, filename):
+ self.f = open(filename, "r", 0)
+ self.filename = filename
+ self.mod = os.path.getmtime(filename)
+ self.count = 0
+
+ def readline(self):
+ line = self.f.readline()
+
+ while line == "":
+ mod2 = os.path.getmtime(self.filename)
+ if mod2 > self.mod:
+ #sys.stderr.write("File changed!\n")
+ self.mod = mod2
+ self.f.close()
+ self.f = open(self.filename, "r", 0)
+
+ new_line = self.f.readline()
+
+ if " ".join(new_line.split(" ")[0:3]) != "# Short log":
+ for i in range(self.count):
+ new_line = self.f.readline()
+ #sys.stderr.write("Read back " + str(i) + ": " + str(new_line) + "\n")
+ new_line = self.f.readline()
+ else:
+ self.count = 0
+
+ line = new_line
+
+ self.count += 1
+ return line
+
+ def close(self):
+ self.f.close()
+
+
+def log(s):
+ for l in log_files:
+ l.write(s)
+
+def debug(s):
+ sys.stderr.write("# DEBUG: " + s + "\n")
+
+
+def log_init(board, players):
+ for l in log_files:
+ l.setup(board, players)
+
+# --- log.py --- #
+
+
+
+
+
+# A thread that runs the game
+class GameThread(StoppableThread):
+ def __init__(self, board, players, server = True):
+ StoppableThread.__init__(self)
+ self.board = board
+ self.players = players
+ self.state = {"turn" : None} # The game state
+ self.error = 0 # Whether the thread exits with an error
+ self.lock = threading.RLock() #lock for access of self.state
+ self.cond = threading.Condition() # conditional for some reason, I forgot
+ self.final_result = ""
+ self.server = server
+
+
+
+
+
+
+ # Run the game (run in new thread with start(), run in current thread with run())
+ def run(self):
+ result = ""
+ while not self.stopped():
+
+ for p in self.players:
+ with self.lock:
+ self.state["turn"] = p.base_player()
+ try:
+ #if True:
+ [x,y] = p.select() # Player selects a square
+ if self.stopped():
+ #debug("Quitting in select")
+ break
+
+ if isinstance(p, NetworkPlayer):
+ if p.network.server == True:
+ result = self.board.select(x, y, colour = p.colour)
+ else:
+ result = None
+
+ else:
+ result = self.board.select(x, y, colour = p.colour)
+
+ result = p.update(result)
+ if self.stopped():
+ break
+ for p2 in self.players:
+ if p2 == p:
+ continue
+ p2.update(result) # Inform players of what happened
+ if self.stopped():
+ break
+
+ if self.stopped():
+ break
+
+
+ log(result)
+
+ target = self.board.grid[x][y]
+ if isinstance(graphics, GraphicsThread):
+ with graphics.lock:
+ graphics.state["moves"] = self.board.possible_moves(target)
+ graphics.state["select"] = target
+
+ time.sleep(turn_delay)
+
+
+ if len(self.board.possible_moves(target)) == 0:
+ #print "Piece cannot move"
+ target.deselect()
+ if isinstance(graphics, GraphicsThread):
+ with graphics.lock:
+ graphics.state["moves"] = None
+ graphics.state["select"] = None
+ graphics.state["dest"] = None
+ continue
+
+ #try:
+ [x2,y2] = p.get_move() # Player selects a destination
+ #except:
+ # self.stop()
+
+ if self.stopped():
+ #debug("Quitting in get_move")
+ break
+
+ if isinstance(p, NetworkPlayer):
+ if p.network.server == True:
+ result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
+ self.board.update_move(x, y, x2, y2)
+ else:
+ result = None
+
+ else:
+ result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
+ self.board.update_move(x, y, x2, y2)
+
+ result = p.update(result)
+ if self.stopped():
+ break
+ for p2 in self.players:
+ if p2 == p:
+ continue
+ p2.update(result) # Inform players of what happened
+ if self.stopped():
+ break
+
+ if self.stopped():
+ break
+
+
+
+ log(result)
+
+
+
+
+ if isinstance(graphics, GraphicsThread):
+ with graphics.lock:
+ graphics.state["moves"] = [[x2,y2]]
+
+ time.sleep(turn_delay)
+
+ if isinstance(graphics, GraphicsThread):
+ with graphics.lock:
+ graphics.state["select"] = None
+ graphics.state["dest"] = None
+ graphics.state["moves"] = None
+
+
+ end = self.board.end_condition()
+ if end != None:
+ with self.lock:
+ if end == "DRAW":
+ self.final_result = self.state["turn"].colour + " " + end
+ else:
+ self.final_result = end
+ self.stop()
+
+ if self.stopped():
+ break
+ except Exception,e:
+ #if False:
+ result = e.message
+ #sys.stderr.write(result + "\n")
+
+ self.stop()
+
+ with self.lock:
+ self.final_result = self.state["turn"].colour + " " + e.message
+ break