-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.py
More file actions
68 lines (60 loc) · 1.91 KB
/
Copy pathboard.py
File metadata and controls
68 lines (60 loc) · 1.91 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
"""
Board module
"""
from dataclasses import dataclass
from math import floor, log2
import pygame
from constants import BLACK, COLS, ROWS, SQUARE_HEIGHT,SQUARE_WIDTH, WHITE
from piece import Piece, draw_piece
@dataclass(frozen=True, eq=True)
class Board:
"""
Board class
"""
board_red: tuple[int, int, int, int, int, int, int] = (0, 0, 0, 0, 0, 0, 0)
board_ylw: tuple[int, int, int, int, int, int, int] = (0, 0, 0, 0, 0, 0, 0)
def draw(self, screen):
"""
Draw board for pygame
"""
self.draw_squares(screen)
for row in range(ROWS):
for col in range(COLS):
piece = self.get_piece(row, col)
draw_piece(screen, piece, row, col)
def get_piece(self, row: int, col: int) -> Piece:
"""
Given row and col return piece
"""
assert 0 <= row < ROWS
assert COLS > col >= 0
ylw = self.board_ylw[col]
red = self.board_red[col]
if ylw & (1 << row):
return Piece.YELLOW
if red & (1 << row):
return Piece.RED
return Piece.EMPTY
def draw_squares(self, screen: pygame.Surface):
"""
Draw base board
"""
screen.fill(BLACK)
for row in range(ROWS):
for col in range(COLS):
color = WHITE
pygame.draw.rect(
screen,
color,
(col * SQUARE_WIDTH, row * SQUARE_HEIGHT, SQUARE_WIDTH, SQUARE_HEIGHT),
)
pygame.draw.rect(
screen,
BLACK,
(col * SQUARE_WIDTH, row * SQUARE_HEIGHT, SQUARE_WIDTH, SQUARE_HEIGHT),
2,
)
def get_height(self, col: int) -> int:
col = self.board_red[col] | self.board_ylw[col]
col = col ^ (col << 1)
return floor(log2(col)) if col > 0 else 0