コード例 #1
0
        /// <summary>
        /// Helper constructor that creates a new chess board based off of a Fen string.
        /// </summary>
        /// <param name="fenBoard">The Fen string that represents the board.</param>
        public ChessBoard(string fenBoard)
        {
            Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_ctor_string);
            Board = new ChessPiece[NumberOfRows, NumberOfColumns];

            FromFenBoard(fenBoard);
        }
コード例 #2
0
 /// <summary>
 /// Constructor that creates a new ChessMove object based on two ChessLocations and a ChessFlag
 /// </summary>
 /// <param name="from">From ChessLocation</param>
 /// <param name="to">To ChessLocation</param>
 /// <param name="flag">The ChessFlag to create the move with.</param>
 public ChessMove(ChessLocation from, ChessLocation to, ChessFlag flag)
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessMove_ctor_ChessLocation_ChessLocation_ChessFlag);
     From           = from;
     To             = to;
     Flag           = flag;
     ToStringPrefix = string.Empty;
 }
コード例 #3
0
        /// <summary>
        /// Creates a string that shows the describes the chess move.
        /// </summary>
        /// <returns>The description string.</returns>
        public override string ToString()
        {
            Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessMove_ToString);
            string moveText = string.Empty;

            if (this.From == null)
            {
                moveText += "The Move's From field was null. ";
            }
            else
            {
                if (!this.From.IsValid)
                {
                    moveText += "The Move's From field was outside the bounds of the board coordinates. ";
                }
            }

            if (this.To == null)
            {
                moveText += "The Move's To field was null. ";
            }
            else
            {
                if (!this.To.IsValid)
                {
                    moveText += "The Move's To field was outside the bounds of the board coordinates. ";
                }
            }

            if (this.From == this.To)
            {
                moveText += "The Move's From and To fields are equal. ";
            }

            if ((moveText != string.Empty) && (this.Flag != ChessFlag.NoFlag))
            {
                moveText += "Flag: " + this.Flag.ToString();
            }
            else
            {
                moveText = ToStringPrefix + "From: [" + this.From.X + ", " + this.From.Y + "]   To: [" + this.To.X + ", " + this.To.Y + "] ";

                if (this.Flag != ChessFlag.NoFlag)
                {
                    moveText += "Flag: " + this.Flag.ToString();;
                }
            }

            moveText.TrimEnd();

            return(moveText);
        }
コード例 #4
0
 /// <summary>
 /// Gets or sets the piece on the board in a specified location; 0,0 is the upper left hand corner of the board.
 /// </summary>
 /// <param name="x">Column of the desired piece location</param>
 /// <param name="y">Row of the desired piece location</param>
 /// <returns>ChessPiece</returns>
 public ChessPiece this[int x, int y]
 {
     get
     {
         Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_get_Indexer_int_int);
         return(Board[x, y]);
     }
     set
     {
         Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_set_Indexer_int_int);
         Board[x, y] = value;
     }
 }
コード例 #5
0
 /// <summary>
 /// Gets or sets the piece on the board in a specified location; 0,0 is the upper left hand corner of the board.
 /// </summary>
 /// <param name="location">Location of the desired piece</param>
 /// <returns>ChessPiece</returns>
 public ChessPiece this[ChessLocation location]
 {
     get
     {
         Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_get_ChessLocationIndexer);
         return(this[location.X, location.Y]);
     }
     set
     {
         Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_set_ChessLocationIndexer);
         this[location.X, location.Y] = value;
     }
 }
コード例 #6
0
 public static bool operator ==(ChessMove move1, ChessMove move2)
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessMove_EQ);
     if ((((object)move1) == null) && (((object)move2) == null))
     {
         return(true);
     }
     if (((object)move1) == null)
     {
         return(false);
     }
     return(move1.Equals(move2));
 }
コード例 #7
0
        private ChessPiece[,] CloneBoard(ChessPiece[,] board)
        {
            Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_CloneBoard_ChessPieceArray);
            ChessPiece[,] retBoard = new ChessPiece[NumberOfRows, NumberOfColumns];
            for (int Y = 0; Y < NumberOfRows; Y++)
            {
                for (int X = 0; X < NumberOfColumns; X++)
                {
                    retBoard[X, Y] = board[X, Y];
                }
            }

            return(retBoard);
        }
コード例 #8
0
        public static bool operator==(ChessLocation lhs, ChessLocation rhs)
        {
            Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessLocation_EQ);
            if (((object)lhs == null) &&
                ((object)rhs == null))
            {
                return(true);
            }

            if (((object)lhs == null) ||
                ((object)rhs == null))
            {
                return(false);
            }

            return(lhs.Equals(rhs));
        }
