45 lines
1.0 KiB
Python
45 lines
1.0 KiB
Python
|
from godot import exposed, export, NodePath
|
||
|
from godot import *
|
||
|
import chess
|
||
|
import random
|
||
|
|
||
|
@exposed
|
||
|
class chessEngine(Node):
|
||
|
board = None
|
||
|
|
||
|
def setup(self):
|
||
|
self.board = chess.Board()
|
||
|
|
||
|
def getboard(self):
|
||
|
return str(self.board)
|
||
|
|
||
|
def isaiturn(self):
|
||
|
return self.board.turn == chess.BLACK
|
||
|
|
||
|
def aimove(self):
|
||
|
l = list(self.board.pseudo_legal_moves)
|
||
|
|
||
|
if self.board.king(chess.BLACK) == None or self.board.legal_moves.count() < 1:
|
||
|
return False
|
||
|
|
||
|
move = random.choice(l)
|
||
|
self.board.push(move)
|
||
|
return True
|
||
|
|
||
|
def isdraw(self):
|
||
|
return self.board.is_stalemate() or self.board.is_insufficient_material()
|
||
|
|
||
|
def makemove(self, f, t):
|
||
|
move = chess.Move(f, t)
|
||
|
moves = self.board.legal_moves
|
||
|
if self.board.king(chess.WHITE) == None or self.board.legal_moves.count() < 1:
|
||
|
return False
|
||
|
|
||
|
promotedMove = chess.Move(f, t, chess.QUEEN)
|
||
|
if move in moves:
|
||
|
self.board.push(move)
|
||
|
elif promotedMove in moves:
|
||
|
self.board.push(promotedMove)
|
||
|
|
||
|
return True
|