Esempio n. 1
0
        /// <summary>
        /// Purpose: To perform the minimax algorithm to determine chess move
        /// </summary>
        /// <param name="boardState"></param>
        /// <param name="color"></param>
        /// <returns>char[] board to represent the move</returns>
        /// 

        public static char[] miniMax(char[] SRFen, ChessColor color)
        {
            TimeSpan maxTime = TimeSpan.FromMilliseconds(5500);
            Char[] initialBoard = (char[])SRFen.Clone();
            bool white;
            int alpha = -10000;
            int beta = 10000;
            int cutoff = 4;
            if (color == ChessColor.White) white = true;
            else white = false;
            int depth = 0;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            int h;
            h = minValue(ref SRFen, depth + 1, white, alpha, beta, cutoff, ref timer);
            if (h == -5000) 
                return SRFen;
            char[] bestSoFar = (char[])SRFen.Clone();
            while (timer.Elapsed < maxTime && h != -9999)
            { 
                cutoff += 2;
                char[] temp = (char[])initialBoard.Clone();
                h = minValue(ref temp, depth + 1, white, alpha, beta, cutoff, bestSoFar, ref timer);
                if (h != -9999) bestSoFar = (char[])temp.Clone();
                if (h == -5000) 
                    return bestSoFar;
            }
            //this.Log("cutoff" + cutoff);
            return bestSoFar;
        }
Esempio n. 2
0
 public ChessSquare(int y, int x, ChessColor color)
 {
     Loc = new Location();
     Loc.Y = y;
     Loc.X = x;
     Color = color;
 }
Esempio n. 3
0
        /// <summary>
        /// Takes a board and color, and returns a simplified state where friend pieces are represented
        /// by positive integers and always start on the same side of the board (0,0). Foe pieces are 
        /// represented as negative values
        /// </summary>
        /// <param name="board">The board to convert</param>
        /// <param name="color">The color of the player (friend)</param>
        /// <returns>A simplified representation of the current board</returns>
        public static int[,] GetSimpleState(ChessBoard board, ChessColor color)
        {
            int[,] state = new int[Columns, Rows];

            for (int col = 0; col < Columns; col++)
            {
                for (int row = 0; row < Rows; row++)
                {
                    int stateRow = row;
                    int stateCol = col;
                    if (color == ChessColor.White)
                    {
                        stateRow = Rows - row - 1;
                        stateCol = Columns - col - 1;
                    }

                    ChessPiece piece = board[col, row];

                    int multiplier = 1;
                    if (color == ChessColor.White && piece < ChessPiece.Empty
                        || color == ChessColor.Black && piece > ChessPiece.Empty)
                        multiplier = -1;

                    state[stateCol, stateRow] = multiplier * GamePieceToStatePiece[piece];
                }
            }
            return state;
        }
Esempio n. 4
0
 /// <summary>
 /// The constructor.
 /// </summary>
 /// <param name="board">The board in which the square is located.</param>
 /// <param name="location">The location of the square.</param>
 /// <param name="color">The color of the square.</param>
 /// <param name="piece">The Chess piece that is on the square.</param>
 public Square(ChessBoard board, Location location, ChessColor color, ChessPiece piece = null)
 {
     this.Board = board;
     this.Location = location;
     this.Color = color;
     this.Piece = piece;
 }
 public Player(string name, ChessColor color)
 {
     // TODO: Validate name lenght
     this.Name = name;
     this.Color = color;
     this.figures = new List<IFigure>();
 }
 /// <summary>
 /// Purpose: To calculate a heuristic value for the given board state
 /// </summary>
 /// <param name="boardState"></param>
 /// <param name="color"></param>
 /// <returns>integer representing the heuristic</returns>
 public static int GetHeuristicValue(byte[] boardState, ChessColor color)
 {
     bool white = color == ChessColor.White;
     int pH = GetPieceValueHeuristic(boardState, color);
     int pS = FEN.GetPieceHazard(boardState, white);
     return pH + pS;
 }
