Example #1
0
        private bool FindEmptyPath(World world0)
        {
            log("FindEmptyPath");
            var t2 = Timings.Start(2);

            // run to empty space
            foreach (var push in new[] { false, true })
            {
                foreach (var eatItem in new[] { " :", " :*" })
                {
                    var qqq = Wave.CreatePathsFull(world0, world0.player_y, world0.player_x, "", eatItem, push, 2, false, 10, null);
                    foreach (var scored in qqq.OrderBy(x => x.StepsCount).Where(x => x.StepsCount >= 6)) //.Reverse())
                    {
                        for (var take = 3; take < 10; take++)
                        {
                            var path = scored.GetPath().Reverse().Select(x => x.Move).Take(take).ToArray();
                            if (path.Length > 0)// && ValidatePath(path, world0.Clone(), 3, false, "FindEmptyPath"))
                            {
                                log("run to empty: {0}", path.Length);
                                _killPath = new Queue <Move>(path);
                                t2.Stop();
                                //return _killPath.Dequeue().Control;
                                return(true);
                            }
                        }
                    }
                }
            }
            t2.Stop();
            return(false);
        }
Example #2
0
        private void ProcessGames(IEnumerable <int> seeds)
        {
            var count   = seeds.Count();
            var counter = 0;

            KillerFallingAI.Logging = false;
            Timings.Clear();
            var data = seeds.AsParallel()
                       .Select(seed =>
            {
                var n     = System.Threading.Interlocked.Add(ref counter, 1);
                var watch = new System.Diagnostics.Stopwatch();
                watch.Start();
                var world = LoadFromFile2(seed, 0);     // 2
                var ai    = new WaveAI_2();
                //var ai = new CollectorAI_2(); world.frames_left = 800;
                //var ai = new KillerFallingAI(); world.frames_left = 600;
                var controller = new ProcessAI(ai);
                var game       = new Game(controller, world, seed);
                game.interval  = 0;
                game.start();
                watch.Stop();
                var txt = string.Format("[{0}/{1}] seed: {2} elapsed: {3:#0.0} sec",
                                        n, count, seed, watch.Elapsed.TotalSeconds);
                System.Diagnostics.Trace.WriteLine(txt);
                return(game.log);
            })
                       .OrderBy(x => x.seed)
                       .ToArray();

            var header = "seed\tscore\tdiamonds\tbufferfies\tstreaks\tlongest\tframes\toutcome";
            var lines  = data.Select(x => string.Join("\t", x.seed, x.score, x.diamonds_collected, x.butterflies_killed, x.streaks, x.longest_streak, x.duration_frames, x.outcome));

            System.IO.File.WriteAllLines("logs\\" + DateTime.Now.ToString("yyyy-MM-dd HH_mm_ss"),
                                         new[] { header }.Concat(lines));

            var avg = data.Average(x => (float)x.score);
            var std = Math.Sqrt(data.Average(x => Math.Pow(x.score - avg, 2)));

            Game.log_function("");
            Game.log_function("total: " + data.Sum(x => x.score) + " of " + data.Length + " games");
            Game.log_function("avg:   " + avg);
            Game.log_function("std:   " + std);
            Game.log_function("killed: " + data.Sum(x => x.butterflies_killed) + ", avg: " + data.Average(x => (float)x.butterflies_killed));
            Game.log_function("");
            foreach (var s in Timings.ToString())
            {
                Game.log_function(s);
            }
        }
