Exemplo n.º 1
0
        public void TestDrawby50moverule()
        {
            var game = new Game();

            game.New();
            var gameFile = GameFile.Load("TestGames\\drawby50moverule.txt");

            game.Load(gameFile);
            Assert.IsTrue(game.Ended);
            game.UndoLastMove();
            Assert.IsFalse(game.Ended);
            Assert.IsTrue(game.TryStringMove("h8-g8"));
            Assert.IsTrue(game.Ended);
        }
Exemplo n.º 2
0
        public void Load(GameFile gameFile)
        {
            Reset();
            InitialPosition = gameFile.InitialPosition;
            var positionItem = gameFile.InitialPosition.Split(',').ToList();
            Debug.Assert(positionItem.First() == "Start");
            var i = 0;
            while (true)
            {
                i++;
                if (positionItem[i] == "White" || positionItem[i] == "Black")
                    break;
                this.AddPiece(positionItem[i]);
            }
            if (positionItem[i] == "White")
                CurrentPlayer = WhitePlayer;
            else
            {
                Debug.Assert(positionItem[i] == "Black");
                CurrentPlayer = BlackPlayer;
            }
            i++;
            for (int j = i; j < positionItem.Count; j++)
            {
                if (positionItem[j] == "WCK")
                    WhitePlayer.CanCastleKingSide = false;
                if (positionItem[j] == "WCQ")
                    WhitePlayer.CanCastleQueenSide = false;
                if (positionItem[j] == "BCK")
                    BlackPlayer.CanCastleKingSide = false;
                if (positionItem[j] == "BCQ")
                    BlackPlayer.CanCastleQueenSide = false;
                if (positionItem[j].StartsWith("ENP:"))
                {
                    var split = positionItem[j].Split(':');
                    EnPassantFile = (File)Enum.Parse(typeof(File), split[1]);
                }
            }
            SetPieceFastAccess();

            PositionsDatabase.Instance.SetStartHash(this);
            InitialMaterial(WhitePlayer);
            InitialMaterial(BlackPlayer);
            foreach (var moveCommand in gameFile.MoveCommands)
            {
                if (!TryPossibleMoveCommand(moveCommand))
                    throw new ApplicationException("Invalid game file");
            }
        }
Exemplo n.º 3
0
        public void TestCopyGame()
        {
            var game = new Game();

            game.New();
            var gameFile = GameFile.Load("TestGames\\mated.txt");

            foreach (var command in gameFile.MoveCommands)
            {
                Assert.IsTrue(game.TryPossibleMoveCommand(command));
            }

            var copy = game.Copy();

            Assert.AreNotSame(game, copy);
        }
Exemplo n.º 4
0
        public static GameFile Load(string fileName)
        {
            var text     = System.IO.File.ReadAllText(fileName);
            var gameFile = new GameFile();
            var fileRows = text.Split('\r').ToList();

            gameFile.InitialPosition = fileRows.First();
            fileRows.RemoveAt(0);
            foreach (var row in fileRows)
            {
                if (string.IsNullOrEmpty(row.Trim()))
                {
                    continue;
                }
                gameFile.MoveCommands.Add(MoveCommand.Parse(row.Trim()));
            }
            return(gameFile);
        }
Exemplo n.º 5
0
        public void TestSaveLoad()
        {
            var game = new Game();

            game.New();
            Assert.IsTrue(game.TryStringMove("e2-e4"));
            Assert.IsTrue(game.TryStringMove("e7-e5"));
            var gameFile = new GameFile(game);
            var fileName = @"gamefile.txt";

            gameFile.Save(fileName);

            var loaded = GameFile.Load(fileName);

            for (int i = 0; i < gameFile.MoveCommands.Count; i++)
            {
                Assert.AreEqual(gameFile.MoveCommands[i].ToString(), loaded.MoveCommands[i].ToString());
            }
        }
Exemplo n.º 6
0
        public void Save(string fileName)
        {
            var gameFile = new GameFile(this);

            gameFile.Save(fileName);
        }