Esempio n. 7
0
 public static Piece GetType(string s, ChessColor color)
 {
     Piece p;
     switch (s)
     {
         case "Bishop":
             p = new Bishop(color);
             break;
         case "King":
             p = new King(color);
             break;
         case "Knight":
             p = new Knight(color);
             break;
         case "Pawn":
             p = new Pawn(color);
             break;
         case "Queen":
             p = new Queen(color);
             break;
         case "Rook":
             p = new Rook(color);
             break;
         default:
             throw new Exception("Piece unknown : " + s);
     }
     return p;
 }
        /// <summary>
        /// Purpose: To calculate a heuristic based purely off piece value
        /// </summary>
        /// <param name="boardState"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        private static int GetPieceValueHeuristic(byte[] boardState, ChessColor color)
        {
            int whiteValue = 0;
            int blackValue = 0;
            for (int i = 0; i < boardState.Length; i++)
            {
                if (boardState[i] != FEN._)
                {
                    if (boardState[i].IsUpper())
                        whiteValue += (pieceValues[boardState[i]] + GetPiecePositionValue(boardState[i], i));
                    else
                        blackValue += (pieceValues[boardState[i]] + GetPiecePositionValue(boardState[i], i));
                }
            }

            //Log("WV -" + whiteValue.ToString());
            //Log("BV -" + blackValue.ToString());

            if (color == ChessColor.White)
            {
                if (whiteValue - 20000 < 1500)
                {
                    lateGame = true;
                }
                return whiteValue - blackValue;
            }
            else
            {
                if (blackValue - 20000 < 1500)
                {
                    lateGame = true;
                }
                return blackValue - whiteValue;
            }
        }
Esempio n. 9
0
 public void GenerateMoves(ChessBoard board, ChessColor color)
 {
     Moves = new List<ChessMove>();
     for (int y = 0; y < ChessBoard.NumberOfRows; y++)
     {
         for (int x = 0; x < ChessBoard.NumberOfColumns; x++)
         {
             if (color == ChessColor.White) // if player is white
             {
                 if (board[x, y] > ChessPiece.Empty)
                 {
                     switch (board[x, y])
                     {
                         case ChessPiece.WhiteBishop:
                             BishopMoves(board, x, y, color);
                             break;
                         case ChessPiece.WhiteQueen:
                             QueenMoves(board, x, y, color);
                             break;
                         case ChessPiece.WhiteKing:
                             break;
                         case ChessPiece.WhiteKnight:
                             KnightMoves(board, x, y, color);
                             break;
                         case ChessPiece.WhitePawn:
                             PawnMoves(board, x, y, color);
                             break;
                         case ChessPiece.WhiteRook:
                             RookMoves(board, x, y, color);
                             break;
                     }
                 }
             }
             else if ( color == ChessColor.Black)
             {
                 switch (board[x, y])
                 {
                     case ChessPiece.BlackBishop:
                         BishopMoves(board, x, y, color);
                         break;
                     case ChessPiece.BlackQueen:
                         QueenMoves(board, x, y, color);
                         break;
                     case ChessPiece.BlackKing:
                         break;
                     case ChessPiece.BlackKnight:
                         KnightMoves(board, x, y, color);
                         break;
                     case ChessPiece.BlackPawn:
                         PawnMoves(board, x, y, color);
                         break;
                     case ChessPiece.BlackRook:
                         RookMoves(board, x, y, color);
                         break;
                 }
             }
         }
     }
 }
Esempio n. 10
0
        public Piece( ChessColor color )
        {
            Rules = new Collection<Rule>();

            Color = color;

            InitializeRules();
        }
Esempio n. 11
0
 /// <summary>
 /// this will define BoardAfterMove, TheMove, and HValue based on move
 /// </summary>
 /// <param name="board"></param>
 /// <param name="move"></param>
 /// <param name="colorofEnemy"></param>
 public Hueristic(ChessBoard board, ChessMove move, ChessColor colorofEnemy)
 {
     BoardBeforeMove = board.Clone();
     BoardAfterMove = board.Clone();
     BoardAfterMove.MakeMove(move);
     TheMove = move;
     //HValue = CalculateHueristicBasic(board, colorofEnemy);
     HValue = CalculateHueristicAdvanced(colorofEnemy);
 }
Esempio n. 12
0
        private bool CheckOtherFigureIfValid(IBoard board, Position to, ChessColor color)
        {
            var otherFigure = board.GetFigureAtPosition(to);
            if (otherFigure != null && otherFigure.Color == color)
            {
                return false;
            }

            return true;
        }
