-
Notifications
You must be signed in to change notification settings - Fork 3
/
Piece.py
116 lines (110 loc) · 3.97 KB
/
Piece.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
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 29 16:53:28 2020
@author: WIKI
"""
import pygame, sys, random
from pygame.locals import *
from copy import copy, deepcopy
class Piece():
def __init__(self,x,y,color,k):
self.x = x
self.y = y
self.color = color
self.king = k
def __copy__(self):
return type(self)(self.x,self.y,self.color)
def __deepcopy__(self, memo): # memo is a dict of id's to copies
id_self = id(self) # memoization avoids unnecesary recursion
_copy = memo.get(id_self)
if _copy is None:
_copy = type(self)(
deepcopy(self.x, memo),
deepcopy(self.y,memo),
deepcopy(self.color,memo),
deepcopy(self.king,memo))
memo[id_self] = _copy
return _copy
def update(self,x,y,board):
self.x = x
self.y = y
x = (x-10)//board.BOXWIDTH
y = (y-10)//board.BOXHEIGHT
if y==0 or y==7:
self.king=True
if self.color == 'black':
self.image = pygame.image.load("Images/blackking.png")
else:
self.image = pygame.image.load("Images/whiteking.png")
def draw(self, target_surface):
image = pygame.image.load("Images/black.png") if self.color == 'black' else pygame.image.load("Images/white.png")
if self.king == True:
if self.color == 'black':
image = pygame.image.load("Images/blackking.png")
else:
image = pygame.image.load("Images/whiteking.png")
target_surface.blit(image, (self.x,self.y))
def check(self,x,y,board):
for piece in board.pieces:
xx = (piece.x-10)//board.BOXWIDTH
yy = (piece.y-10)//board.BOXHEIGHT
if xx==x and yy ==y:
return False
return True
def who_is_there(self,x,y,board):
x = x * board.BOXWIDTH + 10
y = y * board.BOXHEIGHT + 10
for piece in board.pieces:
if(piece.x == x and piece.y == y):
return piece
return -1
def can_eat(self,board):
x = (self.x-10)//board.BOXWIDTH
y = (self.y-10)//board.BOXHEIGHT
L = []
for i in [-2,2]:
if (i == -2 and x<=1) or (i==2 and x>=6):
continue
for j in [-2,2]:
if (j==-2 and y<=1) or (j==2 and y>=6):
continue
elif(j<0 and self.color=='black' and self.king==False):
continue
elif(j>0 and self.color=='white' and self.king==False):
continue
if self.check(x+i,y+j,board):
jj = j
ii = i
if ii<0:
ii+=1
else:
ii-=1
if jj<0:
jj+=1
else:
jj-=1
piece = self.who_is_there(x+ii,y+jj,board)
if piece == -1:
continue
if piece.color != self.color:
L.append((int(x+i),int(y+j)))
return L
def display_possible_moves(self,board):
x = (self.x-10)//board.BOXWIDTH
y = (self.y-10)//board.BOXHEIGHT
L = self.can_eat(board)
if len(L)>0:
return L
for i in [-1,1]:
if (i==-1 and x==0) or (i==1 and x==7):
continue
for j in [-1,1]:
if (j==-1 and y==0) or (j==1 and y==7):
continue
elif(j<0 and self.color=='black' and self.king==False):
continue
elif(j>0 and self.color=='white' and self.king==False):
continue
if self.check(x+i,y+j,board):
L.append((int(x+i),int(y+j)))
return L