예제 #1
0
    def send(self, message):
        """
        Send messages to the game logic. Sender can be the view or the timer manager.\n
        Before the message is handled the lock is acquired.
        """
        with self.__lock:
            if message["type"] == "card-click":
                if game["state"]["faceup-delay"]:
                    return
                if not game["in-device"]:
                    msg_str = json.dumps(message)
                    ret_val = ws.send(msg_str)
                    if not ret_val:
                        print("Controller.send ws.send failed",
                              json.dumps(message))
                    else:
                        print("Controller.send", "card-click", "to server",
                              msg_str)
                    return

                prev_state = json.dumps(game["state"])
                game_logic.play(game, message["message"])
                if json.dumps(game["state"]) != prev_state:
                    # Counter is needed so that kivy notices that the dictproperty has been changed
                    self.counter += 1
                    self.reply_message = {
                        "type": MESSAGE_TYPE_GAME_STATE,
                        "message": game["state"],
                        "counter": self.counter
                    }

            elif message["type"] == "create-game":
                set_id()
                set_num_players(1)
                set_num_pairs(message["num_pairs"])
                set_level_time(message["num_pairs"] * 2 * 5)
                game_logic.init(game, message["change-level"])
                print("Controller.send", game["state"]["board"])
                self.reply_message = {
                    "type": MESSAGE_TYPE_START_GAME,
                    "message": game["state"]
                }

            elif message["type"] == "join-game":
                print("Controller.send", "join-game")
                ret_val = ws.send(json.dumps(message))
                if not ret_val:
                    print("Controller.send ws.send failed",
                          json.dumps(message))

            elif message["type"] == "my-name":
                if "ws" not in ws.store:
                    connected = ws.conn()
                    if not connected:
                        print("Controller.send: ws.conn failed")
                        return
                ret_val = ws.send(json.dumps(message))
                if not ret_val:
                    print("Controller.send ws.send failed",
                          json.dumps(message))
def main():

    # Make 1 call to scrape_quotes(), when the program is initially run, and
    # not for each time the user chooses to play again.
    quote_db = scrape_quotes()

    play(quote_db)

    while True:
        play_again(quote_db)
예제 #3
0
    def turn_card(self, user, cardI):
        print('user', user, 'cardI', cardI)

        message = {
            'gameId': self.game.gameId,
            'userName': user,
            'cardI': int(cardI)
        }
        self.prev_game_state = self.game.state
        play(self.game, message)

        print('game.state=', self.game.state)
                default_probability_of_stand=default_probability_of_stand)

            player_instance = player.Player(strategy=player_learning_strategy)
            dealer_win_count = 0
            player_win_count = 0
            player_win_history = []
            player_win_history_average = [0]
            player_win_history_std = []
            # total_player_win_average = 0
            draw_count = 0

            # playing blackjack games with fixed strategy
            for episode_no in range(episode_count):
                # 1/(1 + episode_no) #probability_of_random_choice
                player_instance.probability_of_random_choice = probability_of_random_choice
                game_status, player_visited_bare_states = game_logic.play(
                    player_instance)
                player_final_reward = game_logic.get_player_reward(game_status)
                player_instance.end_game(player_final_reward,
                                         player_visited_bare_states,
                                         discount_factor, learning_rate)

                # game ended, check who has won the game
                if game_status == game_definitions.Status.DEALER_WON:
                    dealer_win_count += 1
                    player_win_history.append(0)

                    last_element = player_win_history_average[-1]
                    n = len(player_win_history_average)
                    player_win_history_average.append(last_element +
                                                      (0 - last_element) / n)
                    # total_player_win_average += (0-total_player_win_average)/n
예제 #5
0
 def reward(self, game_state):
     atrs = play(game_state)
     reward = terminate_fitness(atrs)
     return reward