Esempio n. 13
0
        internal Piece(Piece piece)
        {
            PieceColor = piece.PieceColor;
            PieceType = piece.PieceType;
            Moved = piece.Moved;
            PieceValue = piece.PieceValue;
            PieceActionValue = piece.PieceActionValue;

            if (piece.ValidMoves != null)
                LastValidMoveCount = piece.ValidMoves.Count;
        }
Esempio n. 14
0
        /// <summary>
        /// Purpose: Lower the Heuristic value if our queen is put in danger
        /// </summary>
        /// <param name="boardState"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        private static int PieceSafety(char[] boardState, ChessColor color)
        {
            int hazardPenalty = 0;
            bool white = color == ChessColor.White;
            hazardPenalty += FENExtensions.PieceNotSafe(boardState, FENExtensions.GetPiecePos(boardState, white, 'q'), white) ? -400 : 0;
            hazardPenalty += FENExtensions.PieceNotSafe(boardState, FENExtensions.GetPiecePos(boardState, white, 'r'), white) ? -200 : 0;
            hazardPenalty += FENExtensions.PieceNotSafe(boardState, FENExtensions.GetPiecePos(boardState, white, 'n'), white) ? -150 : 0;
            hazardPenalty += FENExtensions.PieceNotSafe(boardState, FENExtensions.GetPiecePos(boardState, white, 'b'), white) ? -155 : 0;
            hazardPenalty += FENExtensions.PieceNotSafe(boardState, FENExtensions.GetPiecePos(boardState, white, 'p'), white) ? -40 : 0;

            return hazardPenalty;
        }
Esempio n. 15
0
 public Node(ChessColor c, ChessMove m, Node p)
 {
     color = c;
     move = m;
     parent = p;
     if (parent == null)
     {
         depth = 0;
     }
     else
     {
         depth = parent.depth + 1;
     }
 }
Esempio n. 16
0
		public GameGump( Mobile m, ChessGame game, ChessColor color, string message, bool move, bool moving ): base( 60, 25 )
		{
			m.CloseGump( typeof( GameGump ) );

			m_Game = game;
			m_Message = message;
			m_Color = color;
			m_Move = move;
			m_Moving = moving;

			if ( move && ! moving )
				m_Game.SendMoveTarget( m );

			MakeGump();
		}
        /// <summary>
        /// Purpose: Lower the Heuristic value if our queen is put in danger
        /// </summary>
        /// <param name="boardState"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        private static int PieceSafety(byte[] boardState, ChessColor color)
        {
            int hazardPenalty = 0;
            bool white = color == ChessColor.White;

            hazardPenalty = FEN.GetPieceHazard (boardState, white);
            /*
                hazardPenalty += FEN.PieceNotSafe(boardState, FEN.GetPiecePos(boardState, white, FEN.q), white) ? -400 : 0;
            //hazardPenalty += FEN.PieceNotSafe(boardState, FEN.GetPiecePos(boardState, white, FEN.r), white) ? -200 : 0;
            //hazardPenalty += FEN.PieceNotSafe(boardState, FEN.GetPiecePos(boardState, white, FEN.n), white) ? -150 : 0;
            //hazardPenalty += FEN.PieceNotSafe(boardState, FEN.GetPiecePos(boardState, white, FEN.b), white) ? -155 : 0;
            //hazardPenalty += FEN.PieceNotSafe(boardState, FEN.GetPiecePos(boardState, white, FEN.p), white) ? -40 : 0;*/

            return hazardPenalty;
        }
Esempio n. 18
0
        /// <summary>
        /// High Number = better board for your color
        /// </summary>
        /// <param name="board"></param>
        /// <param name="colorOfEnemyTeam"></param>
        /// <returns></returns>
        int CalculateBoardHP(ChessBoard board, ChessColor colorOfMyTeam)
        {
            int score = 0;
            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    if (colorOfMyTeam == ChessColor.Black && BoardAfterMove[x, y] < ChessPiece.Empty) //if black
                        score += StudentAI.Piece.CalculatePieceValue(BoardAfterMove[x, y]);
                    else if (colorOfMyTeam == ChessColor.White && BoardAfterMove[x, y] > ChessPiece.Empty) //if white
                        score += StudentAI.Piece.CalculatePieceValue(BoardAfterMove[x, y]);
                }
            }

            return score;
        }
