/
board.py
88 lines (79 loc) · 3.3 KB
/
board.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
import pygame
import cfg
import piece
from piece import King, Queen, Bishop, Knight, Rook, Pawn
BLACK = False
WHITE = True
class Board:
def __init__(self): #setup board and pieces
self.blackPieces = []
self.whitePieces = []
self.blackPieces.append(King(4, 0, BLACK))
self.blackPieces.append(Queen(3, 0, BLACK))
self.blackPieces.append(Bishop(2, 0, BLACK))
self.blackPieces.append(Bishop(5, 0, BLACK))
self.blackPieces.append(Knight(1, 0, BLACK))
self.blackPieces.append(Knight(6, 0, BLACK))
self.blackPieces.append(Rook(0, 0, BLACK))
self.blackPieces.append(Rook(7, 0, BLACK))
for i in range(0, 8):
self.blackPieces.append(Pawn(i, 1, BLACK))
self.whitePieces.append(King(4, 7, WHITE))
self.whitePieces.append(Queen(3, 7, WHITE))
self.whitePieces.append(Bishop(2, 7, WHITE))
self.whitePieces.append(Bishop(5, 7, WHITE))
self.whitePieces.append(Knight(1, 7, WHITE))
self.whitePieces.append(Knight(6, 7, WHITE))
self.whitePieces.append(Rook(0, 7, WHITE))
self.whitePieces.append(Rook(7, 7, WHITE))
for i in range(0, 8):
self.whitePieces.append(Pawn(i, 6, WHITE))
def getPiece(self, x, y):
#returns piece currently at position (x,y), None if no piece
for piece in self.blackPieces:
if x == piece.x and y == piece.y:
return piece
for piece in self.whitePieces:
if x == piece.x and y == piece.y:
return piece
return None
def removePiece(self, x, y):
#removes piece currently at position (x,y)
#returns True if sucessfully removed, False otherwise
pieceToRemove = self.getPiece(x, y)
if pieceToRemove is not None:
if pieceToRemove.isWhite:
self.whitePieces.remove(pieceToRemove)
else:
self.blackPieces.remove(pieceToRemove)
return True
return False
def movePiece(self, x, y, piece, valid = None):
#move the given piece to (x,y), takes enemy piece if there is one, returns boolean
#indicating whether the move was successful
#takes into account collision with any other pieces in the path
target = (x,y)
if valid is None:
valid = piece.allValidMoves(self)
if target in valid:
piece.move(x, y, self)
return True
return False
def draw(self, validMoves = None):
for i in range(0, 8):
for j in range(0, 8):
tileRect = (i*100, j*100, 100, 100)
if (i+j)%2 == 0:
pygame.draw.rect(cfg.screen, cfg.whiteTile, tileRect, 0)
else:
pygame.draw.rect(cfg.screen, cfg.blackTile, tileRect, 0)
#draw possible moves for picked up piece
if validMoves is not None:
for moves in validMoves:
tileRect = (moves[0]*100, moves[1]*100, 100, 100)
pygame.draw.rect(cfg.screen, cfg.greenTile, tileRect, 10)
#draw pieces
for piece in self.blackPieces:
piece.draw()
for piece in self.whitePieces:
piece.draw()