private void CellSelected(object sender, RoutedEventArgs e) { var cell = e.ButtonTag <Cell>(); if (cell.Figure.Side == Core.Board.Side.Empty) { MakeMove(this, null); } else { AvailableMoves.Clear(); if (WalkMoves.TryGetValue(cell.Figure, out var figureMoves) && figureMoves.Length > 0) { foreach (var move in figureMoves.Select((gameMove, i) => new MovesModel($"{i + 1}", i, gameMove))) { AvailableMoves.Add(move); } } } SelectedFigure = cell.Figure; foreach (var c in Cells) { if (c.Type == PieceType.None) { c.Active = false; } } }
private void RedrawBoard() { UpdateGameBoardCells(_game); AvailableMoves.Clear(); SelectedMoveModel = null; SelectedFigure = Figure.Nop; Info = $"Status: {_game.Status}{Environment.NewLine}Turn: {_game.Turn}{Environment.NewLine}Side move now: {_game.CurrentPlayer.Side}"; }
public override void GeneratePossibleMoves(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer playerCopy, ChessPlayer enemyCopy, bool checkMovesValidity, bool checkSpecialMoves) { AvailableMoves.Clear(); Vector2Int newSquare = Vector2Int.zero; for (int i = 0; i < possibleDirections.GetLength(0); i++) { if (i == 1 && MovedFirstTime) { break; } int newX = SquareIndex.x + possibleDirections[i, 0]; int newY = SquareIndex.y + possibleDirections[i, 1]; newSquare.Set(newX, newY); if (grid.CheckIfSquareIndexIsValid(newSquare)) { Piece piece = grid.GetPieceOnSquareIndex(newSquare); Move move = CheckPromotionFlag(newSquare, piece); if (checkMovesValidity && piece == null) { Piece attackedPieceCopy = piece == null ? null : gridCopy.GetPieceOnSquareIndex(piece.SquareIndex); Move moveCopy = new Move(gridCopy.GetPieceOnSquareIndex(SquareIndex), newSquare, attackedPieceCopy, move.PromotionFlag); bool areMovesValid = CheckIfMoveIsValid(gridCopy, playerCopy, enemyCopy, moveCopy); if (!areMovesValid) { continue; } } if (piece == null) { AvailableMoves.Add(move); } else { break; } } } PieceAttack(grid, gridCopy, playerCopy, enemyCopy, checkMovesValidity); EnPassant(grid, gridCopy, playerCopy, enemyCopy, checkMovesValidity); }
public override void GeneratePossibleMoves(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer playerCopy, ChessPlayer enemyCopy, bool checkMovesValidity, bool checkSpecialMoves) { AvailableMoves.Clear(); Vector2Int newSquare = Vector2Int.zero; for (int i = 0; i < possibleDirections.GetLength(0); i++) { int newX = SquareIndex.x + possibleDirections[i, 0]; int newY = SquareIndex.y + possibleDirections[i, 1]; newSquare.Set(newX, newY); if (grid.CheckIfSquareIndexIsValid(newSquare)) { Piece piece = grid.GetPieceOnSquareIndex(newSquare); Move move = new Move(this, newSquare, piece); if (checkMovesValidity && piece != null && piece.PieceType == PieceType.King) { continue; } if (checkMovesValidity && (piece == null || (piece != null && piece.TeamColor != TeamColor))) { Piece attackedPieceCopy = piece == null ? null : gridCopy.GetPieceOnSquareIndex(piece.SquareIndex); Move moveCopy = new Move(gridCopy.GetPieceOnSquareIndex(SquareIndex), newSquare, attackedPieceCopy); bool areMovesValid = CheckIfMoveIsValid(gridCopy, playerCopy, enemyCopy, moveCopy); if (!areMovesValid) { continue; } } if (piece == null) { AvailableMoves.Add(move); } else if (piece != null && TeamColor != piece.TeamColor) { AvailableMoves.Add(move); } } } }
private async Task StartGame(GameSide side) { WalkMoves.Clear(); AvailableMoves.Clear(); Cells.Clear(); await _game.Stop(); Side = side; _botPlayer = new BotPlayer(side == GameSide.Black ? GameSide.Red : GameSide.Black, _rules, _boardScoring); if (_userPlayer.Side == GameSide.Red) { _game.Start(_userPlayer, _botPlayer); } else { _game.Start(_botPlayer, _userPlayer); } RedrawBoard(); }