| 1 |
/* netToe Version 1.5.1 |
| 2 |
* |
| 3 |
* Copyright 2000,2001 Gabriele Giorgetti <[email protected]> |
| 4 |
* 2009-2014 Mats Erik Andersson <[email protected]> |
| 5 |
* |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
# include <config.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <sys/types.h> |
| 32 |
#include <sys/socket.h> |
| 33 |
#include <netinet/in.h> |
| 34 |
#include <arpa/inet.h> |
| 35 |
#include <sys/wait.h> |
| 36 |
#include <netdb.h> |
| 37 |
#include <signal.h> |
| 38 |
#include <time.h> |
| 39 |
|
| 40 |
#include "terminal.h" |
| 41 |
/* New Code for the nettoe board matrix, not used yet. |
| 42 |
* #include "matrices.h" |
| 43 |
*/ |
| 44 |
#include "misc.h" |
| 45 |
#include "network.h" |
| 46 |
#include "game.h" |
| 47 |
#include "board.h" |
| 48 |
|
| 49 |
/* Has network partner reported EOF? */ |
| 50 |
int has_given_eof = 0; |
| 51 |
|
| 52 |
enum { |
| 53 |
I_AM_SERVER = 0, |
| 54 |
I_AM_CLIENT = 1, |
| 55 |
}; |
| 56 |
|
| 57 |
#define ASK_SERVER I_AM_CLIENT |
| 58 |
#define ASK_CLIENT I_AM_SERVER |
| 59 |
|
| 60 |
static char player1_name[MAX_PNAME_LEN]; |
| 61 |
static char player2_name[MAX_PNAME_LEN]; |
| 62 |
|
| 63 |
static int game_mode = 0; |
| 64 |
static int who_starts = 0; |
| 65 |
static int computer_AI = 0; |
| 66 |
static int winner = GAME_IS_ALIVE; |
| 67 |
|
| 68 |
static int player1_status = 0; |
| 69 |
static int player2_status = 0; |
| 70 |
static int computer_status = 0; |
| 71 |
|
| 72 |
static int sock = -1; |
| 73 |
|
| 74 |
static void main_menu (void); |
| 75 |
static void cpu_AI_menu (void); |
| 76 |
static void network_menu (void); |
| 77 |
|
| 78 |
void print_header (int type); |
| 79 |
static void init_one_player_game (void); |
| 80 |
static void init_two_player_game (void); |
| 81 |
static void do_one_player_game (void); |
| 82 |
static void do_two_player_game (void); |
| 83 |
static void show_game (void); |
| 84 |
static void show_drawed_game (void); |
| 85 |
static void show_waiting_for_move (const char *name); |
| 86 |
static void get_player1_move (void); |
| 87 |
static void get_player2_move (void); |
| 88 |
extern void get_cpu_move (int); |
| 89 |
|
| 90 |
static void server_start (void); |
| 91 |
static void client_start (void); |
| 92 |
static void init_server_network_game (void); |
| 93 |
static void init_client_network_game (void); |
| 94 |
|
| 95 |
static void recv_matrix (int fd); |
| 96 |
static void send_matrix (int fd); |
| 97 |
|
| 98 |
static void quit_game (void); |
| 99 |
|
| 100 |
/* Reset colour at interrupts SIGINT, SIGKILL, SIGTERM. |
| 101 |
* Use ECMA-48 character code. */ |
| 102 |
static void reset_color (int sig) |
| 103 |
{ |
| 104 |
(void) sig; /* Unused variable, silence warning. */ |
| 105 |
printf ("%c[0m\n", 0x1B); |
| 106 |
exit (EXIT_SUCCESS); |
| 107 |
} |
| 108 |
|
| 109 |
int main (int argc, char *argv[]) |
| 110 |
{ |
| 111 |
|
| 112 |
NO_BEEP = 0; /* beeps are enabled by default */ |
| 113 |
/* --no-beep disable beeps */ |
| 114 |
NO_COLORS = 0; /* colors are enabled by default */ |
| 115 |
/* --no-colors disable colors */ |
| 116 |
|
| 117 |
parse_cmd_args(argc, argv); |
| 118 |
|
| 119 |
/* Remove any elevated access. */ |
| 120 |
setegid(getgid()); |
| 121 |
setgid(getgid()); |
| 122 |
seteuid(getuid()); |
| 123 |
setuid(getuid()); |
| 124 |
|
| 125 |
srandom(time(0)); |
| 126 |
|
| 127 |
#if USE_TERMINFO || USE_TERMCAP |
| 128 |
setup_terminfo(); |
| 129 |
#endif |
| 130 |
|
| 131 |
/* Reset colour at normal exit. */ |
| 132 |
atexit(nettoe_term_set_default_color); |
| 133 |
/* And at abnormal exits. */ |
| 134 |
signal(SIGINT, reset_color); |
| 135 |
signal(SIGKILL, reset_color); |
| 136 |
signal(SIGTERM, reset_color); |
| 137 |
|
| 138 |
while (1) |
| 139 |
main_menu (); |
| 140 |
|
| 141 |
quit_game (); |
| 142 |
|
| 143 |
return EXIT_SUCCESS; |
| 144 |
} |
| 145 |
|
| 146 |
#define SINGLE_DASHED_LINE \ |
| 147 |
"+-----------------------------------------------------------+\n" |
| 148 |
#define BROKEN_DASHED_LINE \ |
| 149 |
"+--------------------------+--------------------------------+\n" |
| 150 |
#define SPACES_AND_BARS \ |
| 151 |
" | | | | | \n" |
| 152 |
#define BLOCKERS_AND_BARS \ |
| 153 |
" ---|---|--- | ----|----|---- \n" |
| 154 |
|
| 155 |
#define YOU_WIN \ |
| 156 |
"\n You win !\n" |
| 157 |
#define COMPUTER_WINS \ |
| 158 |
"\n Computer wins !\n" |
| 159 |
#define IS_A_DRAW \ |
| 160 |
"\n This is a draw !\n" |
| 161 |
#define COMPUTER_CONSIDERS \ |
| 162 |
"\n Computer is considering its move ...\n" |
| 163 |
#define WAITING_FOR_NAMED \ |
| 164 |
"\n Waiting for %s to choose whether to play again, or not ...\n" |
| 165 |
#define WANT_TO_PLAY \ |
| 166 |
"\n Do you want to play again ? [y/n]: " |
| 167 |
#define UNKNOWN_ANSWER \ |
| 168 |
" Unknown answer. I suppose you want to play again.\n" |
| 169 |
|
| 170 |
void print_header (int type) |
| 171 |
{ |
| 172 |
nettoe_term_clear (); |
| 173 |
|
| 174 |
nettoe_term_reset_color(); |
| 175 |
printf (SINGLE_DASHED_LINE); |
| 176 |
printf ("|"); |
| 177 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 178 |
printf (" netToe %-5.5s ", PACKAGE_VERSION); |
| 179 |
nettoe_term_reset_color(); |
| 180 |
printf ("|-XOX-OXO-OOO-XXX-|"); |
| 181 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 182 |
printf (" The Net Tic Tac Toe Game "); |
| 183 |
nettoe_term_reset_color(); |
| 184 |
printf ("|\n"); |
| 185 |
|
| 186 |
if (type == 0) |
| 187 |
printf (SINGLE_DASHED_LINE); |
| 188 |
else |
| 189 |
printf (BROKEN_DASHED_LINE); |
| 190 |
|
| 191 |
nettoe_term_set_default_color(); |
| 192 |
} |
| 193 |
|
| 194 |
static void main_menu (void) |
| 195 |
{ |
| 196 |
int selection = 0; |
| 197 |
char answer[20]; |
| 198 |
|
| 199 |
/* Make sure this is an interactive session. |
| 200 |
* Otherwise, quit silently. |
| 201 |
*/ |
| 202 |
if (!isatty (STDIN_FILENO)) |
| 203 |
exit (EXIT_FAILURE); |
| 204 |
|
| 205 |
print_header (0); |
| 206 |
nettoe_term_reset_color(); |
| 207 |
printf ("\n [ "); |
| 208 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 209 |
printf ("Main Menu"); |
| 210 |
nettoe_term_reset_color(); |
| 211 |
printf (" ]\n\n"); |
| 212 |
|
| 213 |
/* Player vs CPU */ |
| 214 |
printf (" ("); |
| 215 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 216 |
printf ("1"); |
| 217 |
nettoe_term_reset_color(); |
| 218 |
printf (") Player vs "); |
| 219 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 220 |
printf ("C"); |
| 221 |
nettoe_term_reset_color(); |
| 222 |
printf ("PU\n"); |
| 223 |
|
| 224 |
/* Player vs Player */ |
| 225 |
printf (" ("); |
| 226 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 227 |
printf ("2"); |
| 228 |
nettoe_term_reset_color(); |
| 229 |
printf (") Player vs "); |
| 230 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 231 |
printf ("P"); |
| 232 |
nettoe_term_reset_color(); |
| 233 |
printf ("layer\n"); |
| 234 |
|
| 235 |
/* Two players over network */ |
| 236 |
printf (" ("); |
| 237 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 238 |
printf ("3"); |
| 239 |
nettoe_term_reset_color(); |
| 240 |
printf (") Two players over "); |
| 241 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 242 |
putchar ('n'); |
| 243 |
nettoe_term_reset_color(); |
| 244 |
printf ("etwork\n"); |
| 245 |
|
| 246 |
/* Info */ |
| 247 |
puts (""); |
| 248 |
printf (" ("); |
| 249 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 250 |
printf ("4"); |
| 251 |
nettoe_term_reset_color(); |
| 252 |
printf (") "); |
| 253 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 254 |
putchar ('I'); |
| 255 |
nettoe_term_reset_color(); |
| 256 |
printf ("nfo\n"); |
| 257 |
|
| 258 |
/* Quit */ |
| 259 |
printf (" ("); |
| 260 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 261 |
printf ("q"); |
| 262 |
nettoe_term_reset_color(); |
| 263 |
printf (") "); |
| 264 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 265 |
putchar ('Q'); |
| 266 |
nettoe_term_reset_color(); |
| 267 |
printf ("uit\n"); |
| 268 |
|
| 269 |
while ( selection == 0 ) |
| 270 |
{ |
| 271 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 272 |
printf ("\n Choose an action: "); |
| 273 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 274 |
|
| 275 |
fflush (stdin); |
| 276 |
if (!fgets (answer, sizeof(answer), stdin)) |
| 277 |
exit(EXIT_FAILURE); |
| 278 |
|
| 279 |
if ( sscanf (answer, " %d", &selection) == 0 ) |
| 280 |
{ |
| 281 |
char *p = answer; |
| 282 |
|
| 283 |
while (*p == ' ') |
| 284 |
++p; |
| 285 |
switch (*p) |
| 286 |
{ |
| 287 |
case 'c': |
| 288 |
case 'C': selection = 1; |
| 289 |
break; |
| 290 |
case 'p': |
| 291 |
case 'P': selection = 2; |
| 292 |
break; |
| 293 |
case 'n': |
| 294 |
case 'N': selection = 3; |
| 295 |
break; |
| 296 |
case 'i': |
| 297 |
case 'I': selection = 4; |
| 298 |
break; |
| 299 |
case 'q': |
| 300 |
case 'Q': selection = 5; |
| 301 |
break; |
| 302 |
default: selection = 0; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
if ( (selection < 1) || (selection > 5) ) |
| 307 |
{ |
| 308 |
selection = 0; |
| 309 |
} |
| 310 |
} |
| 311 |
nettoe_term_set_default_color (); |
| 312 |
|
| 313 |
/* Reset scores every time a new main mode has been selected. */ |
| 314 |
player1_status = 0; |
| 315 |
player2_status = 0; |
| 316 |
computer_status = 0; |
| 317 |
has_given_eof = 0; |
| 318 |
|
| 319 |
if (selection == 1) |
| 320 |
{ |
| 321 |
cpu_AI_menu (); |
| 322 |
|
| 323 |
if (computer_AI) |
| 324 |
init_one_player_game (); |
| 325 |
} |
| 326 |
else if (selection == 2) |
| 327 |
{ |
| 328 |
init_two_player_game (); |
| 329 |
} |
| 330 |
else if (selection == 3) |
| 331 |
{ |
| 332 |
network_menu (); |
| 333 |
} |
| 334 |
else if (selection == 4) |
| 335 |
{ |
| 336 |
print_header (0); |
| 337 |
print_infos_screen (); |
| 338 |
} |
| 339 |
else if (selection == 5) |
| 340 |
{ |
| 341 |
quit_game (); |
| 342 |
} |
| 343 |
else |
| 344 |
{ |
| 345 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 346 |
printf ("\n Error:"); |
| 347 |
nettoe_term_reset_color(); |
| 348 |
printf (" Incorrect choice.\n"); |
| 349 |
nettoe_term_set_default_color (); |
| 350 |
quit_game (); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
static void cpu_AI_menu (void) |
| 355 |
{ |
| 356 |
int selection = 0; |
| 357 |
char answer[20]; |
| 358 |
|
| 359 |
computer_AI = 0; /* Unset level. */ |
| 360 |
print_header (0); |
| 361 |
|
| 362 |
nettoe_term_reset_color(); |
| 363 |
printf ("\n [ "); |
| 364 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 365 |
printf ("Level of difficulty"); |
| 366 |
nettoe_term_reset_color(); |
| 367 |
printf (" ]\n\n"); |
| 368 |
|
| 369 |
printf (" ("); |
| 370 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 371 |
printf ("1"); |
| 372 |
nettoe_term_reset_color(); |
| 373 |
printf (") "); |
| 374 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 375 |
putchar ('W'); |
| 376 |
nettoe_term_reset_color(); |
| 377 |
printf ("eak \n"); |
| 378 |
|
| 379 |
printf (" ("); |
| 380 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 381 |
printf ("2"); |
| 382 |
nettoe_term_reset_color(); |
| 383 |
printf (") "); |
| 384 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 385 |
putchar ('N'); |
| 386 |
nettoe_term_reset_color(); |
| 387 |
printf ("ormal\n"); |
| 388 |
|
| 389 |
printf (" ("); |
| 390 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 391 |
printf ("3"); |
| 392 |
nettoe_term_reset_color(); |
| 393 |
printf (") "); |
| 394 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 395 |
putchar ('B'); |
| 396 |
nettoe_term_reset_color(); |
| 397 |
printf ("etter\n"); |
| 398 |
|
| 399 |
puts (""); |
| 400 |
printf (" ("); |
| 401 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 402 |
printf ("q"); |
| 403 |
nettoe_term_reset_color(); |
| 404 |
printf (") "); |
| 405 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 406 |
putchar ('R'); |
| 407 |
nettoe_term_reset_color(); |
| 408 |
printf ("eturn to "); |
| 409 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 410 |
putchar ('m'); |
| 411 |
nettoe_term_reset_color(); |
| 412 |
printf ("ain menu\n"); |
| 413 |
|
| 414 |
while ( selection == 0 ) |
| 415 |
{ |
| 416 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 417 |
printf ("\n Choice: "); |
| 418 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 419 |
|
| 420 |
fflush (stdin); |
| 421 |
if (!fgets (answer, sizeof(answer), stdin)) |
| 422 |
exit(EXIT_FAILURE); |
| 423 |
|
| 424 |
if ( sscanf (answer, "%d", &selection) == 0 ) |
| 425 |
{ |
| 426 |
char *p = answer; |
| 427 |
|
| 428 |
while (*p == ' ') |
| 429 |
++p; |
| 430 |
switch (*p) |
| 431 |
{ |
| 432 |
case 'w': |
| 433 |
case 'W': selection = 1; |
| 434 |
break; |
| 435 |
case 'n': |
| 436 |
case 'N': selection = 2; |
| 437 |
break; |
| 438 |
case 'b': |
| 439 |
case 'B': selection = 3; |
| 440 |
break; |
| 441 |
case 'm': |
| 442 |
case 'M': |
| 443 |
case 'q': |
| 444 |
case 'Q': |
| 445 |
case 'r': |
| 446 |
case 'R': selection = 4; |
| 447 |
break; |
| 448 |
default: selection = 0; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
if ( (selection < 1) || (selection > 4) ) |
| 453 |
{ |
| 454 |
selection = 0; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
nettoe_term_set_default_color (); |
| 459 |
|
| 460 |
if (selection == 1) |
| 461 |
{ |
| 462 |
computer_AI = 1; |
| 463 |
} |
| 464 |
else if (selection == 2) |
| 465 |
{ |
| 466 |
computer_AI = 2; |
| 467 |
} |
| 468 |
else if (selection == 3) |
| 469 |
{ |
| 470 |
computer_AI = 3; |
| 471 |
} |
| 472 |
/* Return to main menu, leaving level unset. */ |
| 473 |
} |
| 474 |
|
| 475 |
static void network_menu (void) |
| 476 |
{ |
| 477 |
int selection = 0; |
| 478 |
char answer[MAX_PNAME_LEN]; |
| 479 |
|
| 480 |
print_header (0); |
| 481 |
nettoe_term_reset_color(); |
| 482 |
printf ("\n [ "); |
| 483 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 484 |
printf ("Network game options"); |
| 485 |
nettoe_term_reset_color(); |
| 486 |
printf (" ]\n\n"); |
| 487 |
|
| 488 |
printf (" ("); |
| 489 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 490 |
printf ("1"); |
| 491 |
nettoe_term_reset_color(); |
| 492 |
printf (") "); |
| 493 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 494 |
putchar ('H'); |
| 495 |
nettoe_term_reset_color(); |
| 496 |
printf ("ost the game\n"); |
| 497 |
|
| 498 |
printf (" ("); |
| 499 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 500 |
printf ("2"); |
| 501 |
nettoe_term_reset_color(); |
| 502 |
printf (") "); |
| 503 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 504 |
putchar ('C'); |
| 505 |
nettoe_term_reset_color(); |
| 506 |
printf ("onnect to host\n"); |
| 507 |
|
| 508 |
puts (""); |
| 509 |
printf (" ("); |
| 510 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 511 |
printf ("q"); |
| 512 |
nettoe_term_reset_color(); |
| 513 |
printf (") "); |
| 514 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 515 |
putchar ('B'); |
| 516 |
nettoe_term_reset_color(); |
| 517 |
printf ("ack to "); |
| 518 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 519 |
putchar ('m'); |
| 520 |
nettoe_term_reset_color(); |
| 521 |
printf ("ain menu\n"); |
| 522 |
|
| 523 |
while ( selection == 0 ) |
| 524 |
{ |
| 525 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 526 |
printf ("\n Choose an action: "); |
| 527 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 528 |
|
| 529 |
fflush (stdin); |
| 530 |
if (!fgets (answer, sizeof(answer), stdin)) |
| 531 |
exit(EXIT_FAILURE); |
| 532 |
|
| 533 |
if ( sscanf (answer, "%d", &selection) == 0 ) |
| 534 |
{ |
| 535 |
char *p = answer; |
| 536 |
|
| 537 |
while (*p == ' ') |
| 538 |
++p; |
| 539 |
switch (*p) |
| 540 |
{ |
| 541 |
case 'h': |
| 542 |
case 'H': selection = 1; |
| 543 |
break; |
| 544 |
case 'c': |
| 545 |
case 'C': selection = 2; |
| 546 |
break; |
| 547 |
case 'b': |
| 548 |
case 'B': |
| 549 |
case 'm': |
| 550 |
case 'M': |
| 551 |
case 'q': |
| 552 |
case 'Q': selection = 3; |
| 553 |
break; |
| 554 |
default: selection = 0; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
if ( (selection < 1) || (selection > 3) ) |
| 559 |
{ |
| 560 |
selection = 0; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
nettoe_term_reset_color (); |
| 565 |
|
| 566 |
if (selection == 1) |
| 567 |
{ |
| 568 |
signal(SIGPIPE, SIG_IGN); |
| 569 |
server_start (); |
| 570 |
} |
| 571 |
else if (selection == 2) |
| 572 |
{ |
| 573 |
signal(SIGPIPE, SIG_IGN); |
| 574 |
client_start (); |
| 575 |
} |
| 576 |
|
| 577 |
/* Third choice returns to main menu. */ |
| 578 |
|
| 579 |
if ( has_given_eof ) |
| 580 |
{ |
| 581 |
printf("\n\n Your opponent has resigned the game by cutting connections !\n"); |
| 582 |
printf (" (A carrage return continues. )\b\b\b"); |
| 583 |
fflush (stdin); |
| 584 |
getchar(); |
| 585 |
return; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
static void init_one_player_game (void) |
| 590 |
{ |
| 591 |
who_starts = who_start_first (); |
| 592 |
|
| 593 |
nettoe_term_reset_color(); |
| 594 |
printf ("\n Please state your name: "); |
| 595 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 596 |
get_player_pname(player1_name, MAX_PNAME_LEN); |
| 597 |
|
| 598 |
nettoe_term_set_default_color (); |
| 599 |
|
| 600 |
while ((check_pname(player1_name, MAX_PNAME_LEN)) != 0) |
| 601 |
{ |
| 602 |
printf (" Error: name cannot be more than %d chars long.\n", |
| 603 |
MAX_PNAME_LEN); |
| 604 |
printf ("\n Please state your name: "); |
| 605 |
get_player_pname(player1_name, MAX_PNAME_LEN); |
| 606 |
} |
| 607 |
|
| 608 |
nettoe_term_reset_color(); |
| 609 |
printf ("\n Who will start first ? "); |
| 610 |
sleep ( 1 ); |
| 611 |
|
| 612 |
if (who_starts == 1) |
| 613 |
printf ("You will begin ! "); |
| 614 |
else |
| 615 |
printf ("Computer will begin ! "); |
| 616 |
|
| 617 |
nettoe_term_set_default_color (); |
| 618 |
fflush (stdout); |
| 619 |
sleep ( 3 ); |
| 620 |
|
| 621 |
do_one_player_game (); |
| 622 |
} |
| 623 |
|
| 624 |
static void init_two_player_game (void) |
| 625 |
{ |
| 626 |
who_starts = who_start_first (); |
| 627 |
|
| 628 |
nettoe_term_reset_color(); |
| 629 |
printf ("\n Player 1, please type your name: "); |
| 630 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 631 |
get_player_pname(player1_name, MAX_PNAME_LEN); |
| 632 |
nettoe_term_reset_color (); |
| 633 |
|
| 634 |
while ((check_pname(player1_name, MAX_PNAME_LEN)) != 0) |
| 635 |
{ |
| 636 |
printf (" Error: name cannot be more than %d chars long.\n", |
| 637 |
MAX_PNAME_LEN); |
| 638 |
printf ("\n Player 1, please type your name: "); |
| 639 |
get_player_pname(player1_name, MAX_PNAME_LEN); |
| 640 |
} |
| 641 |
|
| 642 |
printf ("\n Player 2, please type your name: "); |
| 643 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 644 |
get_player_pname(player2_name, MAX_PNAME_LEN); |
| 645 |
nettoe_term_reset_color (); |
| 646 |
|
| 647 |
while ((check_pname(player2_name, MAX_PNAME_LEN)) != 0) |
| 648 |
{ |
| 649 |
printf (" Error: name cannot be more than %d chars long.\n", |
| 650 |
MAX_PNAME_LEN); |
| 651 |
printf ("\n Player 2, please type your name: "); |
| 652 |
get_player_pname(player2_name, MAX_PNAME_LEN); |
| 653 |
} |
| 654 |
|
| 655 |
nettoe_term_reset_color(); |
| 656 |
printf ("\n Who will start first ? "); |
| 657 |
sleep ( 1 ); |
| 658 |
|
| 659 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 660 |
printf ("%s", (who_starts == 1) ? player1_name : player2_name); |
| 661 |
nettoe_term_reset_color(); |
| 662 |
printf (" will begin ! "); |
| 663 |
nettoe_term_set_default_color (); |
| 664 |
fflush (stdout); |
| 665 |
sleep ( 3 ); |
| 666 |
|
| 667 |
do_two_player_game (); |
| 668 |
} |
| 669 |
|
| 670 |
static void do_one_player_game (void) |
| 671 |
{ |
| 672 |
char y_n[12]; |
| 673 |
winner = GAME_IS_ALIVE; |
| 674 |
game_mode = 1; |
| 675 |
init_matrix (); |
| 676 |
|
| 677 |
do |
| 678 |
{ |
| 679 |
if (who_starts == 1) |
| 680 |
{ |
| 681 |
show_game (); |
| 682 |
get_player1_move (); |
| 683 |
} |
| 684 |
else |
| 685 |
{ |
| 686 |
printf (COMPUTER_CONSIDERS); |
| 687 |
get_cpu_move (computer_AI); |
| 688 |
} |
| 689 |
|
| 690 |
winner = game_check (); |
| 691 |
if ( winner != GAME_IS_ALIVE ) |
| 692 |
break; |
| 693 |
|
| 694 |
if (who_starts == 1) |
| 695 |
{ |
| 696 |
printf (COMPUTER_CONSIDERS); |
| 697 |
get_cpu_move (computer_AI); |
| 698 |
} |
| 699 |
else |
| 700 |
{ |
| 701 |
show_game (); |
| 702 |
get_player1_move (); |
| 703 |
} |
| 704 |
|
| 705 |
winner = game_check (); |
| 706 |
if ( winner != GAME_IS_ALIVE) |
| 707 |
break; |
| 708 |
} |
| 709 |
while (winner == GAME_IS_ALIVE); |
| 710 |
|
| 711 |
/* Switch roles of players for next round. |
| 712 |
* An easy arithmetic trick. |
| 713 |
*/ |
| 714 |
who_starts = (1 + 2) - who_starts; |
| 715 |
|
| 716 |
if (winner == GAME_IS_X_WIN) |
| 717 |
{ |
| 718 |
player1_status++; |
| 719 |
show_game (); |
| 720 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 721 |
printf (YOU_WIN); |
| 722 |
} |
| 723 |
else if (winner == GAME_IS_O_WIN) |
| 724 |
{ |
| 725 |
computer_status++; |
| 726 |
show_game (); |
| 727 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 728 |
printf (COMPUTER_WINS); |
| 729 |
} |
| 730 |
else /* Draw */ |
| 731 |
show_drawed_game (); |
| 732 |
|
| 733 |
nettoe_term_set_default_color (); |
| 734 |
nettoe_term_reset_color(); |
| 735 |
printf (WANT_TO_PLAY); |
| 736 |
if (scanf ("%11s", y_n) != 1) |
| 737 |
y_n[0] = 'n', y_n[1] = '\0'; |
| 738 |
getchar(); |
| 739 |
nettoe_term_set_default_color (); |
| 740 |
|
| 741 |
if ( *y_n == 'y' || *y_n == 'Y' ) |
| 742 |
do_one_player_game (); |
| 743 |
else if ( *y_n == 'n' || *y_n == 'N' ) |
| 744 |
return; |
| 745 |
else |
| 746 |
{ |
| 747 |
nettoe_term_reset_color(); |
| 748 |
printf (UNKNOWN_ANSWER); |
| 749 |
nettoe_term_set_default_color (); |
| 750 |
sleep ( 2 ); |
| 751 |
do_one_player_game (); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
static void do_two_player_game (void) |
| 756 |
{ |
| 757 |
char y_n[12]; |
| 758 |
winner = GAME_IS_ALIVE; |
| 759 |
game_mode = 2; |
| 760 |
init_matrix (); |
| 761 |
|
| 762 |
do |
| 763 |
{ |
| 764 |
show_game (); |
| 765 |
|
| 766 |
if (who_starts == 1) |
| 767 |
get_player1_move (); |
| 768 |
else |
| 769 |
get_player2_move (); |
| 770 |
|
| 771 |
winner = game_check (); |
| 772 |
if ( winner != GAME_IS_ALIVE ) |
| 773 |
break; |
| 774 |
|
| 775 |
show_game (); |
| 776 |
|
| 777 |
if (who_starts == 1) |
| 778 |
get_player2_move (); |
| 779 |
else |
| 780 |
get_player1_move (); |
| 781 |
|
| 782 |
winner = game_check (); |
| 783 |
if ( winner != GAME_IS_ALIVE ) |
| 784 |
break; |
| 785 |
} |
| 786 |
while (winner == GAME_IS_ALIVE); |
| 787 |
|
| 788 |
/* Switch roles of players for next round. |
| 789 |
* An easy arithmetic trick. |
| 790 |
*/ |
| 791 |
who_starts = (1 + 2) - who_starts; |
| 792 |
|
| 793 |
if (winner == GAME_IS_X_WIN || winner == GAME_IS_O_WIN) |
| 794 |
{ |
| 795 |
if (winner == GAME_IS_X_WIN) |
| 796 |
player1_status++; |
| 797 |
else |
| 798 |
player2_status++; |
| 799 |
|
| 800 |
show_game (); |
| 801 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 802 |
|
| 803 |
printf ("\n %s", (winner == GAME_IS_X_WIN) ? player1_name |
| 804 |
: player2_name); |
| 805 |
nettoe_term_reset_color(); |
| 806 |
printf (" wins !\n"); |
| 807 |
} |
| 808 |
else /* Draw */ |
| 809 |
show_drawed_game (); |
| 810 |
|
| 811 |
printf (WANT_TO_PLAY); |
| 812 |
if (scanf ("%11s", y_n) != 1) |
| 813 |
y_n[0] = 'n', y_n[1] = '\0'; |
| 814 |
getchar(); |
| 815 |
|
| 816 |
if ( *y_n == 'y' || *y_n == 'Y' ) |
| 817 |
do_two_player_game (); |
| 818 |
else if ( *y_n == 'n' || *y_n == 'N' ) |
| 819 |
return; |
| 820 |
else |
| 821 |
{ |
| 822 |
printf (UNKNOWN_ANSWER); |
| 823 |
sleep ( 2 ); |
| 824 |
do_two_player_game (); |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
static void show_board_section (char p1, char p2, char p3, |
| 829 |
char *n1, char *n2, char *n3) |
| 830 |
{ |
| 831 |
printf (" %c | %c | %c |", p1, p2, p3); |
| 832 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 833 |
printf (" %.2s", n1); |
| 834 |
nettoe_term_reset_color(); |
| 835 |
printf (" |"); |
| 836 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 837 |
printf (" %.2s", n2); |
| 838 |
nettoe_term_reset_color(); |
| 839 |
printf (" |"); |
| 840 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 841 |
printf (" %.2s \n", n3); |
| 842 |
nettoe_term_reset_color(); |
| 843 |
} |
| 844 |
|
| 845 |
static void show_game (void) |
| 846 |
{ |
| 847 |
print_header (1); |
| 848 |
|
| 849 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 850 |
printf (" Current game"); |
| 851 |
nettoe_term_reset_color(); |
| 852 |
printf (" | "); |
| 853 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 854 |
printf ("Coordinate matrix\n"); |
| 855 |
nettoe_term_reset_color(); |
| 856 |
|
| 857 |
printf (SPACES_AND_BARS); |
| 858 |
show_board_section (c11, c12, c13, "a1", "a2", "a3"); |
| 859 |
printf (BLOCKERS_AND_BARS); |
| 860 |
show_board_section (c21, c22, c23, "b1", "b2", "b3"); |
| 861 |
printf (BLOCKERS_AND_BARS); |
| 862 |
show_board_section (c31, c32, c33, "c1", "c2", "c3"); |
| 863 |
printf (SPACES_AND_BARS); |
| 864 |
nettoe_term_set_default_color(); |
| 865 |
|
| 866 |
nettoe_term_reset_color(); |
| 867 |
printf (BROKEN_DASHED_LINE); |
| 868 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 869 |
printf (" Score:"); |
| 870 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 871 |
printf (" %s", player1_name); |
| 872 |
nettoe_term_reset_color(); |
| 873 |
printf (" %d,", player1_status); |
| 874 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 875 |
printf (" %s", (game_mode <= 1) ? "Computer" : player2_name); |
| 876 |
nettoe_term_reset_color(); |
| 877 |
printf (" %d.\n", (game_mode <= 1) ? computer_status : player2_status); |
| 878 |
printf (SINGLE_DASHED_LINE); |
| 879 |
nettoe_term_set_default_color(); |
| 880 |
} |
| 881 |
|
| 882 |
static void show_drawed_game () |
| 883 |
{ |
| 884 |
show_game (); |
| 885 |
printf (IS_A_DRAW); |
| 886 |
} |
| 887 |
|
| 888 |
static void show_waiting_for_move (const char *name) |
| 889 |
{ |
| 890 |
show_game (); |
| 891 |
printf("\n Waiting for "); |
| 892 |
nettoe_term_set_color(COLOR_BLUE, ATTRIB_BRIGHT); |
| 893 |
printf("%s", name); |
| 894 |
nettoe_term_reset_color(); |
| 895 |
printf(" to move ... "); |
| 896 |
fflush(stdout); |
| 897 |
} |
| 898 |
|
| 899 |
static void get_player_move (const char *name, char pawn) |
| 900 |
{ |
| 901 |
char move[4]; |
| 902 |
|
| 903 |
nettoe_beep (); |
| 904 |
|
| 905 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 906 |
printf("\n %s", name); |
| 907 |
nettoe_term_reset_color(); |
| 908 |
printf(", playing %c, it is your turn: ", pawn); |
| 909 |
|
| 910 |
while (1) |
| 911 |
{ |
| 912 |
if (scanf ("%3s", move) != 1) |
| 913 |
move[0] = '\0'; |
| 914 |
|
| 915 |
if (attempt_move (move, pawn) == VALID_MOVE) |
| 916 |
break; |
| 917 |
|
| 918 |
fflush(stdin); |
| 919 |
printf(" Invalid move. Try again: "); |
| 920 |
} /* while(1) */ |
| 921 |
} |
| 922 |
|
| 923 |
static void get_player1_move (void) |
| 924 |
{ |
| 925 |
get_player_move (player1_name, 'X'); |
| 926 |
} |
| 927 |
|
| 928 |
static void get_player2_move (void) |
| 929 |
{ |
| 930 |
get_player_move (player2_name, 'O'); |
| 931 |
} |
| 932 |
|
| 933 |
static void server_start (void) |
| 934 |
{ |
| 935 |
char local_ip_address[INET6_ADDRSTRLEN]; |
| 936 |
char peer_ip_address[INET6_ADDRSTRLEN]; |
| 937 |
|
| 938 |
print_header (0); |
| 939 |
|
| 940 |
nettoe_term_reset_color(); |
| 941 |
printf ("\n [ "); |
| 942 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 943 |
printf ("Host the game"); |
| 944 |
nettoe_term_reset_color(); |
| 945 |
printf (" ]\n\n"); |
| 946 |
|
| 947 |
printf (" Player name: "); |
| 948 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 949 |
get_player_pname(player1_name, MAX_PNAME_LEN); |
| 950 |
nettoe_term_reset_color(); |
| 951 |
local_ip_address[0] = '\0'; |
| 952 |
give_local_IP (local_ip_address, sizeof(local_ip_address)); |
| 953 |
|
| 954 |
printf ("\n You should now tell your IP address to any interested\n"); |
| 955 |
printf (" competitor. There might be more than one address, but\n"); |
| 956 |
printf (" \"%s\" should do.\n", local_ip_address); |
| 957 |
printf ("\n Connected players:\n"); |
| 958 |
printf (" ------------------\n"); |
| 959 |
printf (" -"); |
| 960 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 961 |
printf (" %s", player1_name); |
| 962 |
nettoe_term_reset_color(); |
| 963 |
printf (" ("); |
| 964 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 965 |
printf ("you"); |
| 966 |
nettoe_term_reset_color(); |
| 967 |
printf (")\n"); |
| 968 |
|
| 969 |
sock = establish_listening_socket (SERVER_PORT_NUMBER, peer_ip_address, |
| 970 |
sizeof(peer_ip_address)); |
| 971 |
|
| 972 |
if (sock < 0) |
| 973 |
{ |
| 974 |
printf (" (A carrage return continues. )\b\b\b"); |
| 975 |
fflush (stdin); |
| 976 |
getchar(); |
| 977 |
return; |
| 978 |
} |
| 979 |
|
| 980 |
write_to_socket (sock, player1_name, sizeof(player1_name)); |
| 981 |
|
| 982 |
sleep ( 2 ); |
| 983 |
|
| 984 |
read_from_socket (sock, player2_name, sizeof(player2_name)); |
| 985 |
nettoe_term_reset_color(); |
| 986 |
printf (" -"); |
| 987 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 988 |
printf (" %s", player2_name); |
| 989 |
nettoe_term_reset_color(); |
| 990 |
printf (" ("); |
| 991 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 992 |
printf ("your opponent"); |
| 993 |
nettoe_term_reset_color(); |
| 994 |
printf (")\n"); |
| 995 |
|
| 996 |
printf ("\n Two players are present. Let us play !\n"); |
| 997 |
sleep ( 3 ); |
| 998 |
|
| 999 |
printf ("\n Who will start first ? "); |
| 1000 |
|
| 1001 |
who_starts = who_start_first (); |
| 1002 |
|
| 1003 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 1004 |
|
| 1005 |
if (who_starts == 1) |
| 1006 |
{ |
| 1007 |
printf ("%s", player1_name); |
| 1008 |
write_to_socket (sock, player1_name, sizeof(player1_name)); |
| 1009 |
} |
| 1010 |
else |
| 1011 |
{ |
| 1012 |
printf ("%s", player2_name); |
| 1013 |
write_to_socket (sock, player2_name, sizeof(player2_name)); |
| 1014 |
} |
| 1015 |
|
| 1016 |
nettoe_term_reset_color(); |
| 1017 |
printf (" will begin !\n"); |
| 1018 |
printf ("\n Starting game ... "); |
| 1019 |
fflush (stdout); |
| 1020 |
|
| 1021 |
nettoe_term_set_default_color (); |
| 1022 |
|
| 1023 |
sleep ( 2 ); |
| 1024 |
|
| 1025 |
if ( ! has_given_eof ) |
| 1026 |
init_server_network_game (); |
| 1027 |
|
| 1028 |
return; |
| 1029 |
} |
| 1030 |
|
| 1031 |
static void client_start (void) |
| 1032 |
{ |
| 1033 |
char buf[MAXDATASIZE]; |
| 1034 |
char host_ip_number[INET6_ADDRSTRLEN + 1]; |
| 1035 |
|
| 1036 |
print_header (0); |
| 1037 |
|
| 1038 |
nettoe_term_reset_color(); |
| 1039 |
printf ("\n [ "); |
| 1040 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 1041 |
printf ("Connect to host"); |
| 1042 |
nettoe_term_reset_color(); |
| 1043 |
printf (" ]\n\n"); |
| 1044 |
printf (" Player name: "); |
| 1045 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 1046 |
get_player_pname(player2_name, MAX_PNAME_LEN); |
| 1047 |
nettoe_term_reset_color(); |
| 1048 |
printf ("\n Host to connect to (IP or hostname): "); |
| 1049 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 1050 |
if (scanf (" %46s", host_ip_number) != 1) |
| 1051 |
host_ip_number[0] = '\0'; |
| 1052 |
nettoe_term_reset_color(); |
| 1053 |
printf ("\n Connecting ... "); |
| 1054 |
|
| 1055 |
sock = connect_to_socket (host_ip_number, SERVER_PORT_NUMBER); |
| 1056 |
|
| 1057 |
if (sock < 0) |
| 1058 |
{ |
| 1059 |
printf (" (A carrage return continues. )\b\b\b"); |
| 1060 |
fflush (stdin); |
| 1061 |
getchar(); |
| 1062 |
return; |
| 1063 |
} |
| 1064 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 1065 |
printf ("connected !\n\n"); |
| 1066 |
nettoe_term_reset_color(); |
| 1067 |
printf (" The players are:\n\n"); |
| 1068 |
read_from_socket (sock, player1_name, sizeof(player1_name)); |
| 1069 |
|
| 1070 |
printf (" -"); |
| 1071 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 1072 |
printf (" %s", player1_name); |
| 1073 |
nettoe_term_reset_color(); |
| 1074 |
printf (" ("); |
| 1075 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 1076 |
printf ("your opponent"); |
| 1077 |
nettoe_term_reset_color(); |
| 1078 |
printf (")\n"); |
| 1079 |
|
| 1080 |
printf (" -"); |
| 1081 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 1082 |
printf (" %s", player2_name); |
| 1083 |
nettoe_term_reset_color(); |
| 1084 |
printf (" ("); |
| 1085 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 1086 |
printf ("you"); |
| 1087 |
nettoe_term_reset_color(); |
| 1088 |
printf (")\n"); |
| 1089 |
|
| 1090 |
write_to_socket (sock, player2_name, sizeof(player2_name)); |
| 1091 |
|
| 1092 |
printf ("\n Two players are present. Let us play !\n"); |
| 1093 |
sleep ( 3 ); |
| 1094 |
|
| 1095 |
printf ("\n Who will start first ? "); |
| 1096 |
|
| 1097 |
if ( ! has_given_eof ) |
| 1098 |
{ |
| 1099 |
read_from_socket (sock, buf, MAX_PNAME_LEN); |
| 1100 |
|
| 1101 |
if ( has_given_eof ) |
| 1102 |
return; |
| 1103 |
|
| 1104 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 1105 |
printf ("%s", buf); |
| 1106 |
nettoe_term_reset_color(); |
| 1107 |
printf (" will begin !\n"); |
| 1108 |
printf ("\n Starting game ... "); |
| 1109 |
fflush (stdout); |
| 1110 |
nettoe_term_set_default_color (); |
| 1111 |
sleep ( 3 ); |
| 1112 |
|
| 1113 |
init_client_network_game (); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
static void recv_remote_play (int sd, int dir, const char *name) |
| 1118 |
{ |
| 1119 |
char buf[MAXDATASIZE]; |
| 1120 |
|
| 1121 |
show_waiting_for_move (name); |
| 1122 |
|
| 1123 |
buf[0] = 'n'; |
| 1124 |
while ( strncmp (buf, "y", 2) && ! has_given_eof ) |
| 1125 |
read_from_socket (sd, buf, 2); |
| 1126 |
|
| 1127 |
recv_matrix (sd); |
| 1128 |
|
| 1129 |
buf[0] = 'n'; |
| 1130 |
while ( strncmp (buf, (dir == ASK_SERVER) ? "S" : "C", 2) |
| 1131 |
&& ! has_given_eof ) |
| 1132 |
read_from_socket (sd, buf, 2); |
| 1133 |
} |
| 1134 |
|
| 1135 |
static void send_local_play (int sd, int dir) |
| 1136 |
{ |
| 1137 |
show_game (); |
| 1138 |
if (dir == I_AM_SERVER) |
| 1139 |
get_player1_move (); |
| 1140 |
else |
| 1141 |
get_player2_move (); |
| 1142 |
write_to_socket (sd, "y", 2); |
| 1143 |
send_matrix (sd); |
| 1144 |
write_to_socket (sd, (dir == I_AM_SERVER) ? "S" : "C", 2); |
| 1145 |
} |
| 1146 |
|
| 1147 |
static void init_server_network_game (void) |
| 1148 |
{ |
| 1149 |
char y_n[12]; |
| 1150 |
char yes_no[2]; |
| 1151 |
char buf[MAXDATASIZE]; |
| 1152 |
|
| 1153 |
/* Player 1 is local player. */ |
| 1154 |
|
| 1155 |
game_mode = 3; |
| 1156 |
|
| 1157 |
init_matrix (); |
| 1158 |
|
| 1159 |
write_to_socket (sock, (who_starts == 1) ? "1" : "2", 1); |
| 1160 |
|
| 1161 |
do |
| 1162 |
{ |
| 1163 |
if (who_starts == 1) |
| 1164 |
send_local_play (sock, I_AM_SERVER); |
| 1165 |
else |
| 1166 |
{ |
| 1167 |
recv_remote_play (sock, ASK_CLIENT, player2_name); |
| 1168 |
|
| 1169 |
if ( has_given_eof ) |
| 1170 |
break; |
| 1171 |
} |
| 1172 |
|
| 1173 |
winner = game_check (); |
| 1174 |
if ( winner != GAME_IS_ALIVE ) |
| 1175 |
break; |
| 1176 |
|
| 1177 |
if (who_starts == 1) |
| 1178 |
{ |
| 1179 |
recv_remote_play (sock, ASK_CLIENT, player2_name); |
| 1180 |
|
| 1181 |
if ( has_given_eof ) |
| 1182 |
break; |
| 1183 |
} |
| 1184 |
else |
| 1185 |
send_local_play (sock, I_AM_SERVER); |
| 1186 |
|
| 1187 |
winner = game_check (); |
| 1188 |
if ( winner != GAME_IS_ALIVE ) |
| 1189 |
break; |
| 1190 |
} |
| 1191 |
while (winner == GAME_IS_ALIVE && ! has_given_eof); |
| 1192 |
|
| 1193 |
/* Switch roles of players for next round. |
| 1194 |
* An easy arithmetic trick. |
| 1195 |
*/ |
| 1196 |
who_starts = (1 + 2) - who_starts; |
| 1197 |
|
| 1198 |
if (winner == GAME_IS_X_WIN) |
| 1199 |
{ |
| 1200 |
player1_status++; |
| 1201 |
show_game (); |
| 1202 |
printf (YOU_WIN); |
| 1203 |
} |
| 1204 |
else if (winner == GAME_IS_O_WIN) |
| 1205 |
{ |
| 1206 |
player2_status++; |
| 1207 |
show_game (); |
| 1208 |
nettoe_term_set_color(COLOR_BLUE, ATTRIB_BRIGHT); |
| 1209 |
printf("\n %s", player2_name); |
| 1210 |
nettoe_term_reset_color(); |
| 1211 |
printf (" wins !\n"); |
| 1212 |
} |
| 1213 |
else if (winner == GAME_IS_DRAW) /* Draw */ |
| 1214 |
show_drawed_game (); |
| 1215 |
else /* EOF */ |
| 1216 |
return; |
| 1217 |
|
| 1218 |
winner = GAME_IS_ALIVE; |
| 1219 |
|
| 1220 |
nettoe_beep(); |
| 1221 |
printf (WANT_TO_PLAY); |
| 1222 |
if (scanf ("%11s", y_n) != 1) |
| 1223 |
y_n[0] = 'n', y_n[1] = '\0'; |
| 1224 |
getchar(); |
| 1225 |
|
| 1226 |
if ( *y_n == 'n' || *y_n == 'N' ) |
| 1227 |
{ |
| 1228 |
write_to_socket (sock, "y", 2); |
| 1229 |
write_to_socket (sock, "n", 2); |
| 1230 |
sleep ( 3 ); |
| 1231 |
close (sock); |
| 1232 |
} |
| 1233 |
else |
| 1234 |
{ |
| 1235 |
if ( *y_n != 'y' && *y_n != 'Y' ) |
| 1236 |
{ |
| 1237 |
printf (UNKNOWN_ANSWER); |
| 1238 |
sleep ( 2 ); |
| 1239 |
} |
| 1240 |
write_to_socket (sock, "y", 2); |
| 1241 |
write_to_socket (sock, "y", 2); |
| 1242 |
printf (WAITING_FOR_NAMED, player2_name); |
| 1243 |
|
| 1244 |
buf[0] = 'n'; |
| 1245 |
while ( strncmp (buf, "y", 2) && ! has_given_eof ) |
| 1246 |
read_from_socket (sock, buf, 2); |
| 1247 |
|
| 1248 |
read_from_socket (sock, yes_no, 2); |
| 1249 |
|
| 1250 |
if (!has_given_eof |
| 1251 |
&& (!strncmp (yes_no, "y", 2) || !strncmp (yes_no, "Y", 2))) |
| 1252 |
{ |
| 1253 |
printf ("\n %s wants to play again.\n", player2_name); |
| 1254 |
printf (" Starting ... "); |
| 1255 |
fflush (stdout); |
| 1256 |
sleep ( 4 ); |
| 1257 |
init_server_network_game (); |
| 1258 |
} |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
static void init_client_network_game (void) |
| 1263 |
{ |
| 1264 |
char y_n[12]; |
| 1265 |
char yes_no[2]; |
| 1266 |
char buf[MAXDATASIZE]; |
| 1267 |
|
| 1268 |
/* Player 2 is local player. */ |
| 1269 |
|
| 1270 |
game_mode = 3; |
| 1271 |
init_matrix (); |
| 1272 |
|
| 1273 |
read_from_socket (sock, buf, 1); |
| 1274 |
|
| 1275 |
who_starts = strncmp (buf, "1", 1) ? 2 : 1; |
| 1276 |
|
| 1277 |
do |
| 1278 |
{ |
| 1279 |
/* Player 2 is local player. */ |
| 1280 |
if (who_starts == 1) |
| 1281 |
{ |
| 1282 |
recv_remote_play (sock, ASK_SERVER, player1_name); |
| 1283 |
|
| 1284 |
if ( has_given_eof ) |
| 1285 |
break; |
| 1286 |
} |
| 1287 |
else |
| 1288 |
send_local_play (sock, I_AM_CLIENT); |
| 1289 |
|
| 1290 |
winner = game_check (); |
| 1291 |
if ( winner != GAME_IS_ALIVE ) |
| 1292 |
break; |
| 1293 |
|
| 1294 |
if (who_starts == 1) |
| 1295 |
send_local_play (sock, I_AM_CLIENT); |
| 1296 |
else |
| 1297 |
{ |
| 1298 |
recv_remote_play (sock, ASK_SERVER, player1_name); |
| 1299 |
|
| 1300 |
if ( has_given_eof ) |
| 1301 |
break; |
| 1302 |
} |
| 1303 |
|
| 1304 |
winner = game_check (); |
| 1305 |
if ( winner != GAME_IS_ALIVE ) |
| 1306 |
break; |
| 1307 |
} |
| 1308 |
while (winner == GAME_IS_ALIVE && ! has_given_eof); |
| 1309 |
|
| 1310 |
/* Switch roles of players for next round. |
| 1311 |
* An easy arithmetic trick. |
| 1312 |
*/ |
| 1313 |
who_starts = (1 + 2) - who_starts; |
| 1314 |
|
| 1315 |
if (winner == GAME_IS_X_WIN) |
| 1316 |
{ |
| 1317 |
player1_status++; |
| 1318 |
show_game (); |
| 1319 |
nettoe_term_set_color(COLOR_BLUE, ATTRIB_BRIGHT); |
| 1320 |
printf("\n %s", player1_name); |
| 1321 |
nettoe_term_reset_color(); |
| 1322 |
printf (" wins !\n"); |
| 1323 |
} |
| 1324 |
else if (winner == GAME_IS_O_WIN) |
| 1325 |
{ |
| 1326 |
player2_status++; |
| 1327 |
show_game (); |
| 1328 |
printf (YOU_WIN); |
| 1329 |
} |
| 1330 |
else if (winner == GAME_IS_DRAW) /* Draw */ |
| 1331 |
show_drawed_game (); |
| 1332 |
else /* EOF */ |
| 1333 |
return; |
| 1334 |
|
| 1335 |
winner = GAME_IS_ALIVE; |
| 1336 |
printf (WAITING_FOR_NAMED, player1_name); |
| 1337 |
|
| 1338 |
buf[0] = 'n'; |
| 1339 |
while ( strncmp (buf, "y", 2) && ! has_given_eof ) |
| 1340 |
read_from_socket (sock, buf, 2); |
| 1341 |
|
| 1342 |
read_from_socket (sock, yes_no, 2); |
| 1343 |
|
| 1344 |
if (has_given_eof) |
| 1345 |
return; |
| 1346 |
|
| 1347 |
if (!strncmp (yes_no, "y", 2) || !strncmp (yes_no, "Y", 2)) |
| 1348 |
{ |
| 1349 |
printf ("\n %s wants to play again. What about you ?\n", player1_name); |
| 1350 |
nettoe_beep(); |
| 1351 |
printf (WANT_TO_PLAY); |
| 1352 |
if (scanf ("%11s", y_n) != 1) |
| 1353 |
y_n[0] = 'n', y_n[1] = '\0'; |
| 1354 |
getchar(); |
| 1355 |
|
| 1356 |
if ( *y_n == 'n' || *y_n == 'N' ) |
| 1357 |
{ |
| 1358 |
write_to_socket (sock, "y", 2); |
| 1359 |
write_to_socket (sock, "n", 2); |
| 1360 |
printf ("Ending session ...\n"); |
| 1361 |
close (sock); |
| 1362 |
} |
| 1363 |
else |
| 1364 |
{ |
| 1365 |
if ( *y_n != 'y' && *y_n != 'Y' ) |
| 1366 |
{ |
| 1367 |
printf (UNKNOWN_ANSWER); |
| 1368 |
sleep ( 2 ); |
| 1369 |
} |
| 1370 |
write_to_socket (sock, "y", 2); |
| 1371 |
write_to_socket (sock, "y", 2); |
| 1372 |
printf (" Starting ... "); |
| 1373 |
fflush (stdout); |
| 1374 |
sleep ( 4 ); |
| 1375 |
init_client_network_game (); |
| 1376 |
} |
| 1377 |
} |
| 1378 |
else |
| 1379 |
{ |
| 1380 |
printf ("\n %s doesn't want to play again. Sorry.\n", player1_name); |
| 1381 |
sleep ( 3 ); |
| 1382 |
close (sock); |
| 1383 |
return; |
| 1384 |
} |
| 1385 |
} |
| 1386 |
|
| 1387 |
static void send_matrix (int fd ) |
| 1388 |
{ |
| 1389 |
write_to_socket (fd, &c11, 1); |
| 1390 |
write_to_socket (fd, &c12, 1); |
| 1391 |
write_to_socket (fd, &c13, 1); |
| 1392 |
write_to_socket (fd, &c21, 1); |
| 1393 |
write_to_socket (fd, &c22, 1); |
| 1394 |
write_to_socket (fd, &c23, 1); |
| 1395 |
write_to_socket (fd, &c31, 1); |
| 1396 |
write_to_socket (fd, &c32, 1); |
| 1397 |
write_to_socket (fd, &c33, 1); |
| 1398 |
} |
| 1399 |
|
| 1400 |
static void recv_matrix ( int fd ) |
| 1401 |
{ |
| 1402 |
read_from_socket (fd, &c11, 1); |
| 1403 |
read_from_socket (fd, &c12, 1); |
| 1404 |
read_from_socket (fd, &c13, 1); |
| 1405 |
read_from_socket (fd, &c21, 1); |
| 1406 |
read_from_socket (fd, &c22, 1); |
| 1407 |
read_from_socket (fd, &c23, 1); |
| 1408 |
read_from_socket (fd, &c31, 1); |
| 1409 |
read_from_socket (fd, &c32, 1); |
| 1410 |
read_from_socket (fd, &c33, 1); |
| 1411 |
} |
| 1412 |
|
| 1413 |
static void quit_game (void) |
| 1414 |
{ |
| 1415 |
nettoe_term_reset_color(); |
| 1416 |
|
| 1417 |
printf("\n Goodbye !\n\n"); |
| 1418 |
|
| 1419 |
printf("netToe Copyright 2000,2001 Gabriele Giorgetti\n"); |
| 1420 |
printf(" Copyright 2009-2014 Mats Erik Andersson\n"); |
| 1421 |
printf("netToe project homepage: %s\n\n", HOMEPAGE); |
| 1422 |
|
| 1423 |
close (sock); |
| 1424 |
|
| 1425 |
exit (EXIT_SUCCESS); |
| 1426 |
} |