Esempio n. 19
0
        /// <summary>
        /// AIProfiler keeps the profiling stats for the AI. This object is what's passed
        /// to each AI when they call the Profiler property from IChessAI
        /// </summary>
        /// <param name="myColor">This is the color of the AI being profiled. It's mostly used when outputing the results</param>
        internal AIProfiler(ChessColor myColor)
        {
            NodesPerSecond = -1;
            MinisProfilerTag = -1;
            MaxsProfilerTag = -1;
            AIName = string.Empty;
            AIColor = myColor;
            Depth = 0;

            _numFrameworkMethods = Enum.GetValues(typeof(ProfilerMethodKey)).Length;
            IsEnabled = false;
            MoveDepths = new List<int>();
            MoveTimes = new List<TimeSpan>();
            Turns = new List<int[]>();
            FxTurns = new List<int[]>();
        }
Esempio n. 20
0
        /// <summary>
        /// Converts a state based move to a ChessBoard move
        /// </summary>
        /// <param name="stateMove">The move the convert</param>
        /// <param name="playerColor">The color of hte player</param>
        /// <returns>The move corrected to work with ChessBoard</returns>
        public static ChessMove GetGameMove(ChessMove stateMove, ChessColor playerColor)
        {
            if (stateMove == null)
                return null;

            ChessMove gameMove = stateMove.Clone();

            if (playerColor == ChessColor.White)
            {
                gameMove.From.X = Columns - gameMove.From.X - 1;
                gameMove.From.Y = Rows - gameMove.From.Y - 1;
                gameMove.To.X = Columns - gameMove.To.X - 1;
                gameMove.To.Y = Rows - gameMove.To.Y - 1;
            }

            return gameMove;
        }
Esempio n. 21
0
 /// <summary>
 /// gets a list of available moves for a color
 /// </summary>
 public static List<ChessMove> getmovesofcolor(StudentAI ai, ChessColor color, ChessBoard board)
 {
     List<ChessMove> colormoves = new List<ChessMove>();
     for (int y = 0; y < 8; y++)
     {
         for (int x = 0; x < 8; x++)
         {
             if (color == ChessColor.Black)
             {
                 switch (board[x, y])
                 {
                     case ChessPiece.BlackPawn:
                     case ChessPiece.BlackKnight:
                     case ChessPiece.BlackBishop:
                     case ChessPiece.BlackQueen:
                     case ChessPiece.BlackRook:
                     case ChessPiece.BlackKing:
                         colormoves.AddRange(getmovesofpiece(ai, color, board, new ChessLocation(x, y)));
                         break;
                     default:
                         break;
                 }
             }
             if (color == ChessColor.White)
             {
                 switch (board[x, y])
                 {
                     case ChessPiece.WhitePawn:
                     case ChessPiece.WhiteKnight:
                     case ChessPiece.WhiteBishop:
                     case ChessPiece.WhiteQueen:
                     case ChessPiece.WhiteRook:
                     case ChessPiece.WhiteKing:
                         colormoves.AddRange(getmovesofpiece(ai, color, board, new ChessLocation(x, y)));
                         break;
                     default:
                         break;
                 }
             }
         }
     }
     return colormoves;
 }
        //checks if the move is still located on the board & is moved onto empty or kill spot
        private static bool OnBoardAndKillOrEmpty(int X, int Y,ChessBoard board,ChessColor color)
        {
            if (X > 8 || X < 0 || Y > 8 || Y < 0)
                return false;

            if(board[X,Y] == ChessPiece.Empty)
            {
                if(color==ChessColor.White)
                {
                    switch (board[X,Y])
                    {
                        case ChessPiece.BlackPawn:
                        case ChessPiece.BlackKnight:
                        case ChessPiece.BlackBishop:
                        case ChessPiece.BlackQueen:
                        case ChessPiece.BlackRook:
                        case ChessPiece.BlackKing:
                            return true;
                        default:
                            return false;
                    }
                }
                else if(color==ChessColor.Black)
                {
                    switch (board[X, Y])
                    {
                        case ChessPiece.WhiteBishop:
                        case ChessPiece.WhiteKing:
                        case ChessPiece.WhiteKnight:
                        case ChessPiece.WhitePawn:
                        case ChessPiece.WhiteQueen:
                        case ChessPiece.WhiteRook:
                            return true;
                        default:
                            return false;
                    }
                }
            }

            return true;
        }
