public Game(IPlayer whitePlayer, IPlayer blackPlayer, Action <Board, PiecesColor> onNewTurn, Func <Board, PiecesColor, Move, bool> onMoveConfirm) { Board = new Board(); WhitePlayer = whitePlayer; BlackPlayer = blackPlayer; OnNewTurn = onNewTurn; OnMoveConfirm = onMoveConfirm; TurnColor = PiecesColor.White; }
public bool IsPositionUnderCheck(Board board, PiecesColor turnColor, Position pos) { foreach (var(_, srcPos) in board.PiecesPositions[turnColor]) { var move = new Move(srcPos, pos); if (Validate(board, turnColor, move) == MoveValidationResult.Valid) { return(true); } } return(false); }
public Move?GetMove(UserPlayer player, Board board, PiecesColor turnColor) { var move = player.OnMoveRequest(board, turnColor); if (move != null) { var validationResult = _moveValidationService.Validate(board, turnColor, move); player.OnMoveValidated(move, validationResult); return(validationResult switch { MoveValidationResult.Valid => move, _ => GetMove(player, board, turnColor) });
public List <CpuMove> GetValidCpuMoves(Board board, PiecesColor turnColor) { var validMoves = new List <CpuMove>(); foreach (var(piece, srcPos) in board.PiecesPositions[turnColor]) { foreach (var move in GetPotentialDestinations(piece, srcPos) .Select(dstPos => new CpuMove(srcPos, dstPos)) .Where(move => _moveValidationService.Validate(board, turnColor, move) == MoveValidationResult.Valid)) { move.Value = EvaluateMoveValue(board, move); validMoves.Add(move); } } return(validMoves); }
public bool CheckIfCheck(PiecesColor color) { foreach (ChessPieceBase chessPiece in ChessPieces) { List <Vector3> moves = chessPiece?.GetMoves(); if (moves?.Count > 0) { foreach (Vector3 position in moves) { ChessPieceBase piece = CheckIfPositionOccupied(position); if (piece?.Type == PiecesType.King && chessPiece.isActiveAndEnabled && piece?.Color == color) { Debug.Log("CHECK!!!"); return(true); } } } } return(false); }
public MoveValidationResult Validate(Board board, PiecesColor turnColor, Move move) { var srcPiece = board[move.Src]; var dstPiece = board[move.Dst]; if (srcPiece != null && srcPiece.Color == turnColor) { if (dstPiece == null || dstPiece.Color != turnColor) { if (IsMoveValidForPiece(move, srcPiece, dstPiece != null)) { if (srcPiece is Knight || IsMoveValidForPath(board, move)) { if (!IsKingUnderCheck(board, turnColor, move)) { return(MoveValidationResult.Valid); } return(MoveValidationResult.KingUnderCheck); } return(MoveValidationResult.InvalidPath); } if (srcPiece is King king) { return(ValidateCastling(board, turnColor, move, king)); } return(MoveValidationResult.InvalidMove); } return(MoveValidationResult.InvalidDst); } return(MoveValidationResult.InvalidSrc); }
private Move?GetMove(IPlayer player, Board board, PiecesColor turnColor) => player switch {
public Pawn(PiecesColor color) : base(color, color == PiecesColor.White ? 'P' : 'p', 1) { }
public NewGameConfig(PiecesColor userColor, int recursionDepth) { UserColor = userColor; RecursionDepth = recursionDepth; }
public void Deconstruct(out PiecesColor userColor, out int recursionDepth) { userColor = UserColor; recursionDepth = RecursionDepth; }
private CpuMove?GetBestMove(CpuPlayer player, Board board, PiecesColor turnColor, int recursionLevel, int recursionDepth) { void EvaluateMove(CpuMove move) { var tempBoard = new Board(board); tempBoard.ApplyMove(move); if (recursionLevel < recursionDepth) { var response = GetBestMove(player, tempBoard, turnColor.Invert(), recursionLevel + 1, recursionDepth); move.Response = response ?? (ICpuMoveResponse?)new CpuMoveCheckMate(); } else if (_moveValidationService.IsPositionUnderCheck(tempBoard, turnColor.Invert(), move.Dst)) { move.Response = new CpuMoveCheck(tempBoard[move.Dst]?.Value ?? 0); } else { move.Response = new CpuMoveUnknown(); } move.Value -= move.Response?.Value ?? 0; if (recursionLevel == 0) { move.Value += EvaluateBoardCoverageValue(tempBoard, turnColor); } } var startTime = DateTime.Now; var moves = _moveEvaluationService.GetValidCpuMoves(board, turnColor); if (!moves.Any()) { return(null); } if (recursionLevel == 0) { Parallel.ForEach(moves, EvaluateMove); } else { foreach (var move in moves) { EvaluateMove(move); } } var bestValue = moves.Max(move => move.Value); var bestMoves = moves.Where(move => move.Value == bestValue).ToArray(); var bestMove = bestMoves[_random.Next(bestMoves.Length)]; if (recursionLevel <= MaxNotifiableLevel) { player.OnMoveEvaluated(recursionLevel, moves, bestMove, DateTime.Now - startTime); } return(bestMove); }
public Move?GetMove(CpuPlayer player, Board board, PiecesColor turnColor) => GetBestMove(player, board, turnColor, 0, player.RecursionDepth);
public void SetColor(PiecesColor color) { _color = color; }
public Queen(PiecesColor color) : base(color, color == PiecesColor.White ? 'Q' : 'q', 10) { }
private decimal EvaluateBoardCoverageValue(Board board, PiecesColor turnColor) { var coverage = _moveEvaluationService.GetValidCpuMoves(board, turnColor); return(Math.Round(0.01m * coverage.Count, 1)); }
protected Piece(PiecesColor color, char symbol, int value) { Color = color; Symbol = symbol; Value = value; }
public Rook(PiecesColor color) : base(color, color == PiecesColor.White ? 'R' : 'r', 5) { }
public GameSettings(PiecesColor col, Difficulty diff) { color = col; difficulty = diff; }
public Knight(PiecesColor color) : base(color, color == PiecesColor.White ? 'N' : 'n', 4) { }
protected CastlingPiece(PiecesColor color, char symbol, int value) : base(color, symbol, value) { }
public Bishop(PiecesColor color) : base(color, color == PiecesColor.White ? 'B' : 'b', 3) { }
public King(PiecesColor color) : base(color, color == PiecesColor.White ? 'K' : 'k', 0) { }
public static PiecesColor Invert(this PiecesColor color) => color == PiecesColor.White ? PiecesColor.Black : PiecesColor.White;