Exemplo n.º 1
0
        public Game[] Build(string inputFilename)
        {
            var games = new List<Game>();

            var input = new InputReader().Read(inputFilename);

            var board = Board.CreateEmpty(input.height, input.width);
            foreach (var busyCell in input.filled)
            {
                board.Fill(busyCell.ToPoint());
            }

            var units = input.units.Select(x => x.ToUnit()).ToArray();
            foreach (var seed in input.sourceSeeds)
            {
                var generator = new RandomGenerator(seed);
                var unitSequence = generator.Generate().Select(x => units[x % units.Length]).Take(input.sourceLength).ToArray();

                var game = new Game(board, null, unitSequence, 0, 0, 0, input.id, seed, string.Empty, 0);
                game = game.TrySpawnNew();

                games.Add(game);
            }

            return games.ToArray();
        }
Exemplo n.º 2
0
        public void TestTrySpawnNew3()
        {
            var board = Board.Create(new[]
            {
                "..*...",
                "......",
                "......"
            });
            var unit = Unit.Create(new Point(0, 0), new[] { new Point(-1, 0), new Point(0, 0), new Point(1, 0), new Point(0, 1), });
            var game = new Game(board, null, new[] { unit }, 0, 0, 0, -1, -1, string.Empty, 0);

            var actual = game.TrySpawnNew();

            Assert.AreEqual(GameState.GameOver, actual.State);
        }
Exemplo n.º 3
0
        public void TestMakeCommandWithUpdatedScore2()
        {
            var board = Board.Create(new[]
            {
                ".....",
                ".....",
                ".....",
                ".....",
                ".....",
            });
            var unit = Unit.Create(new Point(0, 0), new[] { new Point(0, 0), });
            var unit2 = Unit.Create(new Point(0, 0), new[] { new Point(0, 0), });
            var game = new Game(board, null, new[] { unit, unit2 }, 0, 0, 200, -1, -1, "e", 0);

            var actual = game.TrySpawnNew();
            Assert.AreEqual(GameState.NewIsSpawned, actual.State);

            actual = actual.TryMakeStep('i');
            Assert.AreEqual(GameState.Ok, actual.State);

            actual = actual.TryMakeStep('!');
            Assert.AreEqual(GameState.Ok, actual.State);

            Assert.AreEqual(506, actual.Score);
        }
Exemplo n.º 4
0
        public void TestTrySpawnNew2()
        {
            var board = Board.CreateEmpty(10, 5);
            var game = new Game(board, null, new Unit[0], 0, 0, 0, -1, -1, string.Empty, 0);

            var actual = game.TrySpawnNew();

            Assert.AreEqual(GameState.GameOver, actual.State);
        }
Exemplo n.º 5
0
        public void TestMakeCommandWithUpdatedScore()
        {
            var board = Board.Create(new[]
            {
                "*..",
                "*.*",
                "***"
            });
            var unit = Unit.Create(new Point(0, 0), new[] { new Point(0, 0), });
            var unit2 = Unit.Create(new Point(0, 0), new[] { new Point(0, 0), });
            var game = new Game(board, null, new[] { unit, unit2 }, 0, 12, 200, -1, -1, string.Empty, 0);

            var actual = game.TrySpawnNew();
            Assert.AreEqual(GameState.NewIsSpawned, actual.State);

            actual = actual.TryMakeStep('l');
            Assert.AreEqual(GameState.Ok, actual.State);
            Assert.AreEqual(1, actual.Current.GetAbsolutePoints().First().Col);
            Assert.AreEqual(1, actual.Current.GetAbsolutePoints().First().Row);

            actual = actual.TryMakeStep('l');
            Assert.AreEqual(GameState.NewIsSpawned, actual.State);
            Assert.AreEqual(2, actual.LastUnitLinesCleared);
            Assert.AreEqual(832, actual.Score);
        }
Exemplo n.º 6
0
        public void TestMakeCommandWest()
        {
            var board = Board.Create(new[]
            {
                "...",
                "...",
                "..."
            });
            var unit = Unit.Create(new Point(0, 0), new[] { new Point(0, 0), });
            var unit2 = Unit.Create(new Point(0, 0), new[] { new Point(0, 0), });
            var game = new Game(board, null, new[] { unit, unit2 }, 0, 0, 0, -1, -1, string.Empty, 0);

            var actual = game.TrySpawnNew();
            Assert.AreEqual(GameState.NewIsSpawned, actual.State);

            actual = actual.TryMakeStep('p');
            Assert.AreEqual(GameState.Ok, actual.State);

            actual = actual.TryMakeStep('p');
            Assert.AreEqual(GameState.NewIsSpawned, actual.State);
            Assert.AreEqual(string.Join(Environment.NewLine, new[]
            {
                "*..",
                "...",
                "..."
            }), actual.Board.ToString());
        }
