-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBomberBot.cs
88 lines (75 loc) · 2.58 KB
/
BomberBot.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace cSharpbot
{
class BomberBot
{
static void Main(string[] args)
{
new BomberBot();
}
byte[] inFromServer = new byte[1280];
private Socket socketCliente = null;
private Bot bot = null;
private bool conectado = false;
public BomberBot()
{
try
{
conectar("cSharpBot", "984198716");
controlConexion();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadKey();
}
}
private void conectar(String user, String token)
{
socketCliente = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketCliente.Connect("bomberbot.com", 5000);
socketCliente.Receive(inFromServer);
String bienvenida = Encoding.UTF8.GetString(inFromServer);
Console.WriteLine(bienvenida);
byte[] msg = System.Text.Encoding.UTF8.GetBytes(user + "," + token);
socketCliente.Send(msg, msg.Length, SocketFlags.None);
conectado = true;
}
private void controlConexion()
{
byte[] response = new byte[512];
while (conectado)
{
Console.WriteLine("turno");
socketCliente.Receive(response);
String serverMessage = Encoding.UTF8.GetString(response);
string[] message = Regex.Split(serverMessage, ";");
if (message.Length == 0)
continue;
if (message[0] == "EMPEZO")
{
bot = new Bot(message[2][0]);
bot.updateMap(message[1]);
}
else if (message[0] == "TURNO")
{
Console.WriteLine("turno: " + message[1]);
bot.updateMap(message[2]);
byte[] msg = System.Text.Encoding.UTF8.GetBytes(bot.move());
socketCliente.Send(msg, msg.Length, SocketFlags.None);
}
else if (message[0] == "PERDIO")
{
Console.WriteLine("perdi :(");
}
}
}
}
}