forked from deepseasw/OmokQLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OmokPlay.py
115 lines (80 loc) · 2.67 KB
/
OmokPlay.py
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
# -*- coding: utf-8 -*-
from OmokTrain import OmokEnvironment, X, W1, b1, input_layer, W2, b2, hidden_layer, W3, b3, output_layer, Y, cost, optimizer
import tensorflow as tf
import numpy as np
import random
import math
import os
import sys
import time
#------------------------------------------------------------
# 변수 설정
#------------------------------------------------------------
STONE_NONE = 0
STONE_PLAYER1 = 1
STONE_PLAYER2 = 2
gridSize = 10
#------------------------------------------------------------
#------------------------------------------------------------
# 화면 출력 함수
#------------------------------------------------------------
def showBoard(env):
for y in xrange(gridSize):
for x in xrange(gridSize):
if( env.state[y * gridSize + x] == STONE_PLAYER1 ):
sys.stdout.write('O')
elif( env.state[y * gridSize + x] == STONE_PLAYER2 ):
sys.stdout.write('X')
else:
sys.stdout.write('.')
sys.stdout.write('\n')
sys.stdout.write('\n')
#------------------------------------------------------------
#------------------------------------------------------------
# 게임 플레이 함수
#------------------------------------------------------------
def playGame(env, sess):
env.reset()
gameOver = False
currentPlayer = STONE_PLAYER1
while( gameOver != True ):
action = - 9999
if( currentPlayer == STONE_PLAYER1 ):
currentState = env.getState()
else:
currentState = env.getStateInverse()
action = env.getAction(sess, currentState)
nextState, reward, gameOver = env.act(currentPlayer, action)
showBoard(env)
time.sleep(3)
if( currentPlayer == STONE_PLAYER1 ):
currentPlayer = STONE_PLAYER2
else:
currentPlayer = STONE_PLAYER1
#------------------------------------------------------------
#------------------------------------------------------------
# 메인 함수
#------------------------------------------------------------
def main(_):
# 환경 인스턴스 생성
env = OmokEnvironment(gridSize)
# 텐서플로우 초기화
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# 세이브 설정
saver = tf.train.Saver()
# 모델 로드
if( os.path.isfile(os.getcwd() + "/OmokModel.ckpt.index") == True ):
saver.restore(sess, os.getcwd() + "/OmokModel.ckpt")
print('saved model is loaded!')
# 게임 플레이
playGame(env, sess)
# 세션 종료
sess.close()
#------------------------------------------------------------
#------------------------------------------------------------
# 메인 함수 실행
#------------------------------------------------------------
if __name__ == '__main__':
tf.app.run()
#------------------------------------------------------------