diff -r c6a1ad6cf749 -r a285ee393860 src/game.c --- a/src/game.c Mon Apr 07 14:08:57 2014 +0200 +++ b/src/game.c Mon Apr 07 17:39:46 2014 +0200 @@ -32,11 +32,27 @@ #include "input.h" #include #include +#include static const uint8_t boardx = 10, boardy = 10; +static void draw_time(GameState *gamestate, GameInfo *gameinfo) { + if (gameinfo->timecontrol) { + // TODO: correct time display + + uint16_t whitem = gameinfo->time / 60; + uint16_t whites = gameinfo->time % 60; + uint16_t blackm = gameinfo->time / 60; + uint16_t blacks = gameinfo->time % 60; + + mvprintw(boardy+4, boardx-1, + "White time: %4" PRIu16 ":%02" PRIu16, whitem, whites); + mvprintw(boardy+5, boardx-1, + "Black time: %4" PRIu16 ":%02" PRIu16, blackm, blacks); + } +} + static void draw_board(GameState *gamestate) { - for (uint8_t y = 0 ; y < 8 ; y++) { for (uint8_t x = 0 ; x < 8 ; x++) { uint8_t col = gamestate->board[y][x] & COLOR_MASK; @@ -134,13 +150,14 @@ } } -static int domove_singlemachine(GameState *gamestate) { +static int domove_singlemachine(GameState *gamestate, GameInfo *gameinfo) { const size_t buflen = 8; char movestr[buflen]; int inputy = getmaxy(stdscr) - 6; while (1) { + draw_time(gamestate, gameinfo); move(inputy, 0); printw( "Use chess notation to enter your move.\n" @@ -148,45 +165,46 @@ "Type your move: "); clrtoeol(); refresh(); - getnstr(movestr, buflen); - - if (strncmp(movestr, "surr", buflen) == 0) { - printw("%s surrendered!", - gamestate->mycolor==WHITE?"White":"Black"); - clrtoeol(); - refresh(); - return 1; - } else if (strncmp(movestr, "remis", buflen) == 0) { - printw("Game ends remis."); - clrtoeol(); - refresh(); - return 1; - } else { - Move move; - int eval_result = eval_move(gamestate, movestr, &move); - switch (eval_result) { - case VALID_MOVE_SYNTAX: - if (validate_move(gamestate, &move)) { - apply_move(gamestate, &move); - if (gamestate->checkmate) { - printw("Checkmate!"); - clrtoeol(); - return 1; - } else if (gamestate->stalemate) { - printw("Stalemate!"); - clrtoeol(); - return 1; + + if (asyncgetnstr(movestr, buflen)) { + if (strncmp(movestr, "surr", buflen) == 0) { + printw("%s surrendered!", + gamestate->mycolor==WHITE?"White":"Black"); + clrtoeol(); + refresh(); + return 1; + } else if (strncmp(movestr, "remis", buflen) == 0) { + printw("Game ends remis."); + clrtoeol(); + refresh(); + return 1; + } else { + Move move; + int eval_result = eval_move(gamestate, movestr, &move); + switch (eval_result) { + case VALID_MOVE_SYNTAX: + if (validate_move(gamestate, &move)) { + apply_move(gamestate, &move); + if (gamestate->checkmate) { + printw("Checkmate!"); + clrtoeol(); + return 1; + } else if (gamestate->stalemate) { + printw("Stalemate!"); + clrtoeol(); + return 1; + } else { + return 0; + } } else { - return 0; + printw("Invalid move."); } - } else { - printw("Invalid move."); + break; + default: + eval_move_failed_msg(eval_result); } - break; - default: - eval_move_failed_msg(eval_result); + clrtoeol(); } - clrtoeol(); } } } @@ -346,12 +364,12 @@ memset(&gamestate, 0, sizeof(GameState)); init_board(&gamestate); gamestate.mycolor = WHITE; - // TODO: time limit + _Bool running; do { clear(); draw_board(&gamestate); - running = !domove_singlemachine(&gamestate); + running = !domove_singlemachine(&gamestate, &(settings->gameinfo)); gamestate.mycolor = opponent_color(gamestate.mycolor); } while (running); move(0,0);