Esempio n. 1
0
        /// <summary>
        /// Creates a deep copy of ChessState
        /// </summary>
        /// <returns></returns>
        public ChessState Clone()
        {
            ChessState newState = new ChessState();

            if (this.CurrentBoard == null)
            {
                newState.CurrentBoard = null;
            }
            else
            {
                newState.CurrentBoard = this.CurrentBoard.Clone();
            }

            if (this.PreviousBoard == null)
            {
                newState.PreviousBoard = null;
            }
            else
            {
                newState.PreviousBoard = this.PreviousBoard.Clone();
            }

            if (this.PreviousMove == null)
            {
                newState.PreviousMove = null;
            }
            else
            {
                newState.PreviousMove = this.PreviousMove.Clone();
            }

            newState.CurrentPlayerColor = this.CurrentPlayerColor;

            newState.CanWhiteCastleKingSide  = this.CanWhiteCastleKingSide;
            newState.CanWhiteCastleQueenSide = this.CanWhiteCastleQueenSide;
            newState.CanBlackCastleKingSide  = this.CanBlackCastleKingSide;
            newState.CanBlackCastleQueenSide = this.CanBlackCastleQueenSide;

            if (this.EnPassant != null)
            {
                newState.EnPassant = this.EnPassant.Clone();
            }
            newState.HalfMoves = this.HalfMoves;
            newState.FullMoves = this.FullMoves;

            return(newState);
        }
