universe@6: /* universe@6: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@6: * universe@6: * Copyright 2014 Mike Becker. All rights reserved. universe@6: * universe@6: * Redistribution and use in source and binary forms, with or without universe@6: * modification, are permitted provided that the following conditions are met: universe@6: * universe@6: * 1. Redistributions of source code must retain the above copyright universe@6: * notice, this list of conditions and the following disclaimer. universe@6: * universe@6: * 2. Redistributions in binary form must reproduce the above copyright universe@6: * notice, this list of conditions and the following disclaimer in the universe@6: * documentation and/or other materials provided with the distribution. universe@6: * universe@6: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@6: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@6: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@6: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@6: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@6: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@6: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@6: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@6: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@6: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@6: * POSSIBILITY OF SUCH DAMAGE. universe@6: * universe@6: */ universe@6: universe@6: #include "game.h" universe@19: #include "network.h" universe@7: #include "input.h" universe@7: #include universe@7: #include universe@7: universe@7: static const uint8_t boardx = 10, boardy = 10; universe@7: universe@23: static void draw_board(GameState *gamestate) { universe@7: universe@7: for (uint8_t y = 0 ; y < 8 ; y++) { universe@7: for (uint8_t x = 0 ; x < 8 ; x++) { universe@23: uint8_t col = gamestate->board[y][x] & COLOR_MASK; universe@23: uint8_t piece = gamestate->board[y][x] & PIECE_MASK; universe@18: char piecec; universe@18: if (piece) { universe@18: piecec = piece == PAWN ? 'P' : getpiecechr(piece); universe@18: } else { universe@18: piecec = ' '; universe@7: } universe@7: universe@7: attrset((col == WHITE ? A_BOLD : A_DIM) | universe@7: COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW)); universe@7: universe@23: int cy = gamestate->mycolor == WHITE ? boardy-y : boardy-7+y; universe@23: int cx = gamestate->mycolor == WHITE ? boardx+x*3 : boardx+21-x*3; universe@8: mvaddch(cy, cx, ' '); universe@8: mvaddch(cy, cx+1, piecec); universe@8: mvaddch(cy, cx+2, ' '); universe@7: } universe@7: } universe@7: universe@7: attrset(A_NORMAL); universe@7: for (uint8_t i = 0 ; i < 8 ; i++) { universe@23: int x = gamestate->mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3; universe@23: int y = gamestate->mycolor == WHITE ? boardy-i : boardy-7+i; universe@8: mvaddch(boardy+1, x, 'a'+i); universe@8: mvaddch(y, boardx-2, '1'+i); universe@7: } universe@18: universe@18: /* move log */ universe@18: // TODO: introduce window to avoid bugs with a long move log universe@18: uint8_t logy = 0; universe@18: const uint8_t logx = boardx + 30; universe@18: int logi = 1; universe@23: MoveList *logelem = gamestate->movelist; universe@18: universe@18: while (logelem) { universe@18: logi++; universe@18: if (logi % 2 == 0) { universe@18: if ((logi - 2) % 4 == 0) { universe@18: logy++; universe@23: move(logy, logx); universe@18: } universe@18: printw("%d. ", logi / 2); universe@18: } universe@18: universe@18: if (logelem) { universe@18: Move move = logelem->move; universe@25: if ((move.piece&PIECE_MASK) == KING && universe@25: abs(move.tofile-move.fromfile) == 2) { universe@25: addstr(move.tofile==fileidx('c')?"O-O-O":"O-O"); universe@25: } else { universe@25: char logstr[] = { universe@25: getpiecechr(move.piece), universe@25: filechr(move.fromfile), rowchr(move.fromrow), universe@25: move.capture ? 'x':'\0', universe@25: filechr(move.tofile), rowchr(move.torow), universe@25: move.check ? '+' : (move.checkmate ? '#' : universe@25: (move.promotion ? '=' : '\0')), universe@25: move.promotion ? getpiecechr(move.promotion) : '\0' universe@25: }; universe@25: for (int stri = 0 ; stri < sizeof(logstr) ; stri++) { universe@25: if (logstr[stri]) { universe@25: addch(logstr[stri]); universe@25: } universe@18: } universe@18: } universe@25: addch(' '); universe@18: universe@18: logelem = logelem->next; universe@18: } universe@18: } universe@7: } universe@7: universe@8: universe@23: static int sendmove(GameState *gamestate, int opponent) { universe@18: universe@7: const size_t buflen = 8; universe@8: char movestr[buflen]; universe@7: _Bool remisrejected = FALSE; universe@11: uint8_t code; universe@7: universe@23: int inputy = getmaxy(stdscr) - 6; universe@7: while (1) { universe@18: move(inputy, 0); universe@7: if (remisrejected) { universe@7: printw( universe@7: "Use chess notation to enter your move.\n" universe@7: "Remis offer rejected - type 'surr' to surrender. \n\n" universe@7: "Type your move: "); universe@7: } else { universe@7: printw( universe@7: "Use chess notation to enter your move.\n" universe@7: "Or type 'surr' to surrender or 'remis' to offer remis.\n\n" universe@7: "Type your move: "); universe@7: } universe@7: clrtoeol(); universe@7: refresh(); universe@8: getnstr(movestr, buflen); universe@7: universe@8: if (strncmp(movestr, "surr", buflen) == 0) { universe@7: printw("You surrendered!"); universe@16: clrtoeol(); universe@8: refresh(); universe@7: net_send_code(opponent, NETCODE_SURRENDER); universe@7: return 1; universe@8: } else if (strncmp(movestr, "remis", buflen) == 0) { universe@7: if (!remisrejected) { universe@7: net_send_code(opponent, NETCODE_REMIS); universe@7: printw("Remis offer sent - waiting for acceptance..."); universe@7: refresh(); universe@7: if (net_recieve_code(opponent) == NETCODE_ACCEPT) { universe@7: printw("\rRemis accepted!"); universe@7: clrtoeol(); universe@8: refresh(); universe@7: return 1; universe@7: } else { universe@7: remisrejected = TRUE; universe@7: } universe@7: } universe@7: } else { universe@8: Move move; universe@23: int eval_result = eval_move(gamestate, movestr, &move); universe@16: switch (eval_result) { universe@18: case VALID_MOVE_SYNTAX: universe@22: net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move)); universe@18: code = net_recieve_code(opponent); universe@18: move.check = code == NETCODE_CHECK; universe@18: move.checkmate = code == NETCODE_CHECKMATE; universe@18: if (code == NETCODE_DECLINE) { universe@18: printw("Invalid move."); universe@18: } else { universe@23: apply_move(gamestate, &move); universe@18: if (move.checkmate) { universe@18: printw("Checkmate!"); universe@18: clrtoeol(); universe@18: return 1; universe@14: } else { universe@18: return 0; universe@11: } universe@18: } universe@18: break; universe@18: case AMBIGUOUS_MOVE: universe@18: printw("Ambiguous move - please specify the piece to move."); universe@18: break; universe@18: case INVALID_POSITION: universe@18: printw("Cannot find the piece that shall be moved."); universe@18: break; universe@18: case NEED_PROMOTION: universe@18: printw("You need to promote the pawn (append \"=Q\" e.g.)!"); universe@18: break; universe@18: default: universe@18: printw("Can't interpret move - please use algebraic notation."); universe@8: } universe@16: clrtoeol(); universe@7: } universe@7: } universe@7: } universe@7: universe@23: static int recvmove(GameState *gamestate, int opponent) { universe@7: universe@23: int inputy = getmaxy(stdscr) - 6; universe@7: while (1) { universe@18: move(inputy, 0); universe@7: printw("Awaiting opponent move..."); universe@7: clrtoeol(); universe@7: refresh(); universe@7: universe@7: // TODO: nonblocking universe@7: uint32_t code = net_recieve_code(opponent); universe@8: universe@8: Move move; universe@7: switch (code) { universe@7: case NETCODE_SURRENDER: universe@7: printw("\rYour opponent surrendered!"); universe@7: clrtoeol(); universe@7: return 1; universe@7: case NETCODE_REMIS: universe@7: if (prompt_yesno( universe@7: "\rYour opponent offers remis - do you accept")) { universe@7: printw("\rRemis accepted!"); universe@7: clrtoeol(); universe@7: net_send_code(opponent, NETCODE_ACCEPT); universe@7: return 1; universe@7: } else { universe@7: net_send_code(opponent, NETCODE_DECLINE); universe@7: } universe@7: break; universe@7: case NETCODE_MOVE: universe@8: net_recieve_data(opponent, &move, sizeof(Move)); universe@23: if (validate_move(gamestate, &move)) { universe@23: apply_move(gamestate, &move); universe@11: if (move.check) { universe@11: net_send_code(opponent, NETCODE_CHECK); universe@11: } else if (move.checkmate) { universe@11: net_send_code(opponent, NETCODE_CHECKMATE); universe@11: } else { universe@11: net_send_code(opponent, NETCODE_ACCEPT); universe@11: } universe@8: return 0; universe@8: } else { universe@8: net_send_code(opponent, NETCODE_DECLINE); universe@8: } universe@7: } universe@7: } universe@7: } universe@6: universe@6: void game_start(Settings *settings, int opponent) { universe@7: _Bool myturn = is_server(settings) == universe@7: (settings->gameinfo.servercolor == WHITE); universe@8: universe@23: GameState gamestate; universe@23: Board initboard = { universe@7: {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK}, universe@7: {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN}, universe@7: {0, 0, 0, 0, 0, 0, 0, 0}, universe@7: {0, 0, 0, 0, 0, 0, 0, 0}, universe@7: {0, 0, 0, 0, 0, 0, 0, 0}, universe@7: {0, 0, 0, 0, 0, 0, 0, 0}, universe@7: {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN}, universe@7: {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK} universe@7: }; universe@23: memcpy(gamestate.board, initboard, sizeof(Board)); universe@23: gamestate.mycolor = myturn ? WHITE:BLACK; universe@23: gamestate.movelist = gamestate.lastmove = NULL; universe@7: universe@23: _Bool running; universe@7: do { universe@7: clear(); universe@23: draw_board(&gamestate); universe@7: if (myturn) { universe@23: running = !sendmove(&gamestate, opponent); universe@7: } else { universe@23: running = !recvmove(&gamestate, opponent); universe@7: flushinp(); // flush any input the user hacked in while waiting universe@7: } universe@11: myturn ^= TRUE; universe@7: } while (running); universe@7: universe@23: gamestate_cleanup(&gamestate); universe@18: universe@23: mvaddstr(getmaxy(stdscr)-1, 0, universe@7: "Game has ended. Press any key to leave..."); universe@7: getch(); universe@6: }