コード例 #9
0
        /// <summary>
        /// Create a ChessMove exactly like this one.
        /// </summary>
        /// <returns>The cloned move</returns>
        public ChessMove Clone()
        {
            Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessMove_Clone);
            ChessMove newMove = new ChessMove(null, null);

            if (this.To != null)
            {
                newMove.To = this.To.Clone();
            }

            if (this.From != null)
            {
                newMove.From = this.From.Clone();
            }

            newMove.ValueOfMove = this.ValueOfMove;
            newMove.Flag        = this.Flag;

            return(newMove);
        }
コード例 #10
0
        /// <summary>
        /// Executes a move on the ChessBoard.
        /// </summary>
        /// <param name="move">The move to execute.</param>
        public void MakeMove(ChessMove move)
        {
            Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_MakeMove_ChessMove);
            if (move.IsBasicallyValid)
            {
                // Handle Queening
                if ((this[move.From] == ChessPiece.WhitePawn) && (move.From.Y == 1) && (move.To.Y == 0))
                {
                    this[move.To] = ChessPiece.WhiteQueen;
                }
                else if ((this[move.From] == ChessPiece.BlackPawn) && (move.From.Y == 6) && (move.To.Y == 7))
                {
                    this[move.To] = ChessPiece.BlackQueen;
                }
                else
                {
                    this[move.To] = this[move.From];
                }

                this[move.From] = ChessPiece.Empty;
            }
        }
コード例 #11
0
        public override bool Equals(object obj)
        {
            Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessLocation_Equals);
            if (obj == null)
            {
                return(false);
            }

            if (!(obj is ChessLocation))
            {
                return(false);
            }

            ChessLocation tmpLoc = (ChessLocation)obj;

            if ((this.X == tmpLoc.X) &&
                (this.Y == tmpLoc.Y))
            {
                return(true);
            }

            return(false);
        }
コード例 #12
0
        public override bool Equals(object obj)
        {
            Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessMove_Equals);
            if (obj == null)
            {
                return(false);
            }
            if (!(obj is ChessMove))
            {
                return(false);
            }
            ChessMove move2 = (ChessMove)obj;

            if (this.Flag != move2.Flag)
            {
                return(false);
            }
            if ((this.From != move2.From) || (this.To != move2.To))
            {
                return(false);
            }

            return(true);
        }
コード例 #13
0
        /// <summary>
        /// Accepts a full Fen board and sets the ChessBoard object to that state.
        /// </summary>
        /// <param name="fenBoard">The Fen string</param>
        public void FromFenBoard(string fenBoard)
        {
            Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_FromFenBoard_string);
            string[] lines  = fenBoard.Split(' ')[0].Split('/');
            int      spaces = 0;

            for (int y = 0; y < ChessBoard.NumberOfRows; ++y)
            {
                for (int x = 0; x < ChessBoard.NumberOfColumns; ++x)
                {
                    this[x, y] = ChessPiece.Empty;
                }
            }
            ChessPiece piece = ChessPiece.Empty;

            for (int y = 0; y < ChessBoard.NumberOfRows; ++y)
            {
                for (int x = 0, boardCol = 0; x < lines[y].Length; ++x, ++boardCol)
                {
                    if (Char.IsDigit(lines[y][x]))
                    {
                        spaces    = Convert.ToInt32(lines[y][x]) - 48;
                        boardCol += spaces - 1;
                    }
                    else
                    {
                        switch (lines[y][x])
                        {
                        case 'r':
                            piece = ChessPiece.BlackRook;
                            break;

                        case 'n':
                            piece = ChessPiece.BlackKnight;
                            break;

                        case 'b':
                            piece = ChessPiece.BlackBishop;
                            break;

                        case 'q':
                            piece = ChessPiece.BlackQueen;
                            break;

                        case 'k':
                            piece = ChessPiece.BlackKing;
                            break;

                        case 'p':
                            piece = ChessPiece.BlackPawn;
                            break;

                        case 'K':
                            piece = ChessPiece.WhiteKing;
                            break;

                        case 'Q':
                            piece = ChessPiece.WhiteQueen;
                            break;

                        case 'R':
                            piece = ChessPiece.WhiteRook;
                            break;

                        case 'B':
                            piece = ChessPiece.WhiteBishop;
                            break;

                        case 'N':
                            piece = ChessPiece.WhiteKnight;
                            break;

                        case 'P':
                            piece = ChessPiece.WhitePawn;
                            break;

                        default:
                            throw new Exception("Invalid FEN board");
                        }
                        this[boardCol, y] = piece;
                    }
                }
            }
        }
コード例 #14
0
 /// <summary>
 /// Creates a complete copy of this object and returns it.
 /// </summary>
 /// <returns>The ChessLocation copy.</returns>
 public ChessLocation Clone()
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessLocation_Clone);
     return(new ChessLocation(this.X, this.Y));
 }
コード例 #15
0
 /// <summary>
 /// Creates a ChessLocation object
 /// </summary>
 /// <param name="x">Column number. Ranges 0 - 7</param>
 /// <param name="y">Row number. Ranges from 0 - 7</param>
 public ChessLocation(int x, int y)
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessLocation_ctor_int_int);
     X = x;
     Y = y;
 }
