-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTwelveShogi.py
More file actions
132 lines (124 loc) · 5.01 KB
/
Copy pathTwelveShogi.py
File metadata and controls
132 lines (124 loc) · 5.01 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
#-*- encoding: utf-8 -*-
import sys, string, os, getopt
import pygame
from pygame.locals import *
from ShogiBoard import *
from mcts import *
from mcts_pure import MCTSPlayer as MCTS_Pure
from TSAI import *
import time
#initialization
pygame.init()
# Set the window size, the figure size is 569*643,
window = pygame.display.set_mode((569, 670))
# analyze the arguments
startColor = SHOGIMAN_COLOR_UP
mode = 0 # 0 People vs People
# 1 People vs Computer
# 2 Computer vs Computer
try:
opts, args = getopt.getopt(sys.argv[1:], "c:m:")
except getopt.GetoptError:
print('TwelveShogi.py -c <startColor(0 or 1)> -m <mode(0 or 1 or 2)>')
sys.exit()
for opt, arg in opts:
if opt == '-c':
startColor = int(arg)
elif opt == '-m':
mode = int(arg)
shogiboard = ShogiBoard()
shogiboard.curStepColor = startColor
shogiboard.redrawBoard(window)
if mode == 1:
# AIPlayer = TSAI()
AIPlayer = MCTS_Pure()
# AIPlayer = MCTSPlayer()
elif mode == 2:
# AIPlayer = TSAI()
AIPlayer = MCTS_Pure()
trainCounts = 0
# AIPlayer = MCTSPlayer(is_selfplay = 1)
top = 80
left = 70
yGap = 125
xGap = 140
curRow = 3
curCol = 0
#current mouse position
curPos, rc = load_image("./BMP/curPos.bmp", 0xffffff)
window.blit(curPos, (curCol * xGap + left, curRow * yGap + top))
# event loop
while True:
# show update
pygame.display.update()
for event in pygame.event.get():
moveResult = 0
if event.type == pygame.QUIT: # Quit if window closed
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:# Quit if ESC pressed
sys.exit()
if mode == 0 or mode == 1:
# mouse control
leftMouseButton = pygame.mouse.get_pressed()[0]
if leftMouseButton:
(xPos, yPos) = pygame.mouse.get_pos()
# print("TwelveShogi: xPos:%f, yPos:%f" % (xPos, yPos))
# Down prison
if yPos > 18.0000 and yPos < 53.0000:
curRow = SHOGIBOARD_PRISON_ROW_DOWN
curCol = int((xPos - 155) / 40)
# Up prison
elif yPos > 593.000 and yPos < 628.000:
curRow = SHOGIBOARD_PRISON_ROW_UP
curCol = int((xPos - 155) / 40)
else:
curRow = int((yPos - top) / yGap)
curCol = int((xPos - left) / xGap)
# print("TwelveShogi: curRow:%d, curCol:%d" % (curRow, curCol))
moveResult = shogiboard.moveShogiman(curRow, curCol)
if moveResult == 1 and mode == 1:
shogiboard.redrawBoard(window)
shogiboard.showTipInfo(window)
window.blit(curPos,(curCol * xGap + left, curRow * yGap + top))
move = AIPlayer.get_action(board=shogiboard)
result = shogiboard.doMove(move)
if result == 1:
shogiboard.tipInfo = ('game over, up win!')
print('ShogiBoard-moveShogiman: game over, up win!')
shogiboard.resetBoard()
elif result == 2:
shogiboard.tipInfo = ('game over, down win!')
print('ShogiBoard-moveShogiman: game over, down win!')
shogiboard.resetBoard()
elif result == 3:
shogiboard.tipInfo = ('game over, tie!')
print('ShogiBoard-moveShogiman: game over, tie!')
shogiboard.resetBoard()
elif moveResult == 0:
pass
shogiboard.redrawBoard(window)
shogiboard.showTipInfo(window)
window.blit(curPos,(curCol * xGap + left, curRow * yGap + top))
#show update
pygame.display.update()
if mode == 2:
move = AIPlayer.get_action(board=shogiboard)
result = shogiboard.doMove(move)
if result != 0:
if result == 1:
shogiboard.tipInfo = ('game over, up win!')
print('ShogihogiBoard-moveShogiman: game over, up win! train counts: %d' % trainCounts)
shogiboard.resetBoard()
elif result == 2:
shogiboard.tipInfo = ('game over, down win!')
print('ShogiBoard-moveShogiman: game over, down win! train counts: %d' % trainCounts)
shogiboard.resetBoard()
elif result == 3:
shogiboard.tipInfo = ('game over, tie!')
print('ShogiBoard-moveShogiman: game over, tie! train counts: %d' % trainCounts)
shogiboard.resetBoard()
trainCounts = trainCounts + 1
shogiboard.redrawBoard(window)
shogiboard.showTipInfo(window)
window.blit(curPos,(curCol * xGap + left, curRow * yGap + top))