コード例 #1
0
def test_basic_game():
    players = [
        TestPlayer(roles=[['K', 'K', 'Q']], targets=[['2', 'A', 'A']]),
        TestPlayer(roles=[['J', 'J', 'Q']], targets=[['2', '3', '4']])
    ]
    game = Game(*players)
    game.play()
コード例 #2
0
    def next_data(self):
        # 参数设置
        epsilon = 0.001 if self.flag in ["explore", "display"] else 0.1
        init_epsilon, final_epsilon = 0.1, 0.001
        action_dim = 2
        # 初始化
        num = 40 if self.flag in ["explore", "train"] else 1
        game = Game(num)    # game为环境

        action = np.array([1, 0])
        image, reward, terminal = game.frame_step(action)

        image = convert(image)
        for _ in range(4):
            self.memory_append(image, [*action, reward, terminal])
        epsilon -= (init_epsilon - final_epsilon) / self.explore * self.count * self.pre_step_epoch
        epsilon = np.clip(epsilon, a_max=init_epsilon, a_min=final_epsilon)

        # 获取当前状态
        count = self.count * self.pre_step_epoch
        try:
            while True:
                # 获取动作
                if random.random() < epsilon:
                    action_ind = np.random.randint(0, action_dim)
                else:
                    idx = (self.next - np.arange(1, self.time_step+1)) % self.max_length
                    state = self.memory["image"][idx]

                    state = np.transpose(state[np.newaxis, :, :], [0, 2, 3, 1])
                    action_ind = self.func(state).argmax(-1).astype("int")[0]   # 智能体产生动作

                epsilon -= (init_epsilon - final_epsilon) / self.explore
                epsilon = np.clip(epsilon, a_max=init_epsilon, a_min=final_epsilon)
                count += 1

                action = game.get_event(action_ind)     # 游戏中事件触发

                image, reward, terminal = game.frame_step(action)   # 环境的激励
                image = convert(image)  # 80*80

                self.memory_append(image, [*action, reward, terminal])
                data = self.batch_data()
                if data is not None:
                    yield data

        except GameOver:
            print("\n{}> game close <{}".format("="*10, "="*10))
コード例 #3
0
def main():
    m = generate_maze(10,10)
    g = Game(m)

    while True:
        print m 
        if g.can_player_hear():
            print "ouyay ancay earhay a umpusway omingcay!!"
        d = wait_for_dir()
        try:
            g.player.move(d)
        except ValueError:
            print "ouyay an'tcay ogay atthay ayway!"

        if g.has_player_won():
            print "ouyay avehay onway. OOOWAY!"
            break
        g.move_wumpus()     
コード例 #4
0
#Alec Bayliff
from play import Game

mygame = Game(15, 15, .25, .1, 1000, 42, "testout.txt")
mygame.play()
コード例 #5
0
ファイル: qlearning.py プロジェクト: DomenPozrl/Tarock1.0
    #create learning instance
    #Q = QLearningTarock(0.5, 0.1, 0.1)

    #initialize qtables, only run if you want to start the learning from scraths, otherwise use load_qtables
    #Q.initialize_qtables(state_labels1, state_labels2, action_labels1, action_labels2, constraints1=constraints1, constraints2=constraints2)
    #Q.store_qtables("q1_1.pickle", "q2_1.pickle")

    #Q.load_qtables("q1_1.pickle", "q2_1.pickle")
    Q = QLearningTarock(0.3, 0.1, 0.1)
    Q.initialize_qtables(state_labels1,
                         state_labels2,
                         action_labels1,
                         action_labels2,
                         constraints1=constraints1,
                         constraints2=constraints2)
    #print(Q.qtable2)
    #print(Q.qtable1)
    #exit(-1)
    action_function = Q.return_action
    update_function = Q.update_table
    N_epochs = 100000
    counter = 0
    while counter < N_epochs:
        print(f"Take: {counter}, {int((counter/N_epochs)*10000)/100}% done")
        game = Game(action_function, action_function, action_function,
                    update_function)
        a = game.play_game()
        #print(f"Final rewards: {a}")
        counter += 1
    Q.store_qtables("q1_2.pickle", "q2_2.pickle")
コード例 #6
0
ファイル: main_state.py プロジェクト: gkstnqls114/2DGFproject
def enter():
    global game
    game = Game.Game()
    game.enter()
    pass
