Пример #1
0
        public bool LoadGame(string fileName, out Board newBoard, out int playerOnMove, out int player1settings, out int player2settings)
        {
            newBoard        = null;
            player1settings = -1;
            player2settings = -1;

            playerOnMove = 1;
            if (fileName == null)
            {
                return(false);
            }

            int          pointer     = 0;
            List <int[]> loadedMoves = new List <int[]>();

            try
            {
                XmlDocument document = new XmlDocument();
                document.Load(fileName);

                XmlNode root = document.DocumentElement;

                foreach (XmlNode level1node in root)
                {
                    switch (level1node.Name)
                    {
                    case "Players":
                        XmlElement player = (XmlElement)level1node;
                        player1settings = int.Parse(player.GetAttribute("Player1"));
                        player2settings = int.Parse(player.GetAttribute("Player2"));
                        break;

                    case "Moves":
                        XmlElement moves = (XmlElement)level1node;
                        pointer = int.Parse(moves.GetAttribute("Pointer"));
                        foreach (XmlNode level2node in level1node)
                        {
                            XmlElement oneMove = (XmlElement)level2node;
                            int        fromX   = int.Parse(oneMove.GetAttribute("FromX"));
                            int        fromY   = int.Parse(oneMove.GetAttribute("FromY"));
                            int        toX     = int.Parse(oneMove.GetAttribute("ToX"));
                            int        toY     = int.Parse(oneMove.GetAttribute("ToY"));
                            if (ValidateMove(fromX, fromY, toX, toY))
                            {
                                loadedMoves.Add(new int[] { fromX, fromY, toX, toY });
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        break;
                    }
                }
            }


            catch
            {
                return(false);
            }

            if (player1settings < 0 || player1settings > 4 || player2settings < 0 || player2settings > 4)
            {
                return(false);
            }
            if (pointer > loadedMoves.Count)
            {
                return(false);
            }

            newBoard = new Board();
            Rules rules = new Rules();

            rules.NaplnBoard(newBoard);
            foreach (int[] move in loadedMoves)
            {
                bool validMoveFound = false;
                foreach (int[] validMove in rules.GenerujPlatneTahy(newBoard, playerOnMove))
                {
                    if (move[0] == validMove[0] && move[1] == validMove[1] && move[2] == validMove[4] && move[3] == validMove[5])
                    {
                        newBoard.VykonejTah(validMove);
                        playerOnMove  *= -1;
                        validMoveFound = true;
                        break;
                    }
                }
                if (!validMoveFound)
                {
                    return(false);
                }
            }
            while (newBoard.IndexSeznamu > pointer)
            {
                newBoard.TahZpet();
                playerOnMove *= -1;
            }
            return(true);
        }