def __init__(self, screen, team=None): self.player1 = Player(self, 1) self.player2 = Player(self, 2) self.running = False self.going_player = self.player2 self.board = Board(screen, self)
def start_new_game(self) -> Optional[Model]: """ Check if a new game can be started, and if so, start it. :return: the new Model object if a game was started, otherwise None """ if len(self.new_game_queue) >= 2: sid1, sid2 = self.new_game_queue[:2] s1 = self._sessions[sid1] s2 = self._sessions[sid2] self.new_game_queue = self.new_game_queue[2:] # Create associated players p1, p2 = Player(s1.username), Player(s2.username) game = Game([p1, p2]) model = Model(game) model.register_view(ServerView(sid1, self)) model.register_view(ServerView(sid2, self)) s1.start_game(model) s2.start_game(model) print("Started game between {} and {}".format(p1.name, p2.name)) s1.outb += self.pack({'type': 'new-game-request-reply', 'data': {'status': 'success', 'game-state': ViewGameState.from_game(game, 0), 'player-number': 0}}) s2.outb += self.pack({'type': 'new-game-request-reply', 'data': {'status': 'success', 'game-state': ViewGameState.from_game(game, 1), 'player-number': 1}}) return Model(game) else: return None
def test_board_check_all_directions(monkeypatch): def fake_sounds(self): return None WIN = 'window' player1 = Player('ZYGFRYYD', RED) player2 = Player('Radziwił', BLUE) checker = Checker(0, 0, RED, RED_BASE, BLUE_BASE, 50) monkeypatch.setattr('board.Board.get_jump_sounds', fake_sounds) board = Board(50, WIN, player1, player2) board.board = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, checker, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] assert board.check_all_directions(1, 1) == [(2, 1), (1, 0), (1, 2), (0, 1), (2, 0), (3, 3), (0, 0), (0, 2)] assert board.check_all_directions(0, 0) == [(1, 0), (0, 1)]
def test_board_get_all_jumps_from_square(monkeypatch): def fake_sounds(self): return None WIN = 'window' player1 = Player('ZYGFRYYD', RED) player2 = Player('Radziwił', BLUE) checker = Checker(0, 0, RED, RED_BASE, BLUE_BASE, 50) monkeypatch.setattr('board.Board.get_jump_sounds', fake_sounds) board = Board(50, WIN, player1, player2) board.board = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, checker, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] assert board.get_all_jumps_from_square(1, 1) == [(3, 3)] assert board.get_all_jumps_from_square(4, 3) == [(4, 5), (2, 5)]
def test_game_init(self): p1 = Player("p1") p2 = Player("p2") pl = [p1, p2] g = Game(pl) self.assertEqual(len(g.players), 2, "players list is 2") self.assertEqual(len(g.deck.cards), 52, "deck cards list is 52")
def test_board_cut_path(monkeypatch): def fake_sounds(self): return None WIN = 'window' player1 = Player('ZYGFRYYD', RED) player2 = Player('Radziwił', BLUE) checker = Checker(0, 0, RED, RED_BASE, BLUE_BASE, 50) monkeypatch.setattr('board.Board.get_jump_sounds', fake_sounds) board = Board(50, WIN, player1, player2) board.board = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, checker, 0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [0, 0, 0, 0, checker, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] assert board.cut_path((1, 1), (7, 5)) == [(1, 1), (3, 3), (5, 3), (7, 3), (7, 5)] assert board.cut_path((1, 1), (3, 3)) == [(1, 1), (3, 3)]
def test_start(self) -> None: players = [Player("a"), Player("b")] game = Game() game.start(players) assert set(game.players) == set(players) assert len(game.fields) == len(players)
def handle_login(): username = escape(request.form.get("username", str, None)) if not username: return {"error": "bad_request"}, 400 session['username'] = username session['uuid'] = game.get_uuid() if game.get_player_count() == 2: game.add_spectator(Spectator(username)) session['type'] = 'spectator' elif game.get_x_player() is None: game.set_x_player(Player(username, 'X')) session['type'] = 'player' session['player'] = 'X' else: game.set_o_player(Player(username, 'O')) session['type'] = 'player' session['player'] = 'O' game.set_status(1) socketio.emit('game', get_game(), broadcast=True, include_self=False, namespace='/', skip_sid=True) return { k: v for k, v in session.items() if k in ('username', 'type', 'player') }, 201
def thread_client(conn, player_num): #유저 한 명의 스레드 생성 global cur_player players[player_num - 1].start_game() #유저가 2명인 지 확인 conn.send(pickle.dumps(players[player_num - 1])) while True: try: data = pickle.loads(conn.recv(2048 * 3)) #데이터를 서버로부터 받음 players[player_num - 1] = data if not data: print("disconnected") break else: if player_num == 1: #reply는 상대방 객체를 뜻함 reply = players[1] else: reply = players[0] conn.sendall(pickle.dumps(reply)) #reply를 서버로 보냄 except: break print("lost connection") cur_player -= 1 if player_num == 1: players[player_num - 1] = Player('left', key_set, True) elif player_num == 2: players[player_num - 1] = Player('right', key_set, True) conn.close()
def test_that_construction_is_possible_with_enough_productions(self): right_player = Player({'production': {}}) left_player = Player({'production': {}}) player = Player({'production': {}}) player.with_neighbor(right_player, left_player) player.build_structure(self.structures['Stone Pit']) self.assertEqual(player.build_cost(self.structures['Baths']['cost']), 0)
def test_initial_score(self): left_player = Player({'production': {}}) right_player = Player({'production': {}}) player = Player({'production': {}}) player.with_neighbor(left_player, right_player) right_player.with_neighbor(player, left_player) left_player.with_neighbor(right_player, player) self.assertEqual(player.score(), 1)
def __init__(self, screen): # if (game_type == "hotseat"): self.player1 = Player(self, 1) self.player2 = Player(self, 2) self.running = False self.going_player = self.player2 self.board = Board(screen, self)
def test_attack(self) -> None: game = Game(10) player_a = Player("a") player_b = Player("b") game.start([player_a, player_b]) game.attack(player_b, "а1", CellState.WOUNDED) assert game.fields[player_b.id][0] == CellState.WOUNDED
def Task1(self) -> int: player1 = Player(self.data[0]) player2 = Player(self.data[1]) game = Combat(player1, player2) game.play() return game.calculateWinner()
def test_coins_score(self): left_player = Player({'production': {}}) right_player = Player({'production': {}}) player = Player({'production': {}}) player.with_neighbor(left_player, right_player) right_player.with_neighbor(player, left_player) left_player.with_neighbor(right_player, player) player.discard_structure() self.assertEqual(player.score(), 2)
def test_finish_empty_match(self): match = Match(max_players=2) match.add_player(Player("Player 1")) match.add_player(Player("Player 2")) self.assertEqual(match.status, Match.Status.RUN) match.remove_player(match.players[0]) self.assertEqual(match.status, Match.Status.RUN) match.remove_player(match.players[1]) self.assertEqual(match.status, Match.Status.FINISH)
def test_winner_is_board_solver(self): match = Match(max_players=2) player1 = Player("Player 1") player2 = Player("Player 2") match.add_player(player1) match.add_player(player2) match.get_player_board(player1).solve() match.update_status() self.assertEqual(match.status, Match.Status.FINISH) self.assertEqual(match.winner, player1)
def test_that_construction_is_possible_with_left_commerce(self): left_player = Player({'production': {}}) right_player = Player({'production': {}}) player = Player({'production': {}}) player.with_neighbor(left_player, right_player) right_player.with_neighbor(player, left_player) left_player.with_neighbor(right_player, player) left_player.build_structure(self.structures['Stone Pit']) self.assertEqual(player.build_cost(self.structures['Baths']['cost']), 2)
def test_that_construction_is_possible_with_chaining(self): left_player = Player({'production': {}}) right_player = Player({'production': {}}) player = Player({'production': {}}) player.with_neighbor(left_player, right_player) right_player.with_neighbor(player, left_player) left_player.with_neighbor(right_player, player) player.build_structure(self.structures['Theater']) self.assertEqual( player.build_cost(self.structures_2['Statue']['cost']), 0)
def test_wonder_score(self): left_player = Player({'production': {}}) right_player = Player({'production': {}}) player = Player(self.wonders['Gizah']['sides']['A']) player.with_neighbor(left_player, right_player) right_player.with_neighbor(player, left_player) left_player.with_neighbor(right_player, player) player.build_structure(self.structures['Stone Pit']) player.build_wonder_stage() self.assertEqual(player.score(), 4)
def new_game(self): # Создание новой игры game = Game() player1 = Player(player=True) # TODO: город добавить тут player1.add_city((17, 17), name='Персеполис') player2 = Player() player2.add_city((15, 20), name='Научград') self.add_obj_to_map(obj='Варвары', pos=ad.tile_to_world_adv((16, 22)), coords=(16, 22), player=player2) game.players = [player1, player2] config.game = game config.current_player = player1 ad.set_screen('main', self.manager)
def test_board_space_crosswise_down_right(monkeypatch): def fake_sounds(self): return None WIN = 'window' player1 = Player('ZYGFRYYD', RED) player2 = Player('Radziwił', BLUE) monkeypatch.setattr('board.Board.get_jump_sounds', fake_sounds) board = Board(50, WIN, player1, player2) assert board.crosswise_down_right(4, 1) == (5, 2) assert board.crosswise_down_right(13, 12) == None
def test_board_check_jump_down_left(monkeypatch): def fake_sounds(self): return None WIN = 'window' player1 = Player('ZYGFRYYD', RED) player2 = Player('Radziwił', BLUE) monkeypatch.setattr('board.Board.get_jump_sounds', fake_sounds) board = Board(50, WIN, player1, player2) assert board.check_jump_down_left(13, 12) == (15, 10) assert board.check_jump_down_left(15, 15) == None
def test_board_check_jump_up_right(monkeypatch): def fake_sounds(self): return None WIN = 'window' player1 = Player('ZYGFRYYD', RED) player2 = Player('Radziwił', BLUE) monkeypatch.setattr('board.Board.get_jump_sounds', fake_sounds) board = Board(50, WIN, player1, player2) assert board.check_jump_up_right(2, 3) == (0, 5) assert board.check_jump_up_right(1, 4) == None
def test_board_space_down(monkeypatch): def fake_sounds(self): return None WIN = 'window' player1 = Player('ZYGFRYYD', RED) player2 = Player('Radziwił', BLUE) monkeypatch.setattr('board.Board.get_jump_sounds', fake_sounds) board = Board(50, WIN, player1, player2) assert board.space_down(4, 4) == (5, 4) assert board.space_down(15, 15) == None
def setUp(self): self.game1 = Game() player1 = Player('John') player2 = Player('Paul') self.game1.players["1"] = player1 self.game1.players["2"] = player2 self.game1.kills.append(Kill('1', '2', '5')) self.game1.kills.append(Kill('1', '2', '6')) self.game1.kills.append(Kill('1', '2', '7')) self.game1.kills.append(Kill('1', '2', '8')) self.game1.kills.append(Kill('2', '1', '2')) self.game1.proccess_kills()
def test_board_get_checker(monkeypatch): def fake_sounds(self): return None WIN = 'window' player1 = Player('ZYGFRYYD', RED) player2 = Player('Radziwił', BLUE) checker = Checker(0, 0, RED, RED_BASE, BLUE_BASE, 50) monkeypatch.setattr('board.Board.get_jump_sounds', fake_sounds) board = Board(50, WIN, player1, player2) assert board.get_checker(0, 0) != 0 assert board.get_checker(8, 8) == 0
def do_game(self, agent1, agent2): g = Game.Game(GAME_SIZE, GAME_SIZE, Player.Player(0, 3, 3, GAME_SIZE, GAME_SIZE, decision_maker=agent1, movement=random.choice([[0, 1], [0, -1], [1, 0], [-1, 0]])), Player.Player(1, 11, 11, GAME_SIZE, GAME_SIZE, decision_maker=agent2, movement=random.choice([[0, 1], [0, -1], [1, 0], [-1, 0]]))) for i in range(self.game_length): g.update() if g.done: return g.get_winner() # Update later for victory by score return g.get_winner()
def reset(data): global G global players global player_not_ready for i in range(4): emit('player_not_ready', 'p'+str(i), broadcast=True) emit('game_action',{"action" : "reload"}, broadcast=True) socketio.sleep(1) players = [Player(0, "key1"), Player(1, "keeq"), Player(2, "keqw"), Player(3, "kee")] G = Game(players, cards[:]) player_not_ready = [1,1,1,1]
def test_iter(self) -> None: game = Game() game.start([Player("a"), Player("b")]) player_1 = game.players[0] player_2 = game.players[1] assert next(game) == (player_1, player_2) assert next(game) == (player_2, player_1) # and again assert next(game) == (player_1, player_2) assert next(game) == (player_2, player_1) # and again assert next(game) == (player_1, player_2)