@@ -39,8 +39,15 @@ private void MainWindow_FormClosing(object sender, System.ComponentModel.CancelE
39
39
private void button_connect_Click ( object sender , EventArgs e ) {
40
40
// Check if the username is suitable
41
41
username = txtBoxUsername . Text . Trim ( ) ;
42
- if ( username . Length < 4 || username . Length > 16 ) {
43
- logs . AppendText ( "Please make sure your username length is between 4 and 16 characters.\n " ) ;
42
+ if ( username . Length < 4 || username . Length > 12 ) {
43
+ logs . AppendText ( "Please make sure your username length is between 4 and 12 characters.\n " ) ;
44
+ return ;
45
+ }
46
+ if ( ! CheckUsername ( ) ) {
47
+ logs . AppendText ( "Please make sure your username only consists of:\n " +
48
+ "- Uppercase (A-Z)\n " +
49
+ "- Lowercase (a-z)\n " +
50
+ "- Digits (0-9)\n " ) ;
44
51
return ;
45
52
}
46
53
// Disable the button while attempting to connect
@@ -60,7 +67,7 @@ private void button_connect_Click(object sender, EventArgs e) {
60
67
string ip = txtBoxIp . Text ;
61
68
int portNum ;
62
69
if ( ! Int32 . TryParse ( txtBoxPort . Text , out portNum ) ) {
63
- logs . AppendText ( "Check the port.\n " ) ;
70
+ logs . AppendText ( "Please enter a valid port number .\n " ) ;
64
71
btnConnect . Enabled = true ;
65
72
return ;
66
73
}
@@ -76,18 +83,19 @@ private void button_connect_Click(object sender, EventArgs e) {
76
83
int bytesRead = clientSocket . Receive ( buffer ) ;
77
84
string serverResponse = Encoding . Default . GetString ( buffer , 0 , bytesRead ) . Trim ( '\0 ' ) ;
78
85
// Close the socket if the username is not available or the server is full
79
- if ( serverResponse . Equals ( "USERNAME_NOT_AVAILABLE " ) ) {
80
- logs . AppendText ( "Cannot join the server as this username is taken .\n " ) ;
86
+ if ( serverResponse . Equals ( "SERVER_IS_FULL " ) ) {
87
+ logs . AppendText ( "Server: The server is at maximum capacity .\n " ) ;
81
88
clientSocket . Close ( ) ;
82
89
}
83
- else if ( serverResponse . Equals ( "SERVER_IS_FULL " ) ) {
84
- logs . AppendText ( "Cannot join the server as it is at maximum capacity .\n " ) ;
90
+ else if ( serverResponse . Equals ( "USERNAME_NOT_AVAILABLE " ) ) {
91
+ logs . AppendText ( "Server: This username is not available .\n " ) ;
85
92
clientSocket . Close ( ) ;
86
93
}
87
94
// Positive response, update the UI
88
95
else {
89
96
connected = true ;
90
97
logs . AppendText ( $ "Connected to the server as { username } .\n ") ;
98
+ logs . AppendText ( $ "Server: Welcome to the game, { username } !\n ") ;
91
99
txtBoxUsername . Enabled = false ;
92
100
txtBoxIp . Enabled = false ;
93
101
txtBoxPort . Enabled = false ;
@@ -114,24 +122,24 @@ private void button_connect_Click(object sender, EventArgs e) {
114
122
}
115
123
// Handle exceptions
116
124
catch {
117
- logs . AppendText ( "Could not connect to the server! \n " ) ;
125
+ logs . AppendText ( "Could not connect to the server. \n " ) ;
118
126
}
119
127
btnConnect . Enabled = true ;
120
128
}
121
129
122
130
// Send game choices to the server
123
131
private void btnPlay_Click ( object sender , EventArgs e ) {
124
132
string message = txtBoxChoice . Text ;
133
+ txtBoxChoice . Clear ( ) ;
125
134
int playerChoice ;
126
135
if ( Int32 . TryParse ( message , out playerChoice ) && 1 <= playerChoice && playerChoice <= 9 ) {
127
136
// DEBUG logs.AppendText($"{username}: {message}\n");
128
- txtBoxChoice . Text = "" ;
129
137
clientSocket . Send ( Encoding . Default . GetBytes ( message ) ) ;
130
138
btnPlay . Enabled = false ;
131
139
txtBoxChoice . Enabled = false ;
132
140
}
133
141
else {
134
- logs . AppendText ( "Please enter a number . (1-9)\n " ) ;
142
+ logs . AppendText ( "Please make a valid move . (1-9)\n " ) ;
135
143
}
136
144
}
137
145
@@ -179,7 +187,6 @@ private void ReceiveMessages() {
179
187
btnPlay . Enabled = true ;
180
188
continue ;
181
189
}
182
-
183
190
if ( message . Equals ( "GAME_PAUSE" ) ) {
184
191
txtBoxChoice . Enabled = false ;
185
192
btnPlay . Enabled = false ;
@@ -188,7 +195,6 @@ private void ReceiveMessages() {
188
195
logs . AppendText ( "Server: The game has been paused.\n " ) ;
189
196
continue ;
190
197
}
191
-
192
198
if ( message . Equals ( "GAME_RESET" ) ) {
193
199
ClearBoard ( ) ;
194
200
txtBoxChoice . Enabled = false ;
@@ -223,7 +229,7 @@ private void ReceiveMessages() {
223
229
}
224
230
}
225
231
// Handle exceptions
226
- catch ( Exception e ) {
232
+ catch {
227
233
// logs.AppendText($"Exception occurred: {e}\n");
228
234
logs . AppendText ( "Disconnected from the server.\n " ) ;
229
235
ClearBoard ( ) ;
@@ -261,6 +267,15 @@ private void ClearBoard() {
261
267
board [ i ] . Text = i . ToString ( ) ;
262
268
}
263
269
}
270
+
271
+ private bool CheckUsername ( ) {
272
+ foreach ( char c in username ) {
273
+ if ( ( c < 'a' || c > 'z' ) && ( c < 'A' || c > 'Z' ) && ( c < '0' || c > '9' ) ) {
274
+ return false ;
275
+ }
276
+ }
277
+ return true ;
278
+ }
264
279
265
280
// --- Helper Functions End ---
266
281
}
0 commit comments