-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
123 lines (105 loc) · 4.51 KB
/
Program.cs
File metadata and controls
123 lines (105 loc) · 4.51 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MiniAiCupPaperio
{
class Program
{
private const int MaxDepth = 9;
private static List<TreeNode> _captureNodes = new List<TreeNode>();
private static List<TreeNode> _otherNodes = new List<TreeNode>();
private const int MaxTickCount = 1500;
static int _currentTick = 0;
private static void Main(string[] args)
{
//Debugger.Launch();
while (true)
{
try
{
var input = Console.ReadLine();
if (string.IsNullOrEmpty(input))
{
continue;
}
var jObject = JObject.Parse(input);
var model = JsonConvert.DeserializeObject<InputModel>(jObject.ToString());
if (model.Type == ModelType.StartGame)
{
World.XCount = model.Params.XCount;
World.YCount = model.Params.YCount;
World.Speed = model.Params.Speed;
World.Width = model.Params.Width;
continue;
}
if (model.Type == ModelType.EndGame)
{
break;
}
_captureNodes = new List<TreeNode>();
_otherNodes = new List<TreeNode>();
_currentTick = model.Params.TickNum;
var myPlayerModel = model.Params.Players.First(p => p.Key == "i").Value;
var enemyPlayersModel = model.Params.Players.Where(p => p.Key != "i").Select(p => p.Value).ToList();
Global.MapBonuses = model.Params.Bonuses.Select(b => new MapBonus(b)).ToList();
Global.Enemies = enemyPlayersModel.Select(p => new EnemyPlayer(p)).ToList();
Global.MyTerritory = new HashSet<Point>(myPlayerModel.Territory.Select(t => new Point(t)));
foreach (var enemy in enemyPlayersModel)
{
Global.EnemyTerritory.UnionWith(enemy.Territory.Select(t => new Point(t)));
}
var firstNode = new TreeNode {My = new Player(myPlayerModel), Parent = null, Depth = 0};
MacroLevel.SetDirectionsScore(firstNode.My.Position);
BuildTree(firstNode);
var nodes = _captureNodes.Count > 0 ? _captureNodes : _otherNodes;
var maxScoreNode = nodes.OrderByDescending(n => n.My.Score).ThenBy(n => n.Depth).First();
while (maxScoreNode.Depth != 1)
{
maxScoreNode = maxScoreNode.Parent;
}
Console.WriteLine("{{\"command\": \"{0}\"}}", maxScoreNode.My.Direction);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
private static void BuildTree(TreeNode tree)
{
var possibleDirections = DirectionExtension.GetPossible(tree.My.Direction);
foreach (var direction in possibleDirections)
{
var next = Simulator.GetNext(tree.My, direction, tree.Depth);
if (next == null)
{
// движение невозможно
continue;
}
var nextNode = new TreeNode {My = next, Parent = tree, Depth = tree.Depth + 1};
if (nextNode.My.HasCapture)
{
// произведен захват территории
_captureNodes.Add(nextNode);
continue;
}
_otherNodes.Add(nextNode);
if (nextNode.Depth == MaxDepth)
{
// достигнута максимальная глубина рассчета
continue;
}
if (_currentTick + (nextNode.Depth + 1) * World.OneMoveTicks > MaxTickCount)
{
// рассчет невозможен, т.к. конец игры
continue;
}
BuildTree(nextNode);
}
}
}
}