Beispiel #1
0
        /// <summary>
        ///   Load game from the specified file name.
        /// </summary>
        /// <param name="strFileName"> The file name. </param>
        /// <returns> True if load was successful. </returns>
        private static bool LoadGame(string strFileName)
        {
            MoveRedoList.Clear();
            XmlDocument xmldoc = new XmlDocument();

            try
            {
                xmldoc.Load(strFileName);
            }
            catch
            {
                return(false);
            }

            XmlElement xmlnodeGame = (XmlElement)xmldoc.SelectSingleNode("/Game");

            if (xmlnodeGame == null)
            {
                return(false);
            }

            if (xmlnodeGame.GetAttribute("FEN") != string.Empty)
            {
                NewInternal(xmlnodeGame.GetAttribute("FEN"));
            }

            if (xmlnodeGame.GetAttribute("WhitePlayer") != string.Empty)
            {
                PlayerWhite.Intellegence = xmlnodeGame.GetAttribute("WhitePlayer") == "Human"
                                               ? Player.PlayerIntellegenceNames.Human
                                               : Player.PlayerIntellegenceNames.Computer;
            }

            if (xmlnodeGame.GetAttribute("BlackPlayer") != string.Empty)
            {
                PlayerBlack.Intellegence = xmlnodeGame.GetAttribute("BlackPlayer") == "Human"
                                               ? Player.PlayerIntellegenceNames.Human
                                               : Player.PlayerIntellegenceNames.Computer;
            }

            if (xmlnodeGame.GetAttribute("BoardOrientation") != string.Empty)
            {
                Board.Orientation = xmlnodeGame.GetAttribute("BoardOrientation") == "White"
                                        ? Board.OrientationNames.White
                                        : Board.OrientationNames.Black;
            }

            if (xmlnodeGame.GetAttribute("DifficultyLevel") != string.Empty)
            {
                DifficultyLevel = int.Parse(xmlnodeGame.GetAttribute("DifficultyLevel"));
            }

            if (xmlnodeGame.GetAttribute("ClockMoves") != string.Empty)
            {
                ClockMaxMoves = int.Parse(xmlnodeGame.GetAttribute("ClockMoves"));
            }

            if (xmlnodeGame.GetAttribute("ClockMinutes") != string.Empty)
            {
                ClockTime = new TimeSpan(0, int.Parse(xmlnodeGame.GetAttribute("ClockMinutes")), 0);
            }

            if (xmlnodeGame.GetAttribute("ClockSeconds") != string.Empty)
            {
                ClockTime = new TimeSpan(0, 0, int.Parse(xmlnodeGame.GetAttribute("ClockSeconds")));
            }

            if (xmlnodeGame.GetAttribute("MaximumSearchDepth") != string.Empty)
            {
                MaximumSearchDepth = int.Parse(xmlnodeGame.GetAttribute("MaximumSearchDepth"));
            }

            if (xmlnodeGame.GetAttribute("Pondering") != string.Empty)
            {
                EnablePondering = xmlnodeGame.GetAttribute("Pondering") == "1";
            }

            if (xmlnodeGame.GetAttribute("UseRandomOpeningMoves") != string.Empty)
            {
                UseRandomOpeningMoves = xmlnodeGame.GetAttribute("UseRandomOpeningMoves") == "1";
            }

            XmlNodeList xmlnodelist = xmldoc.SelectNodes("/Game/Move");

            if (xmlnodelist != null)
            {
                foreach (XmlElement xmlnode in xmlnodelist)
                {
                    Square from;
                    Square to;
                    if (xmlnode.GetAttribute("FromFile") != string.Empty)
                    {
                        from = Board.GetSquare(
                            Convert.ToInt32(xmlnode.GetAttribute("FromFile")),
                            Convert.ToInt32(xmlnode.GetAttribute("FromRank")));
                        to = Board.GetSquare(
                            Convert.ToInt32(xmlnode.GetAttribute("ToFile")),
                            Convert.ToInt32(xmlnode.GetAttribute("ToRank")));
                    }
                    else
                    {
                        from = Board.GetSquare(xmlnode.GetAttribute("From"));
                        to   = Board.GetSquare(xmlnode.GetAttribute("To"));
                    }

                    MakeAMoveInternal(Move.MoveNameFromString(xmlnode.GetAttribute("Name")), from.Piece, to);
                    TimeSpan tsnTimeStamp;
                    if (xmlnode.GetAttribute("SecondsElapsed") == string.Empty)
                    {
                        if (MoveHistory.Count <= 2)
                        {
                            tsnTimeStamp = new TimeSpan(0);
                        }
                        else
                        {
                            tsnTimeStamp = MoveHistory.PenultimateForSameSide.TimeStamp + (new TimeSpan(0, 0, 30));
                        }
                    }
                    else
                    {
                        tsnTimeStamp = new TimeSpan(0, 0, int.Parse(xmlnode.GetAttribute("SecondsElapsed")));
                    }

                    MoveHistory.Last.TimeStamp = tsnTimeStamp;
                    MoveHistory.Last.Piece.Player.Clock.TimeElapsed = tsnTimeStamp;
                }

                int intTurnNo = xmlnodeGame.GetAttribute("TurnNo") != string.Empty
                                    ? int.Parse(xmlnodeGame.GetAttribute("TurnNo"))
                                    : xmlnodelist.Count;

                for (int intIndex = xmlnodelist.Count; intIndex > intTurnNo; intIndex--)
                {
                    UndoMoveInternal();
                }
            }

            return(true);
        }