draugr_contrib /SDK/api/CSharp/SimpleWalker/SimpleWalkerBot.cs

Language C# Lines 136
MD5 Hash 87ec73654eddf544eceaa69334022349 Estimated Cost $2,839 (why?)
Repository https://bitbucket.org/ehvattum/draugr_contrib.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using System.Linq;
using Skyport;

namespace SimpleWalker
{
    internal class SimpleWalkerBot : SkyportObserver
    {
        private static readonly Random rnd = new Random();
        public string BotName = "csharpwalker";
        private SkyportConnection connection;
        private string primary;
        private string secondary;

        #region SkyportObserver Members

        public void OnConnectionEstablished(SkyportConnection connectionArg)
        {
            connection = connectionArg;
            Console.WriteLine("Connection established");
            connection.SendHandshake(BotName);
        }

        public void OnHandshakeSuccessful()
        {
            Console.WriteLine("Handshake successful");
        }

        public void OnEndturn()
        {
            Console.WriteLine("Endturn!");
        }

        public void OnAction(string player, string type, Dictionary<string, object> parameters)
        {
            Console.WriteLine("Got Action from " + player + " of type " + type);
            foreach (KeyValuePair<string, object> pair in parameters)
            {
                try
                {
                    Console.WriteLine("parameter: {0}: {1}", pair.Key, pair.Value);
                }
                catch (InvalidCastException e)
                {
                } // value wasn't a string (happens for droids -- it's a list)
            }
        }

        public void OnGamestart(Map MapData)
        {
            Console.WriteLine("Got Gamestart!");
            primary = RandomChoice(new[] {"mortar", "laser", "droid"});
            do
            {
                secondary = RandomChoice(new[] {"mortar", "laser", "droid"});
            } while (primary.Equals(secondary));
            connection.SendLoadout(primary, secondary);
        }

        public void OnGamestate(long TurnNumber, Map MapData, PlayerData[] players)
        {
            foreach (PlayerData player in players)
            {
                /*Console.WriteLine(
                    "player {0} has {1} life, {2} score, is at position {3},{4}, has a {5} lvl {6} and a {7} lvl {8}",
                    player.name, player.health, player.score, player.j, player.k, player.PrimaryWeapon, player.PrimaryWeaponLevel,
                    player.SecondaryWeapon, player.SecondaryWeaponLevel);*/
            }
            if (players[0].name.Equals(BotName))
            {
                connection.SendMove(RandomChoice(new[] {"up", "down", "right-up", "right-down", "left-up", "left-down"}));
                connection.SendMove(RandomChoice(new[] {"up", "down", "right-up", "right-down", "left-up", "left-down"}));
                // mortar, laser, droid, mine, upgrade
                switch (rnd.Next(5))
                {
                    case 0:
                        RandomMortar();
                        break;
                    case 1:
                        RandomLaser();
                        break;
                    case 2:
                        RandomDroid();
                        break;
                    case 3:
                        Console.WriteLine("RANDOM MINE");
                        connection.Mine();
                        break;
                    case 4:
                        Console.WriteLine("RANDOM UPGRADE");
                        connection.Upgrade(RandomChoice(new[] {primary, secondary}));
                        break;
                }
            }
        }

        public void OnError(string errormessage)
        {
            Console.WriteLine("Got error: " + errormessage);
        }

        #endregion

        private T RandomChoice<T>(T[] list)
        {
            return list[rnd.Next(list.Count())];
        }

        public void RandomMortar()
        {
            long j, k;
            j = RandomChoice(new[] {-4, -3, -2, -1, 1, 2, 3, 4});
            k = RandomChoice(new[] {-4, -3, -2, -1, 1, 2, 3, 4});
            connection.AttackMortar(j, k);
            Console.WriteLine("RANDOM MORTAR");
        }

        public void RandomDroid()
        {
            List<string> list = new List<string>();
            for (int i = 0; i < 5; i++)
            {
                list.Add(RandomChoice(new[] {"up", "down", "right-up", "right-down", "left-up", "left-down"}));
            }
            connection.AttackDroid(list.ToArray());
            Console.WriteLine("RANDOM DROID");
        }

        public void RandomLaser()
        {
            connection.AttackLaser(RandomChoice(new[] {"up", "down", "right-up", "right-down", "left-up", "left-down"}));
            Console.WriteLine("RANDOM LASER");
        }
    }
}
Back to Top