-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworking.cs
More file actions
70 lines (60 loc) · 1.97 KB
/
Networking.cs
File metadata and controls
70 lines (60 loc) · 1.97 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
using System;
using System.Collections.Generic;
namespace Halite
{
/// <summary>
/// Used for communication with server
/// </summary>
public static class Networking
{
/// <summary>
/// Call once at the start of a game to load the map and player tag from the first four stdin lines.
/// </summary>
public static Map GetInit()
{
int playerTag;
var tag = ReadNextLine();
var mapSize = ReadNextLine();
var productionMap = ReadNextLine();
var gameMap = ReadNextLine();
// Line 1: Player tag
if (!int.TryParse(tag, out playerTag))
throw new ApplicationException("Could not get player tag from stdin during init");
// Lines 2-4: Map
var map = Map.ParseMap(mapSize, productionMap, gameMap, playerTag);
return map;
}
/// <summary>
/// Call every frame to update the map to the next one provided by the environment.
/// </summary>
public static void GetFrame(Map map)
{
map.Update(ReadNextLine());
Config.IncrementTurn();
}
/// <summary>
/// Call to acknowledge the initail game map and start the game.
/// </summary>
public static void SendInit(string botName)
{
SendString(botName);
}
/// <summary>
/// Call to send your move orders and complete your turn.
/// </summary>
public static void SendMoves(IEnumerable<Move> moves)
{
SendString(Move.MovesToString(moves));
}
private static string ReadNextLine()
{
var str = Console.ReadLine();
if (str == null) throw new ApplicationException("Could not read next line from stdin");
return str;
}
private static void SendString(string str)
{
Console.WriteLine(str);
}
}
}