summaryrefslogtreecommitdiff
path: root/Entelect.BattleCity.Challenge/GameInProgress.cs
blob: 61fa436c7b892b78a29e8a6234af59de82f60681 (plain)
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
using System;
using System.Windows;
using System.Collections.Generic;
using System.Threading;

namespace Entelect.BattleCity.Challenge
{
    class GameInProgress
    {
        public static void run(ChallengeService.ChallengeClient service, ChallengeService.state?[][] state)
        {
            AiAgent agent = new AiAgent();

            while (true)
            {
                var game = service.getStatus();
                long currentTick = DateTime.Now.Ticks;
                long nextTick = game.nextTickTime.Ticks;
                if (currentTick > nextTick)
                {
                    continue;
                }

                // AI logic here
                Move tank1Move = agent.GetBestMove(state, game, 0);
                Move tank2Move = agent.GetBestMove(state, game, 1);

                if (tank1Move != null)
                {
                    service.setActionAsync(tank1Move.Tank, tank1Move.Action);
                }
                if (tank2Move != null)
                {
                    service.setActionAsync(tank2Move.Tank, tank2Move.Action);
                }

                currentTick = DateTime.Now.Ticks;

                long sleepTime = nextTick - currentTick;
                if (sleepTime < 0L)
                {
                    Console.Error.WriteLine("ERROR: Gone passed the next tick time");
                }
                else
                {
                    Console.WriteLine("Sleeping until {1} for {0}ms", sleepTime, game.nextTickTime.ToString());
                }

                try
                {
                    Thread.Sleep(TimeSpan.FromTicks(sleepTime));
                }
                catch (Exception)
                {
                    continue;
                }
                //while (startTick < nextTick)
                //{
                //    startTick += (DateTime.Now.Ticks - startTick);
                //}
            }
        }
    }
}