src/game.c

Mon, 31 Mar 2014 15:03:25 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 31 Mar 2014 15:03:25 +0200
changeset 23
824c9522ce66
parent 22
41bbfd4d17a3
child 25
3ab0c2e1a4e2
permissions
-rw-r--r--

introduced game state structure

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2014 Mike Becker. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  *
    28  */
    30 #include "game.h"
    31 #include "network.h"
    32 #include "input.h"
    33 #include <ncurses.h>
    34 #include <string.h>
    36 static const uint8_t boardx = 10, boardy = 10;
    38 static void draw_board(GameState *gamestate) {
    40     for (uint8_t y = 0 ; y < 8 ; y++) {
    41         for (uint8_t x = 0 ; x < 8 ; x++) {
    42             uint8_t col = gamestate->board[y][x] & COLOR_MASK;
    43             uint8_t piece = gamestate->board[y][x] & PIECE_MASK;
    44             char piecec;
    45             if (piece) {
    46                 piecec = piece == PAWN ? 'P' : getpiecechr(piece);
    47             } else {
    48                 piecec = ' ';
    49             }
    51             attrset((col == WHITE ? A_BOLD : A_DIM) |
    52                 COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW));
    54             int cy = gamestate->mycolor == WHITE ? boardy-y : boardy-7+y;
    55             int cx = gamestate->mycolor == WHITE ? boardx+x*3 : boardx+21-x*3;
    56             mvaddch(cy, cx, ' ');
    57             mvaddch(cy, cx+1, piecec);
    58             mvaddch(cy, cx+2, ' ');
    59         }
    60     }
    62     attrset(A_NORMAL);
    63     for (uint8_t i = 0 ; i < 8 ; i++) {
    64         int x = gamestate->mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3;
    65         int y = gamestate->mycolor == WHITE ? boardy-i : boardy-7+i;
    66         mvaddch(boardy+1, x, 'a'+i);
    67         mvaddch(y, boardx-2, '1'+i);
    68     }
    70     /* move log */
    71     // TODO: introduce window to avoid bugs with a long move log
    72     uint8_t logy = 0;
    73     const uint8_t logx = boardx + 30;
    74     int logi = 1;
    75     MoveList *logelem = gamestate->movelist;
    77     while (logelem) {
    78         logi++;
    79         if (logi % 2 == 0) {
    80             if ((logi - 2) % 4 == 0) {
    81                 logy++;
    82                 move(logy, logx);
    83             }
    84             printw("%d. ", logi / 2);
    85         }
    87         if (logelem) {
    88             Move move = logelem->move;
    89             char logstr[] = {
    90                 getpiecechr(move.piece),
    91                 filechr(move.fromfile), rowchr(move.fromrow),
    92                 move.capture ? 'x':'\0',
    93                 filechr(move.tofile), rowchr(move.torow),
    94                 move.check ? '+' : (move.checkmate ? '#' : 
    95                     (move.promotion ? '=' : '\0')),
    96                 move.promotion ? getpiecechr(move.promotion) : '\0',
    97                 ' '
    98             };
    99             for (int stri = 0 ; stri < sizeof(logstr) ; stri++) {
   100                 if (logstr[stri]) {
   101                     addch(logstr[stri]);
   102                 }
   103             }
   105             logelem = logelem->next;
   106         }
   107     }
   108 }
   111 static int sendmove(GameState *gamestate, int opponent) {
   113     const size_t buflen = 8;
   114     char movestr[buflen];
   115     _Bool remisrejected = FALSE;
   116     uint8_t code;
   118     int inputy = getmaxy(stdscr) - 6;
   119     while (1) {
   120         move(inputy, 0);
   121         if (remisrejected) {
   122             printw(
   123                 "Use chess notation to enter your move.\n"
   124                 "Remis offer rejected - type 'surr' to surrender.      \n\n"
   125                 "Type your move: ");
   126         } else {
   127             printw(
   128                 "Use chess notation to enter your move.\n"
   129                 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
   130                 "Type your move: ");
   131         }
   132         clrtoeol();
   133         refresh();
   134         getnstr(movestr, buflen);
   136         if (strncmp(movestr, "surr", buflen) == 0) {
   137             printw("You surrendered!");
   138             clrtoeol();
   139             refresh();
   140             net_send_code(opponent, NETCODE_SURRENDER);
   141             return 1;
   142         } else if (strncmp(movestr, "remis", buflen) == 0) {
   143             if (!remisrejected) {
   144                 net_send_code(opponent, NETCODE_REMIS);
   145                 printw("Remis offer sent - waiting for acceptance...");
   146                 refresh();
   147                 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
   148                     printw("\rRemis accepted!");
   149                     clrtoeol();
   150                     refresh();
   151                     return 1;
   152                 } else {
   153                     remisrejected = TRUE;
   154                 }
   155             }
   156         } else {
   157             Move move;
   158             int eval_result = eval_move(gamestate, movestr, &move);
   159             switch (eval_result) {
   160             case VALID_MOVE_SYNTAX:
   161                 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
   162                 code = net_recieve_code(opponent);
   163                 move.check = code == NETCODE_CHECK;
   164                 move.checkmate = code == NETCODE_CHECKMATE;
   165                 if (code == NETCODE_DECLINE) {
   166                     printw("Invalid move.");
   167                 } else {
   168                     apply_move(gamestate, &move);
   169                     if (move.checkmate) {
   170                         printw("Checkmate!");
   171                         clrtoeol();
   172                         return 1;
   173                     } else {
   174                         return 0;
   175                     }
   176                 }
   177                 break;
   178             case AMBIGUOUS_MOVE:
   179                 printw("Ambiguous move - please specify the piece to move.");
   180                 break;
   181             case INVALID_POSITION:
   182                 printw("Cannot find the piece that shall be moved.");
   183                 break;
   184             case NEED_PROMOTION:
   185                 printw("You need to promote the pawn (append \"=Q\" e.g.)!");
   186                 break;
   187             default:
   188                 printw("Can't interpret move - please use algebraic notation.");
   189             }
   190             clrtoeol();
   191         }
   192     }
   193 }
   195 static int recvmove(GameState *gamestate, int opponent) {
   197     int inputy = getmaxy(stdscr) - 6;
   198     while (1) {
   199         move(inputy, 0);
   200         printw("Awaiting opponent move...");
   201         clrtoeol();
   202         refresh();
   204         // TODO: nonblocking
   205         uint32_t code = net_recieve_code(opponent);
   207         Move move;
   208         switch (code) {
   209             case NETCODE_SURRENDER:
   210                 printw("\rYour opponent surrendered!");
   211                 clrtoeol();
   212                 return 1;
   213             case NETCODE_REMIS:
   214                 if (prompt_yesno(
   215                     "\rYour opponent offers remis - do you accept")) {
   216                     printw("\rRemis accepted!");
   217                     clrtoeol();
   218                     net_send_code(opponent, NETCODE_ACCEPT);
   219                     return 1;
   220                 } else {
   221                     net_send_code(opponent, NETCODE_DECLINE);
   222                 }
   223                 break;
   224             case NETCODE_MOVE:
   225                 net_recieve_data(opponent, &move, sizeof(Move));
   226                 if (validate_move(gamestate, &move)) {
   227                     apply_move(gamestate, &move);
   228                     if (move.check) {
   229                         net_send_code(opponent, NETCODE_CHECK);
   230                     } else if (move.checkmate) {
   231                         net_send_code(opponent, NETCODE_CHECKMATE);
   232                     } else {
   233                         net_send_code(opponent, NETCODE_ACCEPT);
   234                     }
   235                     return 0;
   236                 } else {
   237                     net_send_code(opponent, NETCODE_DECLINE);
   238                 }
   239         }
   240     }
   241 }
   243 void game_start(Settings *settings, int opponent) {
   244     _Bool myturn = is_server(settings) ==
   245         (settings->gameinfo.servercolor == WHITE);
   247     GameState gamestate;
   248     Board initboard = {
   249         {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
   250         {WPAWN, WPAWN,   WPAWN,   WPAWN,  WPAWN, WPAWN,   WPAWN,   WPAWN},
   251         {0,     0,       0,       0,      0,     0,       0,       0},
   252         {0,     0,       0,       0,      0,     0,       0,       0},
   253         {0,     0,       0,       0,      0,     0,       0,       0},
   254         {0,     0,       0,       0,      0,     0,       0,       0},
   255         {BPAWN, BPAWN,   BPAWN,   BPAWN,  BPAWN, BPAWN,   BPAWN,   BPAWN},
   256         {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
   257     };
   258     memcpy(gamestate.board, initboard, sizeof(Board));
   259     gamestate.mycolor = myturn ? WHITE:BLACK;
   260     gamestate.movelist = gamestate.lastmove = NULL;
   262     _Bool running;
   263     do {
   264         clear();
   265         draw_board(&gamestate);
   266         if (myturn) {
   267             running = !sendmove(&gamestate, opponent);
   268         } else {
   269             running = !recvmove(&gamestate, opponent);
   270             flushinp(); // flush any input the user hacked in while waiting
   271         }
   272         myturn ^= TRUE;
   273     }  while (running);
   275     gamestate_cleanup(&gamestate);
   277     mvaddstr(getmaxy(stdscr)-1, 0,
   278         "Game has ended. Press any key to leave...");
   279     getch();
   280 }

mercurial