src/game.c

Tue, 01 Apr 2014 12:30:25 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 01 Apr 2014 12:30:25 +0200
changeset 25
3ab0c2e1a4e2
parent 23
824c9522ce66
child 26
e0a76ee1bb2b
permissions
-rw-r--r--

implemented king

     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             if ((move.piece&PIECE_MASK) == KING &&
    90                 abs(move.tofile-move.fromfile) == 2) {
    91                 addstr(move.tofile==fileidx('c')?"O-O-O":"O-O");
    92             } else {
    93                 char logstr[] = {
    94                     getpiecechr(move.piece),
    95                     filechr(move.fromfile), rowchr(move.fromrow),
    96                     move.capture ? 'x':'\0',
    97                     filechr(move.tofile), rowchr(move.torow),
    98                     move.check ? '+' : (move.checkmate ? '#' : 
    99                         (move.promotion ? '=' : '\0')),
   100                     move.promotion ? getpiecechr(move.promotion) : '\0'
   101                 };
   102                 for (int stri = 0 ; stri < sizeof(logstr) ; stri++) {
   103                     if (logstr[stri]) {
   104                         addch(logstr[stri]);
   105                     }
   106                 }
   107             }
   108             addch(' ');
   110             logelem = logelem->next;
   111         }
   112     }
   113 }
   116 static int sendmove(GameState *gamestate, int opponent) {
   118     const size_t buflen = 8;
   119     char movestr[buflen];
   120     _Bool remisrejected = FALSE;
   121     uint8_t code;
   123     int inputy = getmaxy(stdscr) - 6;
   124     while (1) {
   125         move(inputy, 0);
   126         if (remisrejected) {
   127             printw(
   128                 "Use chess notation to enter your move.\n"
   129                 "Remis offer rejected - type 'surr' to surrender.      \n\n"
   130                 "Type your move: ");
   131         } else {
   132             printw(
   133                 "Use chess notation to enter your move.\n"
   134                 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
   135                 "Type your move: ");
   136         }
   137         clrtoeol();
   138         refresh();
   139         getnstr(movestr, buflen);
   141         if (strncmp(movestr, "surr", buflen) == 0) {
   142             printw("You surrendered!");
   143             clrtoeol();
   144             refresh();
   145             net_send_code(opponent, NETCODE_SURRENDER);
   146             return 1;
   147         } else if (strncmp(movestr, "remis", buflen) == 0) {
   148             if (!remisrejected) {
   149                 net_send_code(opponent, NETCODE_REMIS);
   150                 printw("Remis offer sent - waiting for acceptance...");
   151                 refresh();
   152                 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
   153                     printw("\rRemis accepted!");
   154                     clrtoeol();
   155                     refresh();
   156                     return 1;
   157                 } else {
   158                     remisrejected = TRUE;
   159                 }
   160             }
   161         } else {
   162             Move move;
   163             int eval_result = eval_move(gamestate, movestr, &move);
   164             switch (eval_result) {
   165             case VALID_MOVE_SYNTAX:
   166                 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
   167                 code = net_recieve_code(opponent);
   168                 move.check = code == NETCODE_CHECK;
   169                 move.checkmate = code == NETCODE_CHECKMATE;
   170                 if (code == NETCODE_DECLINE) {
   171                     printw("Invalid move.");
   172                 } else {
   173                     apply_move(gamestate, &move);
   174                     if (move.checkmate) {
   175                         printw("Checkmate!");
   176                         clrtoeol();
   177                         return 1;
   178                     } else {
   179                         return 0;
   180                     }
   181                 }
   182                 break;
   183             case AMBIGUOUS_MOVE:
   184                 printw("Ambiguous move - please specify the piece to move.");
   185                 break;
   186             case INVALID_POSITION:
   187                 printw("Cannot find the piece that shall be moved.");
   188                 break;
   189             case NEED_PROMOTION:
   190                 printw("You need to promote the pawn (append \"=Q\" e.g.)!");
   191                 break;
   192             default:
   193                 printw("Can't interpret move - please use algebraic notation.");
   194             }
   195             clrtoeol();
   196         }
   197     }
   198 }
   200 static int recvmove(GameState *gamestate, int opponent) {
   202     int inputy = getmaxy(stdscr) - 6;
   203     while (1) {
   204         move(inputy, 0);
   205         printw("Awaiting opponent move...");
   206         clrtoeol();
   207         refresh();
   209         // TODO: nonblocking
   210         uint32_t code = net_recieve_code(opponent);
   212         Move move;
   213         switch (code) {
   214             case NETCODE_SURRENDER:
   215                 printw("\rYour opponent surrendered!");
   216                 clrtoeol();
   217                 return 1;
   218             case NETCODE_REMIS:
   219                 if (prompt_yesno(
   220                     "\rYour opponent offers remis - do you accept")) {
   221                     printw("\rRemis accepted!");
   222                     clrtoeol();
   223                     net_send_code(opponent, NETCODE_ACCEPT);
   224                     return 1;
   225                 } else {
   226                     net_send_code(opponent, NETCODE_DECLINE);
   227                 }
   228                 break;
   229             case NETCODE_MOVE:
   230                 net_recieve_data(opponent, &move, sizeof(Move));
   231                 if (validate_move(gamestate, &move)) {
   232                     apply_move(gamestate, &move);
   233                     if (move.check) {
   234                         net_send_code(opponent, NETCODE_CHECK);
   235                     } else if (move.checkmate) {
   236                         net_send_code(opponent, NETCODE_CHECKMATE);
   237                     } else {
   238                         net_send_code(opponent, NETCODE_ACCEPT);
   239                     }
   240                     return 0;
   241                 } else {
   242                     net_send_code(opponent, NETCODE_DECLINE);
   243                 }
   244         }
   245     }
   246 }
   248 void game_start(Settings *settings, int opponent) {
   249     _Bool myturn = is_server(settings) ==
   250         (settings->gameinfo.servercolor == WHITE);
   252     GameState gamestate;
   253     Board initboard = {
   254         {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
   255         {WPAWN, WPAWN,   WPAWN,   WPAWN,  WPAWN, WPAWN,   WPAWN,   WPAWN},
   256         {0,     0,       0,       0,      0,     0,       0,       0},
   257         {0,     0,       0,       0,      0,     0,       0,       0},
   258         {0,     0,       0,       0,      0,     0,       0,       0},
   259         {0,     0,       0,       0,      0,     0,       0,       0},
   260         {BPAWN, BPAWN,   BPAWN,   BPAWN,  BPAWN, BPAWN,   BPAWN,   BPAWN},
   261         {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
   262     };
   263     memcpy(gamestate.board, initboard, sizeof(Board));
   264     gamestate.mycolor = myturn ? WHITE:BLACK;
   265     gamestate.movelist = gamestate.lastmove = NULL;
   267     _Bool running;
   268     do {
   269         clear();
   270         draw_board(&gamestate);
   271         if (myturn) {
   272             running = !sendmove(&gamestate, opponent);
   273         } else {
   274             running = !recvmove(&gamestate, opponent);
   275             flushinp(); // flush any input the user hacked in while waiting
   276         }
   277         myturn ^= TRUE;
   278     }  while (running);
   280     gamestate_cleanup(&gamestate);
   282     mvaddstr(getmaxy(stdscr)-1, 0,
   283         "Game has ended. Press any key to leave...");
   284     getch();
   285 }

mercurial