2023-09-18 20:11:39 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-07-28 19:34:53 +01:00
|
|
|
import pygame
|
2023-08-22 16:31:16 +01:00
|
|
|
from utilities.Board import Board
|
2023-07-28 19:34:53 +01:00
|
|
|
from utilities.constants import GREEN, WHITE, BLUE, SQUARE_SIZE
|
|
|
|
|
2023-08-22 16:31:16 +01:00
|
|
|
|
2023-07-28 19:34:53 +01:00
|
|
|
class GameManager:
|
2023-09-18 20:11:39 +01:00
|
|
|
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
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
self._init(colour)
|
|
|
|
self.win = win
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def _init(self, colour: int) -> None:
|
|
|
|
"""
|
|
|
|
Initializes the game
|
|
|
|
:param colour: the colour of the player
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
self.selected = None
|
|
|
|
self.board = Board()
|
|
|
|
self.turn = colour
|
|
|
|
self.validMoves = {}
|
|
|
|
self.legCount = 0
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def update(self) -> None:
|
|
|
|
"""
|
|
|
|
Updates the GUI
|
|
|
|
return: None
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
self.board.draw(self.win)
|
|
|
|
self.drawValidMoves(self.validMoves)
|
|
|
|
pygame.display.update()
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def reset(self) -> None:
|
|
|
|
"""
|
|
|
|
Resets the game
|
|
|
|
:return: None
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
self._init(self.turn)
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
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
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
if self.selected:
|
|
|
|
result = self._move(row, col)
|
|
|
|
if not result:
|
|
|
|
self.selected = None
|
|
|
|
self.select(row, col)
|
|
|
|
piece = self.board.getPiece(row, col)
|
2023-08-22 16:31:16 +01:00
|
|
|
if piece != 0 and piece.colour == self.turn:
|
2023-07-28 19:34:53 +01:00
|
|
|
self.selected = piece
|
|
|
|
self.validMoves = self.board.getValidMoves(piece)
|
|
|
|
return True
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
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
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
piece = self.board.getPiece(row, col)
|
2023-08-22 16:31:16 +01:00
|
|
|
if self.selected and piece == 0 and (row, col) in self.validMoves:
|
2023-07-28 19:34:53 +01:00
|
|
|
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
|
2023-08-22 16:31:16 +01:00
|
|
|
return
|
|
|
|
self.turn = GREEN
|
2023-07-28 19:34:53 +01:00
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def drawValidMoves(self, moves: list) -> None:
|
|
|
|
"""
|
|
|
|
Draws the valid moves
|
|
|
|
:param moves: list of valid moves
|
|
|
|
:return: None
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
for row, col in moves:
|
|
|
|
pygame.draw.circle(self.win, BLUE,
|
|
|
|
(col * SQUARE_SIZE + SQUARE_SIZE // 2, row * SQUARE_SIZE + SQUARE_SIZE // 2), 15)
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def winner(self) -> int or None:
|
|
|
|
"""
|
|
|
|
Gets the winner
|
|
|
|
:return: The winner
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
return self.board.winner()
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def getBoard(self) -> Board:
|
|
|
|
"""
|
|
|
|
Gets the board
|
|
|
|
:return: The board
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
return self.board
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def aiMove(self, board: Board) -> None:
|
|
|
|
"""
|
|
|
|
Makes a move for the AI
|
|
|
|
:param board: The new board
|
|
|
|
:return: None
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
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()
|