Example #1
0
        // Return true if the given side type is under check state
        public bool IsUnderCheck(Side.SideType PlayerSide)
        {
            Cell      OwnerKingCell = null;
            ArrayList OwnerCells    = m_Board.GetSideCell(PlayerSide);

            // loop all the owner squars and get his king cell
            foreach (string CellName in OwnerCells)
            {
                if (m_Board[CellName].piece.Type == Piece.PieceType.King)
                {
                    OwnerKingCell = m_Board[CellName]; // store the enemy cell position
                    break;                             // break the loop
                }
            }

            // Loop all the enemy squars and get their possible moves
            ArrayList EnemyCells = m_Board.GetSideCell((new Side(PlayerSide)).Enemy());

            foreach (string CellName in EnemyCells)
            {
                ArrayList moves = GetPossibleMoves(m_Board[CellName]);                  // Get the moves for the enemy piece
                // King is directly under attack
                if (moves.Contains(OwnerKingCell))
                {
                    return(true);
                }
            }
            return(false);
        }
Example #2
0
 // Return back the given side type
 public Player GetPlayerBySide(Side.SideType type)
 {
     if (type == Side.SideType.Black)
     {
         return(m_BlackPlayer);
     }
     else
     {
         return(m_WhitePlayer);
     }
 }
Example #3
0
 // Return true if the given side is stalemate
 public bool IsStaleMate(Side.SideType PlayerSide)
 {
     // if player is not under check and he has no moves
     if (!IsUnderCheck(PlayerSide) && GetCountOfPossibleMoves(PlayerSide) == 0)
     {
         return(true);                   // player is checkmate
     }
     else
     {
         return(false);
     }
 }
Example #4
0
        // Reset the game board and all player status
        public void Reset()
        {
            m_MovesHistory.Clear();
            m_RedoMovesHistory.Clear();

            // Reset player timers
            m_WhitePlayer.ResetTime();
            m_BlackPlayer.ResetTime();

            GameTurn = Side.SideType.White;          // In chess first turn is always of white
            m_WhitePlayer.TimeStart();               // Player time starts
            Board.Init(this.AbideByChess960RuleSet); // Initialize the board object
        }
Example #5
0
        // Reset the game board and all player status
        public void Reset(string mode = "Normal")
        {
            m_MovesHistory.Clear();
            m_RedoMovesHistory.Clear();

            // Reset player timers
            m_WhitePlayer.ResetTime();
            m_BlackPlayer.ResetTime();

            GameTurn = Side.SideType.White;     // In chess first turn is always of white
            m_WhitePlayer.TimeStart();          // Player time starts
            Board.Init(mode);                   // Initialize the board object
        }
Example #6
0
        // Returns true if the give move place the user under check
        private bool CauseCheck(Move move)
        {
            bool CauseCheck = false;

            Side.SideType PlayerSide = move.StartCell.piece.Side.type;

            // To check if a move cause check, we actually need to execute and check the result of that move
            ExecuteMove(move);
            CauseCheck = IsUnderCheck(PlayerSide);
            UndoMove(move);             // undo the move

            return(CauseCheck);
        }
Example #7
0
        // Reset the game board and all player status
        public void Reset(bool FisherStart = false)             //takes in a bool for randomization because this is where the board init is called from
        {
            m_MovesHistory.Clear();
            m_RedoMovesHistory.Clear();

            // Reset player timers
            m_WhitePlayer.ResetTime();
            m_BlackPlayer.ResetTime();

            GameTurn = Side.SideType.White;     // In chess first turn is always of white
            m_WhitePlayer.TimeStart();          // Player time starts
            Board.Init(FisherStart);            // Initialize the board object(with a bool to determine weather or not to randomize it)
        }
Example #8
0
        // Returns a count of all the possilbe moves for given side
        private int GetCountOfPossibleMoves(Side.SideType PlayerSide)
        {
            int TotalMoves = 0;

            // Loop all the owner squars and get their possible moves
            ArrayList PlayerCells = m_Board.GetSideCell(PlayerSide);

            foreach (string CellName in PlayerCells)
            {
                ArrayList moves = GetLegalMoves(m_Board[CellName]);                     // Get all the legal moves for the owner piece
                TotalMoves += moves.Count;
            }
            return(TotalMoves);
        }
Example #9
0
 // Set game turn for the next player
 public void NextPlayerTurn()
 {
     if (GameTurn == Side.SideType.White)
     {
         m_WhitePlayer.TimeEnd();
         m_BlackPlayer.TimeStart();              // Start player timer
         GameTurn = Side.SideType.Black;         // Set black's turn
     }
     else
     {
         m_BlackPlayer.TimeEnd();
         m_WhitePlayer.TimeStart();              // Start player timer
         GameTurn = Side.SideType.White;         // Set white's turn
     }
 }
