示例#1
0
文件: Algo1.cs 项目: martugin/tetris
        public AlgoResult Run(int seed)
        {
            DebugPrinter.WriteLine("------------------------\nseed: {0}", seed);
            int cnt = 0;
            #if DEBUG
            var allMoves = new List<int>();
            #endif
            var allMovesStr = "";
            int prevScore = 0;
            int totalBricks = 0;

            var randSequence = RandGen.RandSequence(seed, _gameData.InputData.sourceLength).Select(x => x % _gameData.Units.Length).ToArray();

            var field = new Field(_gameData.InputData);
            field.MetricUnits = new MetricUnits(field, _gameData, randSequence);
            field.MetricUnits.CalcTailMetrics(0);
            DebugPrinter.WriteLine("RowPower: {0}", field.MetricUnits.AverageRowPower);
            for (int i = 0; i < randSequence.Length; i++)
            {
                if ((i+1) % 10 == 0)
                    DebugPrinter.WriteTrace("        unit #{0}/{1}", i + 1, randSequence.Length);

                var rnd = randSequence[i];
                field.MetricUnits.CalcTailMetrics(i);
                int unitIndex = rnd % _gameData.Units.Length;
                DebugPrinter.WriteLine("step {0}, unit #{1}", cnt, unitIndex);

                Unit unit = _gameData.Units[unitIndex];
                DebugPrinter.WriteLine(PrettyPrinter.PrettyPrint(unit));

                var startState = field.SpawnUnit(unit);
                if (startState == null)
                {
                    DebugPrinter.WriteLine("No space to spawn unit");
                    break;
                }

                var bfs = new BFS(field, unit, _gameData);
                BestStateInfo bestState = null;

                PowerPhraseInfo[] powerPhrases = _ChoosePowerPhrases();

                foreach (var bfsState in bfs.DoBfs(startState, powerPhrases))
                {
                    //  DebugPrinter.WriteLine("got state: ({0}, {1}), rot {2}", unitState.Pivot.x, unitState.Pivot.y, unitState.Rotation);
                    var newBestState = new BestStateInfo(field, unit, bfsState);
                    if (bestState != null && !newBestState.IsBetter(bestState))
                        continue;

                    bestState = newBestState;
                }

            #if DEBUG
                Debug.Assert(bestState != null);
            #else
                if (bestState == null)
                    continue;
            #endif

                DebugPrinter.WriteLine("best state: ({0}, {1}), rot {2}, minY {3}, maxY {4}",
                    bestState.BfsState.UnitState.Pivot.x, bestState.BfsState.UnitState.Pivot.y, bestState.BfsState.UnitState.Rotation, bestState.MinY, bestState.MaxY);

            #if DEBUG
                var moves = bfs.GetMoves(bestState.BfsState.UnitState);
                DebugPrinter.WriteLine("Moves:");
                foreach (int move in moves)
                    DebugPrinter.Write("{0},", move);
                DebugPrinter.WriteLine("");

                DebugPrinter.WriteLine(MovesPrinter.PrintMoves(moves));

                allMoves.AddRange(moves);
                allMoves.Add(bestState.BfsState.BlockingMove);
            #endif

                var movesStr = bfs.GetMovesString(bestState.BfsState.UnitState);
                DebugPrinter.WriteLine(movesStr);
                allMovesStr += movesStr;
                allMovesStr += MovesPrinter.PrintOneMove(bestState.BfsState.BlockingMove);

                DebugPrinter.WriteLine("Blocking move: {0} {1}", bestState.BfsState.BlockingMove, MovesPrinter.PrintOneMove(bestState.BfsState.BlockingMove));

                DebugPrinter.WriteLine("Before update:");
                DebugPrinter.WriteLine(PrettyPrinter.PrettyPrintBeforeUpdate(field, unit, bestState.BfsState.UnitState));

                field.Update(unit, bestState.BfsState.UnitState);
                //DebugPrinter.WriteLine("After update:");
                //DebugPrinter.Write(PrettyPrinter.PrettyPrint(field));
                DebugPrinter.WriteLine("Score: {0}", field.Score);
                DebugPrinter.WriteLine("Score diff: {0}", field.Score - prevScore);
                DebugPrinter.WriteLine("");

                prevScore = field.Score;
                totalBricks += unit.members.Length;
                _UpdatePowerPhraseUsage(movesStr);
                ++cnt;
                //if (cnt >= 20)
                //    break;
            }

            #if DEBUG
            DebugPrinter.WriteLine("All moves:");
            foreach (int move in allMoves)
                DebugPrinter.Write("{0},", move);
            DebugPrinter.WriteLine("");
            #endif

            DebugPrinter.WriteLine("Total move score: {0}", field.Score);
            int phraseScore = _gameData.GetPowerPhraseScore(allMovesStr);
            DebugPrinter.WriteLine("Total phrase score: {0}", phraseScore);
            DebugPrinter.WriteLine("Total score: {0}", field.Score + phraseScore);

            DebugPrinter.WriteLine("Total bricks: {0}", totalBricks);
            int totalRowsKilled = field.RowsKilled.Sum(r => r.Key*r.Value);
            DebugPrinter.WriteLine("Total rows killed: {0}", totalRowsKilled);
            var rowsKilledKeys = field.RowsKilled.Keys.ToArray();
            Array.Sort(rowsKilledKeys);
            foreach (int rows in rowsKilledKeys)
                DebugPrinter.WriteLine("Rows {0}: {1}",  rows, field.RowsKilled[rows]);

            DebugPrinter.WriteTrace("Total move score: {0}", field.Score);
            DebugPrinter.WriteTrace("Total phrase score: {0}", phraseScore);
            DebugPrinter.WriteTrace("Total score: {0}", field.Score + phraseScore);

            DebugPrinter.WriteTrace("Total bricks: {0}", totalBricks);
            DebugPrinter.WriteTrace("Total rows killed: {0}", totalRowsKilled);
            foreach (int rows in rowsKilledKeys)
                DebugPrinter.WriteTrace("Rows {0}: {1}", rows, field.RowsKilled[rows]);

            string res = allMovesStr; //MovesPrinter.PrintMoves(allMoves);
            DebugPrinter.WriteLine(res);
            return new AlgoResult(res, field.Score + phraseScore);
        }