コード例 #16
0
 public override int GetHashCode()
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessLocation_GetHashCode);
     return(X * 10 + Y);
 }
コード例 #17
0
 public static bool operator !=(ChessLocation lhs, ChessLocation rhs)
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessLocation_NE);
     return(!(lhs == rhs));
 }
コード例 #18
0
 /// <summary>
 /// Helper constructor that creates a new chess board based off of a ChessPiece array board.
 /// </summary>
 /// <param name="board">The ChessPiece array that represents the board.</param>
 public ChessBoard(ChessPiece[,] board)
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_ctor_ChessPieceArray);
     Board = CloneBoard(board);
 }
コード例 #19
0
 public static bool operator !=(ChessMove move1, ChessMove move2)
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessMove_NE);
     return(!(move1 == move2));
 }
コード例 #20
0
 /// <summary>
 /// The main constructor that defaults the ChessBoard to the normal starting chess board state.
 /// </summary>
 public ChessBoard() : this(ChessState.FenStartState)
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_ctor);
 }
コード例 #21
0
 /// <summary>
 /// This is used to allow sorting of ChessMoves based on their values.
 /// </summary>
 /// <param name="other">a ChessMove to compare.</param>
 /// <returns></returns>
 public int CompareTo(ChessMove other)
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessMove_CompareTo_ChessMove);
     // Sorts it from lowest value move to highest value move
     return(this.ValueOfMove - other.ValueOfMove);
 }
コード例 #22
0
 /// <summary>
 /// Returns the hashcode for this object
 /// </summary>
 /// <returns>the hash code.</returns>
 public override int GetHashCode()
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_GetHashCode);
     return(this.ToPartialFenBoard().GetHashCode());
 }
コード例 #23
0
        /// <summary>
        /// Creates a partial Fen string from the ChessBoard.
        /// It is only partial however, since Fen strings contain more data than just the board.
        /// </summary>
        /// <returns>The partial Fen string.</returns>
        public string ToPartialFenBoard()
        {
            Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_ToPartialFenBoard);
            StringBuilder strBuild = new StringBuilder();

            for (int y = 0; y < ChessBoard.NumberOfRows; ++y)
            {
                int spaces = 0;
                for (int x = 0; x < ChessBoard.NumberOfColumns; ++x)
                {
                    char c = 'x';
                    switch (this[x, y])
                    {
                    case ChessPiece.WhitePawn:
                        c = 'P';
                        break;

                    case ChessPiece.WhiteRook:
                        c = 'R';
                        break;

                    case ChessPiece.WhiteKnight:
                        c = 'N';
                        break;

                    case ChessPiece.WhiteBishop:
                        c = 'B';
                        break;

                    case ChessPiece.WhiteQueen:
                        c = 'Q';
                        break;

                    case ChessPiece.WhiteKing:
                        c = 'K';
                        break;

                    case ChessPiece.BlackPawn:
                        c = 'p';
                        break;

                    case ChessPiece.BlackRook:
                        c = 'r';
                        break;

                    case ChessPiece.BlackKnight:
                        c = 'n';
                        break;

                    case ChessPiece.BlackBishop:
                        c = 'b';
                        break;

                    case ChessPiece.BlackQueen:
                        c = 'q';
                        break;

                    case ChessPiece.BlackKing:
                        c = 'k';
                        break;

                    case ChessPiece.Empty:
                        ++spaces;
                        continue;

                    default:
                        throw new Exception("Invalid chess piece");
                    }
                    if ((c != 'x') && (spaces > 0))
                    {
                        strBuild.Append(spaces.ToString());
                        spaces = 0;
                    }
                    strBuild.Append(c);
                }

                if (spaces > 0)
                {
                    strBuild.Append(spaces.ToString());
                    spaces = 0;
                }
                if (y < 7)
                {
                    strBuild.Append('/');
                }
            }
            return(strBuild.ToString());
        }
コード例 #24
0
 public override int GetHashCode()
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessMove_GetHashCode);
     return(From.GetHashCode() * 10 + To.GetHashCode());
 }
コード例 #25
0
 /// <summary>
 /// Constructor that creates a new ChessMove object based on two ChessLocations
 /// </summary>
 /// <param name="from">From ChessLocation</param>
 /// <param name="to">To ChessLocation</param>
 public ChessMove(ChessLocation from, ChessLocation to) : this(from, to, ChessFlag.NoFlag)
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessMove_ctor_ChessLocation_ChessLocation);
 }
コード例 #26
0
 /// <summary>
 /// Creates a complete copy of this object and returns it.
 /// </summary>
 /// <returns>The copy ChessBoard</returns>
 public ChessBoard Clone()
 {
     Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_Clone);
     return(new ChessBoard(this.Board));
 }