def test_roll_dice(capsys): clue = CluedoGame(2, 'green') for i in range(10): clue_dice = clue.roll_dice() LOGGER.info("CAPTURED: " + str(clue_dice)) LOGGER.info("ASSUMED: " + 'Range 2 - 13') assert clue_dice > 1 and clue_dice < 13
def load_page(self, player_count, character_chosen): """Load the gameboard GUI Callback: process_turn() Args: player_count (int): number of players of this game in total character_chosen (str): name of the token selected by player """ # reset, clear cache self.next_callback = None self.prev_callback = None self.next_btn = self.ids['next_btn'] self.prev_btn = self.ids['prev_btn'] if self.next_callback: self.next_btn.unbind(on_press=self.next_callback) if self.prev_callback: self.prev_btn.unbind(on_press=self.prev_callback) self.next_btn.background_normal = 'images/Next.png' self.prev_btn.background_normal = 'images/Black_Box.png' self.layer_control = self.ids['layer_control'] self.game_reference = CluedoGame(player_count, character_chosen) # prepare gameboard display size = self.game_reference.setup["setup"]["map"]["size"] self.gameboard = self.ids['gameboard'] self.gameboard.clear_widgets() self.info_text = self.ids['basic_info'] self.gameboard.rows = size[1] self.gameboard.cols = size[0] Logger.info(self.game_reference.cards) for row in range(0, size[1]): for col in range(0, size[0]): gameboard_data = self.game_reference.gameboard.board[row][col] if gameboard_data == 0: self.gameboard.add_widget(Grid_button(row, col)) else: self.gameboard.add_widget(Trans_button(row, col)) #gameboard.add_widget(Button(text = str(row) + ',' + str(col))) self.info_text.text = 'Playing as [b]' + character_chosen.capitalize( ) + '[/b]' # while character_chosen.capitalize() not in self.game_reference.players[self.game_reference.next_player].name: # self.game_reference.next_player += 1 self.process_turn( self.game_reference.players[self.game_reference.next_player])
def test_check_accuse(capsys): clue = CluedoGame(2, 'green') test_answer = {} for this_card in clue.cards['tokens']: if this_card.is_answer: test_answer['token'] = this_card for this_card in clue.cards['weapons']: if this_card.is_answer: test_answer['weapon'] = this_card for this_card in clue.cards['rooms']: if this_card.is_answer: test_answer['room'] = this_card LOGGER.info("CAPTURED: " + str(test_answer)) assert clue.check_accuse(test_answer)
def test_display_info(capsys): clue = CluedoGame(2, 'green') test_player = clue.players[0] test_move_points = 10 test_reachable_rooms = [] clue.display_info(test_player, test_move_points, test_reachable_rooms) clue_captured = capsys.readouterr() LOGGER.info("CAPTURED: " + str(clue_captured)) assert_output_part1 = 'Rev Green' LOGGER.info("ASSUMED: " + str(assert_output_part1)) assert_output_part2 = 'you can move 10 steps' LOGGER.info("ASSUMED: " + str(assert_output_part2)) assert_output_part3 = 'you can reach those rooms []' LOGGER.info("ASSUMED: " + str(assert_output_part3)) assert assert_output_part1 in clue_captured.out assert assert_output_part2 in clue_captured.out assert assert_output_part3 in clue_captured.out
def test_load_gameboard(capsys): clue = CluedoGame(2, 'green') clue_gameboard_plot1 = clue.gameboard.board[0][0] LOGGER.info("CAPTURED: " + str(clue_gameboard_plot1)) assert_gameboard_plot1 = 1 LOGGER.info("ASSUMED: " + str(assert_gameboard_plot1)) assert clue_gameboard_plot1 == assert_gameboard_plot1 clue_gameboard_plot2 = clue.gameboard.board[8][10] LOGGER.info("CAPTURED: " + str(clue_gameboard_plot2)) assert_gameboard_plot2 = 888 LOGGER.info("ASSUMED: " + str(assert_gameboard_plot2)) assert clue_gameboard_plot2 == assert_gameboard_plot2 clue_gameboard_plot3 = clue.gameboard.board[23][15] LOGGER.info("CAPTURED: " + str(clue_gameboard_plot3)) assert_gameboard_plot3 = 0 LOGGER.info("ASSUMED: " + str(assert_gameboard_plot3)) assert clue_gameboard_plot3 == assert_gameboard_plot3
def test_generate_answer(capsys): clue = CluedoGame(2, 'green') clue_answer_count = 0 clue_answer_count_token = 0 clue_answer_count_weapon = 0 clue_answer_count_room = 0 for this_card in clue.cards['tokens']: if this_card.is_answer: clue_answer_count += 1 clue_answer_count_token = 1 for this_card in clue.cards['weapons']: if this_card.is_answer: clue_answer_count += 1 clue_answer_count_weapon = 1 for this_card in clue.cards['rooms']: if this_card.is_answer: clue_answer_count += 1 clue_answer_count_room = 1 assert_answer_count = 3 assert_answer_count_token = 1 assert_answer_count_weapon = 1 assert_answer_count_room = 1 LOGGER.info("CAPTURED: " + str(clue_answer_count)) LOGGER.info("ASSUMED: " + str(assert_answer_count)) LOGGER.info("CAPTURED: " + str(clue_answer_count_token)) LOGGER.info("ASSUMED: " + str(assert_answer_count_token)) LOGGER.info("CAPTURED: " + str(clue_answer_count_weapon)) LOGGER.info("ASSUMED: " + str(assert_answer_count_weapon)) LOGGER.info("CAPTURED: " + str(clue_answer_count_room)) LOGGER.info("ASSUMED: " + str(assert_answer_count_room)) assert clue_answer_count == assert_answer_count assert clue_answer_count_token == assert_answer_count_token assert clue_answer_count_weapon == assert_answer_count_weapon assert clue_answer_count_room == assert_answer_count_room
def test_cluedo_game(capsys): # this is combined with unit test of # __init__(), load_setup(), load_cards(), load_players() and deal_card() clue = CluedoGame(2, 'green') clue_type = type(clue) LOGGER.info("CAPTURED: " + str(clue_type)) assert_type = CluedoGame LOGGER.info("ASSUMED: " + str(assert_type)) assert clue_type == assert_type clue_player_count = len(clue.players) LOGGER.info("CAPTURED: " + str(clue_player_count)) assert_player_count = 2 LOGGER.info("ASSUMED: " + str(assert_player_count)) assert clue_player_count == assert_player_count clue_first_player = clue.players[clue.next_player].name LOGGER.info("CAPTURED: " + str(clue_first_player)) assert_first_player = 'Rev Green' LOGGER.info("ASSUMED: " + str(assert_first_player)) assert clue_first_player == assert_first_player clue_cards_number = (len(clue.cards['tokens']) + len(clue.cards['weapons']) + len(clue.cards['rooms'])) LOGGER.info("CAPTURED: " + str(clue_cards_number)) assert_cards_number = 21 LOGGER.info("ASSUMED: " + str(assert_cards_number)) assert clue_cards_number == assert_cards_number clue_cards_dealt = 0 for this_player in clue.players: clue_cards_dealt += len(this_player.cards_in_hand) LOGGER.info("CAPTURED: " + str(clue_cards_dealt)) assert_cards_dealt = 18 LOGGER.info("ASSUMED: " + str(assert_cards_dealt)) assert clue_cards_dealt == assert_cards_dealt
def test_gameboard(capsys): clue = CluedoGame(2, 'green') board = clue.gameboard test_start = clue.players[0] test_move_points = 11 clue_rooms = board.check_reachable_rooms(test_start, test_move_points) LOGGER.info("CAPTURED: " + str(clue_rooms)) assert_room1 = 'Ball Room' LOGGER.info("ASSUMED: " + str(assert_room1)) assert_room2 = 'Conservatory' LOGGER.info("ASSUMED: " + str(assert_room2)) assert assert_room1 in str(clue_rooms) assert assert_room2 in str(clue_rooms) board.move_player_to_room(test_start, clue_rooms[1]) test_in_room = test_start.in_room LOGGER.info("CAPTURED:: " + str(test_in_room)) assert_in_room = clue_rooms[1] LOGGER.info("ASSUMED: " + str(assert_in_room)) assert test_in_room == assert_in_room clue_rooms = board.check_reachable_rooms(test_start, test_move_points) LOGGER.info("CAPTURED: " + str(clue_rooms)) assert_room3 = 'Billiard Room' LOGGER.info("ASSUMED: " + str(assert_room3)) assert_room4 = 'Lounge' LOGGER.info("ASSUMED: " + str(assert_room4)) assert assert_room3 in str(clue_rooms) assert assert_room4 in str(clue_rooms)
class Game_body(Screen): """Page: Main game, including gameboard and main information display Attrs: next_callback (lambda): callback function of 'Next' button prev_callback (lambda): callback function of 'Previous' button next_btn (Next_btn): the instance of 'Next' button prev_btn (Prev_btn): the instance of 'Previous' button layer_control (PageLayout): controller to switch between layers game_reference (Cluedo_game): command line game at the backend gameboard (GridLayout): container of all grid buttons info_text (Label): primary information text of the game """ def load_page(self, player_count, character_chosen): """Load the gameboard GUI Callback: process_turn() Args: player_count (int): number of players of this game in total character_chosen (str): name of the token selected by player """ # reset, clear cache self.next_callback = None self.prev_callback = None self.next_btn = self.ids['next_btn'] self.prev_btn = self.ids['prev_btn'] if self.next_callback: self.next_btn.unbind(on_press=self.next_callback) if self.prev_callback: self.prev_btn.unbind(on_press=self.prev_callback) self.next_btn.background_normal = 'images/Next.png' self.prev_btn.background_normal = 'images/Black_Box.png' self.layer_control = self.ids['layer_control'] self.game_reference = CluedoGame(player_count, character_chosen) # prepare gameboard display size = self.game_reference.setup["setup"]["map"]["size"] self.gameboard = self.ids['gameboard'] self.gameboard.clear_widgets() self.info_text = self.ids['basic_info'] self.gameboard.rows = size[1] self.gameboard.cols = size[0] # need this for demo use: # Logger.info(self.game_reference.cards) for row in range(0, size[1]): for col in range(0, size[0]): gameboard_data = self.game_reference.gameboard.board[row][col] if gameboard_data == 0: self.gameboard.add_widget(Grid_button(row, col)) else: self.gameboard.add_widget(Trans_button(row, col)) self.info_text.text = 'Playing as [b]' + character_chosen.capitalize( ) + '[/b]' self.process_turn( self.game_reference.players[self.game_reference.next_player]) def process_turn(self, player): """Starting a new turn for current player Callback: display_info() Args: player (Player): current player in action during this turn """ move_points = self.game_reference.roll_dice() backup_move_points = move_points reachable_rooms = [] # ensure player can at least get into one of the rooms while len(reachable_rooms) == 0: reachable_rooms = self.game_reference.gameboard.check_reachable_rooms( player, move_points) move_points += 1 if self.next_callback: self.next_btn.unbind(on_press=self.next_callback) if self.prev_callback: self.prev_btn.unbind(on_press=self.prev_callback) self.next_callback = lambda root: self.display_info( player, backup_move_points, reachable_rooms) self.next_btn.bind(on_press=self.next_callback) def process_suspect(self, player, cards): """Preparing to display 'make suggestion' page for the player to choose Callback: load_choice() Args: player (Player): current player in action cards (list): list of all cards used in this game """ self.manager.get_screen('player_action').load_choice(2, player) self.manager.current = 'player_action' def display_info(self, player, move_points, reachable_rooms): """Display information of current player, including move points get from the dices, cards in player's hold and possible rooms the player can get in Callback: to_room_on_press() Args: player (Player): current player in action move_points (int): the step number player can use reachable_rooms (list): list of rooms player can enter """ # display dice points self.layer_control.page = 1 current_layer = self.ids['player_overlay'] self.ids['dice_points'].text = '[b]' + str(move_points) + '[/b]' # display cards on hold hand = self.ids['cards_in_hand'] hand.clear_widgets() for this_card in player.cards_in_hand: hand.add_widget( Card_display(this_card.category, this_card.description)) # display rooms can go choose_room = self.ids['rooms_to_go'] choose_room.clear_widgets() for this_room in reachable_rooms: btn_callback = lambda root, this_player=player, temp_room=this_room: self.to_room_on_press( this_player, temp_room) this_btn = Button(text=this_room.name, size_hint=(None, None), size=(140, 48), font_size=20, background_normal='images/White_Box.png', background_color=(1, 1, 1, 1), color=(0.7, 0, 0, 1), on_press=btn_callback) choose_room.add_widget(this_btn) def to_room_on_press(self, player, room): """Handler for player selecting which room to enter Callback: process_suspect() Args: player (Player): current player in action room (Room): the room player decided to enter """ self.game_reference.gameboard.move_player_to_room(player, room) self.info_text.text = player.name + ' has moved to ' + room.name self.layer_control.page = 0 suspect = self.process_suspect(player, self.game_reference.cards) def submit_callback(self, action, submission): """Process the suggestion or accuse submitted by player Callback: do_accuse(): after a suggestion, ask for player make an accusation or not Game_ending.load_page(): after an accusation, display the result (player will either lose or win) Args: action (str): 'suspect' for suggestion, 'accuse' for accusation submission (dict): the dictionary including token, weapon and room """ if action == 'suspect': response = self.game_reference.check_suspect( submission, self.game_reference.players[self.game_reference.next_player]) self.manager.current = 'game_body' info_str = '' for this_response in response: key = str(list(this_response.keys())[0]) value = list(this_response.values())[0] info_str = (info_str + '[b]' + key + '[/b]' + ' showed you: ' + '[b]' + value[1].category.capitalize() + '[/b]' + ', ' + '[b]' + value[1].description + '[/b]' + '\n') if info_str == '': info_str = 'Nobody can help you with your suspect!' self.info_text.text = info_str self.next_btn.unbind(on_press=self.next_callback) self.next_callback = lambda root: self.do_accuse( self.game_reference.cards) self.next_btn.bind(on_press=self.next_callback) if action == 'accuse': response = self.game_reference.check_accuse(submission) if response: self.manager.get_screen('game_ending').load_page( 'win', self.game_reference.players[ self.game_reference.next_player]) self.manager.current = 'game_ending' else: self.manager.get_screen('game_ending').load_page( 'lose', self.game_reference.players[ self.game_reference.next_player]) self.manager.current = 'game_ending' def do_accuse(self, cards): """Ask if the player want to make an accusation Callback: next_turn(): the player do not want an accusation, start a new turn for next player process_accuse(): the player choose to make an accusation Args: cards (list): list of all cards used in this game """ self.info_text.text = 'Do you want to [b]Make Accuse[/b]?' self.prev_btn.background_normal = 'images/No_Btn.png' if self.prev_callback: self.prev_btn.unbind(on_press=self.prev_callback) self.prev_callback = lambda root: self.next_turn( self.game_reference.players[self.game_reference.next_player]) self.prev_btn.bind(on_press=self.prev_callback) self.next_btn.background_normal = 'images/Yes_Btn.png' self.next_btn.unbind(on_press=self.next_callback) self.next_callback = lambda root, temp_cards=cards: self.process_accuse( temp_cards) self.next_btn.bind(on_press=self.next_callback) def process_accuse(self, cards): """Preparing to display 'make accusation' page for the player to choose Callback: load_choice() Args: cards (list): list of all cards used in this game """ self.manager.get_screen('player_action').load_choice(3) self.manager.current = 'player_action' def next_turn(self, player): """Ready for next turn, find next player that can move and clearing cache Callback: process_turn() Args: player (Player): current player in action """ the_game = self.game_reference the_game.next_player = (the_game.next_player + 1) % len( the_game.players) while the_game.players[the_game.next_player].skipped: the_game.next_player = (the_game.next_player + 1) % len( the_game.players) the_next = the_game.players[the_game.next_player] self.info_text.text = 'Playing as [b]' + the_next.name.capitalize( ) + '[/b]' self.next_btn.background_normal = 'images/Next.png' self.prev_btn.background_normal = 'images/Black_Box.png' self.process_turn(the_next) def pressed(self, the_button): """DEBUG: check which button was pressed Args: the_button (Button): button got pressed """ pass