示例#2
0
        public void TestUpdate()
        {
            var f = new Field(2, 2);
            var u = new Unit(new[] {new Cell(0, 0)}, new Cell(0, 0));
            var us = new UnitState(new Cell(1, 1), 0);
            Assert.AreEqual(2, f.MinFilledRow);
            f.Update(u, us);
            Assert.AreEqual(true, f[1][1]);
            Assert.AreEqual(false, f[1][0]);
            Assert.AreEqual(1, f.MinFilledRow);
            Assert.IsTrue(f.RowCells(1).Contains(1));
            Assert.IsFalse(f.RowCells(1).Contains(0));
            Assert.AreEqual(f.RowCells(1).Count, 1);
            Assert.IsFalse(f.RowCells(0).Contains(0));
            Assert.IsFalse(f.RowCells(0).Contains(1));
            Assert.AreEqual(f.RowCells(0).Count, 0);
            Assert.AreEqual(1, f.Score);

            u = new Unit(new[] { new Cell(0, 0), new Cell(1, 0), new Cell(0, 1) }, new Cell(0, 0));
            us = new UnitState(new Cell(0, 0), 0);
            f.Update(u, us);
            Assert.AreEqual(false, f[0][0]);
            Assert.AreEqual(false, f[0][1]);
            Assert.AreEqual(false, f[1][0]);
            Assert.AreEqual(false, f[1][1]);
            Assert.AreEqual(2, f.MinFilledRow);
            Assert.IsFalse(f.RowCells(1).Contains(1));
            Assert.IsFalse(f.RowCells(1).Contains(0));
            Assert.IsFalse(f.RowCells(0).Contains(0));
            Assert.IsFalse(f.RowCells(0).Contains(1));
            Assert.AreEqual(304, f.Score);

            u = new Unit(new[] { new Cell(0, 0), new Cell(1, 0), new Cell(0, 1), new Cell(1, 1) }, new Cell(0, 0));
            us = new UnitState(new Cell(0, 0), 0);
            f.Update(u, us);
            Assert.AreEqual(false, f[0][0]);
            Assert.AreEqual(false, f[0][1]);
            Assert.AreEqual(false, f[1][0]);
            Assert.AreEqual(false, f[1][1]);
            Assert.AreEqual(2, f.MinFilledRow);
            Assert.IsFalse(f.RowCells(1).Contains(1));
            Assert.IsFalse(f.RowCells(1).Contains(0));
            Assert.IsFalse(f.RowCells(0).Contains(0));
            Assert.IsFalse(f.RowCells(0).Contains(1));
            Assert.AreEqual(638, f.Score);

            u = new Unit(new[] {new Cell(1, 0), new Cell(0, 1), new Cell(1, 1) }, new Cell(0, 0));
            us = new UnitState(new Cell(0, 0), 0);
            f.Update(u, us);
            Assert.AreEqual(false, f[0][0]);
            Assert.AreEqual(false, f[0][1]);
            Assert.AreEqual(false, f[1][0]);
            Assert.AreEqual(true, f[1][1]);
            Assert.AreEqual(1, f.MinFilledRow);
            Assert.IsTrue(f.RowCells(1).Contains(1));
            Assert.IsFalse(f.RowCells(1).Contains(0));
            Assert.IsFalse(f.RowCells(0).Contains(0));
            Assert.IsFalse(f.RowCells(0).Contains(1));
            Assert.AreEqual(751, f.Score);

            u = new Unit(new[] { new Cell(0, 0), new Cell(1, 0) }, new Cell(0, 0));
            us = new UnitState(new Cell(0, 0), 0);
            f.Update(u, us);
            Assert.AreEqual(false, f[0][0]);
            Assert.AreEqual(false, f[0][1]);
            Assert.AreEqual(false, f[1][0]);
            Assert.AreEqual(true, f[1][1]);
            Assert.AreEqual(1, f.MinFilledRow);
            Assert.IsTrue(f.RowCells(1).Contains(1));
            Assert.IsFalse(f.RowCells(1).Contains(0));
            Assert.IsFalse(f.RowCells(0).Contains(0));
            Assert.IsFalse(f.RowCells(0).Contains(1));
            Assert.AreEqual(853, f.Score);

            Assert.AreEqual(13, f.TotalBricks);
            Assert.AreEqual(2, f.RowsKilled[1]);
            Assert.AreEqual(2, f.RowsKilled[2]);
        }