Esempio n. 23
0
        public Position GetKingPosition(ChessColor color)
        {
            var currentRow = 0;
            var currentCol = (char)('a');

            for (int row = 0; row < this.TotalCols; row++)
            {
                for (int col = 0; col < this.TotalRows; col++)
                {
                    currentRow = row;
                    currentCol = (char)('a' + col);
                    var position = new Position(currentRow, currentCol);
                    var figureAtPosition = this.GetFigureAtPosition(position);
                    if (figureAtPosition is Figures.King && figureAtPosition.Color == color)
                    {
                        return new Position(currentRow, currentCol);
                    }
                }
            }

            return new Position(currentRow, currentCol);
        }
Esempio n. 24
0
        public static byte[] miniMax(byte[] SRFen, ChessColor color)
        {
            TimeSpan maxTime = TimeSpan.FromMilliseconds(5500);
            byte[] initialBoard = (byte[])SRFen.Clone();
            bool white= color == ChessColor.White? true:false;
            int alpha = -10000;
            int beta = 10000;
            int cutoff = 4;
            int depth = 0;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            int h;

            //test is we are going back and forth
            bool repeat= testRepeat(latestMove);
            if (repeat)
                h = minValue(ref SRFen, depth + 1, white, alpha, beta, cutoff, ref timer, latestMove[1]);
            else
                h = minValue(ref SRFen, depth + 1, white, alpha, beta, cutoff, ref timer, null);
            if (h == -5000) 
                return SRFen;
            byte[] bestSoFar = (byte[])SRFen.Clone();
            while (timer.Elapsed < maxTime && h != -9999)
            { 
                cutoff += 2;
                byte[] temp = (byte[])initialBoard.Clone();
                if (repeat)
                    h = minValue(ref temp, depth + 1, white, alpha, beta, cutoff, bestSoFar, ref timer, latestMove[1]);
                else
                    h = minValue(ref temp, depth + 1, white, alpha, beta, cutoff, bestSoFar, ref timer, null);
                if (temp!=null) bestSoFar = (byte[])temp.Clone();
                if (h == -5000) 
                    return bestSoFar;
            }
          //  Log("cutoff" + cutoff);
            //add bestSoFar to latest move
            addLatestMove(ref latestMove, bestSoFar);
            return bestSoFar;
        }
Esempio n. 25
0
        /// <summary>
        /// The lower the number returned the better off you are
        /// </summary>
        /// <param name="board"></param>
        /// <param name="colorOfEnemyTeam"></param>
        /// <returns></returns>
        int CalculateHueristicAdvanced(ChessColor colorOfEnemyTeam)
        {
            int scoreB = 0;
            int scoreW = 0;
            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    if (BoardAfterMove[x, y] < ChessPiece.Empty) //if black
                        scoreB += StudentAI.Piece.CalculatePieceValue(BoardAfterMove[x, y]);
                    else if (BoardAfterMove[x, y] > ChessPiece.Empty) //if white
                        scoreW += StudentAI.Piece.CalculatePieceValue(BoardAfterMove[x, y]);
                }
            }

            if(TheMove.From==null)
                return colorOfEnemyTeam == ChessColor.Black ? scoreB - scoreW : scoreW - scoreB;

            return colorOfEnemyTeam == ChessColor.Black ? scoreB - scoreW + StudentAI.Piece.CalculatePieceActionValue(BoardBeforeMove[TheMove.From.X, TheMove.From.Y])
                : scoreW - scoreB + StudentAI.Piece.CalculatePieceActionValue(BoardBeforeMove[TheMove.From.X, TheMove.From.Y]);

            //return score - StudentAI.Piece.CalculatePieceActionValue(board[TheMove.From.X, TheMove.From.Y]);
        }
Esempio n. 26
0
 public Knight(ChessColor color) : base(color)
 {
 }