Example #3
0
        private bool FindOpenPath(World world0)
        {
            var t1 = Timings.Start(1);
            //var openPath = Wave.CreatePaths(world0, world0.player_y, world0.player_x, ""/*Butterfly.Chars*/, "", true, 1, true, 0, true, _bpos.ToArray());
            var openPath = Wave.CreatePaths(world0.Screen, world0.player_y, world0.player_x, Butterfly.Chars, "", true, 0, 0, _bpos.ToArray(), Move.Moves2);
            var ppp      = _bpos.Where(z => !openPath.Any(q => q.X == z[1] && q.Y == z[0])).ToArray();

            // _bpos minus openPath coords ???
            log("FindOpenPath: {0}; {1} -> {2}", openPath.Length, _bpos.Count, ppp.Length);
            if (ppp.Length > 0)// (openPath.Length <= butterfliesCount)
            {
                foreach (var eatItem in new[] { ":", ":*" })
                {
                    foreach (var push in new[] { false, true })
                    {
                        var qqq = Wave.CreatePathsFull(world0, world0.player_y, world0.player_x,
                                                       "" /*Butterfly.Chars*/, eatItem, push, 1, true, 0, ppp);//_bpos.ToArray());
                        foreach (var scored in qqq)
                        {
                            var path0 = scored.GetPath().Reverse().ToArray();
                            if (path0.Count(z => z.Eaten) != 0)
                            {
                                for (var skip = 1; skip <= 2; skip++)
                                {
                                    var path = path0.Take(path0.Length - skip).ToArray();
                                    if (path.Length > 0 &&
                                        ((Step2)path.Last()).World.is_playable() &&
                                        ValidatePath(path.Select(x => x.Move).ToArray(), world0.Clone(), 3, false, "FindOpenPath"))
                                    {
                                        log("open: {0}", path.Length);
                                        _killPath = new Queue <Move>(path.Select(x => x.Move));
                                        //return _killPath.Dequeue().Control;
                                        t1.Stop();
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                //Game.log_function("opened at frame " + world0.frame);
            }
            t1.Stop();
            return(false);
        }
        public char play(string[] screen, World world0)
        {
            //if (false && _frame++ == 0)
            //{
            //    var www = Wave.CreatePathsFull(world0, world0.player_y, world0.player_x, "*", ":", true, 2, false, 0, null).OrderBy(x => x.Y + x.X).Where(x => x.World.is_playable());
            //    foreach (var scored in www)
            //    {
            //        var path = scored.GetPath().Reverse().Select(x => x.Move).ToArray();
            //        _path = new Queue<Move>(path);
            //        return _path.Dequeue().Control;
            //    }
            //}

            if (_path.Count != 0)
            {
                return(_path.Dequeue().Control);
            }

            var t5     = Timings.Start(5);
            var player = screen.FindItem("A").FirstOrDefault();
            var data   = Wave.CreatePaths(screen, player[0], player[1], "*", "*:", true, 0, 0, null, Move.Moves2)
                         .OrderBy(x => x.StepsCount)
                         .Where(x => CollectorAI.CalcFalling(screen, x.Y, x.X) == 0)
                         //.Take(20)
                         .Select(step =>
            {
                var count = -1;
                if (step.StepsCount < 20)
                {
                    var scr = (string[])screen.Clone();
                    scr.SetAt(player[0], player[1], ' ');
                    scr.SetAt(step.Y, step.X, 'A');
                    count = Wave.CreatePaths(scr, step.Y, step.X, "*", "*:", true, 0, 0, null, Move.Moves2)
                            .Count(x => x.StepsCount < 20 && CollectorAI.CalcFalling(scr, x.Y, x.X) == 0);
                }
                return(new { Step = step, Count = count });
            })
                                                 //.Where(x => x.Step.StepsCount < 19)
                                                 //.OrderBy(x => world0.streak >= 3 ? x.Step.StepsCount : 0)
                         .OrderBy(x => x.Step.StepsCount)
                         .ThenBy(x => + x.Count) // !!!! minus -
                         .ToArray();

            //var data = Wave.CreatePaths(screen, player[0], player[1], "*", ":", true, 0, 0, null, Move.Moves2)
            //    .OrderBy(x => x.StepsCount)
            //    .Where(x => CollectorAI.CalcFalling(screen, x.Y, x.X) == 0)
            //    .Take(20)
            //    .Select(step =>
            //    {
            //        var count = -1;
            //        if (step.StepsCount < 20)
            //        {
            //            var scr = (string[])screen.Clone();
            //            scr.SetAt(player[0], player[1], ' ');
            //            scr.SetAt(step.Y, step.X, 'A');
            //            var w2 = Wave.CreatePaths(scr, step.Y, step.X, "*", ":", true, 0, 0, null, Move.Moves2)
            //                .Where(x => x.StepsCount <= 20 && CollectorAI.CalcFalling(scr, x.Y, x.X) == 0)
            //                .Take(20)
            //                .ToArray();
            //            count = w2.Length;
            //            foreach (var s2 in w2)
            //            {
            //                var scr2 = (string[])scr.Clone();
            //                scr2.SetAt(step.Y, step.X, ' ');
            //                scr2.SetAt(s2.Y, s2.X, 'A');
            //                count += Wave.CreatePaths(scr2, s2.Y, s2.X, "*", ":", true, 0, 0, null, Move.Moves2)
            //                    .Count(x => x.StepsCount <= 20 && CollectorAI.CalcFalling(scr, x.Y, x.X) <= 0);
            //            }
            //        }
            //        return new { Step = step, Count = count };
            //    })
            //    //.Where(x => x.Step.StepsCount < 19)
            //    .OrderBy(x => world0.streak >= 3 ? x.Step.StepsCount : 0)
            //    .ThenBy(x => x.Count)
            //    .ToArray();

            foreach (var item in data)
            {
                var path = item.Step.GetPath().Reverse().Select(x => x.Move).ToArray();
                if (KillerFallingAI.ValidatePath(path, world0.Clone(), 3, false, "CollectorAI_2"))
                {
                    _path = new Queue <Move>(path);
                    t5.Stop();
                    return(_path.Dequeue().Control);
                }
            }
            t5.Stop();

            var t6 = Timings.Start(6);

            {
                foreach (var scored in Wave.CreatePathsFull(world0, player[0], player[1], "*", ":", true, 2, false, 0, null))
                {
                    var path  = scored.GetPath().Reverse().Take(3000).ToArray();
                    var moves = path.Select(x => x.Move).ToArray();
                    var last  = (Step2)path.Last();
                    if (KillerFallingAI.ValidatePath(moves, world0.Clone() /*last.World.Clone()*/, 3, false, "CollectorAI_2:empty"))
                    {
                        _path = new Queue <Move>(moves);
                        t6.Stop();
                        return(_path.Dequeue().Control);
                    }
                }
            }
            t6.Stop();

            return('q');
            //return Move.None.Control;
        }
Example #5
0
        public char play(string[] screen, World world0)
        {
            if (world0.frame < 0)
            {
                return(' ');
            }

            var pos = screen.FindItem(Butterfly.Chars).ToArray();

            foreach (var p in pos)
            {
                _bpos.Enqueue(p);
            }
            while (_bpos.Count > 2 * pos.Length)
            {
                _bpos.Dequeue();
            }
            //_bpos = new Queue<int[]>(pos);

            var butterfliesCount = pos.Count();
            var killed           = world0.frame != 0 && _prevButterfliesCount != butterfliesCount;

            _prevButterfliesCount = butterfliesCount;
            if (killed)
            {
                _lastKillFrame = world0.frame;
            }

            if (world0.frame < 10)
            {
                return(' ');
            }
            if (pos.Length == 0 && screen.FindItem("1234").Count() == 0)
            {
                //var path = Method.Process(screen, world0).OrderBy(x => x.From).ToArray();
                var w    = new System.Diagnostics.Stopwatch();
                var path = TSPSolver.Process(screen, world0).ToArray();
                w.Stop();
                log("path len: {0}, dist: {1}, breaks: {2}, time: {3} ms",
                    path.Length, path.Sum(x => x.Distance), path.Count(x => x.Distance > 20), w.ElapsedMilliseconds);
                return('q');
            }

            var t0 = Timings.Start(0);

            if (_killPath.Count == 0 || _scanForKills)
            {
                var path = FindKillPath(screen, world0);
                if (path != null && (_killPath.Count == 0 || path.Length < _killPath.Count))// || !_isKillPath))
                {
                    log("kill path found: {0}", path.Length);
                    _killPath   = new Queue <Move>(path);
                    _isKillPath = true;
                    _time2kill  = Math.Max(3 * path.Length, _time2kill);
                    //_scanForKills = false;
                }
            }
            t0.Stop();

            if (_killPath.Count != 0)
            {
                log("{0}: {1} / {2} [{3}]", world0.frame, _killPath.Peek(), _killPath.Count, world0.get_hash());
                return(_killPath.Dequeue().Control);
            }
            _scanForKills = true;
            _num++;
            _isKillPath = false;

            if (FindOpenPath(world0) || FindEmptyPath(world0))
            {
                log("{0}: {1} / {2} [{3}]", world0.frame, _killPath.Peek(), _killPath.Count, world0.get_hash());
                return(_killPath.Dequeue().Control);
            }
            log("{0}: {1} [{2}]", world0.frame, Move.None, world0.get_hash());
            return(Move.None.Control);
        }