Exemplo n.º 7
0
        public void TestMakeCommandTurnCounterClockWise()
        {
            var board = Board.Create(new[]
            {
                "...",
                "...",
                "..."
            });
            var unit = Unit.Create(new Point(0, 1), new[] { new Point(0, 0), });
            var unit2 = Unit.Create(new Point(0, 0), new[] { new Point(0, 0), });
            var game = new Game(board, null, new[] { unit, unit2 }, 0, 0, 0, -1, -1, string.Empty, 0);

            var actual = game.TrySpawnNew();
            Assert.AreEqual(GameState.NewIsSpawned, actual.State);

            actual = actual.TryMakeStep('k');
            Assert.AreEqual(GameState.Ok, actual.State);
            Assert.AreEqual(0, actual.Current.GetAbsolutePoints().First().Col);
            Assert.AreEqual(1, actual.Current.GetAbsolutePoints().First().Row);

            actual = actual.TryMakeStep('k');
            Assert.AreEqual(GameState.Ok, actual.State);
            Assert.AreEqual(1, actual.Current.GetAbsolutePoints().First().Col);
            Assert.AreEqual(2, actual.Current.GetAbsolutePoints().First().Row);
        }
Exemplo n.º 8
0
        public void TestMakeCommandSouthEast()
        {
            var board = Board.Create(new[]
            {
                "...",
                "...",
                "..."
            });
            var unit = Unit.Create(new Point(0, 0), new[] { new Point(0, 0), });
            var unit2 = Unit.Create(new Point(0, 0), new[] { new Point(0, 0), });
            var game = new Game(board, null, new[] { unit, unit2 }, 0, 0, 0, -1, -1, string.Empty, 0);

            var actual = game.TrySpawnNew();
            Assert.AreEqual(GameState.NewIsSpawned, actual.State);

            actual = actual.TryMakeStep('l');
            Assert.AreEqual(GameState.Ok, actual.State);
            Assert.AreEqual(1, actual.Current.UnitPosition.PivotLocation.Col);
            Assert.AreEqual(1, actual.Current.UnitPosition.PivotLocation.Row);

            actual = actual.TryMakeStep('l');
            Assert.AreEqual(GameState.Ok, actual.State);
            Assert.AreEqual(2, actual.Current.UnitPosition.PivotLocation.Col);
            Assert.AreEqual(2, actual.Current.UnitPosition.PivotLocation.Row);

            actual = actual.TryMakeStep('l');
            Assert.AreEqual(GameState.NewIsSpawned, actual.State);
            Assert.AreEqual(string.Join(Environment.NewLine, new[]
            {
                "...",
                "...",
                "..*"
            }), actual.Board.ToString());
        }
Exemplo n.º 9
0
        public Game TryMakeStep(char ch)
        {
            var command = CommandConverter.Convert(ch);
            if (command == Command.Empty)
            {
                return this;
            }
            if (Current == null)
            {
                throw new Exception("Current game unit is null");
            }

            if (!IsValid(Current))
            {
                throw new Exception("Current game unit is not valid, new step is impossible");
            }

            var newGameUnit = Current.MakeStep(command);
            var lastSymbols = LastSymbols.Insert(LastSymbols.Length, ch.ToString());
            if (lastSymbols.Length == 52)
            {
                lastSymbols = lastSymbols.Substring(1);
            }
            int wordsMask;
            var wordsScore = CalculateWordsScore(lastSymbols, out wordsMask);

            if (!IsValid(newGameUnit))
            {
                var lockedCells = Current.GetAbsolutePoints();
                var updateResult = Board.Place(lockedCells).Update();
                var additionalScore = CalculateScore(LastUnitLinesCleared, updateResult.RowsCleaned, Current.Unit.Points.Length);

                var gameWithNewUnit = new Game(updateResult.NewBoard, null, UnitsSequence, CurrentUnitNumber + 1, updateResult.RowsCleaned, Score + additionalScore + wordsScore, ProblemId, Seed, lastSymbols, wordsMask);
                return gameWithNewUnit.TrySpawnNew();
            }

            return MoveCurrent(newGameUnit, lastSymbols, Score + wordsScore, wordsMask);
        }