Compare commits
18 Commits
Author | SHA1 | Date |
---|---|---|
|
2771cb9d53 | 3 years ago |
|
f1db685d34 | 3 years ago |
|
f1d23c7e2c | 3 years ago |
|
82bba6d366 | 3 years ago |
|
00bedd6254 | 3 years ago |
|
39f0f8904f | 3 years ago |
|
8adcfb9ffa | 3 years ago |
|
79a7312cc3 | 3 years ago |
|
73eb23d13f | 3 years ago |
|
d1037b57c3 | 3 years ago |
|
7a388bfad2 | 3 years ago |
|
f2bb19e189 | 3 years ago |
|
49b3ed55ee | 3 years ago |
|
b3743ffc75 | 3 years ago |
|
4178846802 | 3 years ago |
|
d7ba67d5b3 | 3 years ago |
|
fcb7f1b95f | 3 years ago |
|
d77569a648 | 3 years ago |
@ -0,0 +1 @@
|
||||
main1.py
|
@ -0,0 +1,95 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="375dd140-0c03-4650-b68b-e13354b63b81" name="Changes" comment="" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="Git.Settings">
|
||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="MarkdownSettingsMigration">
|
||||
<option name="stateVersion" value="1" />
|
||||
</component>
|
||||
<component name="ProjectId" id="2DqehZ2YnATdfUzD9pHpNQK1ttM" />
|
||||
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent">{
|
||||
"keyToString": {
|
||||
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"last_opened_file_path": "E:/快乐大学生活/程序设计综合实践/Jungle"
|
||||
}
|
||||
}</component>
|
||||
<component name="RunManager">
|
||||
<configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
|
||||
<module name="Jungle" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
<option name="PARENT_ENVS" value="true" />
|
||||
<envs>
|
||||
<env name="PYTHONUNBUFFERED" value="1" />
|
||||
</envs>
|
||||
<option name="SDK_HOME" value="E:\ProgramData\Anaconda3\python.exe" />
|
||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src" />
|
||||
<option name="IS_MODULE_SDK" value="false" />
|
||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/main.py" />
|
||||
<option name="PARAMETERS" value="" />
|
||||
<option name="SHOW_COMMAND_LINE" value="false" />
|
||||
<option name="EMULATE_TERMINAL" value="false" />
|
||||
<option name="MODULE_MODE" value="false" />
|
||||
<option name="REDIRECT_INPUT" value="false" />
|
||||
<option name="INPUT_FILE" value="" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="main1" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
|
||||
<module name="Jungle" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
<option name="PARENT_ENVS" value="true" />
|
||||
<envs>
|
||||
<env name="PYTHONUNBUFFERED" value="1" />
|
||||
</envs>
|
||||
<option name="SDK_HOME" value="E:\ProgramData\Anaconda3\python.exe" />
|
||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src" />
|
||||
<option name="IS_MODULE_SDK" value="false" />
|
||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/main1.py" />
|
||||
<option name="PARAMETERS" value="" />
|
||||
<option name="SHOW_COMMAND_LINE" value="false" />
|
||||
<option name="EMULATE_TERMINAL" value="false" />
|
||||
<option name="MODULE_MODE" value="false" />
|
||||
<option name="REDIRECT_INPUT" value="false" />
|
||||
<option name="INPUT_FILE" value="" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||
<component name="TaskManager">
|
||||
<task active="true" id="Default" summary="Default task">
|
||||
<changelist id="375dd140-0c03-4650-b68b-e13354b63b81" name="Changes" comment="" />
|
||||
<created>1661431161619</created>
|
||||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1661431161619</updated>
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
<component name="Vcs.Log.Tabs.Properties">
|
||||
<option name="TAB_STATES">
|
||||
<map>
|
||||
<entry key="MAIN">
|
||||
<value>
|
||||
<State />
|
||||
</value>
|
||||
</entry>
|
||||
</map>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,249 @@
|
||||
import copy
|
||||
from typing import List, Tuple
|
||||
import pygame
|
||||
import heapq
|
||||
import socket_client as s
|
||||
from main import right_move, left_move
|
||||
import init
|
||||
|
||||
inf = 99999999
|
||||
MAP1 = init.clac_MAP1()
|
||||
|
||||
def getMAP(Animals):
|
||||
copyMAP = [[0 for i in range(7)] for j in range(9)]
|
||||
for animalId in range(1, len(Animals)):
|
||||
x, y = Animals[animalId]['old_x'], Animals[animalId]['old_y']
|
||||
copyMAP[x][y] = animalId
|
||||
return copyMAP
|
||||
|
||||
|
||||
class PriorityQueue:
|
||||
|
||||
def __init__(self):
|
||||
self.heap = []
|
||||
self.count = 0
|
||||
|
||||
def push(self, item, priority):
|
||||
entry = (priority, self.count, item)
|
||||
heapq.heappush(self.heap, entry)
|
||||
self.count += 1
|
||||
|
||||
def pop(self):
|
||||
(_, _, item) = heapq.heappop(self.heap)
|
||||
return item
|
||||
|
||||
def isEmpty(self):
|
||||
return len(self.heap) == 0
|
||||
|
||||
def update(self, item, priority):
|
||||
|
||||
for index, (p, c, i) in enumerate(self.heap):
|
||||
if i == item:
|
||||
if p <= priority:
|
||||
break
|
||||
del self.heap[index]
|
||||
self.heap.append((priority, c, item))
|
||||
heapq.heapify(self.heap)
|
||||
break
|
||||
else:
|
||||
self.push(item, priority)
|
||||
|
||||
|
||||
def getMovement(Id, Animals):
|
||||
MAP = getMAP(Animals)
|
||||
Movements = []
|
||||
o_x, o_y = Animals[Id]['old_x'], Animals[Id]['old_y']
|
||||
for x in range(len(MAP)):
|
||||
for y in range(len(MAP[0])):
|
||||
if 1 <= Id <= 7:
|
||||
if right_move(o_x, o_y, x, y, Id, MAP, MAP1):
|
||||
move = (Id, x - o_x, y - o_y)
|
||||
Movements.append(move)
|
||||
else:
|
||||
if left_move(o_x, o_y, x, y, Id, MAP, MAP1):
|
||||
move = (Id, x - o_x, y - o_y)
|
||||
Movements.append(move)
|
||||
return Movements
|
||||
|
||||
|
||||
def getSuccessors(Animals, turn: int):
|
||||
Successors = []
|
||||
Actions = []
|
||||
if turn == 0:
|
||||
# 1 ~ 7
|
||||
for Id in range(1, 8):
|
||||
Move = getMovement(Id, Animals)
|
||||
for oneStep in Move:
|
||||
Actions.append(oneStep)
|
||||
|
||||
else:
|
||||
# 8 ~ 14
|
||||
for Id in range(8, 15):
|
||||
Move = getMovement(Id, Animals)
|
||||
for oneStep in Move:
|
||||
Actions.append(oneStep)
|
||||
|
||||
# update the map
|
||||
for action in Actions:
|
||||
Id, dx, dy = action[0], action[1], action[2]
|
||||
t_Animals = copy.deepcopy(Animals)
|
||||
x = Animals[Id]['old_x'] + dx
|
||||
y = Animals[Id]['old_y'] + dy
|
||||
t_Animals[Id]['x'] = x
|
||||
t_Animals[Id]['y'] = y
|
||||
target = (Id, x, y)
|
||||
#print(t_Animals)
|
||||
Successors.append((t_Animals, target))
|
||||
# updateMAP()
|
||||
return Successors
|
||||
|
||||
|
||||
class Information:
|
||||
def __init__(self):
|
||||
self.piece_score = {1: 100, 8: 100,
|
||||
2: 70, 9: 70,
|
||||
3: 60, 10: 60,
|
||||
4: 80, 11: 80,
|
||||
5: 90, 12: 90,
|
||||
6: 120, 13: 120,
|
||||
7: 120, 14: 120
|
||||
}
|
||||
self.mouse_score = [[11, 25, 50, 100000, 50, 25, 13],
|
||||
[11, 20, 25, 50, 25, 20, 13],
|
||||
[10, 15, 20, 20, 20, 15, 13],
|
||||
[8, 9, 9, 11, 12, 13, 13],
|
||||
[8, 8, 8, 9, 11, 12, 12],
|
||||
[8, 8, 8, 9, 11, 12, 11],
|
||||
[8, 8, 8, 9, 10, 10, 10],
|
||||
[8, 8, 8, 9, 9, 9, 9],
|
||||
[8, 8, 8, 0, 8, 8, 8]]
|
||||
|
||||
self.eagle_score = [[11, 15, 50, 100000, 50, 15, 11],
|
||||
[12, 12, 20, 50, 20, 15, 12],
|
||||
[14, 15, 20, 20, 20, 14, 14],
|
||||
[13, 0, 0, 13, 0, 0, 13],
|
||||
[12, 0, 0, 12, 0, 0, 12],
|
||||
[11, 0, 0, 11, 0, 0, 11],
|
||||
[10, 10, 10, 10, 10, 10, 10],
|
||||
[8, 8, 8, 8, 8, 8, 8],
|
||||
[8, 8, 8, 0, 8, 8, 8]]
|
||||
|
||||
self.fox_score = [[11, 15, 50, 100000, 50, 15, 11],
|
||||
[12, 12, 20, 50, 20, 15, 12],
|
||||
[14, 15, 20, 20, 20, 14, 14],
|
||||
[13, 0, 0, 13, 0, 0, 13],
|
||||
[12, 0, 0, 12, 0, 0, 12],
|
||||
[11, 0, 0, 11, 0, 0, 11],
|
||||
[10, 10, 10, 11, 10, 10, 10],
|
||||
[8, 8, 9, 10, 9, 8, 8],
|
||||
[8, 8, 10, 0, 10, 8, 8]]
|
||||
|
||||
self.wolf_score = [[11, 15, 50, 100000, 50, 15, 11],
|
||||
[12, 12, 20, 50, 20, 15, 12],
|
||||
[14, 15, 20, 20, 20, 14, 14],
|
||||
[13, 0, 0, 13, 0, 0, 13],
|
||||
[12, 0, 0, 12, 0, 0, 12],
|
||||
[11, 0, 0, 11, 0, 0, 11],
|
||||
[10, 10, 10, 11, 10, 10, 10],
|
||||
[8, 8, 9, 10, 9, 8, 8],
|
||||
[8, 8, 10, 0, 10, 8, 8]]
|
||||
|
||||
self.leopard_score = [[11, 15, 50, 100000, 50, 15, 11],
|
||||
[12, 12, 20, 50, 20, 15, 12],
|
||||
[14, 15, 20, 20, 20, 14, 14],
|
||||
[13, 0, 0, 13, 0, 0, 13],
|
||||
[12, 0, 0, 12, 0, 0, 12],
|
||||
[11, 0, 0, 11, 0, 0, 11],
|
||||
[10, 10, 10, 11, 10, 10, 10],
|
||||
[8, 8, 9, 10, 9, 8, 8],
|
||||
[8, 8, 10, 0, 10, 8, 8]]
|
||||
|
||||
self.lion_score = [[20, 40, 50, 100000, 50, 40, 20],
|
||||
[20, 25, 40, 50, 40, 25, 20],
|
||||
[18, 30, 30, 20, 30, 30, 18],
|
||||
[15, 0, 0, 15, 0, 0, 15],
|
||||
[12, 0, 0, 15, 0, 0, 12],
|
||||
[11, 0, 0, 15, 0, 0, 11],
|
||||
[14, 16, 16, 9, 16, 16, 14],
|
||||
[12, 12, 12, 12, 12, 12, 12],
|
||||
[5, 12, 12, 0, 12, 12, 5]]
|
||||
|
||||
self.elephant_score = [[20, 40, 50, 100000, 50, 40, 20],
|
||||
[20, 25, 40, 50, 40, 25, 20],
|
||||
[18, 30, 30, 20, 30, 30, 18],
|
||||
[15, 0, 0, 15, 0, 0, 15],
|
||||
[12, 0, 0, 15, 0, 0, 12],
|
||||
[11, 0, 0, 15, 0, 0, 11],
|
||||
[14, 16, 16, 9, 16, 16, 14],
|
||||
[12, 12, 12, 12, 12, 12, 12],
|
||||
[5, 12, 12, 0, 12, 12, 5]]
|
||||
|
||||
self.position_score = {1: self.mouse_score,
|
||||
8: self.mouse_score[::-1],
|
||||
2: self.eagle_score,
|
||||
9: self.eagle_score[::-1],
|
||||
3: self.fox_score,
|
||||
10: self.fox_score[::-1],
|
||||
4: self.wolf_score,
|
||||
11: self.wolf_score[::-1],
|
||||
5: self.leopard_score,
|
||||
12: self.leopard_score[::-1],
|
||||
6: self.lion_score,
|
||||
13: self.lion_score[::-1],
|
||||
7: self.elephant_score,
|
||||
14: self.elephant_score[::-1]
|
||||
}
|
||||
|
||||
|
||||
def Heuristic(Animals, pTurn):
|
||||
score = 0
|
||||
information = Information()
|
||||
piece_score = information.piece_score
|
||||
for animalId in range(1, len(Animals)):
|
||||
x, y = Animals[animalId]['x'], Animals[animalId]['y']
|
||||
|
||||
if 1 <= animalId <= 14:
|
||||
animalIdValue = information.position_score[animalId][x][y]
|
||||
if pTurn == 0:
|
||||
if 1 <= animalId <= 7:
|
||||
score += (animalIdValue + piece_score[animalId])
|
||||
else:
|
||||
score -= (animalIdValue + piece_score[animalId])
|
||||
else:
|
||||
if 1 <= animalId <= 7:
|
||||
score -= (animalIdValue + piece_score[animalId])
|
||||
else:
|
||||
score += (animalIdValue + piece_score[animalId])
|
||||
return score
|
||||
|
||||
|
||||
def depthLimitedSearch2(curAnimals, turn): # depth = 2
|
||||
Successors = getSuccessors(curAnimals, turn)
|
||||
tempNextMove = [(Successor, inf) for Successor in Successors]
|
||||
nextMove = []
|
||||
if turn:
|
||||
turn2 = 0
|
||||
else:
|
||||
turn2 = 1
|
||||
for Move in tempNextMove:
|
||||
Successors2 = getSuccessors(Move[0][0], turn2)
|
||||
nextMove2 = [(Successor2, Heuristic(Successor2[0], turn2)) for Successor2 in Successors2]
|
||||
max2 = -inf
|
||||
for action2 in nextMove2:
|
||||
if action2[1] > max2:
|
||||
max2 = action2[1]
|
||||
nextMove.append((Move, max2))
|
||||
min1 = inf
|
||||
index = 0
|
||||
for i in range(len(nextMove)):
|
||||
if nextMove[i][1] < min1:
|
||||
min1 = nextMove[i][1]
|
||||
index = i
|
||||
return nextMove[index][0][0][1]
|
||||
|
||||
|
||||
axis = [(0, 0), (2, 6), (1, 1), (1, 5), (2, 2), (2, 4), (2, 3), (2, 0), (6, 0), (7, 5), (7, 1), (6, 4), (6, 2),
|
||||
(6, 3), (6, 6)]
|
||||
Axis = s.Chessmen_axis(axis)
|
||||
Animals = Axis.axis
|
||||
print(depthLimitedSearch2(Animals, 0))
|
@ -0,0 +1 @@
|
||||
# Chess
|
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 1.8 KiB |
After Width: | Height: | Size: 1.8 KiB |
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 2.2 KiB |
After Width: | Height: | Size: 1.6 KiB |
After Width: | Height: | Size: 4.0 KiB |
After Width: | Height: | Size: 4.2 KiB |
After Width: | Height: | Size: 516 B |
After Width: | Height: | Size: 3.2 KiB |
After Width: | Height: | Size: 3.4 KiB |
After Width: | Height: | Size: 3.4 KiB |
After Width: | Height: | Size: 2.5 KiB |
After Width: | Height: | Size: 4.2 KiB |
After Width: | Height: | Size: 2.7 KiB |
@ -0,0 +1,548 @@
|
||||
import pygame, win32api, win32con
|
||||
from copy import copy
|
||||
from os import _exit
|
||||
from collections import Counter as mset
|
||||
pygame.init()
|
||||
size = width, height = win32api.GetSystemMetrics(win32con.SM_CXSCREEN), win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
|
||||
ul = 135 * height // 1080
|
||||
def get_rect(i, pos) :
|
||||
j = i.get_rect()
|
||||
j.left, j.top = pos
|
||||
return j
|
||||
def draw() :
|
||||
print("Draw!!!")
|
||||
my_font = pygame.font.SysFont("Consolas", 200)
|
||||
time_surf = my_font.render("DRAW!!!", 1, [255, 255, 255])
|
||||
gor = time_surf.get_rect()
|
||||
gor.center = width / 2, height / 2
|
||||
screen.blit(time_surf, gor)
|
||||
pygame.display.update()
|
||||
running2 = True
|
||||
while running2:
|
||||
for event2 in pygame.event.get():
|
||||
if event2.type == pygame.KEYDOWN:
|
||||
if event2.key == pygame.K_ESCAPE:
|
||||
_exit(0)
|
||||
|
||||
def refresh() :
|
||||
global wking, bking
|
||||
wking = None
|
||||
bking = None
|
||||
for i in rects :
|
||||
for j in i :
|
||||
if j :
|
||||
if type(j) == King :
|
||||
if j.c :
|
||||
wking = j
|
||||
else :
|
||||
bking = j
|
||||
else :
|
||||
j.access(rects)
|
||||
wking.access(rects)
|
||||
bking.access(rects)
|
||||
def tryout() :
|
||||
global canmove
|
||||
canmove = False
|
||||
for i in rects :
|
||||
for j in i :
|
||||
if j and j.c == times :
|
||||
for k, t in j.acc + j.eat :
|
||||
for i2 in occ :
|
||||
for j2 in range(len(i2)) :
|
||||
i2[j2] = []
|
||||
rects2 = []
|
||||
for x, i2 in enumerate(rects):
|
||||
rects2.append([])
|
||||
for j2 in i2:
|
||||
rects2[x].append(copy(j2))
|
||||
rects2[k][t] = copy(j)
|
||||
rects2[k][t].x, rects2[k][t].y = k, t
|
||||
rects2[j.x][j.y] = None
|
||||
wking = None
|
||||
bking = None
|
||||
for i2 in rects2:
|
||||
for j2 in i2:
|
||||
if j2:
|
||||
if type(j2) == King:
|
||||
if j2.c:
|
||||
wking = j2
|
||||
else:
|
||||
bking = j2
|
||||
j2.access(rects2)
|
||||
for i2 in rects2 :
|
||||
for j2 in i2 :
|
||||
if j2 and j2.c != times :
|
||||
for k2, t2 in j2.eat :
|
||||
occ[k2][t2].append(j2)
|
||||
if times :
|
||||
if occ[wking.x][wking.y] :
|
||||
if [k, t] in j.acc :
|
||||
j.acc.remove([k, t])
|
||||
else :
|
||||
j.eat.remove([k, t])
|
||||
else :
|
||||
canmove = True
|
||||
else :
|
||||
if occ[bking.x][bking.y] :
|
||||
a = occ[bking.x][bking.y]
|
||||
if [k, t] in j.acc :
|
||||
j.acc.remove([k, t])
|
||||
else :
|
||||
j.eat.remove([k, t])
|
||||
else :
|
||||
canmove = True
|
||||
def ok(x, y) :
|
||||
return x >= 0 and x < 8 and y >= 0 and y < 8
|
||||
class Base :
|
||||
def __init__(self, c, w, b):
|
||||
self.c = c
|
||||
if self.c:
|
||||
self.img = w
|
||||
else:
|
||||
self.img = b
|
||||
self.x = self.y = 0
|
||||
self.acc = []
|
||||
self.eat = []
|
||||
self.spe = []
|
||||
self.pro = []
|
||||
self.used = False
|
||||
def hint(self):
|
||||
for i, j in self.acc :
|
||||
htrect.center = pos[i][j].center
|
||||
screen.blit(ht, htrect)
|
||||
for i, j in self.eat :
|
||||
screen.blit(cpht, pos[i][j])
|
||||
def access(self, rects):
|
||||
pass
|
||||
def rookacc(self, rects):
|
||||
for i in range(self.x + 1, 8) :
|
||||
if rects[i][self.y] :
|
||||
if rects[i][self.y].c != self.c :
|
||||
self.eat.append([i, self.y])
|
||||
else :
|
||||
self.pro.append([i, self.y])
|
||||
break
|
||||
self.acc.append([i, self.y])
|
||||
for i in range(self.x - 1, -1, -1) :
|
||||
if rects[i][self.y] :
|
||||
if rects[i][self.y].c != self.c :
|
||||
self.eat.append([i, self.y])
|
||||
else :
|
||||
self.pro.append([i, self.y])
|
||||
break
|
||||
self.acc.append([i, self.y])
|
||||
|
||||
for i in range(self.y + 1, 8) :
|
||||
if rects[self.x][i] :
|
||||
if rects[self.x][i].c != self.c :
|
||||
self.eat.append([self.x, i])
|
||||
else :
|
||||
self.pro.append([self.x, i])
|
||||
break
|
||||
self.acc.append([self.x, i])
|
||||
for i in range(self.y - 1, -1, -1) :
|
||||
if rects[self.x][i] :
|
||||
if rects[self.x][i].c != self.c :
|
||||
self.eat.append([self.x, i])
|
||||
else :
|
||||
self.pro.append([self.x, i])
|
||||
break
|
||||
self.acc.append([self.x, i])
|
||||
|
||||
def bisacc(self, rects):
|
||||
for i in range(self.x + 1, 8) :
|
||||
j = self.x + self.y - i
|
||||
if not ok(i, j) :
|
||||
break
|
||||
if rects[i][j] :
|
||||
if rects[i][j].c != self.c:
|
||||
self.eat.append([i, j])
|
||||
else :
|
||||
self.pro.append([i, j])
|
||||
break
|
||||
self.acc.append([i, j])
|
||||
for i in range(self.x - 1, -1, -1) :
|
||||
j = self.x + self.y - i
|
||||
if not ok(i, j) :
|
||||
break
|
||||
if rects[i][j] :
|
||||
if rects[i][j].c != self.c :
|
||||
self.eat.append([i, j])
|
||||
else :
|
||||
self.pro.append([i, j])
|
||||
break
|
||||
self.acc.append([i, j])
|
||||
|
||||
for i in range(self.x + 1, 8):
|
||||
j = i + self.y - self.x
|
||||
if not ok(i, j) :
|
||||
break
|
||||
if rects[i][j]:
|
||||
if rects[i][j].c != self.c:
|
||||
self.eat.append([i, j])
|
||||
else :
|
||||
self.pro.append([i, j])
|
||||
break
|
||||
self.acc.append([i, j])
|
||||
for i in range(self.x - 1, -1, -1):
|
||||
j = i + self.y - self.x
|
||||
if not ok(i, j) :
|
||||
break
|
||||
if rects[i][j]:
|
||||
if rects[i][j].c != self.c:
|
||||
self.eat.append([i, j])
|
||||
else :
|
||||
self.pro.append([i, j])
|
||||
break
|
||||
self.acc.append([i, j])
|
||||
# def move(self):
|
||||
|
||||
class King(Base) :
|
||||
def __init__(self, c):
|
||||
Base.__init__(self, c, wk, bk)
|
||||
|
||||
def access(self, rects):
|
||||
self.acc = []
|
||||
self.eat = []
|
||||
self.pro = []
|
||||
self.spe = []
|
||||
dir = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
|
||||
for i, j in dir :
|
||||
tx, ty = self.x + i, self.y + j
|
||||
if ok(tx, ty) :
|
||||
if rects[tx][ty] :
|
||||
if rects[tx][ty].c != self.c :
|
||||
self.eat.append([tx, ty])
|
||||
else :
|
||||
self.pro.append([tx, ty])
|
||||
else :
|
||||
self.acc.append([tx, ty])
|
||||
t = 7 if self.c else 0
|
||||
r1 = rects[0][t]
|
||||
r2 = rects[7][t]
|
||||
for i2 in occ:
|
||||
for j2 in range(len(i2)):
|
||||
i2[j2] = []
|
||||
for i2 in rects:
|
||||
for j2 in i2:
|
||||
if j2 and j2.c != times:
|
||||
for k2, t2 in j2.acc:
|
||||
occ[k2][t2].append(j2)
|
||||
if not self.used and r1 and not r1.used and not rects[1][t] and not rects[2][t] and not rects[3][t] :
|
||||
if not occ[1][t] and not occ[2][t] and not occ[3][t] and not occ[4][t]:
|
||||
self.acc.append([2, t])
|
||||
self.spe.append([2, t])
|
||||
if not self.used and r2 and not r2.used and not rects[5][t] and not rects[6][t] :
|
||||
if not occ[4][t] and not occ[5][t] and not occ[6][t] :
|
||||
self.acc.append([6, t])
|
||||
self.spe.append([6, t])
|
||||
|
||||
class Queen(Base) :
|
||||
def __init__(self, c):
|
||||
Base.__init__(self, c, wq, bq)
|
||||
def access(self, rects):
|
||||
self.acc = []
|
||||
self.eat = []
|
||||
self.pro = []
|
||||
self.bisacc(rects)
|
||||
self.rookacc(rects)
|
||||
|
||||
class Bishop(Base) :
|
||||
def __init__(self, c):
|
||||
Base.__init__(self, c, wb, bb)
|
||||
def access(self, rects):
|
||||
self.acc = []
|
||||
self.eat = []
|
||||
self.pro = []
|
||||
self.bisacc(rects)
|
||||
|
||||
class Knight(Base) :
|
||||
def __init__(self, c):
|
||||
Base.__init__(self, c, wn, bn)
|
||||
def access(self, rects):
|
||||
self.acc = []
|
||||
self.eat = []
|
||||
self.pro = []
|
||||
dir = [[-2, -1], [-2, 1], [2, -1], [2, 1], [-1, -2], [1, -2], [-1, 2], [1, 2]]
|
||||
for i, j in dir:
|
||||
tx, ty = self.x + i, self.y + j
|
||||
if ok(tx, ty):
|
||||
if rects[tx][ty] :
|
||||
if rects[tx][ty].c != self.c:
|
||||
self.eat.append([tx, ty])
|
||||
else :
|
||||
self.pro.append([tx, ty])
|
||||
else:
|
||||
self.acc.append([tx, ty])
|
||||
|
||||
class Rook(Base) :
|
||||
def __init__(self, c):
|
||||
Base.__init__(self, c, wr, br)
|
||||
def access(self, rects):
|
||||
self.acc = []
|
||||
self.eat = []
|
||||
self.pro = []
|
||||
self.rookacc(rects)
|
||||
|
||||
class Pawn(Base) :
|
||||
def __init__(self, c):
|
||||
Base.__init__(self, c, wp, bp)
|
||||
def access(self, rects):
|
||||
self.acc = []
|
||||
self.eat = []
|
||||
self.spe = []
|
||||
self.pro = []
|
||||
d = 1 if self.c else -1
|
||||
if not self.used and not rects[self.x][self.y - 2 * d] and not rects[self.x][self.y - d] :
|
||||
self.acc.append([self.x, self.y - 2 * d])
|
||||
if ok(self.x, self.y - d) :
|
||||
if not rects[self.x][self.y - d] :
|
||||
self.acc.append([self.x, self.y - d])
|
||||
if self.x > 0 and ok(self.x, self.y - d) :
|
||||
if rects[self.x - 1][self.y - d] :
|
||||
if rects[self.x - 1][self.y - d].c != self.c :
|
||||
self.eat.append([self.x - 1, self.y - d])
|
||||
else :
|
||||
self.pro.append([self.x - 1, self.y - d])
|
||||
if self.x < 7 and ok(self.x, self.y - d) :
|
||||
if rects[self.x + 1][self.y - d] :
|
||||
if rects[self.x + 1][self.y - d].c != self.c :
|
||||
self.eat.append([self.x + 1, self.y - d])
|
||||
else :
|
||||
self.pro.append([self.x + 1, self.y - d])
|
||||
if lastmove and type(lastmove[0]) == Pawn and lastmove[0].c != self.c and abs(lastmove[0].x - self.x) == 1 and lastmove[1].y == self.y :
|
||||
if not self.c and lastmove[0].y == 6 and lastmove[1].y == 4 :
|
||||
self.acc.append([lastmove[0].x, 5])
|
||||
self.spe.append([lastmove[0].x, 5])
|
||||
elif self.c and lastmove[0].y == 1 and lastmove[1].y == 3 :
|
||||
self.acc.append([lastmove[0].x, 2])
|
||||
self.spe.append([lastmove[0].x, 2])
|
||||
|
||||
chessboard = pygame.transform.scale(pygame.image.load("chessboard.png"), (1080, 1080))
|
||||
bk = pygame.transform.scale(pygame.image.load("bk.png"), (ul, ul))
|
||||
bq = pygame.transform.scale(pygame.image.load("bq.png"), (ul, ul))
|
||||
bb = pygame.transform.scale(pygame.image.load("bb.png"), (ul, ul))
|
||||
bn = pygame.transform.scale(pygame.image.load("bn.png"), (ul, ul))
|
||||
br = pygame.transform.scale(pygame.image.load("br.png"), (ul, ul))
|
||||
bp = pygame.transform.scale(pygame.image.load("bp.png"), (ul, ul))
|
||||
wk = pygame.transform.scale(pygame.image.load("wk.png"), (ul, ul))
|
||||
wq = pygame.transform.scale(pygame.image.load("wq.png"), (ul, ul))
|
||||
wb = pygame.transform.scale(pygame.image.load("wb.png"), (ul, ul))
|
||||
wn = pygame.transform.scale(pygame.image.load("wn.png"), (ul, ul))
|
||||
wr = pygame.transform.scale(pygame.image.load("wr.png"), (ul, ul))
|
||||
wp = pygame.transform.scale(pygame.image.load("wp.png"), (ul, ul))
|
||||
ht = pygame.image.load("hint.png")
|
||||
htrect = ht.get_rect()
|
||||
cpht = pygame.image.load("capture-hint.png")
|
||||
rects = []
|
||||
pos = []
|
||||
occ = []
|
||||
canmove = False
|
||||
for i in range(8) :
|
||||
rects.append([])
|
||||
pos.append([])
|
||||
occ.append([])
|
||||
for j in range(8) :
|
||||
occ[i].append([])
|
||||
rects[i].append(None)
|
||||
recthere = wk.get_rect()
|
||||
recthere.left, recthere.top = (width - height) / 2 + ul * i, ul * j
|
||||
pos[i].append(recthere)
|
||||
dir = (1, 0), (-1, 0), (0, 1), (0, -1)
|
||||
times = 1
|
||||
lastmove = []
|
||||
selected = False
|
||||
npawnm = 0
|
||||
ncapm = 0
|
||||
chesslst = []
|
||||
def scan() :
|
||||
tmp2 = []
|
||||
for i in rects :
|
||||
tmp = []
|
||||
for j in i :
|
||||
if j :
|
||||
tmp.append(type(j))
|
||||
yield type(j)
|
||||
else :
|
||||
tmp.append(j)
|
||||
tmp2.append(tmp)
|
||||
chesslst.append(tmp2)
|
||||
def add(i, x, y) :
|
||||
i.x, i.y = x - 1, y - 1
|
||||
rects[x - 1][y - 1] = i
|
||||
add(King(0), 5, 1)
|
||||
add(Queen(0), 4, 1)
|
||||
add(Bishop(0), 3, 1)
|
||||
add(Bishop(0), 6, 1)
|
||||
add(Knight(0), 2, 1)
|
||||
add(Knight(0), 7, 1)
|
||||
add(Rook(0), 1, 1)
|
||||
add(Rook(0), 8, 1)
|
||||
for i in range(8) :
|
||||
add(Pawn(0), i + 1, 2)
|
||||
|
||||
|
||||
add(King(1), 5, 8)
|
||||
add(Queen(1), 4, 8)
|
||||
add(Bishop(1), 3, 8)
|
||||
add(Bishop(1), 6, 8)
|
||||
add(Knight(1), 2, 8)
|
||||
add(Knight(1), 7, 8)
|
||||
add(Rook(1), 1, 8)
|
||||
add(Rook(1), 8, 8)
|
||||
for i in range(8) :
|
||||
add(Pawn(1), i + 1, 7)
|
||||
scan()
|
||||
refresh()
|
||||
running = True
|
||||
|
||||
screen = pygame.display.set_mode(size, flags=pygame.FULLSCREEN)
|
||||
def animate() :
|
||||
screen.blit(chessboard, [(width - height) / 2, 0])
|
||||
if selected :
|
||||
selected.hint()
|
||||
for x, i in enumerate(rects) :
|
||||
for y, j in enumerate(i) :
|
||||
if j :
|
||||
screen.blit(j.img, [(width - height) / 2 + ul * x, ul * y])
|
||||
pygame.display.flip()
|
||||
|
||||
while running :
|
||||
for event in pygame.event.get() :
|
||||
if event.type == pygame.QUIT :
|
||||
running = False
|
||||
elif event.type == pygame.MOUSEBUTTONDOWN :
|
||||
mousex, mousey = pygame.mouse.get_pos()
|
||||
x = y = z = j = False
|
||||
for y, i in enumerate(rects) :
|
||||
for z, j in enumerate(i) :
|
||||
if pos[y][z].collidepoint(mousex, mousey):
|
||||
x = j
|
||||
break
|
||||
if pos[y][z].collidepoint(mousex, mousey):
|
||||
break
|
||||
if x is False :
|
||||
continue
|
||||
if selected :
|
||||
if [y, z] in selected.acc or [y, z] in selected.eat :
|
||||
if type(rects[y][z]) == Pawn :
|
||||
npawnm = 0
|
||||
else :
|
||||
npawnm += 1
|
||||
if [y, z] in selected.eat :
|
||||
ncapm = 0
|
||||
else :
|
||||
ncapm += 1
|
||||
lastmove = []
|
||||
selected.used = True
|
||||
lastmove.append(copy(selected))
|
||||
rects[y][z] = selected
|
||||
rects[selected.x][selected.y] = None
|
||||
rects[y][z].x, rects[y][z].y = y, z
|
||||
lastmove.append(rects[y][z])
|
||||
if [y, z] in selected.spe :
|
||||
if type(selected) == Pawn :
|
||||
if selected.c :
|
||||
rects[y][3] = None
|
||||
else :
|
||||
rects[y][4] = None
|
||||
else :
|
||||
rects[5 if y == 6 else 3][z] = rects[7 if y == 6 else 0][z]
|
||||
rects[5 if y == 6 else 3][z].x, rects[5 if y == 6 else 3][z].y = 5 if y == 6 else 3, z
|
||||
rects[7 if y == 6 else 0][z] = None
|
||||
selected = False
|
||||
if type(rects[y][z]) == Pawn and z == (not rects[y][z].c) * 7:
|
||||
running2 = True
|
||||
animate()
|
||||
recthere = []
|
||||
for i in range(4) :
|
||||
if not rects[y][z].c :
|
||||
recthere.append(get_rect(wq, (pos[y][z].left, 1080 - ul * i - ul)))
|
||||
else :
|
||||
recthere.append(get_rect(wq, (pos[y][z].left, ul * i)))
|
||||
if rects[y][z].c :
|
||||
lsthere = [wq, wn, wr, wb]
|
||||
lsthere2 = [Queen(1), Knight(1), Rook(1), Bishop(1)]
|
||||
else :
|
||||
lsthere = [bq, bn, br, bb]
|
||||
lsthere2 = [Queen(0), Knight(0), Rook(0), Bishop(0)]
|
||||
while running2 :
|
||||
for event2 in pygame.event.get() :
|
||||
if event2.type == pygame.MOUSEBUTTONDOWN:
|
||||
mousex2, mousey2 = pygame.mouse.get_pos()
|
||||
for i, k in zip(recthere, lsthere2) :
|
||||
if i.collidepoint(mousex2, mousey2):
|
||||
rects[y][z] = k
|
||||
rects[y][z].x, rects[y][z].y = y, z
|
||||
running2 = False
|
||||
|
||||
if not rects[y][z].c :
|
||||
pygame.draw.rect(screen, (255, 255, 255), ((pos[y][z].left, 540), (ul, ul * 4)))
|
||||
for j, i in enumerate(lsthere, 1) :
|
||||
screen.blit(i, (pos[y][z].left, 1080 - ul * j))
|
||||
else :
|
||||
pygame.draw.rect(screen, (255, 255, 255), ((pos[y][z].left, 0), (ul, ul * 4)))
|
||||
for j, i in enumerate(lsthere) :
|
||||
screen.blit(i, (pos[y][z].left, ul * j))
|
||||
pygame.display.flip()
|
||||
times ^= 1
|
||||
refresh()
|
||||
tryout()
|
||||
for i2 in occ :
|
||||
for j2 in range(len(i2)) :
|
||||
i2[j2] = []
|
||||
for i in rects:
|
||||
for j in i:
|
||||
if j:
|
||||
for k, t in j.eat:
|
||||
occ[k][t].append(j)
|
||||
animate()
|
||||
scanlst = scan()
|
||||
c = mset()
|
||||
c.update(scanlst)
|
||||
if not canmove :
|
||||
if occ[(wking if times else bking).x][(wking if times else bking).y] :
|
||||
print("Black" if times else "White", "check!!!")
|
||||
my_font = pygame.font.SysFont("Consolas", 200)
|
||||
time_surf = my_font.render("BLACK WIN!!!" if times else "WHITE WIN!!!", 1, [255, 255, 255])
|
||||
gor = time_surf.get_rect()
|
||||
gor.center = width / 2, height / 2
|
||||
screen.blit(time_surf, gor)
|
||||
pygame.display.update()
|
||||
running2 = True
|
||||
while running2:
|
||||
for event2 in pygame.event.get():
|
||||
if event2.type == pygame.KEYDOWN:
|
||||
if event2.key == pygame.K_ESCAPE:
|
||||
_exit(0)
|
||||
else :
|
||||
draw()
|
||||
elif npawnm >= 50 and ncapm >= 50 :
|
||||
draw()
|
||||
elif len(list(scanlst)) == 2 or c == mset([King, King, Knight]) or c == mset([King, King, Bishop]) :
|
||||
draw()
|
||||
elif c == mset([King, King, Bishop, Bishop]) :
|
||||
tmp = []
|
||||
for i in rects :
|
||||
for j in i :
|
||||
if type(j) == Bishop :
|
||||
tmp.append(j)
|
||||
if (tmp[0].x + tmp[0].y) % 2 == (tmp[1].x + tmp[1].y) % 2 :
|
||||
draw()
|
||||
elif chesslst.count(chesslst[-1]) == 3 :
|
||||
draw()
|
||||
|
||||
elif x :
|
||||
if x.c == times :
|
||||
selected = x
|
||||
else :
|
||||
selected = False
|
||||
else :
|
||||
selected = False
|
||||
else:
|
||||
if x :
|
||||
if x.c == times :
|
||||
selected = x
|
||||
|
||||
animate()
|
||||
pygame.quit()
|
@ -0,0 +1,86 @@
|
||||
import pygame
|
||||
|
||||
display_width = 1200
|
||||
display_height = 800
|
||||
WHITE = (255, 255, 255)
|
||||
RED = (255, 0, 0)
|
||||
|
||||
screen = pygame.display.set_mode((display_width,display_height))
|
||||
bg = pygame.image.load('./images/map.png')
|
||||
#begin = pygame.image.load('./images/begin.png')
|
||||
bg = pygame.transform.scale(bg, (display_width,display_height))
|
||||
pygame.init()
|
||||
font_addr = pygame.font.get_default_font()
|
||||
font = pygame.font.Font(font_addr, 36)
|
||||
|
||||
|
||||
class Button(object):
|
||||
def __init__(self, png, color, x=None, y=None, **kwargs):
|
||||
self.surface = font.render(png, True, color)
|
||||
|
||||
self.WIDTH = self.surface.get_width()
|
||||
self.HEIGHT = self.surface.get_height()
|
||||
|
||||
if 'centered_x' in kwargs and kwargs['centered_x']:
|
||||
self.x = display_width // 2 - self.WIDTH // 2
|
||||
else:
|
||||
self.x = x
|
||||
|
||||
if 'centered_y' in kwargs and kwargs['centered_y']:
|
||||
self.x = display_height //2 - self.HEIGHT // 2
|
||||
else:
|
||||
self.y = y
|
||||
|
||||
def display(self):
|
||||
screen.blit(self.surface, (self.x, self.y))
|
||||
|
||||
def check_click(self,position):
|
||||
x_match = (position[0] > self.x) and (position[0] < self.x + self.WIDTH)
|
||||
y_match = (position[1] > self.y) and (position[1] < self.y + self.HEIGHT)
|
||||
if x_match and y_match:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def starting_screen():
|
||||
screen.blit(bg, (0, 0))
|
||||
|
||||
game_title = font.render('Starting Screen', True, WHITE)
|
||||
|
||||
screen.blit(game_title, (display_width//2 - game_title.get_width()//2, 150))
|
||||
|
||||
play_button = Button('Play!', WHITE, None, 350, centered_x=True)
|
||||
exit_button = Button('Exit!', WHITE, None, 400, centered_x=True)
|
||||
|
||||
play_button.display()
|
||||
exit_button.display()
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
while True:
|
||||
|
||||
if play_button.check_click(pygame.mouse.get_pos()):
|
||||
play_button = Button('Play', RED, None, 350, centered_x=True)
|
||||
else:
|
||||
play_button = Button('Play', WHITE, None, 350, centered_x=True)
|
||||
|
||||
if exit_button.check_click(pygame.mouse.get_pos()):
|
||||
exit_button = Button('Exit', RED, None, 400, centered_x=True)
|
||||
else:
|
||||
exit_button = Button('Exit', WHITE, None, 400, centered_x=True)
|
||||
|
||||
play_button.display()
|
||||
exit_button.display()
|
||||
pygame.display.update()
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
pygame.quit()
|
||||
raise SystemExit
|
||||
if pygame.mouse.get_pressed()[0]:
|
||||
if play_button.check_click(pygame.mouse.get_pos()):
|
||||
break
|
||||
if exit_button.check_click(pygame.mouse.get_pos()):
|
||||
break
|
||||
|
||||
starting_screen()
|