Example #10
0
        // Analyze the board and return back the evualted score for the given side
        public int AnalyzeBoard(Side.SideType PlayerSide)
        {
            int       Score      = 0;
            ArrayList OwnerCells = m_Board.GetSideCell(PlayerSide);

            // loop all the owner squars and get his king cell
            foreach (string ChessCell in OwnerCells)
            {
                Score += m_Board[ChessCell].piece.GetWeight();
            }

            //int iPossibleMoves = GetCountOfPossibleMoves(PlayerSide);
            //Score+=iPossibleMoves*5; // Each mobility has 5 points
            return(Score);
        }
Example #11
0
        // get all the cell containg pieces of given side
        public ArrayList GetSideCell(Side.SideType PlayerSide)
        {
            ArrayList CellNames = new ArrayList();

            // Loop all the squars and store them in Array List
            for (int row = 1; row <= 8; row++)
            {
                for (int col = 1; col <= 8; col++)
                {
                    // check and add the current type cell
                    if (this[row, col].piece != null && !this[row, col].IsEmpty() && this[row, col].piece.Side.type == PlayerSide)
                    {
                        CellNames.Add(this[row, col].ToString());                        // append the cell name to list
                    }
                }
            }

            return(CellNames);
        }
Example #12
0
        // Reset the game board and all player status
        public void Reset(bool game960 = false)
        {
            m_MovesHistory.Clear();
            m_RedoMovesHistory.Clear();

            // Reset player timers
            m_WhitePlayer.ResetTime();
            m_BlackPlayer.ResetTime();

            GameTurn = Side.SideType.White;     // In chess first turn is always of white
            m_WhitePlayer.TimeStart();          // Player time starts
            if (game960)
            {
                Board.Init960();    // Initialize the board object as a Chess960 game
            }
            else
            {
                Board.Init();                   // Initialize the board object as a normal game
            }
        }
Example #13
0
        /// <summary>
        /// DeSerialize the Game object from XML String
        /// </summary>
        /// <returns>XML containing the Game object state XML</returns>
        public void XmlDeserialize(XmlNode xmlGame)
        {
            // If this source file doesn't contain the check sum attribut, return back
            if (xmlGame.Attributes["Checksum"] == null)
            {
                return;
            }

            // Read game state attributes
            DoNullMovePruning    = (XMLHelper.GetNodeText(xmlGame, "DoNullMovePruning") == "True");
            DoPrincipleVariation = (XMLHelper.GetNodeText(xmlGame, "DoPrincipleVariation") == "True");
            DoQuiescentSearch    = (XMLHelper.GetNodeText(xmlGame, "DoQuiescentSearch") == "True");

            // Restore the Game turn info
            GameTurn = (XMLHelper.GetNodeText(xmlGame, "DoQuiescentSearch") == "Black") ? Side.SideType.Black : Side.SideType.White;

            // Restore the Board State
            XmlNode xmlBoard = XMLHelper.GetFirstNodeByName(xmlGame, "Board");

            Board.XmlDeserialize(xmlBoard);

            // Restore the Player info
            XmlNode xmlPlayer = XMLHelper.GetFirstNodeByName(xmlGame, "WhitePlayer");

            m_WhitePlayer           = (Player)XMLHelper.XmlDeserialize(typeof(Player), xmlPlayer.InnerXml);
            m_WhitePlayer.GameRules = m_Rules;

            xmlPlayer               = XMLHelper.GetFirstNodeByName(xmlGame, "BlackPlayer");
            m_BlackPlayer           = (Player)XMLHelper.XmlDeserialize(typeof(Player), xmlPlayer.InnerXml);
            m_BlackPlayer.GameRules = m_Rules;

            // Restore all the moves for the move history
            XmlNode xmlMoves = XMLHelper.GetFirstNodeByName(xmlGame, "MovesHistory");

            foreach (XmlNode xmlMove in xmlMoves.ChildNodes)
            {
                Move move = (Move)XMLHelper.XmlDeserialize(typeof(Move), xmlMove.OuterXml);
                m_MovesHistory.Push(move);
            }
        }
Example #14
0
 // Return true if the given side is stalemate
 public bool IsStaleMate(Side.SideType PlayerSide)
 {
     return(m_Rules.IsStaleMate(PlayerSide));
 }
Example #15
0
 // Return true if the given side is checkmate
 public bool IsCheckMate(Side.SideType PlayerSide)
 {
     return(PlayerRules.IsCheckMate(PlayerSide));
 }