Ejemplo n.º 1
0
        public void TestConstructor()
        {
            var f = new Field(3, 3, new Cell(0, 1), new Cell(2, 1));
            var us = new UnitState(new Cell(0, 0), 0);
            var u = new Unit(new[] {new Cell(0, 0)}, new Cell(0, 0));

            var bfss = new BestStateInfo(f, u, new BfsState(us, new Cell(0, 2)));
            Assert.AreEqual(0, bfss.KilledRows);
            Assert.AreEqual(5, bfss.JoinedCellsCount);
            Assert.AreEqual(0, bfss.NewHolesCount);

            bfss = new BestStateInfo(f, u, new BfsState(us, new Cell(1, 1)));
            Assert.AreEqual(1, bfss.KilledRows);
            Assert.AreEqual(2, bfss.JoinedCellsCount);
            Assert.AreEqual(1, bfss.NewHolesCount);

            bfss = new BestStateInfo(f, u, new BfsState(us, new Cell(1, 1), new Cell(0, 2), new Cell(1, 2), new Cell(2, 2)));
            Assert.AreEqual(2, bfss.KilledRows);
            Assert.AreEqual(9, bfss.JoinedCellsCount);
            Assert.AreEqual(0, bfss.NewHolesCount);

            bfss = new BestStateInfo(f, u, new BfsState(us,  new Cell(1, 2)));
            Assert.AreEqual(0, bfss.KilledRows);
            Assert.AreEqual(3, bfss.JoinedCellsCount);
            Assert.AreEqual(1, bfss.NewHolesCount);

            bfss = new BestStateInfo(f, u, new BfsState(us, new Cell(0, 0), new Cell(1, 0), new Cell(1, 1), new Cell(1, 2)));
            Assert.AreEqual(1, bfss.KilledRows);
            Assert.AreEqual(6, bfss.JoinedCellsCount);
            Assert.AreEqual(2, bfss.NewHolesCount);

            f = new Field(3, 3, new Cell(0, 1));
            bfss = new BestStateInfo(f, u, new BfsState(us, new Cell(1, 2), new Cell(1, 1), new Cell(2, 0)));
            Assert.AreEqual(0, bfss.KilledRows);
            Assert.AreEqual(4, bfss.JoinedCellsCount);
            Assert.AreEqual(2, bfss.NewHolesCount);

            f = new Field(4, 4, new Cell(0, 3), new Cell(1, 3), new Cell(2, 3));
            bfss = new BestStateInfo(f, u, new BfsState(us, new Cell(0, 0), new Cell(0, 1), new Cell(1, 1), new Cell(0, 2), new Cell(2, 2)));
            Assert.AreEqual(0, bfss.KilledRows);
            Assert.AreEqual(7, bfss.JoinedCellsCount);
            Assert.AreEqual(1, bfss.NewHolesCount);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
 public bool IsBetter(BestStateInfo state)
 {
     double rows = (RowsPoints(KilledRows) - RowsPoints(state.KilledRows)) * MetricConst.KilledRows;
     double joint = (JoinedCellsCount - state.JoinedCellsCount) * MetricConst.JoinedCellsCount;
     double down = (state.DownCellsCount - DownCellsCount) * MetricConst.DownCellsCount;
     double holes = (state.NewHolesCount / (state.KilledRows + 1.0) - NewHolesCount / (KilledRows + 1.0)) * MetricConst.NewHolesCount;
     double miny = (MinY - state.MinY) * MetricConst.MinY;
     double maxy = (MaxY - state.MaxY) * MetricConst.MaxY;
     double dist = (state.DistFromWall - DistFromWall) * MetricConst.DistFromWall;
     return rows + joint + down + holes + miny + maxy + dist > 0;
 }
Ejemplo n.º 4
0
 public bool IsBetter2(BestStateInfo state)
 {
     if (MaxY > state.MaxY)
         return true;
     if (MaxY < state.MaxY)
         return false;
     if (MinY > state.MinY)
         return true;
     if (MinY < state.MinY)
         return false;
     return DistFromWall < state.DistFromWall;
 }