def setUp(self): self.starting_point = {"red": 0, "green": 13, "yellow": 26, "blue": 39} self.cs = ["red", "green", "yellow", "blue"] self.colour_to_img = { "red": c.RED_PIECE, "green": c.GREEN_PIECE, "yellow": c.YELLOW_PIECE, "blue": c.BLUE_PIECE } self.all_pieces = [ Piece(self.cs[c], num, self.colour_to_img[self.cs[c]], self.starting_point[self.cs[c]]) for c in range(4) for num in range(1, 5) ] self.player = Player("red", "Team_1", self.all_pieces, ["a", "b", "c", "d"])
def test_blue_start_end_range(self): self.blue_player = Player("blue", "Team_1", self.all_pieces, ["a", "b", "c", "d"]) self.assertEqual(self.blue_player.start, 39) self.assertEqual(self.blue_player.end, 37) self.assertEqual(self.blue_player.low_range, 12)
def test_yellow_start_end_range(self): self.yellow_player = Player("yellow", "Team_1", self.all_pieces, ["a", "b", "c", "d"]) self.assertEqual(self.yellow_player.start, 26) self.assertEqual(self.yellow_player.end, 24) self.assertEqual(self.yellow_player.low_range, 8)
def test_green_start_end_range(self): self.green_player = Player("green", "Team_1", self.all_pieces, ["a", "b", "c", "d"]) self.assertEqual(self.green_player.start, 13) self.assertEqual(self.green_player.end, 11) self.assertEqual(self.green_player.low_range, 4)
def test_set_my_player(self): self.player = Player("red", "Team_1", self.all_pieces, ["a", "b", "c", "d"]) self.piece.set_my_player(self.player) self.assertIsInstance(self.piece.my_player, Player)
def set_player(self): self.player = Player("red", "Team_1", self.all_pieces, ["a", "b", "c", "d"]) self.piece.set_my_player(self.player)
def connection_handler(self): """ This function controls all data received from the server, and updates the client-side program according to the received ``JSON`` messages. When referring to ``JSON`` message comments, if the symbols ``<>`` are used, it implies that the data is dynamic, and what will be in there depends on the player colour, roll of the dice etc. """ colors = ["red", "green", "yellow", "blue"] while True: data = self.sock.recv(4096).decode() # decodes received data. print("Received: ", data) data = data.split("}") for msg in data: if len(msg) > 1: msg += "}" msg = json.loads(msg) # Tell the time out function to reset the time. if "chat_msg" not in msg: self.q.put("already push a button") # Start implies it is the first message of the game. # The message comes in the form {"start":True,"colour":<colour>} if "start" in msg: names = msg["names"] self.my_player = Player(msg["colour"], names[colors.index(msg["colour"])], self.ALL_PIECES, names) self.board.my_player = self.my_player # This tells all games which player's turn it is. # Messages come of the form {"turn_token":True,"Colour":<colour>}. if "turn_token" in msg: # If msg["Colour"] is client's colour, then it is their turn. if msg["colour"] == self.my_player.colour: self.board.PLAYER_FIELD.set_msg("MY TURN") self.my_player.turn_token = True self.my_player.diceroll_token = True else: self.board.PLAYER_FIELD.set_msg( self.my_player.names[colors.index(msg[ "colour"])] + "'s turn") self.current_player = msg["colour"] self.board.current_player = msg["colour"] # This message is a response to pressing the "ROLL" button. # It comes in the form {"dicenum":<between 1-6>,"Colour":<colour>} if "dicenum" in msg: roll = msg["dicenum"] # This is for the biased dice roll if roll > 5: roll = 6 self.my_player.roll = roll # Assigned value of dice roll # genie_status is either "take", "return" or None genie_status = msg["genie_result"] if genie_status == "take" and self.board.genie_owner is None: # If you roll to take the genie and no one currently has it SCREEN.blit(GENIE_BIG, (950, 50)) self.board.genie_owner = msg["colour"] # Take the genie elif genie_status == "return" and \ self.board.genie_owner == msg["colour"]: # If you roll to give back the genie and you own it SCREEN.blit(LAMP_BIG, (950, 50)) # The genie goes back to the centre self.board.genie_owner = None self.current_dice = ROLL_TO_IMG[roll] # updates the dice image. self.board.dice_object.roll_dice_gif(900, 230) # If the dicenum is for this player, then react accordingly. if msg["colour"] == self.my_player.colour: self.pieces_playable() # This message is broadcasted if a player moves a piece. # As the player moves it's own pieces, they only react to other if "movement" in msg and msg["colour"] != self.my_player.colour: # It comes in the form {"Movement":<piecenum>, # "Moveforward":<number-of-steps-to-move>,"colour":<colour>} # player's movements. steps = msg["steps_forward"] num = msg["movement"] self.board.move_piece(num, steps) if self.my_player.roll == 6: self.my_player.diceroll_token = True if "finished" in msg and msg["colour"] != self.my_player.colour: self.win_condition() if "chat_msg" in msg: self.chat.new_message(msg) if "disconnected" in msg: self.board.disconnect_function(msg["colour"])