about summary refs log tree commit diff homepage
path: root/client-examples/BrutalmazeClient.cs
blob: cd3edfdf3ce6b1f0067f3c313eeb812b452e409b (plain) (blame)
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
using System;
using System.Text;
using System.Net.Sockets;

namespace BrutalmazeClient
{
	class Program
	{
		static void Main(string[] args)
		{
			const string host = "localhost";
			const int port = 42069;
			Socket client_socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
			client_socket.Connect(host, port);
			Random rnd = new Random();

			int recv, sent;
			byte[] buff = new byte[1000];
			byte[] query;
			string[] matrix = new string[100];
			const int MAGIC = 42; // For escape
			string l, data, l1;
			int sz, nl;
			int nh, ne, nb, score;
			char hC;
			int hX, hY, hA, canAtk, canReg;
			int prevX = 1234, prevY = 5678;
			int dir = 0, deg = 0, atk = 1;
			int needEsc = 0;

			while (42 < 420)
			{
				try
				{
					recv = client_socket.Receive(buff, 7, 0);
				}
				catch (SocketException e)
				{
					Console.WriteLine(e.ToString());
					break;
				}
				l = Encoding.ASCII.GetString(buff, 0, 7);
				sz = Int32.Parse(l);
				if (sz == 0)
					break;
				recv = client_socket.Receive(buff, sz, 0);
				data = Encoding.ASCII.GetString(buff, 0, sz);
				// Standardize Data
				nl = 0;
				l1 = data.Split('\n')[nl];
				nh = Int32.Parse(l1.Split(' ')[0]);
				ne = Int32.Parse(l1.Split(' ')[1]);
				nb = Int32.Parse(l1.Split(' ')[2]);
				score = Int32.Parse(l1.Split(' ')[3]);
				for (int i = 0; i < nh; ++i, ++nl)
					matrix[i] = data.Split('\n')[i + 1];
				l1 = data.Split('\n')[++nl];
				hC = Char.Parse(l1.Split(' ')[0]);
				hX = Int32.Parse(l1.Split(' ')[1]);
				hY = Int32.Parse(l1.Split(' ')[2]);
				hA = Int32.Parse(l1.Split(' ')[3]);
				canAtk = Int32.Parse(l1.Split(' ')[4]);
				canReg = Int32.Parse(l1.Split(' ')[5]);
				for(int i = 1; i <= ne; ++i, ++nl)
				{
				}
				for(int i = 1; i <= nb; ++i, ++nl)
				{
				}
				// Process
				if (needEsc == 0)
				{
					dir = 0;
					if (prevX == hX && prevY == hY)
					{
						int matX = hX / 100, matY = hY / 100;
						if (matrix[matY - 1][matX + 2] == '0' && matrix[matY - 1][matX - 2] == '1')
						{
							dir = 5;
							needEsc = 1;
						}
						if (matrix[matY - 1][matX + 2] == '1' && matrix[matY + 1][matX - 2] == '0')
						{
							dir = 7;
							needEsc = 1;
						}
					}
				}
				else
				{
					needEsc = (needEsc + 1) % MAGIC;
				}
				deg = rnd.Next(-4, 5) * 10;
				atk = rnd.Next(1, 1);
				query = Encoding.ASCII.GetBytes(dir.ToString() + " " + deg.ToString() + " " + atk.ToString());
				sent = client_socket.Send(query);
				prevX = hX;
				prevY = hY;
			}
			client_socket.Shutdown(SocketShutdown.Both);
			client_socket.Close();
		}
	}
}