-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathShogiMan.py
187 lines (170 loc) · 5.88 KB
/
ShogiMan.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
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
#-*- encoding: utf-8 -*-
from ShogiGlobal import *
import os
class ShogiMan:
'''
Shogiman Base Class
Data Member: Shogiman type(Wang, Jiang, Xiang, Zi), Shogiman Color(Black, White),
Shogiman current location
Operation: 1. Judge Where Shogiman can go
2. Get Shogiman Image
'''
def getImage(self):
'''
load image based on Shogiman type and color
'''
kind = ""
if SHOGIMAN_KIND_WANG == self.kind:
kind = 'Wang'
if SHOGIMAN_KIND_JIANG == self.kind:
kind = 'Jiang'
if SHOGIMAN_KIND_XIANG == self.kind:
kind = 'Xiang'
if SHOGIMAN_KIND_ZI == self.kind:
kind = 'Zi'
if SHOGIMAN_KIND_HOU == self.kind:
kind = 'Hou'
if SHOGIMAN_KIND_CATCHED_JIANG == self.kind:
kind = 'Catched_Jiang'
if SHOGIMAN_KIND_CATCHED_XIANG == self.kind:
kind = 'Catched_Xiang'
if SHOGIMAN_KIND_CATCHED_ZI == self.kind:
kind = 'Catched_Zi'
color = 'Up'
if SHOGIMAN_COLOR_DOWN == self.color:
color = 'Down'
#print(__file__
curpath = os.path.dirname(__file__)
#print(curpath
curpath = curpath.replace('TwelveShogi.exe', '')
filename = './BMP/' + kind + '_' + color + '.bmp'
writeErrorLog(filename)
return load_image(filename)
def __init__(self, kind, color, row, col):
self.kind = kind
self.color = color
self.row = row
self.col = col
def printInfo(self):
arrKind = ('Wang', 'Jiang', 'Xiang', 'Zi', 'None')
arrColor = ('Up', 'Down')
info = arrColor[self.color] + arrKind[self.kind]
info = info + " row:%d, col:%d, color:%d" % (self.row, self.col, self.color)
print("ShogiMan-printInfo: " + info)
return info
def ShogimanMoveJudge(self, rowTo, colTo):
'''
Judge whether the shogiman can move based on type, current location
and objective location
'''
isSuc = 0
if rowTo < 0 or rowTo >3 or colTo < 0 or colTo > 2:
return isSuc
if SHOGIMAN_KIND_WANG == self.kind:
if abs(rowTo - self.row) <= 1 and abs(colTo - self.col) <= 1:
isSuc = 1
elif SHOGIMAN_KIND_JIANG == self.kind:
if self.row == rowTo and abs(colTo - self.col) == 1:
isSuc = 1
elif self.col == colTo and abs(rowTo - self.row) == 1:
isSuc = 1
elif SHOGIMAN_KIND_XIANG == self.kind:
if abs(rowTo - self.row) == 1 and abs(colTo - self.col) == 1:
isSuc = 1
elif SHOGIMAN_KIND_ZI == self.kind:
if colTo == self.col:
if rowTo - self.row == 1 and self.color == SHOGIMAN_COLOR_DOWN:
isSuc = 1
elif self.row - rowTo == 1 and self.color == SHOGIMAN_COLOR_UP:
isSuc = 1
elif SHOGIMAN_KIND_HOU == self.kind:
if self.row == rowTo and abs(colTo - self.col) == 1:
isSuc = 1
elif self.col == colTo and abs(rowTo - self.row) == 1:
isSuc = 1
elif self.color == SHOGIMAN_COLOR_DOWN and rowTo - self.row == 1 and abs(colTo - self.col) == 1:
isSuc = 1
elif self.color == SHOGIMAN_COLOR_UP and self.row - rowTo == 1 and abs(colTo - self.col) == 1:
isSuc = 1
elif SHOGIMAN_KIND_CATCHED_JIANG == self.kind or SHOGIMAN_KIND_CATCHED_XIANG == self.kind or SHOGIMAN_KIND_CATCHED_ZI == self.kind:
if self.color == SHOGIMAN_COLOR_UP and rowTo >= 1 and rowTo <= 3:
isSuc = 1
elif self.color == SHOGIMAN_COLOR_DOWN and rowTo >=0 and rowTo <=2:
isSuc = 1
# print("ShogiMan-ShogimanMoveJudge: %d" % isSuc)
return isSuc
if __name__ == "__main__":
# Test case
wang = ShogiMan(SHOGIMAN_KIND_WANG, SHOGIMAN_COLOR_UP, 1, 1)
print(wang.ShogimanMoveJudge(0, 0))
print(wang.ShogimanMoveJudge(1, 2))
print(wang.ShogimanMoveJudge(3, 1))
jiang = ShogiMan(SHOGIMAN_KIND_JIANG, SHOGIMAN_COLOR_UP, 1, 1)
print(jiang.ShogimanMoveJudge(1, 2))
print(jiang.ShogimanMoveJudge(2, 2))
xiang = ShogiMan(SHOGIMAN_KIND_XIANG, SHOGIMAN_COLOR_UP, 1, 1)
print(xiang.ShogimanMoveJudge(1, 2))
print(xiang.ShogimanMoveJudge(2, 2))
zi = ShogiMan(SHOGIMAN_KIND_ZI, SHOGIMAN_COLOR_UP, 1, 1)
print(zi.ShogimanMoveJudge(1, 2))
print(zi.ShogimanMoveJudge(1, 0))
print(zi.ShogimanMoveJudge(2, 1))
print(zi.ShogimanMoveJudge(0, 1))
zi = ShogiMan(SHOGIMAN_KIND_ZI, SHOGIMAN_COLOR_DOWN, 1, 1)
print(zi.ShogimanMoveJudge(1, 2))
print(zi.ShogimanMoveJudge(1, 0))
# o o o
# o o o
# o x o
# o o o
hou = ShogiMan(SHOGIMAN_KIND_HOU, SHOGIMAN_COLOR_UP, 2, 1)
# o o o
# o x o
# o o o
# o o o
print(hou.ShogimanMoveJudge(1, 1))
# o o o
# o o o
# o o o
# o x o
print(hou.ShogimanMoveJudge(3, 1))
# o o o
# o o o
# x o o
# o o o
print(hou.ShogimanMoveJudge(2, 0))
# o o o
# o o o
# o o x
# o o o
print(hou.ShogimanMoveJudge(2, 2))
# o o o
# o o o
# o o o
# x o o
print(hou.ShogimanMoveJudge(3, 0))
# o o o
# o o o
# o o o
# o o x
print(hou.ShogimanMoveJudge(3, 2))
# o o o
# o x o
# o o o
# o o o
hou = ShogiMan(SHOGIMAN_KIND_HOU, SHOGIMAN_COLOR_DOWN, 1, 1)
# x o o
# o o o
# o o o
# o o o
print(hou.ShogimanMoveJudge(0, 0))
# o o o
# o o o
# o x o
# o o o
print(hou.ShogimanMoveJudge(2, 1))
# o o o
# o o o
# o o o
# x o o
print(hou.ShogimanMoveJudge(3, 0))