133 lines
3.8 KiB
Python
133 lines
3.8 KiB
Python
from __future__ import annotations
|
|
|
|
import pygame
|
|
from utilities.Board import Board
|
|
from utilities.constants import GREEN, WHITE, BLUE, SQUARE_SIZE
|
|
|
|
|
|
class GameManager:
|
|
def __init__(self, win: pygame.display, colour: int) -> None:
|
|
"""
|
|
Constructor for the GameManager class
|
|
:param win: The window
|
|
:param colour: The colour of the player
|
|
"""
|
|
self._init(colour)
|
|
self.win = win
|
|
|
|
def _init(self, colour: int) -> None:
|
|
"""
|
|
Initializes the game
|
|
:param colour: the colour of the player
|
|
"""
|
|
self.selected = None
|
|
self.board = Board()
|
|
self.turn = colour
|
|
self.validMoves = {}
|
|
self.legCount = 0
|
|
|
|
def update(self) -> None:
|
|
"""
|
|
Updates the GUI
|
|
return: None
|
|
"""
|
|
self.board.draw(self.win)
|
|
self.drawValidMoves(self.validMoves)
|
|
pygame.display.update()
|
|
|
|
def reset(self) -> None:
|
|
"""
|
|
Resets the game
|
|
:return: None
|
|
"""
|
|
self._init(self.turn)
|
|
|
|
def select(self, row: int, col: int) -> bool:
|
|
"""
|
|
Selects a piece
|
|
:param row: Row of the piece
|
|
:param col: Column of the piece
|
|
:return: True
|
|
"""
|
|
if self.selected:
|
|
result = self._move(row, col)
|
|
if not result:
|
|
self.selected = None
|
|
self.select(row, col)
|
|
piece = self.board.getPiece(row, col)
|
|
if piece != 0 and piece.colour == self.turn:
|
|
self.selected = piece
|
|
self.validMoves = self.board.getValidMoves(piece)
|
|
return True
|
|
|
|
def _move(self, row: int, col: int) -> bool:
|
|
"""
|
|
Moves a piece
|
|
:param row: Row of the piece
|
|
:param col: Column of the piece
|
|
:return: True if the move was successful, False otherwise
|
|
"""
|
|
piece = self.board.getPiece(row, col)
|
|
if self.selected and piece == 0 and (row, col) in self.validMoves:
|
|
self.board.move(self.selected, row, col)
|
|
skipped = self.validMoves[row, col]
|
|
if self.validMoves[list(self.validMoves.keys())[0]]:
|
|
if self.validMoves[list(self.validMoves.keys())[0]][0].king:
|
|
self.selected.makeKing()
|
|
if skipped:
|
|
self.board.remove(skipped)
|
|
if len(self.validMoves) > 1:
|
|
del self.validMoves[list(self.validMoves.keys())[0]]
|
|
else:
|
|
self.changeTurn()
|
|
else:
|
|
self.changeTurn()
|
|
else:
|
|
return False
|
|
return True
|
|
|
|
def changeTurn(self):
|
|
self.validMoves = {}
|
|
if self.turn == GREEN:
|
|
self.turn = WHITE
|
|
return
|
|
self.turn = GREEN
|
|
|
|
def drawValidMoves(self, moves: list) -> None:
|
|
"""
|
|
Draws the valid moves
|
|
:param moves: list of valid moves
|
|
:return: None
|
|
"""
|
|
for row, col in moves:
|
|
pygame.draw.circle(self.win, BLUE,
|
|
(col * SQUARE_SIZE + SQUARE_SIZE // 2, row * SQUARE_SIZE + SQUARE_SIZE // 2), 15)
|
|
|
|
def winner(self) -> int or None:
|
|
"""
|
|
Gets the winner
|
|
:return: The winner
|
|
"""
|
|
return self.board.winner()
|
|
|
|
def getBoard(self) -> Board:
|
|
"""
|
|
Gets the board
|
|
:return: The board
|
|
"""
|
|
return self.board
|
|
|
|
def aiMove(self, board: Board) -> None:
|
|
"""
|
|
Makes a move for the AI
|
|
:param board: The new board
|
|
:return: None
|
|
"""
|
|
if board is None:
|
|
# colour = "green" if self.turn == GREEN else "white"
|
|
# print("no move left for " + colour + " to make")
|
|
self.changeTurn()
|
|
return
|
|
self.board = board
|
|
self.changeTurn()
|