File size: 4,577 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import pytest
from easydict import EasyDict

from connect4_env import Connect4Env


@pytest.mark.envtest
class TestConnect4Env:

    def test_self_play_mode(self) -> None:
        cfg = EasyDict(
            battle_mode='self_play_mode',
            bot_action_type='rule',  # {'rule', 'mcts'}
            channel_last=False,
            scale=True,
            screen_scaling=9,
            prob_random_action_in_bot=0.,
            render_mode='state_realtime_mode',
            agent_vs_human=False,
            prob_random_agent=0,
            prob_expert_agent=0,
        )
        env = Connect4Env(cfg)
        env.reset()
        print('init board state: ')
        while True:
            """player 1"""
            # action = env.human_to_action()
            action = env.bot_action()
            # action = env.random_action()

            # test legal_actions
            # legal_actions = env.legal_actions
            # print('legal_actions: ', legal_actions)
            # action = legal_actions[-1]

            print('player 1: ' + env.action_to_string(action))
            obs, reward, done, info = env.step(action)
            # print(reward)
            if done:
                if reward > 0:
                    print('player 1 win')
                else:
                    print('draw')
                break

            """player 2"""
            action = env.bot_action()
            print('player 2 : ' + env.action_to_string(action))
            obs, reward, done, info = env.step(action)
            # print(reward)
            env.render()
            if done:
                if reward > 0:
                    print('player 2 win')
                else:
                    print('draw')
                break

    def test_play_with_bot_mode(self) -> None:
        cfg = EasyDict(
            battle_mode='play_with_bot_mode',
            bot_action_type='rule',  # {'rule', 'mcts'}
            channel_last=False,
            scale=True,
            screen_scaling=9,
            prob_random_action_in_bot=0.,
            render_mode='state_realtime_mode',
            agent_vs_human=False,
            prob_random_agent=0,
            prob_expert_agent=0,
        )
        env = Connect4Env(cfg)
        env.reset()
        print('init board state: ')
        while True:
            """player 1"""
            # action = env.human_to_action()
            action = env.bot_action()
            # action = env.random_action()

            # test legal_actions
            # legal_actions = env.legal_actions
            # print('legal_actions: ', legal_actions)
            # action = legal_actions[-1]

            print('player 1: ' + env.action_to_string(action))
            obs, reward, done, info = env.step(action)
            # reward is in the perspective of player1
            if done:
                if reward != 0 and env.current_player == 2:
                    print('player 1 (human player) win')
                elif reward != 0 and env.current_player == 1:
                    print('player 2 (computer player) win')
                else:
                    print('draw')
                break

    def test_eval_mode(self) -> None:
        cfg = EasyDict(
            battle_mode='eval_mode',
            bot_action_type='rule',  # {'rule', 'mcts'}
            channel_last=False,
            scale=True,
            screen_scaling=9,
            prob_random_action_in_bot=0.,
            render_mode='state_realtime_mode',
            agent_vs_human=False,
            prob_random_agent=0,
            prob_expert_agent=0,
        )
        env = Connect4Env(cfg)
        env.reset(replay_name_suffix=f'test_eval_mode')
        print('init board state: ')
        while True:
            """player 1"""
            # action = env.human_to_action()
            action = env.bot_action()
            # action = env.random_action()

            # test legal_actions
            # legal_actions = env.legal_actions
            # print('legal_actions: ', legal_actions)
            # action = legal_actions[-1]

            print('player 1: ' + env.action_to_string(action))
            obs, reward, done, info = env.step(action)
            # reward is in the perspective of player1
            if done:
                if reward != 0 and env.current_player == 2:
                    print('player 1 (human player) win')
                elif reward != 0 and env.current_player == 1:
                    print('player 2 (computer player) win')
                else:
                    print('draw')
                break