diff -r daaf6e5b3501 -r 41468077b5bb src/game.c --- a/src/game.c Wed Mar 19 10:08:25 2014 +0100 +++ b/src/game.c Wed Mar 19 15:36:54 2014 +0100 @@ -28,7 +28,153 @@ */ #include "game.h" +#include "input.h" +#include +#include + +static const uint8_t boardx = 10, boardy = 10; + +static void draw_board(Board board) { + + for (uint8_t y = 0 ; y < 8 ; y++) { + for (uint8_t x = 0 ; x < 8 ; x++) { + uint8_t col = board[y][x] & COLOR_MASK; + uint8_t piece = board[y][x] & PIECE_MASK; + char piecec = ' '; + switch (piece) { + case PAWN: piecec = 'P'; break; + case ROOK: piecec = 'R'; break; + case KNIGHT: piecec = 'N'; break; + case BISHOP: piecec = 'B'; break; + case QUEEN: piecec = 'Q'; break; + case KING: piecec = 'K'; break; + } + + attrset((col == WHITE ? A_BOLD : A_DIM) | + COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW)); + + mvaddch(boardy-y, boardx+x*3, ' '); + mvaddch(boardy-y, boardx+x*3+1, piecec); + mvaddch(boardy-y, boardx+x*3+2, ' '); + } + } + + attrset(A_NORMAL); + for (uint8_t i = 0 ; i < 8 ; i++) { + mvaddch(boardy+1, boardx+i*3+1, 'a'+i); + mvaddch(boardy-i, boardx-2, '1'+i); + } +} + +static int sendmove(int opponent) { + const size_t buflen = 8; + char move[buflen]; + _Bool remisrejected = FALSE; + + while (1) { + move(boardy+3, 0); + if (remisrejected) { + printw( + "Use chess notation to enter your move.\n" + "Remis offer rejected - type 'surr' to surrender. \n\n" + "Type your move: "); + } else { + printw( + "Use chess notation to enter your move.\n" + "Or type 'surr' to surrender or 'remis' to offer remis.\n\n" + "Type your move: "); + } + clrtoeol(); + refresh(); + getnstr(move, buflen); + + if (strncmp(move, "surr", buflen) == 0) { + printw("You surrendered!"); + net_send_code(opponent, NETCODE_SURRENDER); + return 1; + } else if (strncmp(move, "remis", buflen) == 0) { + if (!remisrejected) { + net_send_code(opponent, NETCODE_REMIS); + printw("Remis offer sent - waiting for acceptance..."); + refresh(); + if (net_recieve_code(opponent) == NETCODE_ACCEPT) { + printw("\rRemis accepted!"); + clrtoeol(); + return 1; + } else { + remisrejected = TRUE; + } + } + } else { + // TODO: validate move syntactically + // TODO: send move and await acceptance + } + } +} + +static int recvmove(int opponent) { + + while (1) { + move(boardy+3, 0); + printw("Awaiting opponent move..."); + clrtoeol(); + refresh(); + + // TODO: nonblocking + uint32_t code = net_recieve_code(opponent); + switch (code) { + case NETCODE_SURRENDER: + printw("\rYour opponent surrendered!"); + clrtoeol(); + return 1; + case NETCODE_REMIS: + if (prompt_yesno( + "\rYour opponent offers remis - do you accept")) { + printw("\rRemis accepted!"); + clrtoeol(); + net_send_code(opponent, NETCODE_ACCEPT); + return 1; + } else { + net_send_code(opponent, NETCODE_DECLINE); + } + break; + case NETCODE_MOVE: + // TODO: receive move + // TODO: validate move and accept/reject + return 0; + } + } +} void game_start(Settings *settings, int opponent) { + _Bool myturn = is_server(settings) == + (settings->gameinfo.servercolor == WHITE); + _Bool running; + Board board = { + {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK}, + {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN}, + {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK} + }; + + do { + clear(); + draw_board(board); + if (myturn) { + running = !sendmove(opponent); + } else { + running = !recvmove(opponent); + flushinp(); // flush any input the user hacked in while waiting + } + myturn ^= 1; + } while (running); + + mvaddstr(getmaxy(tchess_window)-1, 0, + "Game has ended. Press any key to leave..."); + getch(); }