-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemotePlayer.cs
158 lines (125 loc) · 3.1 KB
/
RemotePlayer.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class RemotePlayer : Player
{
private Socket connection;
public RemotePlayer( bool p, Random r ) : base( p, r )
{
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
Console.Write( "Waiting for connection... " );
connection = listener.Accept();
Console.WriteLine( connection.RemoteEndPoint.ToString() );
SendString( connection, "Connected " + ( (player)?('X'):('O') ) );
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
~RemotePlayer()
{
try
{
SendString( connection, "Disconect" );
connection.Shutdown( SocketShutdown.Both );
connection.Close();
}
catch( Exception e )
{
Console.WriteLine( e.ToString() );
}
}
public override int MakeMove( GameBoard g )
{
int result;
string message;
SendString( connection, CerealizeBoard( g ) );
Console.WriteLine( "Waiting for response..." );
while( true )
{
message = ReceiveString( connection );
result = DecerealizeMove( message );
if( result == -1 )
{
Console.WriteLine( "Got an invalid move from the client..." );
Console.WriteLine( "Press the any key to continue. . .\n" );
Console.ReadKey( true );
}
else
{
break;
}
}
return result;
}
public static string CerealizeBoard( GameBoard g )
{
char[,] board = g.Board;
string result = "Move ";
foreach( char i in board )
{
result += ( i == ' ')?( '_' ):( i );
result += ',';
}
return result.Substring( 0, result.Length - 1 );
}
public static int DecerealizeMove( string s )
{
int result;
s = s.Split( new char[] { ' ' } )[1];
Int32.TryParse( s, out result );
if( result < 1 || result > 7 )
{
result = -1;
}
return result;
}
public static char[,] DecerealizeBoard( string s )
{
char[,] board = new char[6,7];
s = s.Split( new char[] { ' ' } )[1];
String[] ss = s.Split( new char[] { ',' } );
int k = 0;
for( int i = 0; i < 6; i++ )
{
for( int j = 0; j < 7; j++ )
{
board[i,j] = (ss[k][0] == '_')?( ' ' ):( ss[k][0] );
k++;
}
}
return board;
}
public static void SendString( Socket s, string str )
{
s.Send( Encoding.ASCII.GetBytes( str + "<EOF>" ) );
}
public static string ReceiveString( Socket s )
{
byte[] data;
string result = "";
while (true)
{
data = new byte[1024];
int bytesRec = s.Receive(data);
result += Encoding.ASCII.GetString(data,0,bytesRec);
if (result.IndexOf("<EOF>") > -1) {
break;
}
}
return result.Substring( 0, result.Length - 5 );
}
}