Esempio n. 27
0
        private MovementResult MovePiece(char column, int row, char targetColumn, int targetRow, bool forTest)
        {
            MovementResult result = new MovementResult();

            var selectPiece = GetPiece(column, row);

            // check if there is a piece at start position
            if (selectPiece == null)
            {
                result.IsSuccess   = false;
                result.Description = String.Format("No piece is present at position {0}{1}", column, row.ToString());

                return(result);
            }

            Piece targetPiece = null;
            var   isEnPassant = EnPassant != null && selectPiece is Pawn && targetColumn == EnPassant.Item1 && targetRow == EnPassant.Item2;

            if (!isEnPassant)
            {
                targetPiece = GetPiece(targetColumn, targetRow);
            }
            else
            {
                targetPiece = GetPiece(targetColumn, row);
            }



            // check it is a valid movement for piece (rules piece validator)
            if (!selectPiece.IsValidMovement(
                    (targetPiece != null && !selectPiece.ChessColor.Equals(targetPiece.ChessColor)),
                    row - 1, Columns[column], targetRow - 1, Columns[targetColumn]))
            {
                result.IsSuccess   = false;
                result.Description =
                    String.Format("The {0} {1} at position {2}{3} cannot move to {4}{5}",
                                  selectPiece.ChessColor.ToString(), selectPiece.GetType().Name,
                                  column, row.ToString(), targetColumn, targetRow.ToString());

                return(result);
            }

            // check if the path is free if piece is not a knight
            if (!(selectPiece is Knight) && !checkIfPathIsFree(column, row, targetColumn, targetRow))
            {
                result.IsSuccess   = false;
                result.Description =
                    String.Format("The path from {0}{1} to {2}{3} for {4}{5} is not free.",
                                  column, row.ToString(), targetColumn, targetRow.ToString(),
                                  selectPiece.ChessColor.ToString(), selectPiece.GetType().Name);

                return(result);
            }

            // check if target position there is already present a piece with same color
            if (targetPiece != null && selectPiece.ChessColor.Equals(targetPiece.ChessColor))
            {
                result.IsSuccess   = false;
                result.Description =
                    String.Format("There is already present a {0} piece at position {1}{2}",
                                  selectPiece.ChessColor.ToString(), targetColumn, targetRow);

                return(result);
            }

            //En passant done
            if (isEnPassant && !forTest)
            {
                clearBoardPosition(targetColumn, row);
            }

            // set result information after ate
            result.Ate = (targetPiece != null && !selectPiece.ChessColor.Equals(targetPiece.ChessColor));
            if (result.Ate)
            {
                result.AtePiece = targetPiece;
            }


            // set EnPassant Information
            EnPassant = null;
            if ((selectPiece is Pawn) && (Math.Abs(row - targetRow) == 2))
            {
                //En passant possibilities
                var pieceLeft  = TryGetPiece(Columns[targetColumn], targetRow);
                var pieceRigth = TryGetPiece(Columns[targetColumn] + 2, targetRow);
                if ((pieceLeft != null && pieceLeft.ChessColor != selectPiece.ChessColor && pieceLeft is Pawn)
                    ||
                    (pieceRigth != null && pieceRigth.ChessColor != selectPiece.ChessColor && pieceRigth is Pawn))
                {
                    EnPassant = new Tuple <char, int>(targetColumn, (targetRow + row) / 2);
                }
            }

            //Castling rules
            bool isCastling = false;

            if ((selectPiece is King) && Math.Abs(Columns[column] - Columns[targetColumn]) == 2)
            {
                //1-The king should not have moved yet
                var king = (King)selectPiece;
                if (king.HasAlreadyMoved || !(column == 'E' && (row == 1 || row == 8)))
                {
                    result.IsSuccess   = false;
                    result.Description =
                        String.Format("You cannot do the Castling because your king has already moved.");
                    return(result);
                }
                //2-The Rook should not have moved yet either
                var rookColumn = (targetColumn == 'G') ? 'H' : 'A';
                var pieceFound = GetPiece(rookColumn, targetRow);
                if (pieceFound == null || pieceFound.ChessColor != king.ChessColor || !(pieceFound is Rook))
                {
                    result.IsSuccess   = false;
                    result.Description =
                        String.Format("You cannot do the Castling because there is no Rook at position {0}{1}.", rookColumn, targetRow);
                    return(result);
                }

                var goodRook = (Rook)pieceFound;

                if (goodRook.HasAlreadyMoved)
                {
                    result.IsSuccess   = false;
                    result.Description =
                        String.Format("You cannot do the Castling as your rook has already moved at position {0}{1}.", rookColumn, targetRow);
                    return(result);
                }
                //3-The King should not be chess or passing on a echec position
                var canMoveOnTheKing = CheckKingEchec(king.ChessColor);
                if (canMoveOnTheKing != null)
                {
                    result.IsSuccess   = false;
                    result.Description =
                        String.Format("You cannot do the Castling because you are echec {0}{1}.", canMoveOnTheKing.Item1, canMoveOnTheKing.Item2);
                    return(result);
                }

                //4-Can do one step :
                var oneStepColumn = (targetColumn == 'G') ? 'F' : 'D';
                var canDoOne      = this.MovePiece(column, row, oneStepColumn, targetRow);
                if (!canDoOne.IsSuccess)
                {
                    result.IsSuccess   = false;
                    result.Description =
                        String.Format("You cannot do the Castling because you would be echec at the position {0}{1}.", oneStepColumn, targetRow);
                    return(result);
                }
                else
                {
                    //One step is posible, so rollback, and test two step :
                    PutPiece(king, column, row);
                    clearBoardPosition(oneStepColumn, row);
                    king.HasAlreadyMoved = false;
                    isCastling           = true;
                }
            }

            // change position of piece
            if (!forTest)
            {
                PutPiece(selectPiece, targetColumn, targetRow);
                clearBoardPosition(column, row);

                //Pawn reach the opposite side, can be promoted
                if (selectPiece is Pawn && (targetRow == 1 || targetRow == 8))
                {
                    PawnPromoted      = true;
                    PawnPromotedColor = selectPiece.ChessColor;
                }

                // check if the move does not make our king in chess
                Tuple <char, int> pieceWitchMakeChess = CheckKingEchec(selectPiece.ChessColor);
                if (pieceWitchMakeChess != null)
                {
                    //RollBack
                    PutPiece(selectPiece, column, row);
                    clearBoardPosition(targetColumn, targetRow);
                    PawnPromoted = false;
                    if (result.Ate == true)
                    {
                        var pieceEaten = result.AtePiece;
                        this.PutPiece(pieceEaten, targetColumn, targetRow);
                        result.AtePiece = null;
                        result.Ate      = false;
                    }

                    result.IsSuccess   = false;
                    result.Description =
                        String.Format("Your king is in echec from the piece {0} ({1}{2}).",
                                      selectPiece.GetType().Name, pieceWitchMakeChess.Item1, pieceWitchMakeChess.Item2);

                    return(result);
                }

                if (selectPiece is IHasAlreadyMoved)
                {
                    ((IHasAlreadyMoved)selectPiece).HasAlreadyMoved = true;
                    //TODO : Move the rook in case of castling
                    if (isCastling)
                    {
                        var rookColumnOrigin = (targetColumn == 'G') ? 'H' : 'A';
                        var rookColumnTarget = (targetColumn == 'G') ? 'F' : 'D';
                        var pieceFound       = GetPiece(rookColumnOrigin, row);
                        PutPiece(pieceFound, rookColumnTarget, row);
                        clearBoardPosition(rookColumnOrigin, row);
                    }
                }
            }
            return(result);
        }
