public void OnLeftClick() { if (IsMouseOver()) { bool hasSelectedRoot = ((ChessGameScene)ChessGame.Instance.CurrentScene).HasSelectedRootNode; if (!hasSelectedRoot) { if (PieceInThisPosition != null && PieceInThisPosition.IsCorrectTurn()) { if (PieceInThisPosition.GetPossibleMoves().Count > 0) { NotationBuilder = new StringBuilder(); IsSelected = true; ((ChessGameScene)ChessGame.Instance.CurrentScene).HasSelectedRootNode = true; ((ChessGameScene)ChessGame.Instance.CurrentScene).RootNode = this; List <Vector2D> possibleMoves = PieceInThisPosition.GetPossibleMoves(); List <ChessBoardSprite> possibleMovesSprites = new List <ChessBoardSprite>(); foreach (Vector2D move in possibleMoves) { if (PieceInThisPosition.IsValidMove(move) && MovementValidation.IsPathNotObstructed(PieceInThisPosition.BoardPosition, move)) { possibleMovesSprites.Add(GetBoardPieceByCoord(move.ToCoordinate())); } } //possibleMoves.ForEach(m => possibleMovesSprites.Add(GetBoardPieceByCoord(m.ToCoordinate()))); possibleMovesSprites.ForEach(m => m.MarkAsValidPosition()); NotationBuilder.Append(BoardPosition.ToCoordinate() + " "); } else { Notifications.Notify("That piece has no possible moves."); } } } else { List <ChessBoardSprite> allChessBoardPieces = GetAllChessBoardPieces(); ChessBoardSprite movingPosition = ((ChessGameScene)ChessGame.Instance.CurrentScene).RootNode; ChessPiece movingPiece = movingPosition.PieceInThisPosition; if (movingPiece.IsValidMove(BoardPosition)) { NotationBuilder.Append(BoardPosition.ToCoordinate()); if (PieceInThisPosition != null) { NotationBuilder.Append("*"); } Console.WriteLine("Command: [" + NotationBuilder.ToString() + "]"); NotationParser.ParseString(NotationBuilder.ToString()); UpdateChessPieceTexture(); movingPosition.UpdateChessPieceTexture(); OnRightClick(); } } } }
private void playComputerMove() { while (r_GameManager.CurrentPlayer.IsComputer) { r_GameManager.matchManager(string.Empty); CheckersSquare moveFrom = getCheckersCheckBoxByName(r_GameManager.CurrentMove.CurrentSquare.getSquare()); CheckersSquare moveTo = getCheckersCheckBoxByName(r_GameManager.CurrentMove.NextSquare.getSquare()); if (MovementValidation.IsTryingToJump(r_GameManager.CurrentMove, getCheckersCheckBoxByName(r_GameManager.CurrentMove.CurrentSquare.getSquare()).CoinType)) { Square middleSquare = r_GameManager.CurrentMove.calculateMiddleSquare(); clearSquare(middleSquare); } moveCoinInForm(moveFrom, moveTo); if (r_GameManager.NewKingWasMade) { changeCoinToKing(moveTo); } if (r_GameManager.ThereAreMoreJumps) { disableAllButChecked(); } else { disableAllNonCurrentPlayerSquares(); } } }
private static string ExecuteAndTranslateCapture(string command) { string translated = InvalidMoveText; string[] positions = command.Split(' '); int fromX = positions[0][0].ToString().ToUpper()[0] - LowAsciiCharIndex; int fromY = (positions[0][1].ToString().ToUpper()[0] - (LowAsciiNumIndex + 1)).Inverted(); int toX = positions[1][0].ToString().ToUpper()[0] - LowAsciiCharIndex; int toY = (positions[1][1].ToString().ToUpper()[0] - (LowAsciiNumIndex + 1)).Inverted(); Vector2D fromPosition = new Vector2D(fromX, fromY); Vector2D toPosition = new Vector2D(toX, toY); var chessBoard = ChessGame.ChessBoard.Board; ChessPiece pieceAtFromPosition = chessBoard[fromPosition.Y, fromPosition.X]; // We aren't moving a non-existant piece. if (pieceAtFromPosition != null && pieceAtFromPosition.IsCorrectTurn()) { ChessPiece pieceAtToPosition = chessBoard[toPosition.Y, toPosition.X]; // If the space we're moving to isn't empty, and it's a valid position, and it's a different color. if (pieceAtToPosition != null && pieceAtFromPosition.IsValidMove(toPosition) && (MovementValidation.PiecesAreSeparateTeams(fromPosition, toPosition))) { string fromPieceName = pieceAtFromPosition.GetType().Name; string toPieceName = pieceAtToPosition.GetType().Name; translated = "Move the" + fromPieceName + " at " + positions[0] + " to " + positions[1] + " and capture the " + toPieceName + " that is currently there."; chessBoard[toPosition.Y, toPosition.X] = chessBoard[fromPosition.Y, fromPosition.X]; chessBoard[fromPosition.Y, fromPosition.X] = null; pieceAtFromPosition.BoardPosition = toPosition; pieceAtFromPosition.IsFirstMove = false; GameMaster.AdvanceGame(); } else { translated = "Can not capture piece."; } } return(translated); }
private void makeMoveInBoard(object sender) { m_CurrentCheckBoxChecked.BackColor = Color.White; CheckersSquare moveTo = sender as CheckersSquare; moveTo.BackColor = Color.White; PlayerMove currentMove = new PlayerMove(m_CurrentCheckBoxChecked.Name + ">" + moveTo.Name); if (MovementValidation.IsTryingToJump(currentMove, m_CurrentCheckBoxChecked.CoinType)) { Square middleSquare = currentMove.calculateMiddleSquare(); clearSquare(middleSquare); } r_GameManager.matchManager(currentMove.ToString()); moveCoinInForm(m_CurrentCheckBoxChecked, moveTo); paintAllInWhite(); if (r_GameManager.NewKingWasMade) { changeCoinToKing(moveTo); } if (r_GameManager.ThereAreMoreJumps) { disableAllButChecked(); } else { disableAllNonCurrentPlayerSquares(); } playSounds(); m_CurrentCheckBoxChecked = null; playComputerMove(); if (r_GameManager.GameIsOver) { gameOverOperation(); } }
private static bool IsValidPlay(ChessPiece attacker, Vector2D position) { return(attacker.IsValidMove(position) && MovementValidation.IsPathNotObstructed(attacker.BoardPosition, position)); }