src/game.c

Sat, 22 Mar 2014 17:23:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 22 Mar 2014 17:23:07 +0100
changeset 10
1347e4dabac0
parent 9
4e4f156bba58
child 11
08d7a6e3ec31
permissions
-rw-r--r--

prepared code base for implementing rules

     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 "input.h"
    32 #include "rules/rules.h"
    33 #include <ncurses.h>
    34 #include <string.h>
    36 static const uint8_t boardx = 10, boardy = 10;
    38 static void draw_board(Board board, uint8_t mycolor) {
    40     for (uint8_t y = 0 ; y < 8 ; y++) {
    41         for (uint8_t x = 0 ; x < 8 ; x++) {
    42             uint8_t col = board[y][x] & COLOR_MASK;
    43             uint8_t piece = board[y][x] & PIECE_MASK;
    44             char piecec = ' ';
    45             switch (piece) {
    46                 case PAWN: piecec = 'P'; break;
    47                 case ROOK: piecec = 'R'; break;
    48                 case KNIGHT: piecec = 'N'; break;
    49                 case BISHOP: piecec = 'B'; break;
    50                 case QUEEN: piecec = 'Q'; break;
    51                 case KING: piecec = 'K'; break;
    52             }
    54             attrset((col == WHITE ? A_BOLD : A_DIM) |
    55                 COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW));
    57             int cy = mycolor == WHITE ? boardy-y : boardy-7+y;
    58             int cx = mycolor == WHITE ? boardx+x*3 : boardx+21-x*3;
    59             mvaddch(cy, cx, ' ');
    60             mvaddch(cy, cx+1, piecec);
    61             mvaddch(cy, cx+2, ' ');
    62         }
    63     }
    65     attrset(A_NORMAL);
    66     for (uint8_t i = 0 ; i < 8 ; i++) {
    67         int x = mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3;
    68         int y = mycolor == WHITE ? boardy-i : boardy-7+i;
    69         mvaddch(boardy+1, x, 'a'+i);
    70         mvaddch(y, boardx-2, '1'+i);
    71     }
    72 }
    74 static void apply_move(Board board, Move *move) {
    75     board[move->fromrow][move->fromfile] = 0;
    76     // TODO: care for en passant capture
    77     board[move->torow][move->tofile] = move->piece;
    79     /* castling */
    80     if ((move->piece & PIECE_MASK) == KING &&
    81         move->fromfile == fileidx('e')) {
    82         uint8_t color = move->piece & COLOR_MASK;
    84         if (move->tofile == fileidx('g')) {
    85             board[move->torow][fileidx('h')] = 0;
    86             board[move->torow][fileidx('f')] = color|ROOK;
    87         } else if (move->tofile == fileidx('c')) {
    88             board[move->torow][fileidx('a')] = 0;
    89             board[move->torow][fileidx('d')] = color|ROOK;
    90         }
    91     }
    92 }
    94 static _Bool validate_move(Board board, uint8_t mycolor, Move *move) {
    95     _Bool result;
    97     /* does piece exist */
    98     result = board[move->fromrow][move->fromfile] == move->piece;
   100     switch (move->piece & PIECE_MASK) {
   101     case PAWN:
   102         result = result && pawn_chkrules(board, move);
   103         result = result && !pawn_isblocked(board, move);
   104         break;
   105     case ROOK:
   106         result = result && rook_chkrules(board, move);
   107         result = result && !rook_isblocked(board, move);
   108         break;
   109     case KNIGHT:
   110         result = result && knight_chkrules(board, move);
   111         result = result && !knight_isblocked(board, move);
   112         break;
   113     case BISHOP:
   114         result = result && bishop_chkrules(board, move);
   115         result = result && !bishop_isblocked(board, move);
   116         break;
   117     case QUEEN:
   118         result = result && queen_chkrules(board, move);
   119         result = result && !queen_isblocked(board, move);
   120         break;
   121     case KING:
   122         result = result && king_chkrules(board, move);
   123         result = result && !king_isblocked(board, move);
   124         break;
   125     default:
   126         result = FALSE;
   127     }
   129     /* is piece pinned */
   130     // TODO: make it so
   132     return result;
   133 }
   135 static _Bool eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
   136     memset(move, 0, sizeof(Move));
   138     size_t len = strlen(mstr);
   140     /* remove check */
   141     if (mstr[len-1] == '+') {
   142         len--; mstr[len] = '\0';
   143         move->check = TRUE;
   144     }
   146     if (len == 2) {
   147         /* pawn move (e.g. "e4") */
   148         if (isfile(mstr[0]) && isrow(mstr[1])) {
   149             move->piece = PAWN|mycolor;
   150             move->tofile = fileidx(mstr[0]);
   151             move->torow = rowidx(mstr[1]);
   152             if (!pawn_getlocation(board, move)) {
   153                 move->piece = 0;
   154             }
   155         }
   156     } else if (len == 3) {
   157         if (strcmp(mstr, "O-O") == 0) {
   158             /* king side castling */
   159             move->piece = KING|mycolor;
   160             move->fromfile = fileidx('e');
   161             move->tofile = fileidx('g');
   162             move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
   163         } else {
   164             /* unambiguous move (e.g. "Nf3") */
   165         }
   167     } else if (len == 4) {
   168         /* ambiguous move (e.g. "Ndf3") */
   170         /* unambiguous capture (e.g. "Nxf3", "dxe5") */
   172     } else if (len == 5) {
   173         if (strcmp(mstr, "O-O-O") == 0) {
   174             /* queen side castling "O-O-O" */
   175             move->piece = KING|mycolor;
   176             move->fromfile = fileidx('e');
   177             move->tofile = fileidx('c');
   178             move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
   179         } else {
   180             /* ambiguous capture (e.g. "Ndxf3") */
   182             /* long notation move (e.g. "Nc5a4") */
   184             /* long notation capture (e.g. "e5xf6") */
   185         }
   186     } else if (len == 6) {
   187         /* long notation capture (e.g. "Nc5xf3") */
   188     }
   190     return move->piece != 0;
   191 }
   193 static int sendmove(Board board, uint8_t mycolor, int opponent) {
   194     const size_t buflen = 8;
   195     char movestr[buflen];
   196     _Bool remisrejected = FALSE;
   198     while (1) {
   199         move(boardy+3, 0);
   200         if (remisrejected) {
   201             printw(
   202                 "Use chess notation to enter your move.\n"
   203                 "Remis offer rejected - type 'surr' to surrender.      \n\n"
   204                 "Type your move: ");
   205         } else {
   206             printw(
   207                 "Use chess notation to enter your move.\n"
   208                 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
   209                 "Type your move: ");
   210         }
   211         clrtoeol();
   212         refresh();
   213         getnstr(movestr, buflen);
   215         if (strncmp(movestr, "surr", buflen) == 0) {
   216             printw("You surrendered!");
   217             refresh();
   218             net_send_code(opponent, NETCODE_SURRENDER);
   219             return 1;
   220         } else if (strncmp(movestr, "remis", buflen) == 0) {
   221             if (!remisrejected) {
   222                 net_send_code(opponent, NETCODE_REMIS);
   223                 printw("Remis offer sent - waiting for acceptance...");
   224                 refresh();
   225                 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
   226                     printw("\rRemis accepted!");
   227                     clrtoeol();
   228                     refresh();
   229                     return 1;
   230                 } else {
   231                     remisrejected = TRUE;
   232                 }
   233             }
   234         } else {
   235             Move move;
   236             if (eval_move(board, mycolor, movestr, &move)) {
   237                 net_send_code(opponent, NETCODE_MOVE);
   238                 net_send_data(opponent, &move, sizeof(Move));
   239                 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
   240                     apply_move(board, &move);
   241                     return 0;
   242                 } else {
   243                     printw("Invalid move.");
   244                     clrtoeol();
   245                 }
   246             } else {
   247                 printw("Can't interpret move - please use algebraic notation.");
   248             }
   249         }
   250     }
   251 }
   253 static int recvmove(Board board, uint8_t mycolor, int opponent) {
   255     while (1) {
   256         move(boardy+3, 0);
   257         printw("Awaiting opponent move...");
   258         clrtoeol();
   259         refresh();
   261         // TODO: nonblocking
   262         uint32_t code = net_recieve_code(opponent);
   264         Move move;
   265         switch (code) {
   266             case NETCODE_SURRENDER:
   267                 printw("\rYour opponent surrendered!");
   268                 clrtoeol();
   269                 return 1;
   270             case NETCODE_REMIS:
   271                 if (prompt_yesno(
   272                     "\rYour opponent offers remis - do you accept")) {
   273                     printw("\rRemis accepted!");
   274                     clrtoeol();
   275                     net_send_code(opponent, NETCODE_ACCEPT);
   276                     return 1;
   277                 } else {
   278                     net_send_code(opponent, NETCODE_DECLINE);
   279                 }
   280                 break;
   281             case NETCODE_MOVE:
   282                 net_recieve_data(opponent, &move, sizeof(Move));
   283                 if (validate_move(board, mycolor, &move)) {
   284                     apply_move(board, &move);
   285                     net_send_code(opponent, NETCODE_ACCEPT);
   286                     return 0;
   287                 } else {
   288                     net_send_code(opponent, NETCODE_DECLINE);
   289                 }
   290         }
   291     }
   292 }
   294 void game_start(Settings *settings, int opponent) {
   295     _Bool myturn = is_server(settings) ==
   296         (settings->gameinfo.servercolor == WHITE);
   297     uint8_t mycolor = myturn ? WHITE:BLACK;
   299     _Bool running;
   301     Board board = {
   302         {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
   303         {WPAWN, WPAWN,   WPAWN,   WPAWN,  WPAWN, WPAWN,   WPAWN,   WPAWN},
   304         {0,     0,       0,       0,      0,     0,       0,       0},
   305         {0,     0,       0,       0,      0,     0,       0,       0},
   306         {0,     0,       0,       0,      0,     0,       0,       0},
   307         {0,     0,       0,       0,      0,     0,       0,       0},
   308         {BPAWN, BPAWN,   BPAWN,   BPAWN,  BPAWN, BPAWN,   BPAWN,   BPAWN},
   309         {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
   310     };
   312     do {
   313         clear();
   314         draw_board(board, mycolor);
   315         if (myturn) {
   316             running = !sendmove(board, mycolor, opponent);
   317         } else {
   318             running = !recvmove(board, mycolor, opponent);
   319             flushinp(); // flush any input the user hacked in while waiting
   320         }
   321         myturn ^= 1;
   322     }  while (running);
   324     mvaddstr(getmaxy(tchess_window)-1, 0,
   325         "Game has ended. Press any key to leave...");
   326     getch();
   327 }

mercurial