84 lines
2.5 KiB
Python
84 lines
2.5 KiB
Python
import pygame
|
|
from utilities.Board import Board
|
|
from utilities.constants import GREEN, WHITE, BLUE, SQUARE_SIZE
|
|
|
|
|
|
class GameManager:
|
|
def __init__(self, win, colour):
|
|
self._init(colour)
|
|
self.win = win
|
|
|
|
def _init(self, colour):
|
|
self.selected = None
|
|
self.board = Board()
|
|
self.turn = colour
|
|
self.validMoves = {}
|
|
self.legCount = 0
|
|
|
|
def update(self):
|
|
self.board.draw(self.win)
|
|
self.drawValidMoves(self.validMoves)
|
|
pygame.display.update()
|
|
|
|
def reset(self):
|
|
self._init(self.turn)
|
|
|
|
def select(self, row, col):
|
|
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, col):
|
|
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):
|
|
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):
|
|
return self.board.winner()
|
|
|
|
def getBoard(self):
|
|
return self.board
|
|
|
|
def aiMove(self, board):
|
|
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()
|