basic blocks + baisc view
This commit is contained in:
parent
4493e3a110
commit
e2f6b5c8d8
42
src/logic/board.py
Normal file
42
src/logic/board.py
Normal file
@ -0,0 +1,42 @@
|
||||
from logic.pieces.bishop import Bishop
|
||||
from logic.pieces.king import King
|
||||
from logic.pieces.knight import Knight
|
||||
from logic.pieces.queen import Queen
|
||||
from logic.pieces.rook import Rook
|
||||
from logic.pieces.pawn import Pawn
|
||||
from logic.pieces.piece import Piece
|
||||
from logic.position import Position
|
||||
|
||||
class Board:
|
||||
def __init__(self) -> None:
|
||||
self._white: dict[Position, Piece] = {}
|
||||
self._black: dict[Position, Piece] = {}
|
||||
|
||||
for x in range(8):
|
||||
pos_w_pawn = Position(x, 1)
|
||||
pos_b_pawn = Position(x, 6)
|
||||
|
||||
self._white[pos_w_pawn] = Pawn(pos_w_pawn)
|
||||
self._black[pos_b_pawn] = Pawn(pos_b_pawn)
|
||||
|
||||
pos_w_piece = Position(x, 0)
|
||||
pos_b_piece = Position(x, 7)
|
||||
|
||||
piece = None
|
||||
if x == 0 or x == 7:
|
||||
piece = Rook
|
||||
elif x == 1 or x == 6:
|
||||
piece = Knight
|
||||
elif x == 2 or x == 5:
|
||||
piece = Bishop
|
||||
elif x == 3:
|
||||
piece = Queen
|
||||
elif x == 4:
|
||||
piece = King
|
||||
assert piece != None, f"Didn't know which piece to assign for {x = }"
|
||||
self._white[pos_w_piece] = piece(pos_w_piece)
|
||||
self._black[pos_b_piece] = piece(pos_b_piece)
|
||||
|
||||
|
||||
def create_board():
|
||||
return Board()
|
4
src/logic/pieces/bishop.py
Normal file
4
src/logic/pieces/bishop.py
Normal file
@ -0,0 +1,4 @@
|
||||
from .piece import Piece
|
||||
|
||||
class Bishop(Piece):
|
||||
pass
|
5
src/logic/pieces/king.py
Normal file
5
src/logic/pieces/king.py
Normal file
@ -0,0 +1,5 @@
|
||||
from .piece import Piece
|
||||
|
||||
class King(Piece):
|
||||
pass
|
||||
|
5
src/logic/pieces/knight.py
Normal file
5
src/logic/pieces/knight.py
Normal file
@ -0,0 +1,5 @@
|
||||
from .piece import Piece
|
||||
|
||||
class Knight(Piece):
|
||||
pass
|
||||
|
7
src/logic/pieces/pawn.py
Normal file
7
src/logic/pieces/pawn.py
Normal file
@ -0,0 +1,7 @@
|
||||
from .piece import Piece
|
||||
|
||||
class Pawn(Piece):
|
||||
def __init__(self, pos) -> None:
|
||||
super().__init__(pos)
|
||||
self.already_moved = False
|
||||
|
12
src/logic/pieces/piece.py
Normal file
12
src/logic/pieces/piece.py
Normal file
@ -0,0 +1,12 @@
|
||||
from logic.position import Position
|
||||
|
||||
|
||||
class Piece:
|
||||
def __init__(self, pos) -> None:
|
||||
self.pos = pos
|
||||
|
||||
def position(self) -> Position:
|
||||
return self.pos
|
||||
|
||||
def legal_moves(self, board) -> list[Position]:
|
||||
raise NotImplementedError(f"Can't say what the legal moves are for {type(self).__name__}, the method hasn't been implemented yet")
|
5
src/logic/pieces/queen.py
Normal file
5
src/logic/pieces/queen.py
Normal file
@ -0,0 +1,5 @@
|
||||
from .piece import Piece
|
||||
|
||||
class Queen(Piece):
|
||||
pass
|
||||
|
5
src/logic/pieces/rook.py
Normal file
5
src/logic/pieces/rook.py
Normal file
@ -0,0 +1,5 @@
|
||||
from .piece import Piece
|
||||
|
||||
class Rook(Piece):
|
||||
pass
|
||||
|
25
src/logic/position.py
Normal file
25
src/logic/position.py
Normal file
@ -0,0 +1,25 @@
|
||||
class Position:
|
||||
_MIN_POS = 0
|
||||
_MAX_POS = 7
|
||||
|
||||
def __init__(self, x, y) -> None:
|
||||
assert x >= self._MIN_POS and x <= self._MAX_POS, f"Invalid argument: x should be between {self._MIN_POS} and {self._MAX_POS}, but is {x}"
|
||||
assert y >= self._MIN_POS and y <= self._MAX_POS, f"Invalid argument: y should be between {self._MIN_POS} and {self._MAX_POS}, but is {y}"
|
||||
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def __eq__(self, value: object, /) -> bool:
|
||||
if type(value) != type(self):
|
||||
return False
|
||||
return value.x == self.x and value.y == self.y
|
||||
|
||||
def __hash__(self) -> int:
|
||||
return hash((self.x, self.y))
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.x, self.y}"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return str(self)
|
||||
|
9
src/main.py
Normal file
9
src/main.py
Normal file
@ -0,0 +1,9 @@
|
||||
from logic.board import create_board
|
||||
from view.tui import TUI
|
||||
|
||||
if __name__ == "__main__":
|
||||
board = create_board()
|
||||
|
||||
view = TUI(board)
|
||||
|
||||
view.show()
|
57
src/view/tui.py
Normal file
57
src/view/tui.py
Normal file
@ -0,0 +1,57 @@
|
||||
from logic.board import Board
|
||||
from logic.pieces.bishop import Bishop
|
||||
from logic.pieces.king import King
|
||||
from logic.pieces.knight import Knight
|
||||
from logic.pieces.pawn import Pawn
|
||||
from logic.pieces.piece import Piece
|
||||
from logic.pieces.queen import Queen
|
||||
from logic.pieces.rook import Rook
|
||||
from view.view import View
|
||||
|
||||
class TUI(View):
|
||||
def __init__(self, board: Board) -> None:
|
||||
super().__init__(board)
|
||||
|
||||
def show(self) -> None:
|
||||
board_view = [
|
||||
[" " for _ in range(0, 8)]
|
||||
for _ in range(0, 8)
|
||||
]
|
||||
|
||||
for pos, piece in self.board._white.items():
|
||||
board_view[pos.y][pos.x] = self.string_of(piece).upper()
|
||||
|
||||
for pos, piece in self.board._black.items():
|
||||
board_view[pos.y][pos.x] = self.string_of(piece)
|
||||
|
||||
# we reverse the board because (0, 0) in in the bottom left, not top left
|
||||
board_view.reverse()
|
||||
print(self.to_string(board_view))
|
||||
|
||||
def to_string(self, board_view: list[list[str]]) -> str:
|
||||
VER_SEP = "|"
|
||||
HOR_SEP = "-"
|
||||
ROW_SEP = HOR_SEP * (2*len(board_view[0]) + 1)
|
||||
ret = ROW_SEP + "\n"
|
||||
for row_view in board_view:
|
||||
row_str = VER_SEP + VER_SEP.join(row_view) + VER_SEP
|
||||
ret += row_str + "\n"
|
||||
ret += ROW_SEP + "\n"
|
||||
|
||||
return ret
|
||||
|
||||
def string_of(self, piece: Piece) -> str:
|
||||
type_ = type(piece)
|
||||
if type_ == Pawn:
|
||||
return "p"
|
||||
if type_ == Queen:
|
||||
return "q"
|
||||
if type_ == Bishop:
|
||||
return "b"
|
||||
if type_ == Knight:
|
||||
return "n"
|
||||
if type_ == Rook:
|
||||
return "r"
|
||||
if type_ == King:
|
||||
return "k"
|
||||
raise ValueError(f"Unknown piece type {type(piece)}")
|
10
src/view/view.py
Normal file
10
src/view/view.py
Normal file
@ -0,0 +1,10 @@
|
||||
from logic.board import Board
|
||||
|
||||
|
||||
class View:
|
||||
def __init__(self, board: Board) -> None:
|
||||
self.board: Board = board
|
||||
|
||||
def show(self) -> None:
|
||||
raise NotImplementedError(f"Can't show the board, the show() method of {type(self)} is not implemented")
|
||||
|
Loading…
x
Reference in New Issue
Block a user