Esempio n. 2
0
        public ChessGame(ChessState state, string whitePlayerName, string blackPlayerName)
        {
            IsGameRunning = false;

            _mainChessState = state;
            _whitePlayer    = new ChessPlayer(ChessColor.White);
            _blackPlayer    = new ChessPlayer(ChessColor.Black);

            _whitePlayer.AIName = whitePlayerName;
            _blackPlayer.AIName = blackPlayerName;

            //Load the AI if it isn't loaded already
            LoadAI(_whitePlayer);
            LoadAI(_blackPlayer);

            Profiler.BeginGame();

            // Hook up the AI Log methods to the GUI
            if (_whitePlayer.IsComputer)
            {
                _whitePlayer.AI.Log             += Logger.AddToWhitesLog;
                _whitePlayer.AI.IsMyTurnOver    += _whitePlayer.IsTurnOver;
                _whitePlayer.AI.SetDecisionTree += SetTmpLastDecisionTree;
                _whitePlayer.AI.Profiler         = Profiler.WhiteProfiler;
                Profiler.WhiteProfiler.AIName    = _whitePlayer.AI.Name;
            }

            if (_blackPlayer.IsComputer)
            {
                _blackPlayer.AI.Log             += Logger.AddToBlacksLog;
                _blackPlayer.AI.IsMyTurnOver    += _blackPlayer.IsTurnOver;
                _blackPlayer.AI.SetDecisionTree += SetTmpLastDecisionTree;
                _blackPlayer.AI.Profiler         = Profiler.BlackProfiler;
                Profiler.BlackProfiler.AIName    = _blackPlayer.AI.Name;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a deep copy of ChessState
        /// </summary>
        /// <returns></returns>
        public ChessState Clone()
        {
            ChessState newState = new ChessState();

            if (this.CurrentBoard == null)
                newState.CurrentBoard = null;
            else
                newState.CurrentBoard = this.CurrentBoard.Clone();

            if (this.PreviousBoard == null)
                newState.PreviousBoard = null;
            else
                newState.PreviousBoard = this.PreviousBoard.Clone();

            if (this.PreviousMove == null)
                newState.PreviousMove = null;
            else
                newState.PreviousMove = this.PreviousMove.Clone();

            newState.CurrentPlayerColor = this.CurrentPlayerColor;

            newState.CanWhiteCastleKingSide = this.CanWhiteCastleKingSide;
            newState.CanWhiteCastleQueenSide = this.CanWhiteCastleQueenSide;
            newState.CanBlackCastleKingSide = this.CanBlackCastleKingSide;
            newState.CanBlackCastleQueenSide = this.CanBlackCastleQueenSide;

            if (this.EnPassant != null)
                newState.EnPassant = this.EnPassant.Clone();
            newState.HalfMoves = this.HalfMoves;
            newState.FullMoves = this.FullMoves;

            return newState;
        }
Esempio n. 4
0
        void DoNextMove(ChessPlayer player, ChessPlayer opponent)
        {
            ChessMove nextMove = null;
            //DateTime start = DateTime.Now;
            bool?      isValidMove = false;
            ChessState newstate    = null;

            if (player.IsComputer)
            {
                // Clear out the decision tree
                _tmpDecisionTree = null;

                Profiler.BeginTurn(player.Color);
                nextMove = player.GetNextMove(_mainChessState.CurrentBoard);
                Profiler.EndTurn(player.TimeOfLastMove);
                Logger.Log("Time Of " + player.ColorAndName + "'s last move: " + player.TimeOfLastMove);

                SetDecisionTree(_tmpDecisionTree);

                if (!this.IsGameRunning)
                {
                    // if we're no longer running, leave the method
                    return;
                }

                if ((nextMove == null) || (!nextMove.IsBasicallyValid))
                {
                    IsGameRunning = false;
                    _results      = "The framework caught " + player.ColorAndName + " returning a completely invalid move, therefore " +
                                    player.ColorAndName + " loses!";

                    if (nextMove == null)
                    {
                        Logger.Log(player.ColorAndName + " returned a null move object.");
                    }
                    else
                    {
                        Logger.Log(player.ColorAndName + "'s invalid move was: " + nextMove.ToString());
                    }

                    return;
                }

                if (nextMove.Flag == ChessFlag.AIWentOverTime)
                {
                    // the AI went over it's time limit.
                    IsGameRunning = false;
                    _results      = player.ColorAndName + " went over the time limit to move and grace period. Total move time was: " + player.TimeOfLastMove.ToString();
                    return;
                }

                if (nextMove.Flag != ChessFlag.Stalemate)
                {
                    // The move is not a stale mate, and it's basically valid,
                    // so, let's see if the move is actually valid.
                    newstate = _mainChessState.Clone();
                    newstate.MakeMove(nextMove);

                    isValidMove = opponent.IsValidMove(newstate.PreviousBoard, newstate.PreviousMove);
                    if (!opponent.IsHuman)
                    {
                        Logger.Log("Time Of " + opponent.ColorAndName + "'s validate move: " + opponent.TimeOfLastMove);
                    }
                }
            }
            else //player is human
            {
                this.SetGuiChessBoard_IsLocked(false);

                nextMove = player.GetNextMove(_mainChessState.CurrentBoard);

                if (!IsGameRunning)
                {
                    // if we're no longer running, leave the method
                    return;
                }

                newstate = _mainChessState.Clone();
                newstate.MakeMove(nextMove);

                isValidMove = opponent.IsValidMove(newstate.PreviousBoard, newstate.PreviousMove);
                if (!opponent.IsHuman)
                {
                    Logger.Log("Time Of " + opponent.ColorAndName + "'s validate move: " + opponent.TimeOfLastMove);
                }

                this.SetGuiChessBoard_IsLocked(true);
            }//end if player == human

            if ((UpdatedState != null) && (newstate != null))
            {
                // If someone is sub'd to our update delegate,
                // update them with the new state.
                UpdatedState(newstate);
            }

            if (isValidMove == null)
            {
                // the AI went over it's time limit.
                IsGameRunning = false;
                _results      = opponent.ColorAndName + " went over the time limit to validate a move and grace period. Total move time was: " + opponent.TimeOfLastMove.ToString();
                return;
            }

            if (isValidMove == true)
            {
                _mainChessState = newstate;

                if (player.Color == ChessColor.Black)
                {
                    _mainChessState.FullMoves++;//Increment fullmoves after black's turn
                }

                //Determine if a pawn was moved or a kill was made.
                if (ResetHalfMove())
                {
                    _mainChessState.HalfMoves = 0;
                }
                else
                {
                    if (_mainChessState.HalfMoves < 50)
                    {
                        _mainChessState.HalfMoves++;
                    }
                    else
                    {
                        //end of game: 50 move rule
                        IsGameRunning = false;
                        _results      = "Game is a stalemate. 50 moves were made without a kill or a pawn advancement.";
                    }
                }

                if (nextMove.Flag == ChessFlag.Check)
                {
                    Logger.Log(player.ColorAndName + " has put " + opponent.ColorAndName + " in Check!");
                }

                if (nextMove.Flag == ChessFlag.Checkmate)
                {
                    // Checkmate on a valid move has been signaled.
                    IsGameRunning = false;

                    _results = player.ColorAndName + " has signaled that the game is a checkmate _and_ " +
                               opponent.ColorAndName + " said the last move was valid.";
                }
            }
            else
            {
                // It is either a stalemate or an invalid move. Either way, we're done running.
                IsGameRunning = false;

                if (nextMove.Flag == ChessFlag.Stalemate)
                {
                    // A stalemate has occurred. Since stalemates can occur because the AI can't
                    // make a move, we don't have the other AI check their move (because it would
                    // probably just be an empty move).
                    _results = player.ColorAndName + " has signaled that the game is a stalemate.";
                }
                else
                {
                    _results = opponent.ColorAndName + " has signaled that " + player.ColorAndName +
                               " returned an invalid move!";

                    Logger.Log(player.ColorAndName + "'s invalid move was: " + nextMove.ToString());
                }
            }
        }