Esempio n. 28
0
 /// <summary>
 /// The constructor.
 /// </summary>
 /// <param name="board">The board where the Chess piece is located.</param>
 /// <param name="color">The color of the Chess piece.</param>
 /// <param name="square">The square where the Chess piece is located.</param>
 public Bishop(ChessBoard board, ChessColor color, Square square = null)
     : base(board, color, square)
 {
     this.Movement = new RowMovement(this, MovementDirection.Diagonal);
 }
Esempio n. 29
0
 /// <summary>
 /// Draw chessboard on screen
 /// </summary>
 /// <param name="date">What date is it?</param>
 /// <param name="CC">What is the colour going to be displayed</param>
 public void Draw(string date, ChessColor CC)
 {
     Draw(false, true, CC);
 }
Esempio n. 30
0
        /// <summary>
        /// Draw chessboard on screen
        /// </summary>
        /// <param name="scrollx">X direction to scroll</param>
        /// <param name="scrolly">Y direction to scroll</param>
        /// <param name="CC">What is the colour going to be displayed?</param>
        public void Draw(bool scrollx, bool scrolly, ChessColor CC)
        {
            if (scrolly) this.m_scrollY += 0.005;
            if (scrollx) this.m_scrollX += 0.005;
            if (this.m_scrollY >= 1.0) this.m_scrollY = 0.0;
            if (this.m_scrollX >= 1.0) this.m_scrollX = 0.0;

            if (Util.Fog)
            {
                GL.Enable(EnableCap.Fog);
            }
            if (Util.Lightning)
            {
                GL.Enable(EnableCap.Lighting);
                GL.Enable(EnableCap.Light0);
            }

            GL.Enable(EnableCap.Texture2D);
            GL.BindTexture(TextureTarget.Texture2D, texture[(int)CC]);
            GL.Begin(BeginMode.Quads);
            //GL.TexCoord2(0.0 + this.m_scrollX, 0.0 + this.m_scrollY); GL.Vertex3(-2.30f, -1.50f, -1.0f); // bottom right
            //GL.TexCoord2(0.0 + this.m_scrollX, 5.0 + this.m_scrollY); GL.Vertex3(-2.30f, 0.20f, 1.0f); // top right
            //GL.TexCoord2(5.0 + this.m_scrollX, 5.0 + this.m_scrollY); GL.Vertex3(2.30f, 0.20f, 1.0f); // top left
            //GL.TexCoord2(5.0 + this.m_scrollX, 0.0 + this.m_scrollY); GL.Vertex3(2.30f, -1.50f, -1.0f); // bottom left

            /*
            GL.Color4(System.Drawing.Color.White); GL.TexCoord2(0.0 - this.m_scrollX, 5.0 + this.m_scrollY); GL.Vertex3(2.50f, -1.50f, 0.0f); // bottom left
            GL.Color4(System.Drawing.Color.Yellow); GL.TexCoord2(5.0 - this.m_scrollX, 5.0 + this.m_scrollY); GL.Vertex3(-2.50f, -1.50f, 0.0f); // bottom right
            GL.Color4(System.Drawing.Color.Blue); GL.TexCoord2(5.0 - this.m_scrollX, 0.0 + this.m_scrollY); GL.Vertex3(-3.00f, 0.50f, 5.1f); // top right
            GL.Color4(System.Drawing.Color.Red); GL.TexCoord2(0.0 - this.m_scrollX, 0.0 + this.m_scrollY); GL.Vertex3(3.00f, 0.50f, 5.1f); // top left
            */

            GL.TexCoord2(0.0 + this.m_scrollX, 0.0 + this.m_scrollY); GL.Vertex3(-4.60f, -2.00f, 0.0f); // bottom right
            GL.TexCoord2(0.0 + this.m_scrollX, 5.0 + this.m_scrollY); GL.Vertex3(-4.60f, 0.20f, 5.1f); // top right
            GL.TexCoord2(5.0 + this.m_scrollX, 5.0 + this.m_scrollY); GL.Vertex3(4.60f, 0.20f, 5.1f); // top left
            GL.TexCoord2(5.0 + this.m_scrollX, 0.0 + this.m_scrollY); GL.Vertex3(4.60f, -2.00f, 0.0f); // bottom left

            GL.End();
            GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.Disable(EnableCap.Texture2D);

            if (Util.Fog)
            {
                GL.Disable(EnableCap.Fog);
            }

            if (Util.Lightning)
            {
                GL.Disable(EnableCap.Lighting);
                GL.Disable(EnableCap.Light0);
            }
        }
Esempio n. 31
0
 internal Piece(Piece piece)
 {
     PieceColor = piece.PieceColor;
     PieceType = piece.PieceType;
     Moved = piece.Moved;
     PieceValue = piece.PieceValue;
     PieceActionValue = piece.PieceActionValue;
 }
Esempio n. 32
0
        public Piece[] PawnPromotedChoice()
        {
            ChessColor color = PawnPromotedColor;

            return(new Piece[] { GetPiecePromoted(1, color), GetPiecePromoted(2, color), GetPiecePromoted(3, color), GetPiecePromoted(4, color) });
        }
Esempio n. 33
0
        // piece factory
        public void SetPiece <T>(ChessColor color, char column, int row) where T : Piece
        {
            var piece = Activator.CreateInstance(typeof(T), color) as Piece;

            PutPiece(piece, column, row);
        }
Esempio n. 34
0
            internal Piece(ChessPiece chessPiece, ChessColor chessPieceColor)
            {
                PieceType = chessPiece;
                PieceColor = chessPieceColor;

                PieceValue = CalculatePieceValue(PieceType);
                PieceActionValue = CalculatePieceActionValue(PieceType);
            }
Esempio n. 35
0
 public Rook(ChessColor color) : base(color)
 {
 }