コード例 #7
0
    def on_message(self, message):
        errors = []
        game_id = message.get('game_id')

        raw_data = yield gen.Task(
            redis_client.hmget, 'games:id:{}'.format(game_id),
            ['dimension', 'matrix', 'creator', 'opponent', 'color', 'lineup']
        )

        if raw_data[0] is None:
            errors.append('Wrong game id.')

        if not errors:
            dimension = int(raw_data[0])
            matrix = json.loads(raw_data[1].decode('utf-8'))
            creator = raw_data[2].decode('utf-8')
            opponent = raw_data[3] and raw_data[3].decode('utf-8')
            color = raw_data[4].decode('utf-8')
            lineup = raw_data[5].decode('utf-8')

            if opponent is not None:
                print(opponent, game_id, raw_data)
                errors.append('Game is already started.')

            if self.username == creator:
                errors.append("You can't play with youself.")

        if not errors:
            opponent = self.username
            data_to_save = {'opponent': opponent}

            game = {
                'game_id': game_id,
                'dimension': dimension,
                'cells': Game(matrix, dimension).serialize_cells()
            }

            self.send(json.dumps({
                'status': 'ok',
                'game': game
            }))

            if color == 'black':
                data_to_save.update({'turn': creator})
                opponent_msg = 'You stone is white.'
                creator_msg = 'You stone is black, and now your turn.'
            else:
                data_to_save.update({'turn': opponent})
                creator_msg = 'You stone is white.'
                opponent_msg = 'You stone is black, and now your turn.'

            yield gen.Task(
                redis_client.hmset, 'games:id:{}'.format(game_id),
                data_to_save)

            title = '{0} ({1}x{1}, {2} in row) [{3}]'.format(
                creator, dimension, lineup, color)
            yield gen.Task(
                redis_client.srem, 'games:all',
                '{}:{}'.format(game_id, title))
            games = yield self.get_games()
            self.broadcast_all_channel("games_list", games)

            self.send_channel(
                'note',
                json.dumps({'msg': 'Welcome to the game #{}. {}'.format(
                    game_id, opponent_msg)}))
            self.get_player(creator).send_channel(
                'note',
                json.dumps({'msg': 'Joining new user "{}". {}'.format(
                    opponent, creator_msg)}))
        else:
            self.send_error(errors)
コード例 #8
0
    def on_message(self, message):
        errors = []
        game_id = message.get('game_id')

        raw_data = yield gen.Task(
            redis_client.hgetall, 'games:id:{}'.format(game_id)
        )
        if not raw_data:
            errors.append('This game not existed or already finished.')

        if not errors:
            data = hgetall_group_by(raw_data)

        if not errors and 'opponent' not in data:
            errors.append('Need opponent to join to the game.')

        if not errors:
            creator = data['creator']
            opponent = data['opponent']
            dimension = int(data['dimension'])
            lineup = int(data['lineup'])
            color = data['color']
            turn = data['turn']
            matrix = json.loads(data['matrix'])

        if not errors and self.username not in [creator, opponent]:
            errors.append("You are not participant of this game.")

        if not errors and self.username != turn:
            errors.append(
                "It's not your turn. Please, wait for your opponent's move.")

        if not errors:
            game = Game(matrix, dimension, lineup)

            if self.username == creator:
                action_color = color
            else:
                action_color = 'white' if color == 'black' else 'black'

            color_dict = {
                'white': 0,
                'black': 1
            }
            action, status = game.action(
                message['x'], message['y'], color_dict[action_color])
            if not action:
                errors.append('Bad action.')

        if not errors:
            turn = opponent if turn == creator else creator
            yield gen.Task(
                redis_client.hset, 'games:id:{}'.format(game_id),
                'turn', turn)
            yield gen.Task(
                redis_client.hset, 'games:id:{}'.format(game_id),
                'matrix', json.dumps(game.matrix))

        if not errors:
            for username in [opponent, creator]:
                player = self.get_player(username)
                player.send_channel(
                    'game_action',
                    json.dumps({
                        'status': 'ok',
                        'action': {
                            'x': message['x'],
                            'y': message['y'],
                            'color': action_color
                        }
                    })
                )

                if turn == username:
                    msg = "Now it's your turn."
                else:
                    msg = "Now it's a {}'s turn. Waiting...".format(
                        opponent if username == creator else creator)
                player.send_channel('note', json.dumps({'msg': msg}))
        else:
            self.send_error(errors)

        if status is not None:
            raw_data = yield gen.Task(
                redis_client.delete, 'games:id:{}'.format(game_id)
            )

            url = 'http://localhost:8000/api/round/save'

            post_data = {
                'creator': creator,
                'opponent': opponent,
                'dimension': dimension,
                'lineup': lineup,
                'lead': creator if color == 'black' else opponent,
                'winner': None if status == 'draw' else self.username
            }

            headers = {
                'Content-Type': 'application/json'
            }

            yield http_client.fetch(
                url, method="POST", headers=headers, body=json.dumps(post_data))

            url = 'http://localhost:8000/api/player/top'
            response = yield http_client.fetch(url, method="GET")
            self.broadcast_all_channel('stats', response.body.decode('utf-8'))

            for username in [opponent, creator]:
                player = self.get_player(username)
                if status == 'draw':
                    winner = None
                else:
                    winner = self.username == username
                player.send_channel(
                    'game_finish', json.dumps({'winner': winner})
                )
コード例 #9
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
from play import Game
if __name__ == '__main__':

    game = Game()
    game.run()