src/game.c

Sun, 01 Oct 2023 12:53:35 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 01 Oct 2023 12:53:35 +0200
changeset 71
0bdb910478cc
parent 69
c8f2c280cff7
permissions
-rw-r--r--

add uwproj configure script generator

6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
55
54ea19938d57 updated copyright and version info
Mike Becker <universe@uap-core.de>
parents: 54
diff changeset
4 * Copyright 2016 Mike Becker. All rights reserved.
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
daaf6e5b3501 structured code
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
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "game.h"
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
31 #include "network.h"
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
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"
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
34 #include <ncurses.h>
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
35 #include <string.h>
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
36 #include <inttypes.h>
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
37 #include <sys/select.h>
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
38 #include <stdio.h>
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
39 #include <errno.h>
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
40
63
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
41 static const uint8_t boardx = 4, boardy = 10;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
42 static int inputy = 21; /* should be overridden on game startup */
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
43
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
44 static int timecontrol(GameState *gamestate, GameInfo *gameinfo) {
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
45 if (gameinfo->timecontrol) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
46 uint16_t white = remaining_movetime(gameinfo, gamestate, WHITE);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
47 uint16_t black = remaining_movetime(gameinfo, gamestate, BLACK);
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
48 mvprintw(boardy+4, boardx-1,
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
49 "White time: %4" PRIu16 ":%02" PRIu16,
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
50 white / 60, white % 60);
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
51 mvprintw(boardy+5, boardx-1,
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
52 "Black time: %4" PRIu16 ":%02" PRIu16,
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
53 black / 60, black % 60);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
54
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
55 if (white == 0) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
56 move(inputy, 0);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
57 printw("Time is over - Black wins!");
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
58 clrtobot();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
59 refresh();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
60 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
61 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
62 if (black == 0) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
63 move(inputy, 0);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
64 printw("Time is over - White wins!");
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
65 clrtobot();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
66 refresh();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
67 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
68 }
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
69 }
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
70
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
71 return 0;
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
72 }
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
73
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
74 static void draw_board(GameState *gamestate,
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
75 uint8_t perspective,
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
76 _Bool unicode) {
54
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
77 char fen[90];
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
78 compute_fen(fen, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
79 mvaddstr(0, 0, fen);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
80
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
81 for (uint8_t y = 0 ; y < 8 ; y++) {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
82 for (uint8_t x = 0 ; x < 8 ; x++) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
83 uint8_t col = gamestate->board[y][x] & COLOR_MASK;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
84 uint8_t piece = gamestate->board[y][x] & PIECE_MASK;
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
85 unsigned char piecestr[5];
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
86 if (piece) {
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
87 if (unicode) {
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
88 unsigned char* uc = getpieceunicode(piece);
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
89 strncpy(piecestr, uc, 5);
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
90 } else {
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
91 piecestr[0] = piece == PAWN ? 'P' : getpiecechr(piece);
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
92 piecestr[1] = '\0';
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
93 }
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
94 } else {
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
95 piecestr[0] = ' ';
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
96 piecestr[1] = '\0';
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
97 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
98
35
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
99 _Bool boardblack = (y&1)==(x&1);
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
100 attrset((col==WHITE ? A_BOLD : A_DIM)|
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
101 COLOR_PAIR(col == WHITE ?
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
102 (boardblack ? COL_WB : COL_WW) :
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
103 (boardblack ? COL_BB : COL_BW)
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
104 )
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
105 );
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
106
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
107 int cy = perspective == WHITE ? boardy-y : boardy-7+y;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
108 int cx = perspective == WHITE ? boardx+x*3 : boardx+21-x*3;
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
109 mvprintw(cy, cx, " %s ", piecestr);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
110 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
111 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
112
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
113 attrset(A_NORMAL);
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
114 for (uint8_t i = 0 ; i < 8 ; i++) {
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
115 int x = perspective == WHITE ? boardx+i*3+1 : boardx+22-i*3;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
116 int y = perspective == WHITE ? boardy-i : boardy-7+i;
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
117 mvaddch(boardy+1, x, 'a'+i);
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
118 mvaddch(y, boardx-2, '1'+i);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
119 }
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
120
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
121 /* move log */
63
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
122 uint8_t logy = 2;
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
123 const uint8_t logx = boardx + 28;
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
124 move(logy, logx);
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
125
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
126 unsigned int logi = 0;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
127 MoveList *logelem = gamestate->movelist;
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
128
63
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
129 /* wrap log after 45 moves */
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
130 while (gamestate->movecount/6-logi/3 >= 15) {
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
131 logelem = logelem->next->next;
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
132 logi++;
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
133 }
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
134
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
135 while (logelem) {
63
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
136 _Bool iswhite = (logelem->move.piece & COLOR_MASK) == WHITE;
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
137 if (iswhite) {
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
138 logi++;
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
139 printw("%d. ", logi);
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
140 }
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
141
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
142 addstr(logelem->move.string);
63
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
143 if (!iswhite && logi%3 == 0) {
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
144 move(++logy, logx);
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
145 } else {
611332453da0 move log has now three columns and starts scrolling after 45 moves
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
146 addch(' ');
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
147 }
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
148
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
149 logelem = logelem->next;
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
150 }
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
151 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
152
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
153 static void eval_move_failed_msg(int code) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
154 switch (code) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
155 case AMBIGUOUS_MOVE:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
156 printw("Ambiguous move - please specify the piece to move.");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
157 break;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
158 case INVALID_POSITION:
48
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
159 printw("No piece can be moved this way.");
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
160 break;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
161 case NEED_PROMOTION:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
162 printw("You need to promote the pawn (append \"=Q\" e.g.)!");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
163 break;
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
164 case KING_IN_CHECK:
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
165 printw("Your king is in check!");
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
166 break;
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
167 case PIECE_PINNED:
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
168 printw("This piece is pinned!");
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
169 break;
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
170 case INVALID_MOVE_SYNTAX:
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
171 printw("Can't interpret move - please use algebraic notation.");
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
172 break;
48
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
173 case RULES_VIOLATED:
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
174 printw("Move does not comply chess rules.");
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
175 break;
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
176 case KING_MOVES_INTO_CHECK:
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
177 printw("Can't move the king into a check position.");
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
178 break;
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
179 default:
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
180 printw("Unknown move parser error.");
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
181 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
182 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
183
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
184 static void save_pgn(GameState *gamestate, GameInfo *gameinfo) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
185 printw("Filename: ");
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
186 clrtoeol();
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
187 refresh();
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
188
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
189 char filename[64];
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
190 int y = getcury(stdscr);
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
191 if (getnstr(filename, 64) == OK && filename[0] != '\0') {
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
192 move(y, 0);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
193 FILE *file = fopen(filename, "w");
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
194 if (file) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
195 write_pgn(file, gamestate, gameinfo);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
196 fclose(file);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
197 printw("File saved.");
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
198 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
199 printw("Can't write to file (%s).", strerror(errno));
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
200 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
201 clrtoeol();
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
202 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
203 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
204
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
205 #define MOVESTR_BUFLEN 10
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
206 static int domove_singlemachine(GameState *gamestate,
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
207 GameInfo *gameinfo, uint8_t curcolor) {
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
208
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
209
32
8a0b85303ee8 improved async input + improved build system + added time values to move struct
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
210 size_t bufpos = 0;
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
211 char movestr[MOVESTR_BUFLEN];
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
212
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
213 flushinp();
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
214 while (1) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
215 if (timecontrol(gamestate, gameinfo)) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
216 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
217 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
218
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
219 move(inputy, 0);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
220 printw(
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
221 "Use chess notation to enter your move.\n"
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
222 "Or use a command: remis, resign, savepgn\n\n"
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
223 "Type your move: ");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
224 clrtoeol();
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
225
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
226 if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) {
44
1891d88cbd10 surrender is now resign
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
227 if (strncmp(movestr, "resign", MOVESTR_BUFLEN) == 0) {
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
228 gamestate->resign = 1;
44
1891d88cbd10 surrender is now resign
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
229 printw("%s resigned!",
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
230 curcolor==WHITE?"White":"Black");
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
231 clrtobot();
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
232 refresh();
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
233 return 1;
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
234 } else if (strncmp(movestr, "remis", MOVESTR_BUFLEN) == 0) {
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
235 gamestate->remis = 1;
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
236 printw("Game ends remis.");
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
237 clrtobot();
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
238 refresh();
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
239 return 1;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
240 } else if (strncmp(movestr, "savepgn", MOVESTR_BUFLEN) == 0) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
241 save_pgn(gamestate, gameinfo);
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
242 } else {
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
243 Move move;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
244 int result = eval_move(gamestate, movestr, &move, curcolor);
68
b34de5ce7d0e error message when syntactically validating a King's move into a check position is now correct
Mike Becker <universe@uap-core.de>
parents: 63
diff changeset
245 if (result == VALID_MOVE_SYNTAX) {
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
246 result = validate_move(gamestate, &move);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
247 if (result == VALID_MOVE_SEMANTICS) {
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
248 apply_move(gamestate, &move);
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
249 if (gamestate->checkmate) {
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
250 printw("Checkmate!");
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
251 clrtoeol();
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
252 return 1;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
253 } else if (gamestate->stalemate) {
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
254 printw("Stalemate!");
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
255 clrtoeol();
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
256 return 1;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
257 } else {
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
258 return 0;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
259 }
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
260 } else {
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
261 eval_move_failed_msg(result);
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
262 }
68
b34de5ce7d0e error message when syntactically validating a King's move into a check position is now correct
Mike Becker <universe@uap-core.de>
parents: 63
diff changeset
263 } else {
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
264 eval_move_failed_msg(result);
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
265 }
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
266 clrtoeol();
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
267 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
268 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
269 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
270 }
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
271
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
272 static int sendmove(GameState *gamestate, GameInfo *gameinfo,
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
273 int opponent, uint8_t mycolor) {
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
274
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
275 size_t bufpos = 0;
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
276 char movestr[MOVESTR_BUFLEN];
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
277 _Bool remisrejected = FALSE;
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
278 uint8_t code;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
279
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
280 flushinp();
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
281 while (1) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
282 if (timecontrol(gamestate, gameinfo)) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
283 net_send_code(opponent, NETCODE_TIMEOVER);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
284 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
285 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
286
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
287 move(inputy, 0);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
288 if (remisrejected) {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
289 printw(
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
290 "Use chess notation to enter your move.\n"
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
291 "Remis offer rejected \n\n"
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
292 "Type your move: ");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
293 } else {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
294 printw(
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
295 "Use chess notation to enter your move.\n"
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
296 "Or use a command: remis, resign, savepgn\n\n"
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
297 "Type your move: ");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
298 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
299 clrtoeol();
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
300
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
301 if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) {
44
1891d88cbd10 surrender is now resign
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
302 if (strncmp(movestr, "resign", MOVESTR_BUFLEN) == 0) {
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
303 gamestate->resign = 1;
44
1891d88cbd10 surrender is now resign
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
304 printw("You resigned!");
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
305 clrtoeol();
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
306 refresh();
44
1891d88cbd10 surrender is now resign
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
307 net_send_code(opponent, NETCODE_RESIGN);
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
308 return 1;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
309 } else if (strncmp(movestr, "savepgn", MOVESTR_BUFLEN) == 0) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
310 save_pgn(gamestate, gameinfo);
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
311 } else if (strncmp(movestr, "remis", MOVESTR_BUFLEN) == 0) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
312 if (!remisrejected) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
313 net_send_code(opponent, NETCODE_REMIS);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
314 printw("Remis offer sent - waiting for acceptance...");
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
315 refresh();
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
316 code = net_recieve_code(opponent);
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
317 if (code == NETCODE_ACCEPT) {
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
318 gamestate->remis = 1;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
319 printw("\rRemis accepted!");
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
320 clrtoeol();
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
321 refresh();
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
322 return 1;
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
323 } else if (code == NETCODE_CONNLOST) {
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
324 printw("\rYour opponent left the game.");
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
325 clrtoeol();
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
326 refresh();
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
327 return 1;
14
970748b9a73b fixed crucial bug where both players could move at the same time + added pawn rules (TODO: en passant)
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
328 } else {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
329 remisrejected = TRUE;
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
330 }
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
331 }
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
332 } else {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
333 Move move;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
334 int eval_result = eval_move(gamestate, movestr, &move, mycolor);
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
335 switch (eval_result) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
336 case VALID_MOVE_SYNTAX:
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
337 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
338 code = net_recieve_code(opponent);
48
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
339 move.check = code == NETCODE_CHECK ||
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
340 code == NETCODE_CHECKMATE;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
341 gamestate->checkmate = code == NETCODE_CHECKMATE;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
342 gamestate->stalemate = code == NETCODE_STALEMATE;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
343 if (code == NETCODE_DECLINE) {
48
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
344 uint32_t reason;
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
345 net_recieve_data(opponent, &reason, sizeof(uint32_t));
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
346 reason = ntohl(reason);
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
347 eval_move_failed_msg(reason);
42
21cb830efe91 fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
348 } else if (code == NETCODE_ACCEPT
21cb830efe91 fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
349 || code == NETCODE_CHECK
21cb830efe91 fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
350 || code == NETCODE_CHECKMATE
21cb830efe91 fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
351 || code == NETCODE_STALEMATE) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
352 apply_move(gamestate, &move);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
353 if (gamestate->checkmate) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
354 printw("Checkmate!");
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
355 clrtoeol();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
356 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
357 } else if (gamestate->stalemate) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
358 printw("Stalemate!");
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
359 clrtoeol();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
360 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
361 } else {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
362 return 0;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
363 }
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
364 } else if (code == NETCODE_CONNLOST) {
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
365 printw("Your opponent left the game.");
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
366 return 1;
42
21cb830efe91 fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
367 } else {
21cb830efe91 fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
368 printw("Invalid network response.");
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
369 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
370 break;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
371 default:
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
372 eval_move_failed_msg(eval_result);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
373 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
374 clrtoeol();
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
375 }
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
376 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
377 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
378 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
379
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
380 static int recvmove(GameState *gamestate, GameInfo *gameinfo, int opponent) {
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
381
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
382 struct timeval timeout;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
383 while (1) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
384 timecontrol(gamestate, gameinfo);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
385
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
386 move(inputy, 0);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
387 printw("Awaiting opponent move...");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
388 clrtoeol();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
389 refresh();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
390
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
391 fd_set readfds;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
392
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
393 FD_ZERO(&readfds);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
394 FD_SET(opponent, &readfds);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
395 timeout.tv_sec = 0;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
396 timeout.tv_usec = 1e5;
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
397
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
398 // TODO: allow commands
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
399
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
400 int result = select(opponent+1, &readfds, NULL, NULL, &timeout);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
401 if (result == -1) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
402 printw("\rCannot perform asynchronous network IO");
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
403 cbreak(); getch();
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
404 exit(EXIT_FAILURE);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
405 }
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
406 if (result > 0) {
43
12141136a0da changed netcode numbers
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
407 uint8_t code = net_recieve_code(opponent);
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
408
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
409 Move move;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
410 switch (code) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
411 case NETCODE_TIMEOVER:
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
412 printw("\rYour opponent's time ran out - you win!");
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
413 clrtoeol();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
414 return 1;
44
1891d88cbd10 surrender is now resign
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
415 case NETCODE_RESIGN:
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
416 gamestate->resign = 1;
44
1891d88cbd10 surrender is now resign
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
417 printw("\rYour opponent resigned!");
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
418 clrtoeol();
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
419 return 1;
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
420 case NETCODE_CONNLOST:
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
421 printw("\rYour opponent has left the game.");
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
422 clrtoeol();
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
423 return 1;
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
424 case NETCODE_REMIS:
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
425 if (prompt_yesno(
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
426 "\rYour opponent offers remis - do you accept")) {
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
427 gamestate->remis = 1;
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
428 printw("\rRemis accepted!");
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
429 clrtoeol();
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
430 net_send_code(opponent, NETCODE_ACCEPT);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
431 return 1;
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
432 } else {
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
433 net_send_code(opponent, NETCODE_DECLINE);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
434 }
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
435 break;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
436 case NETCODE_MOVE:
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
437 net_recieve_data(opponent, &move, sizeof(Move));
48
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
438 code = validate_move(gamestate, &move);
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
439 if (code == VALID_MOVE_SEMANTICS) {
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
440 apply_move(gamestate, &move);
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
441 if (gamestate->checkmate) {
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
442 net_send_code(opponent, NETCODE_CHECKMATE);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
443 printw("\rCheckmate!");
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
444 clrtoeol();
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
445 return 1;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
446 } else if (gamestate->stalemate) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
447 net_send_code(opponent, NETCODE_STALEMATE);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
448 printw("\rStalemate!");
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
449 clrtoeol();
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
450 return 1;
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
451 } else if (move.check) {
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
452 net_send_code(opponent, NETCODE_CHECK);
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
453 } else {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
454 net_send_code(opponent, NETCODE_ACCEPT);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
455 }
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
456 return 0;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
457 } else {
48
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
458 uint32_t reason = htonl(code);
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
459 net_send_data(opponent, NETCODE_DECLINE,
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
460 &reason, sizeof(uint32_t));
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
461 }
45
e14a1d9aa91d removed nonblocking sockets for causing problems and not being necessary
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
462 break;
e14a1d9aa91d removed nonblocking sockets for causing problems and not being necessary
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
463 default:
e14a1d9aa91d removed nonblocking sockets for causing problems and not being necessary
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
464 printw("\nInvalid network request.");
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
465 }
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
466 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
467 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
468 }
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
469
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
470 static void post_game(Settings* settings, GameState *gamestate) {
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
471 GameInfo *gameinfo = &(settings->gameinfo);
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
472
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
473 move(0,0);
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
474 draw_board(gamestate, WHITE, settings->unicode);
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
475
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
476 // TODO: network connection is still open here - think about it!
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
477
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
478 mvaddstr(getmaxy(stdscr)-1, 0,
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
479 "Press 'q' to quit or 's' to save a PGN file...");
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
480 refresh();
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
481 flushinp();
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
482
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
483 noecho();
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
484 int c;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
485 do {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
486 c = getch();
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
487 if (c == 's') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
488 addch('\r');
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
489 echo();
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
490 save_pgn(gamestate, gameinfo);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
491 addstr(" Press 'q' to quit...");
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
492 noecho();
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
493 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
494 } while (c != 'q');
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
495 echo();
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
496
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
497 gamestate_cleanup(gamestate);
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
498 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
499
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
500 void game_start_singlemachine(Settings *settings) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
501 inputy = getmaxy(stdscr) - 6;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
502
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
503 GameState gamestate;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
504 gamestate_init(&gamestate);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
505 uint8_t curcol = WHITE;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
506
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
507 if (settings->continuepgn) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
508 FILE *pgnfile = fopen(settings->continuepgn, "r");
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
509 if (pgnfile) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
510 int result = read_pgn(pgnfile, &gamestate, &(settings->gameinfo));
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
511 long position = ftell(pgnfile);
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
512 fclose(pgnfile);
59
3fa1de896666 fixes inappropriate use of EXIT_ macros + adds a sample PGN file
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
513 if (result) {
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
514 printw("Invalid PGN file content at position %ld:\n%s\n",
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
515 position, pgn_error_str(result));
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
516 return;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
517 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
518 if (!is_game_running(&gamestate)) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
519 addstr("Game has ended. Use -S to analyze it.\n");
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
520 return;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
521 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
522 curcol = opponent_color(gamestate.lastmove->move.piece&COLOR_MASK);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
523 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
524 printw("Can't read PGN file (%s)\n", strerror(errno));
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
525 return;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
526 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
527 }
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
528
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
529 _Bool running;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
530 do {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
531 clear();
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
532 draw_board(&gamestate, curcol, settings->unicode);
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
533 running = !domove_singlemachine(&gamestate,
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
534 &(settings->gameinfo), curcol);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
535 curcol = opponent_color(curcol);
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
536 } while (running);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
537
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
538 post_game(settings, &gamestate);
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
539 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
540
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
541 void game_continue(Settings *settings, int opponent, GameState *gamestate) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
542 inputy = getmaxy(stdscr) - 6;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
543
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
544 uint8_t mycolor = is_server(settings) ? settings->gameinfo.servercolor :
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
545 opponent_color(settings->gameinfo.servercolor);
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
546
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
547 _Bool myturn = (gamestate->lastmove ?
53
78b580bfde33 fixed new network games starting with BLACK
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
548 (gamestate->lastmove->move.piece & COLOR_MASK) : BLACK) != mycolor;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
549
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
550 _Bool running;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
551 do {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
552 clear();
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
553 draw_board(gamestate, mycolor, settings->unicode);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
554 if (myturn) {
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
555 running = !sendmove(gamestate, &(settings->gameinfo),
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
556 opponent, mycolor);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
557 } else {
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
558 running = !recvmove(gamestate, &(settings->gameinfo), opponent);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
559 }
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
560 myturn ^= TRUE;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
561 } while (running);
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
562
69
c8f2c280cff7 adds unicode support
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
563 post_game(settings, gamestate);
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
564 }
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
565
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
566 void game_start(Settings *settings, int opponent) {
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
567 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
568 gamestate_init(&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
569
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
570 game_continue(settings, opponent, &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
571 }

mercurial