import imutils from BoardRecognition import BoardRecognition from Board import Board from ChessEngine import ChessEngine from Game import Game imgpath = '../board/28.jpg' img = cv2.imread(imgpath) img = imutils.resize(img, width=500) cv2.imshow('img', img) br = BoardRecognition(img) thresh, img = br.cleanImage(img) extracted = br.initializeMask(thresh, img) edges, colorEdges = br.detectEdges(extracted) horizontal, vertical = br.detectLines(edges, colorEdges) corners = br.detectCorners(horizontal, vertical, colorEdges) squares = br.detectSquares(corners, colorEdges) board = Board(squares) board.draw(img) cv2.imshow('img2',img) board.assignState() engine = ChessEngine() engine.updateMove('a2a4') print() engine.feedToAI() #game = Game()
class Game: ''' Holds the whole game information Interacting with Board and Chess Engine ''' def __init__(self, url): self.over = False self.CPUMoveError = False self.PlayerMoveError = False self.isCheck = False self.winner = "Me" self.url = url def setUp(self): self.camera = Camera(self.url) self.chessEngine = ChessEngine() self.board = 0 self.current = 0 self.previous = 0 self.CPULastMove = "0" def analyzeBoard(self): boardRec = BoardRecognition(self.camera) self.board = boardRec.initializeBoard() self.board.assignState() def checkBoardIsSet(self): self.current = self.camera.takePicture() cv2.imwrite('./ProcessImage/SetUpBoard.jpg', self.current) def playerMove(self): self.previous = self.current self.current = self.camera.takePicture() move = self.board.determineChanges(self.previous, self.current) code = self.chessEngine.updateMove(move) print('Your move:', move) if code == 1: # Illegal move self.PlayerMoveError = True else: self.PlayerMoveError = False f = open('./Gamelog/Game.txt', 'a+') f.write(chess.Move.from_uci(move).uci() + '\r\n') f.close() # Check game over if self.chessEngine.engBoard.is_checkmate(): self.winner = 'You win' self.over = True def playerPromotion(self, move): print(move) code = self.chessEngine.updateMove(move) if code == 1: # Illegal print('Error') self.PlayerMoveError = True else: self.PlayerMoveError = False f = open('./Gamelog/Game.txt', 'a+') f.write(chess.Move.from_uci(move).uci() + '\r\n') f.close() if self.chessEngine.engBoard.is_checkmate(): self.winner = 'You win' self.over = True def CPUMove(self): self.CPULastMove = self.chessEngine.feedToAI() self.isCheck = self.chessEngine.engBoard.is_check() copy = self.current.copy() if len(self.CPULastMove.uci()) == 4: position1 = self.CPULastMove.uci()[:2] position2 = self.CPULastMove.uci()[2:] CPUMoveSquares = [] for square in self.board.squares: if square.position == position1 or square.position == position2: CPUMoveSquares.append(square) CPUMoveSquares[0].draw(copy, (255, 0, 0), 2) CPUMoveSquares[1].draw(copy, (255, 0, 0), 2) cv2.imwrite('./ProcessImage/CPUMove.jpg', copy) if self.chessEngine.engBoard.is_checkmate(): self.winner = 'CPU win' self.over = True return self.CPULastMove def updateCurrent(self): self.previous = self.current self.current = self.camera.takePicture() move = self.board.determineChanges(self.previous, self.current) move = chess.Move.from_uci(move) print('Your move for cpu:', move) print('Cpu move:', self.CPULastMove) # Check if player moved CPU piece correctly if move == self.CPULastMove: self.CPUMoveError = False else: self.CPUMoveError = True