src/main.c

Tue, 13 Aug 2019 00:33:59 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 13 Aug 2019 00:33:59 +0200
changeset 70
5427beba96d1
parent 69
c8f2c280cff7
permissions
-rw-r--r--

pgn parser can now handle comments (although it ignores them for now)

0
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
55
54ea19938d57 updated copyright and version info
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
4 * Copyright 2016 Mike Becker. All rights reserved.
0
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
30 #include "terminal-chess.h"
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
31 #include "game.h"
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
32 #include "input.h"
35
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
33 #include "colors.h"
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
34 #include <string.h>
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
35 #include <time.h>
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
36 #include <getopt.h>
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
37 #include <locale.h>
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
38
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
39 int get_settings(int argc, char **argv, Settings *settings) {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
40 char *valid;
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
41 unsigned long int time, port;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
42 uint8_t timeunit = 60;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
43 size_t len;
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
44
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
45 for (int opt ; (opt = getopt(argc, argv, "a:bc:hp:rsS:t:Uv")) != -1 ;) {
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
46 switch (opt) {
47
d726e4b46c33 refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
47 case 'c':
d726e4b46c33 refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
48 settings->continuepgn = optarg;
d726e4b46c33 refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
49 break;
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
50 case 'b':
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
51 settings->gameinfo.servercolor = BLACK;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
52 break;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
53 case 'r':
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
54 settings->gameinfo.servercolor = rand() & 1 ? WHITE : BLACK;
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
55 break;
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
56 case 's':
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
57 settings->singlemachine = 1;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
58 break;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
59 case 'S':
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
60 settings->analyzepgn = optarg;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
61 break;
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
62 case 'U':
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
63 settings->unicode = 0;
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
64 break;
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
65 case 't':
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
66 case 'a':
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
67 len = strlen(optarg);
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
68 if (optarg[len-1] == 's') {
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
69 optarg[len-1] = '\0';
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
70 timeunit = 1;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
71 }
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
72
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
73 if ((time = strtoul(optarg, &valid, 10))*timeunit > UINT16_MAX
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
74 || *valid != '\0') {
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
75 fprintf(stderr, "Specified time is invalid (%s)"
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
76 "- Maximum: 65535 seconds (1092 minutes)\n", optarg);
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
77 return 1;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
78 } else {
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
79 settings->gameinfo.timecontrol = 1;
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
80 if (opt=='t') {
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
81 settings->gameinfo.time = timeunit * time;
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
82 } else {
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
83 settings->gameinfo.addtime = time;
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
84 }
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
85 }
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
86 break;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
87 case 'p':
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
88 port = strtol(optarg, &valid, 10);
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
89 if (port < 1025 || port > 65535 || *valid != '\0') {
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
90 fprintf(stderr,
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
91 "Invalid port number (%s) - choose a number between "
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
92 "1025 and 65535\n",
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
93 optarg);
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
94 return 1;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
95 } else {
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
96 settings->port = optarg;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
97 }
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
98 break;
52
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
99 case 'v':
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
100 printf("terminal-chess : Version %s (Netcode Version %d)\n",
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
101 PROGRAM_VERSION, NETCODE_VERSION);
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
102 exit(0);
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
103 case 'h':
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
104 case '?':
52
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
105 printf(
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
106 "Usage: terminal-chess [OPTION]... [HOST]\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
107 "Starts/joins a network chess game\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
108 "\nGeneral options\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
109 " -c <PGN file> Continue the specified game\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
110 " -h This help page\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
111 " -p TCP port to use (default: 27015)\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
112 // TODO: implement and activate feature
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
113 //" -S <PGN file> Compute and print statistics for the specified game\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
114 " -v Print version information and exists\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
115 "\nServer options\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
116 " -a <time> Specifies the time to add after each move\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
117 " -b Server plays black pieces (default: white)\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
118 " -r Distribute color randomly\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
119 " -s Single machine mode\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
120 " -t <time> Specifies time limit (default: no limit)\n"
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
121 " -U Disables unicode pieces\n"
52
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
122 "\nNotes\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
123 "The time unit for -a is seconds and for -t minutes by default. To "
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
124 "specify\nseconds for the -t option, use the s suffix.\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
125 "Example: -t 150s\n\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
126 "Use '-' for PGN files to read PGN data from standard input\n"
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
127 );
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
128 exit(0);
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
129 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
130 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
131
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
132 if (optind == argc - 1) {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
133 settings->serverhost = argv[optind];
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
134 } else if (optind < argc - 1) {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
135 fprintf(stderr, "Too many arguments\n");
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
136 return 1;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
137 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
138
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
139
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
140 if (settings->continuepgn) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
141 if (settings->serverhost) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
142 fprintf(stderr, "Can't continue a game when joining a server.\n");
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
143 return 1;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
144 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
145 if (settings->analyzepgn) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
146 fprintf(stderr, "The options -c and -S are mutually exclusive\n");
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
147 return 1;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
148 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
149 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
150
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
151 return 0;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
152 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
153
55
54ea19938d57 updated copyright and version info
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
154 static Settings default_settings() {
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
155 Settings settings;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
156 memset(&settings, 0, sizeof(Settings));
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
157 settings.gameinfo.servercolor = WHITE;
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
158 settings.port = "27015";
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
159 settings.unicode = !!setlocale(LC_CTYPE, "C.UTF-8");
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
160 return settings;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
161 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
162
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
163 void dump_gameinfo(GameInfo *gameinfo) {
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
164 int serverwhite = gameinfo->servercolor == WHITE;
3
3693fd2eb0e9 changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
165 attron(A_UNDERLINE);
3693fd2eb0e9 changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
166 printw("Game details\n");
3693fd2eb0e9 changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
167 attroff(A_UNDERLINE);
3693fd2eb0e9 changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
168 printw(" Server: %s\n Client: %s\n",
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
169 serverwhite?"White":"Black", serverwhite?"Black":"White"
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
170 );
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
171 if (gameinfo->timecontrol) {
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
172 if (gameinfo->time % 60) {
3
3693fd2eb0e9 changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
173 printw(" Time limit: %ds + %ds\n",
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
174 gameinfo->time, gameinfo->addtime);
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
175 } else {
3
3693fd2eb0e9 changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
176 printw(" Time limit: %dm + %ds\n",
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
177 gameinfo->time/60, gameinfo->addtime);
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
178 }
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
179 } else {
3
3693fd2eb0e9 changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
180 printw(" No time limit\n");
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
181 }
3
3693fd2eb0e9 changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
182 refresh();
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
183 }
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
184
51
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
185 void dump_moveinfo(GameState *gamestate) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
186 int i = 1;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
187 for (MoveList *movelist = gamestate->movelist ;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
188 movelist ; movelist = movelist->next) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
189 if (++i % 2 == 0) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
190 printw("%d. %s", i/2, movelist->move.string);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
191 } else {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
192 printw(" %s", movelist->move.string);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
193 }
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
194 if (i % 20) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
195 addch(' ');
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
196 } else {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
197 addch('\n');
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
198 }
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
199 }
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
200 refresh();
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
201 }
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
202
0
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 int main(int argc, char **argv) {
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
204 srand(time(NULL));
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
205
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
206 Settings settings = default_settings();
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
207 if (get_settings(argc, argv, &settings)) {
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
208 return 1;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
209 }
52
26707039d5a6 added program version
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
210
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
211 initscr();
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
212 halfdelay(1);
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
213 keypad(stdscr, TRUE);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
214 if (has_colors()) {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
215 start_color();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
216 init_colorpairs();
35
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
217 bkgd(COLOR_PAIR(COL_APP));
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
218 } else {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
219 fprintf(stderr, "Non-colored terminals are not supported yet.");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
220 endwin();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
221 return EXIT_FAILURE;
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
222 }
55
54ea19938d57 updated copyright and version info
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
223 atexit((void(*)())endwin);
3
3693fd2eb0e9 changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
224
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
225 int exitcode;
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
226 if (settings.singlemachine) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
227 game_start_singlemachine(&settings);
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
228 exitcode = EXIT_SUCCESS;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
229 } else if (settings.analyzepgn) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
230 printw("Not implemented yet.\n");
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
231 exitcode = EXIT_SUCCESS;
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
232 } else {
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
233 exitcode = is_server(&settings) ?
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
234 server_run(&settings) : client_run(&settings);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
235 }
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
236
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
237 mvaddstr(getmaxy(stdscr)-1, 0,
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
238 "Game has ended. Press any key to leave...");
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
239 clrtoeol();
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
240 refresh();
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
241 cbreak();
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
242 flushinp();
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
243 getch();
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
244
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
245 return exitcode;
0
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 }
98034084033f init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247

mercurial