def __init__(self, port=None): self.arrival_sem = threading.Semaphore(0) self.go_sem = [] self.socks = [0, 0, 0, 0] self.skips = [0, 0, 0, 0] self.done = False for i in xrange(4): self.go_sem.append(threading.Semaphore(0)) self.board = Board('original') self.game_logger = GameLogger(self.board, display=True) super(BasicGame, self).__init__()
def __init__(self, port=None): self.arrival_sem = threading.Semaphore(0) self.go_sem = [] self.socks = [0,0,0,0] self.skips = [0,0,0,0] self.done = False for i in xrange(4): self.go_sem.append(threading.Semaphore(0)) self.board = Board('original') self.game_logger = GameLogger(self.board, display=True) super(BasicGame, self).__init__()
class BasicGame(Game): def __init__(self, port=None): self.arrival_sem = threading.Semaphore(0) self.go_sem = [] self.socks = [0, 0, 0, 0] self.skips = [0, 0, 0, 0] self.done = False for i in xrange(4): self.go_sem.append(threading.Semaphore(0)) self.board = Board('original') self.game_logger = GameLogger(self.board, display=True) super(BasicGame, self).__init__() def player(self, player_id): l = threading.local() l.is_first_move = True with self.lock: l.sock, l.user = self.server.get_connection() self.socks[player_id] = l.sock m = Message(l.sock) if not m.match(Message.TYPE_CONTROL, "JOIN"): raise print "Got JOIN from %s (assigned player_id %d)" % (l.user, player_id) Message.serialized(l.sock, Message.TYPE_ID, player_id) Message.serialized(l.sock, Message.TYPE_BOARD, self.board) Message.serialized(l.sock, Message.TYPE_CONTROL, "WAIT") self.arrival_sem.release() while True: self.go_sem[player_id].acquire() if self.done: break if not self.skips[player_id]: if len(self.board.get_remaining_piece_ids(player_id)) == 0: move = Move.skip(player_id) else: try: Message.serialized(l.sock, Message.TYPE_CONTROL, "TURN") m = Message(l.sock, Message.TYPE_MOVE) move = m.message_object except IOError: move = Move.dropped_skip(player_id) if not self.board.is_valid_move(move): Message.serialized(l.sock, Message.TYPE_STATUS,\ [Bot.STATUS_SKIPPED, "Illegal Move"]) print 'ILLEGAL:', move print ' REASON:', self.board._valid_reason move = Move.illegal(player_id) else: l.is_first_move = False if move.is_skip(): self.skips[player_id] = True if sum(self.skips) == 4: self.game_logger.add_move(move) print "==================" print "4 skips. Game Over" for s in self.socks: Message.serialized(s, Message.TYPE_MOVE, move, suppress_err=True) self.done = True for s in self.go_sem: s.release() break self.game_logger.add_move(move) for s in self.socks: Message.serialized(s, Message.TYPE_MOVE, move, suppress_err=True) self.go_sem[(player_id + 1) % 4].release() Message.serialized(l.sock, Message.TYPE_STATUS,\ [Bot.STATUS_GAME_OVER, "This game has ended"]) def play_game(self): players = [0, 1, 2, 3] random.shuffle(players) t = [] t.append(threading.Thread(target=self.player, args=(players[0], ))) t.append(threading.Thread(target=self.player, args=(players[1], ))) t.append(threading.Thread(target=self.player, args=(players[2], ))) t.append(threading.Thread(target=self.player, args=(players[3], ))) for thread in t: thread.start() for thread in t: self.arrival_sem.acquire() self.go_sem[0].release() for thread in t: thread.join() print "THREAD DEAD"
class BasicGame(Game): def __init__(self, port=None): self.arrival_sem = threading.Semaphore(0) self.go_sem = [] self.socks = [0,0,0,0] self.skips = [0,0,0,0] self.done = False for i in xrange(4): self.go_sem.append(threading.Semaphore(0)) self.board = Board('original') self.game_logger = GameLogger(self.board, display=True) super(BasicGame, self).__init__() def player(self, player_id): l = threading.local() l.is_first_move = True with self.lock: l.sock, l.user = self.server.get_connection() self.socks[player_id] = l.sock m = Message(l.sock) if not m.match(Message.TYPE_CONTROL, "JOIN"): raise print "Got JOIN from %s (assigned player_id %d)" % (l.user, player_id) Message.serialized(l.sock, Message.TYPE_ID, player_id) Message.serialized(l.sock, Message.TYPE_BOARD, self.board) Message.serialized(l.sock, Message.TYPE_CONTROL, "WAIT") self.arrival_sem.release() while True: self.go_sem[player_id].acquire() if self.done: break if not self.skips[player_id]: if len(self.board.get_remaining_piece_ids(player_id)) == 0: move = Move.skip(player_id) else: try: Message.serialized(l.sock, Message.TYPE_CONTROL, "TURN") m = Message(l.sock, Message.TYPE_MOVE) move = m.message_object except IOError: move = Move.dropped_skip(player_id) if not self.board.is_valid_move(move): Message.serialized(l.sock, Message.TYPE_STATUS,\ [Bot.STATUS_SKIPPED, "Illegal Move"]) print 'ILLEGAL:',move print ' REASON:',self.board._valid_reason move = Move.illegal(player_id) else: l.is_first_move = False if move.is_skip(): self.skips[player_id] = True if sum(self.skips) == 4: self.game_logger.add_move(move) print "==================" print "4 skips. Game Over" for s in self.socks: Message.serialized(s, Message.TYPE_MOVE, move, suppress_err=True) self.done = True for s in self.go_sem: s.release() break self.game_logger.add_move(move) for s in self.socks: Message.serialized(s, Message.TYPE_MOVE, move, suppress_err=True) self.go_sem[(player_id+1) % 4].release() Message.serialized(l.sock, Message.TYPE_STATUS,\ [Bot.STATUS_GAME_OVER, "This game has ended"]) def play_game(self): players = [0,1,2,3] random.shuffle(players) t = [] t.append(threading.Thread(target=self.player, args=(players[0],))) t.append(threading.Thread(target=self.player, args=(players[1],))) t.append(threading.Thread(target=self.player, args=(players[2],))) t.append(threading.Thread(target=self.player, args=(players[3],))) for thread in t: thread.start() for thread in t: self.arrival_sem.acquire() self.go_sem[0].release() for thread in t: thread.join() print "THREAD DEAD"