def test_setWindowSize(boardLayout): game = Game() game.boardLayout = boardLayout size = game.boardLayout.boardSize * 30 game.setWindowSize() assert Window.size == (90, 108)
def main(): np.random.seed(seed=int(time.time())) parser = argparse.ArgumentParser(description='Tournament generator.') parser.add_argument('--class', type=str, action='append', dest='clazz', help='ML Class', required=True) args = parser.parse_args() clazzs = [getattr(sys.modules[__name__], clazz) for clazz in args.clazz] players = [clazz() for clazz in clazzs] for player in players: if isinstance(player, MLPlayer): player.load_model() game = Game(5, 4) status = None while status is None: next_player = game.next_player() position = game.position.serialize_out(asplayer=next_player) positions = [position] # [?, x, x, 3] shape is required moves = players[next_player - 1].make_game_moves(positions=positions) xy = moves[0][0] + 1, moves[0][1] + 1 game.makemove(player=next_player, position=xy) game.printboard() status, _ = game.game_status()
async def stop( self, ctx): # explicitly stop the game if not enough players are left response = "" if len( ctx.bot.spel.players ) < MIN_PLAYERS: # only possible when the amount of players falls below minimum # first, throw out all remaining players; deepcopy because removing stuff from lists f***s things up response += "\n".join([ ctx.bot.spel.remove_player(player.fullname) for player in deepcopy(ctx.bot.spel.players) ]) response += "\nHet spel is nu afgelopen.\n" \ f"Een nieuw spel begint als er opnieuw {MIN_PLAYERS} spelers zijn." ctx.bot.spel = Game() # print a final message and start a new game gc.collect( ) # explicitly collect garbage here, because we throw away all Game and Player objects here else: # if enough players are left to play, don't allow someone to just throw them all out response = f"Er zijn nog meer dan {MIN_PLAYERS - 1} spelers in het spel. " \ "Om te zorgen dat niet zomaar iedereen een actief spel kan afbreken, " \ f"kan het commando '{const.PREFIX}{const.STOP}' pas gebruikt worden " \ f"als er minder dan {MIN_PLAYERS} overblijven. " \ "Als je echt het spel wil stoppen, " \ f"zal/zullen nog {len(ctx.bot.spel.players) - (MIN_PLAYERS - 1)} " \ f"speler(s) het spel moeten verlaten." await ctx.channel.send(response ) # send the built up response to the channel
def PlayGames(player1, player2, numgames): victories1 = 0 victories2 = 0 for _ in range(numgames): game = Game(n_players=2, dice_number=4, dice_value=3, column_range=[2, 6], offset=2, initial_height=1) is_over = False who_won = None number_of_moves = 0 current_player = game.player_turn while not is_over: moves = game.available_moves() if game.is_player_busted(moves): if current_player == 1: current_player = 2 else: current_player = 1 continue else: if game.player_turn == 1: chosen_play = player1.get_action(game) else: chosen_play = player2.get_action(game) if chosen_play == 'n': if current_player == 1: current_player = 2 else: current_player = 1 #print('Chose: ', chosen_play) #game.print_board() game.play(chosen_play) #game.print_board() number_of_moves += 1 #print() who_won, is_over = game.is_finished() if number_of_moves >= 200: is_over = True who_won = -1 #print('No Winner!') if who_won == 1: victories1 += 1 if who_won == 2: victories2 += 1 #print(victories1, victories2) #print('Player 1: ', victories1 / (victories1 + victories2)) #print('Player 2: ', victories2 / (victories1 + victories2)) if victories1 + victories2 == 0: return (0, 0) p1victoryrate = victories1 / (victories1 + victories2) p2victoryrate = victories2 / (victories1 + victories2) return (p1victoryrate, p2victoryrate)
def update_active(self): """ :return: """ while len(self.games) < self.count and len( self.active_games) < self.active_size: game = Game(5, 4) self.games.append(game) self.statuses.append(None) self.active_games.append(len(self.games) - 1)
def update_active(self): """ :return: """ while len(self.games) < self.count and len( self.active_games) < self.active_size: game = Game(5, 4) self.games.append(game) self.statuses.append(None) players = random.sample(range(1, self.size), 1) self.active_games.append((len(self.games) - 1, 0, players[0]))
def play_game_and_give_action_pairs(player1, player2, max_moves=200): """ Plays a game of Can't-Stop between the given players Returns (-1, -1) if max_moves has been exceeded. Returns (1, 0) if player 1 wins, and returns (0, 1) if player 2 wins. """ state_action_pairs = list() #Create the game game = Game(n_players=2, dice_number=4, dice_value=3, column_range=[2, 6], offset=2, initial_height=1) current_player = game.player_turn #Cycle through the game until it concludes, or until a move count limit has been reached who_won = None move_count = 0 is_over = False while not is_over: moves = game.available_moves() #Progress the game to the next player's turn if a player has busted if game.is_player_busted(moves): current_player = 2 if current_player == 1 else 1 continue #Otherwise, we're in the middle of a turn else: #Get the current player's action and play it play_chooser = player1 if game.player_turn == 1 else player2 chosen_play = play_chooser.get_action(game) state_action_pairs.append((deepcopy(game), deepcopy(chosen_play))) game.play(chosen_play) move_count += 1 #if the player chooses to conclude their turn prior to busting, progress the game if chosen_play == 'n': current_player = 2 if current_player == 1 else 1 #Get whether the game has concluded, as well as the current standings who_won, is_over = game.is_finished() if move_count > max_moves: is_over = True who_won = -1 #Get the final scores #print("who won", who_won) if who_won == -1: final_scores = (0, 0) else: final_scores = (1, 0) if who_won == 1 else (0, 1) return final_scores, state_action_pairs
class GUITest(unittest.TestCase): field = Field(1080, 800) game = Game(field) ui = UserInterface(game) ui.main_loop() main_window = UserInterface.get_main_window() def test_check_main_window_dimensions(self): main_window_height = UserInterface.get_main_window().height main_window_width = UserInterface.get_main_window().width self.assertEqual(1080, main_window_height) self.assertEqual(800, main_window_width) def test_bullet_moving_to_target(self): self.field_ui = GUITest.main_window.field_ui self.player_ui = self.field_ui.player_ui self.bullet_ui = BulletUI(GUITest.main_window, GUITest.game, self.player_ui) initial_y = self.bullet_ui.y self.bullet_ui.move_to_target() self.assertGreater(initial_y, self.bullet_ui.y) def test_check_pausing_the_game(self): self.field_ui = GUITest.main_window.field_ui self.field_ui.pause_game() self.assertEqual(True, GUITest.game.is_paused) def test_check_resuming_the_game(self): self.field_ui = GUITest.main_window.field_ui self.field_ui.resume_game() self.assertEqual(True, GUITest.game.is_running) def test_check_leveling_up(self): self.game = GUITest.game current_level = self.game.level self.game.level_up() self.assertLess(current_level, self.game.level) def test_check_winning_the_game(self): GUITest.main_window.field_ui.win_the_game() self.assertEqual(True, GUITest.game.is_won) def test_check_powerup_dropping_down(self): self.field_ui = GUITest.main_window.field_ui self.powerup_ui = PowerupUI(GUITest.main_window, GUITest.game, PowerupType.player_invinciblility) initial_y = self.powerup_ui.y self.powerup_ui.drop_down() self.assertLess(initial_y, self.powerup_ui.y)
def createRoom(sid): # Creating new game lobby game = Game() room = game.game_id ROOMS[room] = game # Check if player is in another game playerData = findPlayer(sid) # Join new room joinSuccess = joinRoom(sid, room) # Leave the other room if playerData['found'] and joinSuccess: leaveRoom(sid, playerData['room']) printRoomOccupants()
def test_01(self): """ testing: x x x x - - o - - - - - o - - - - - o - - - - - - """ game = Game(5, 4) moves = [(1, 1), (2, 2), (2, 1), (3, 3), (3, 1), (4, 4), (4, 1)] for move in moves: game.makemove(game.next_player(), move) game.printboard() print(game.position.serialize_out())
async def leave(self, ctx): # remove a player from the game response = ctx.bot.spel.remove_player(str( ctx.author)) # actual removing happens here if not ctx.bot.spel.players: # if this was the last player, print a final message and start a new game response += "\nDe laatste speler heeft het spel verlaten. Het spel is nu afgelopen.\n" \ f"Een nieuw spel begint als er opnieuw {MIN_PLAYERS} spelers zijn." ctx.bot.spel = Game() gc.collect( ) # explicitly collect garbage here, because we throw away all Game and Player objects here elif len(ctx.bot.spel.players) <= ( MIN_PLAYERS - 1 ): # if there are not enough players left, display this message response += "\nEr zijn niet genoeg spelers om verder te spelen.\n" \ "Wacht tot er opnieuw genoeg spelers zijn of beƫindig het spel.\n" \ f"Een nieuwe speler kan meedoen door '{const.PREFIX}{const.MEEDOEN}' te typen.\n" \ f"Het spel kan beƫindigd worden door '{const.PREFIX}{const.STOP}' te typen." await ctx.channel.send(response ) # send the built up response to the channel
def test_setExpertLevel(): game = Game() game.setExpertLevel() assert game.boardLayout.boardSize == 20 and game.boardLayout.bombsAmount == 40
def test_setHardLevel(): game = Game() game.setHardLevel() assert game.boardLayout.boardSize == 16 and game.boardLayout.bombsAmount == 26
def test_setNormalLevel(): game = Game() game.setNormalLevel() assert game.boardLayout.boardSize == 12 and game.boardLayout.bombsAmount == 12
def test_setEasyLevel(): game = Game() game.setEasyLevel() assert game.boardLayout.boardSize == 8 and game.boardLayout.bombsAmount == 6
class GameTest(unittest.TestCase): def setUp(self): self.field = Field(1080, 800) self.game = Game(self.field) def check_game_field_dimensions(self): game_dimensions = (self.field.width, self.field.height) self.assertEqual(game_dimensions, self.game.dimensions) def test_checking_is_game_lost(self): self.assertEqual(False, self.game.is_lost) def test_checking_is_game_won(self): self.assertEqual(False, self.game.is_won) def test_checking_is_game_paused(self): self.assertEqual(False, self.game.is_paused) def test_checking_is_game_running(self): self.assertEqual(True, self.game.is_running) def test_checking_is_game_lost_after_losing(self): self.game.lose() self.assertEqual(True, self.game.is_lost) self.assertEqual(False, self.game.is_running) def test_checking_is_game_won_after_winning(self): self.game.win() self.assertEqual(True, self.game.is_won) self.assertEqual(False, self.game.is_lost) self.assertEqual(False, self.game.is_running) def test_checking_is_game_paused_after_pausing(self): self.game.pause() self.assertEqual(True, self.game.is_paused) self.assertEqual(False, self.game.is_running) def test_checking_is_game_running_after_resuming(self): self.game.resume() self.assertEqual(False, self.game.is_paused) self.assertEqual(True, self.game.is_running) def test_setting_new_game_speed(self): new_speed = 300 self.game.set_speed(new_speed) self.assertEqual(new_speed, self.game.game_speed) def test_setting_new_rock_speed(self): new_speed = 30 self.game.set_rock_speed(new_speed) self.assertEqual(new_speed, self.game.rock_speed) def test_leveling_up(self): current_level = self.game.level self.game.level_up() self.assertEqual(current_level + 1, self.game.level) def test_getting_initial_game_speed(self): initial_speed = 630 self.assertEqual(initial_speed, self.game.game_speed) def test_getting_initial_bullet_speed(self): initial_speed = 50 self.assertEqual(initial_speed, self.game.bullet_speed) def test_getting_initial_rock_speed(self): initial_speed = 50 self.assertEqual(initial_speed, self.game.rock_speed) def test_getting_initial_level_speed(self): initial_speed = 30000 self.assertEqual(initial_speed, self.game.level_speed) def test_getting_initial_level(self): initial_level = 1 self.assertEqual(initial_level, self.game.level) def test_get_rock(self): rock = self.field.rock self.assertEqual(rock, self.game.rock) def test_get_bullet(self): bullet = self.field.bullet self.assertEqual(bullet, self.game.bullet) def test_get_powerup(self): powerup = self.field.powerup self.assertEqual(powerup, self.game.powerup) def test_get_player(self): player = self.field.player self.assertEqual(player, self.game.player) def test_get_rocks(self): rocks = self.field.rocks self.assertEqual(rocks, self.game.rocks) def test_get_bullets(self): bullets = self.field.bullets self.assertEqual(bullets, self.game.bullets) def test_get_powerups(self): powerups = self.field.powerups self.assertEqual(powerups, self.game.powerups) def test_resetting_game(self): self.game.reset_game_values() self.assertEqual(630, self.game.game_speed) self.assertEqual(30000, self.game.level_speed) self.assertEqual(1, self.game.level) self.assertEqual(50, self.game.rock_speed)
def test_game02(self): """ testing: x o x o x o x o x o x o x o x x o x o x x o x o o """ game = Game(5, 4) moves = [ (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (5, 5), (1, 4), (2, 4), (3, 4), (4, 4), (1, 5), (2, 5), (3, 5), (4, 5) ] for move in moves: game.makemove(game.next_player(), move) status, _ = game.game_status() #game.printboard() assert_equal(status, None) game.makemove(game.next_player(), (5, 4)) status, _ = game.game_status() game.printboard() assert_equal(status, 0)
class GameTest(unittest.TestCase): def setUp(self): self.field = Field(1080, 800) self.game = Game(self.field) def check_game_field_dimensions(self): game_dimensions = (self.field.width, self.field.height) self.assertEqual(game_dimensions, self.game.dimensions) def test_checking_is_game_lost(self): self.assertEqual(False, self.game.is_lost) def test_checking_is_game_won(self): self.assertEqual(False, self.game.is_won) def test_checking_is_game_paused(self): self.assertEqual(False, self.game.is_paused) def test_checking_is_game_running(self): self.assertEqual(True, self.game.is_running) def test_checking_is_game_lost_after_losing(self): self.game.lose() self.assertEqual(True, self.game.is_lost) self.assertEqual(False, self.game.is_running) def test_checking_is_game_won_after_winning(self): self.game.win() self.assertEqual(True, self.game.is_won) self.assertEqual(False, self.game.is_lost) self.assertEqual(False, self.game.is_running) def test_checking_is_game_paused_after_pausing(self): self.game.pause() self.assertEqual(True, self.game.is_paused) self.assertEqual(False, self.game.is_running) def test_checking_is_game_running_after_resuming(self): self.game.resume() self.assertEqual(False, self.game.is_paused) self.assertEqual(True, self.game.is_running) def test_setting_new_game_speed(self): new_speed = 300 self.game.set_speed(new_speed) self.assertEqual(new_speed, self.game.game_speed) def test_setting_new_rock_speed(self): new_speed = 30 self.game.set_rock_speed(new_speed) self.assertEqual(new_speed, self.game.rock_speed) def test_leveling_up(self): current_level = self.game.level self.game.level_up() self.assertEqual(current_level + 1, self.game.level) def test_getting_initial_game_speed(self): initial_speed = 630 self.assertEqual(initial_speed, self.game.game_speed) def test_getting_initial_bullet_speed(self): initial_speed = 50 self.assertEqual(initial_speed, self.game.bullet_speed) def test_getting_initial_rock_speed(self): initial_speed = 50 self.assertEqual(initial_speed, self.game.rock_speed) def test_getting_initial_level_speed(self): initial_speed = 30000 self.assertEqual(initial_speed, self.game.level_speed) def test_getting_initial_level(self): initial_level = 1 self.assertEqual(initial_level, self.game.level) def test_get_rock(self): rock = self.field.rock self.assertEqual(rock, self.game.rock) def test_get_bullet(self): bullet = self.field.bullet self.assertEqual(bullet, self.game.bullet) def test_get_powerup(self): powerup = self.field.powerup self.assertEqual(powerup, self.game.powerup) def test_get_player(self): player = self.field.player self.assertEqual(player, self.game.player) def test_get_rocks(self): rocks = self.field.rocks self.assertEqual(rocks, self.game.rocks) def test_get_bullets(self): bullets = self.field.bullets self.assertEqual(bullets, self.game.bullets) def test_get_powerups(self): powerups = self.field.powerups self.assertEqual(powerups, self.game.powerups) def test_resetting_game(self): self.game.reset_game_values() self.assertEqual(630, self.game.game_speed) self.assertEqual(30000, self.game.level_speed) self.assertEqual(1, self.game.level) self.assertEqual(50, self.game.rock_speed)
def setUp(self): self.field = Field(1080, 800) self.game = Game(self.field)
def __init__(self, bot): bot.spel = Game() # initialize the game self.bot = bot self.time = datetime.now()
def setUp(self): self.field = Field(1080, 800) self.game = Game(self.field)
def test_game01(self): """ testing: x x x x - - o - - - - - o - - - - - o - - - - - - """ game = Game(5, 4) moves = [(1, 1), (2, 2), (2, 1), (3, 3), (3, 1), (4, 4)] for move in moves: game.makemove(game.next_player(), move) status, _ = game.game_status() assert_equal(status, None) game.makemove(game.next_player(), (4, 1)) status, _ = game.game_status() game.printboard() assert_equal(status, 1)
def build(self): game = Game() return game