-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProgram.cs
More file actions
59 lines (54 loc) · 2.24 KB
/
Copy pathProgram.cs
File metadata and controls
59 lines (54 loc) · 2.24 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
using System;
using System.IO;
using System.Net.Sockets;
using Newtonsoft.Json;
using SnakeWars.SampleBot.Properties;
namespace SnakeWars.SampleBot
{
internal class Program
{
private static void Main(string[] args)
{
try
{
using (var tcpClient = new TcpClient())
{
tcpClient.NoDelay = true;
tcpClient.Connect(Settings.Default.ServerHost, Settings.Default.ServerPort);
using (var reader = new StreamReader(tcpClient.GetStream()))
{
using (var writer = new StreamWriter(tcpClient.GetStream()))
{
writer.AutoFlush = true;
// Server should start with "ID" line.
if (reader.ReadLine() != "ID")
throw new InvalidDataException("Server didn't ask for my identity.");
// Send our login id.
writer.WriteLine(Settings.Default.LoginId);
// Read our snake id.
var mySnakeId = reader.ReadLine();
Console.WriteLine("My snake id: {0}", mySnakeId);
var snakeEngine = new SnakeEngine(mySnakeId);
// Loop till key press.
while (!Console.KeyAvailable)
{
var gameBoardState =
new GameBoardState(JsonConvert.DeserializeObject<GameStateDTO>(reader.ReadLine()));
var move = snakeEngine.GetNextMove(gameBoardState);
if (move)
{
Console.WriteLine("Sending command {0}", move.Command);
writer.WriteLine(move.Command);
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}", ex);
}
}
}
}