-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay_game.py
More file actions
192 lines (147 loc) · 5.87 KB
/
play_game.py
File metadata and controls
192 lines (147 loc) · 5.87 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import env
import numpy as np
import torch
import time
import os
import copy
import random
import keyboard
from agent_structure import ConnectFourDQNAgent, MinimaxAgent
from functions import get_model_config, get_model_and_config_name
X_mark = "\033[31mX\033[0m"
O_mark = "\033[33mO\033[0m"
def print_board_while_gaming(env, pointer, board=None):
os.system('cls')
print("Connect Four")
print("Player1: "+X_mark)
print("Player2: "+O_mark)
print("-----------------------")
empty_space = [" "]*env.n_col
empty_space[pointer] = X_mark if env.player == 1 else O_mark
if board is None:
board = copy.deepcopy(env.board)
else:
empty_space[pointer] = " "
# if self.player == 2:
# board = self.reverse_piece(board)
row_str = " "
for col in range(env.n_col):
row_str += empty_space[col]
row_str += " "
print(row_str)
for row in range(env.n_row):
row_str = "|"
for col in range(env.n_col):
if board[row][col] == 0:
row_str += " "
elif board[row][col] == 1:
row_str += X_mark
elif board[row][col] == 2:
row_str += O_mark
row_str += "|"
print(row_str)
print("+" + "-" * (len(board[0]) * 2 - 1) + "+")
print("player {}'s turn!".format(int(env.player)))
CF = env.ConnectFourEnv()
mode = input("to play with human, type 'human'(else just enter):")
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# 나중에 RandomAgent가 추가된다면 ConnectFourDQNAgent(), RandomAgent(), HeuristicAgent() 등등 선택 가능
#agent = env.HeuristicAgent()
config = get_model_config()
model_name, config_name = get_model_and_config_name('model/model_for_play')
prev_model_config = get_model_config('model/model_for_play/'+config_name)
kwargs={
'use_conv':prev_model_config['use_conv'], \
'use_minimax':prev_model_config['use_minimax'], \
'use_resnet':prev_model_config['use_resnet'],
'next_state_is_op_state':prev_model_config['next_state_is_op_state'],
'use_nash':prev_model_config['use_nash']
}
agent = ConnectFourDQNAgent(**kwargs)
agent.eps = 0
agent.policy_net.load_state_dict(torch.load('model/model_for_play/'+model_name, map_location=device))
agent.update_target_net()
# agent = MinimaxAgent()
pointer = 3
new_pointer = 3
op_pointer = 3
print_board_while_gaming(CF, pointer)
while CF.win==0:
if CF.player==1:
if keyboard.is_pressed("down"):
if CF.board[0,pointer] != 0:
print("이미 가득 찬 곳을 선택하셨습니다. 다시 선택해주세요")
time.sleep(0.1)
continue
for row in range(CF.n_row):
if CF.board[row,pointer] != 0: break
real_time_board = copy.deepcopy(CF.board)
real_time_board[row,pointer] = CF.player
print_board_while_gaming(CF, pointer, board=real_time_board)
time.sleep(0.15)
CF.step_human(pointer)
time.sleep(0.1)
# move left
if keyboard.is_pressed("left"):
if pointer>0: pointer -= 1
print_board_while_gaming(CF, pointer)
time.sleep(0.1)
# move right
if keyboard.is_pressed("right"):
if pointer<CF.n_col-1: pointer += 1
print_board_while_gaming(CF, pointer)
time.sleep(0.1)
else:
print_board_while_gaming(CF, op_pointer)
thinking_time = random.normalvariate(1.2,0.4)
if thinking_time<0: continue
time.sleep(thinking_time)
state_ = env.board_normalization(True,CF, agent.policy_net.model_type)
state = torch.from_numpy(state_).float()
action = agent.select_action(state, CF, player=CF.player)
if isinstance(action, tuple): action = action[0]
while op_pointer != action:
thinking_time = random.normalvariate(0.3,0.5)
if thinking_time<0 or thinking_time>0.6: continue
op_pointer += 1 if op_pointer < action else -1
print_board_while_gaming(CF, op_pointer)
time.sleep(thinking_time)
thinking_time = random.normalvariate(0.5,0.15)
time.sleep(thinking_time)
for row in range(CF.n_row):
if CF.board[row,action] != 0: break
real_time_board = copy.deepcopy(CF.board)
real_time_board[row,action] = CF.player
print_board_while_gaming(CF, action, board=real_time_board)
time.sleep(0.15)
CF.step(action)
op_pointer = action
print_board_while_gaming(CF, pointer)
print_board_while_gaming(CF, pointer)
if CF.win==3:
print("draw!")
else: print("player {} win!".format(int(CF.win)))
# while CF.win==0:
# if CF.player==1:
# col = int(input("어디에 둘 지 고르세요[0~{}]:".format(CF.n_col-1)))
# if col>=CF.n_col or col<0:
# print("잘못된 숫자입니다. 다시 골라주세요")
# continue
# elif CF.board[0,col] != 0:
# print("이미 가득 찬 곳을 선택하셨습니다. 다시 선택해주세요")
# continue
# CF.step_human(col)
# else:
# # random agent가 추가되면 step_cpu() 를 사용하지 않아도 될듯
# # 현재 cpu는 random
# #CF.step_cpu()
# time.sleep(1)
# state_ = env.board_normalization(False,CF, agent.policy_net.model_type)
# state = torch.from_numpy(state_).float()
# action = agent.select_action(state, valid_actions=CF.valid_actions, player=CF.player)
# if isinstance(action, tuple): action = action[0]
# CF.step(action)
# CF.print_board()
# if CF.win==3:
# print("draw!")
# else: print("player {} win!".format(int(CF.win)))