src/game.c

changeset 30
a285ee393860
parent 28
0c1371488d87
child 31
ed440bcd9740
     1.1 --- a/src/game.c	Mon Apr 07 14:08:57 2014 +0200
     1.2 +++ b/src/game.c	Mon Apr 07 17:39:46 2014 +0200
     1.3 @@ -32,11 +32,27 @@
     1.4  #include "input.h"
     1.5  #include <ncurses.h>
     1.6  #include <string.h>
     1.7 +#include <inttypes.h>
     1.8  
     1.9  static const uint8_t boardx = 10, boardy = 10;
    1.10  
    1.11 +static void draw_time(GameState *gamestate, GameInfo *gameinfo) {
    1.12 +    if (gameinfo->timecontrol) {
    1.13 +        // TODO: correct time display
    1.14 +        
    1.15 +        uint16_t whitem = gameinfo->time / 60;
    1.16 +        uint16_t whites = gameinfo->time % 60;
    1.17 +        uint16_t blackm = gameinfo->time / 60;
    1.18 +        uint16_t blacks = gameinfo->time % 60;
    1.19 +        
    1.20 +        mvprintw(boardy+4, boardx-1,
    1.21 +            "White time: %4" PRIu16 ":%02" PRIu16, whitem, whites);
    1.22 +        mvprintw(boardy+5, boardx-1,
    1.23 +            "Black time: %4" PRIu16 ":%02" PRIu16, blackm, blacks);
    1.24 +    }
    1.25 +}
    1.26 +
    1.27  static void draw_board(GameState *gamestate) {
    1.28 -    
    1.29      for (uint8_t y = 0 ; y < 8 ; y++) {
    1.30          for (uint8_t x = 0 ; x < 8 ; x++) {
    1.31              uint8_t col = gamestate->board[y][x] & COLOR_MASK;
    1.32 @@ -134,13 +150,14 @@
    1.33      }
    1.34  }
    1.35  
    1.36 -static int domove_singlemachine(GameState *gamestate) {
    1.37 +static int domove_singlemachine(GameState *gamestate, GameInfo *gameinfo) {
    1.38      
    1.39      const size_t buflen = 8;
    1.40      char movestr[buflen];
    1.41      
    1.42      int inputy = getmaxy(stdscr) - 6;
    1.43      while (1) {
    1.44 +        draw_time(gamestate, gameinfo);
    1.45          move(inputy, 0);
    1.46          printw(
    1.47              "Use chess notation to enter your move.\n"
    1.48 @@ -148,45 +165,46 @@
    1.49              "Type your move: ");
    1.50          clrtoeol();
    1.51          refresh();
    1.52 -        getnstr(movestr, buflen);
    1.53 -
    1.54 -        if (strncmp(movestr, "surr", buflen) == 0) {
    1.55 -            printw("%s surrendered!",
    1.56 -                gamestate->mycolor==WHITE?"White":"Black");
    1.57 -            clrtoeol();
    1.58 -            refresh();
    1.59 -            return 1;
    1.60 -        } else if (strncmp(movestr, "remis", buflen) == 0) {
    1.61 -            printw("Game ends remis.");
    1.62 -            clrtoeol();
    1.63 -            refresh();
    1.64 -            return 1;
    1.65 -        } else {
    1.66 -            Move move;
    1.67 -            int eval_result = eval_move(gamestate, movestr, &move);
    1.68 -            switch (eval_result) {
    1.69 -            case VALID_MOVE_SYNTAX:
    1.70 -                if (validate_move(gamestate, &move)) {
    1.71 -                    apply_move(gamestate, &move);
    1.72 -                    if (gamestate->checkmate) {
    1.73 -                        printw("Checkmate!");
    1.74 -                        clrtoeol();
    1.75 -                        return 1;
    1.76 -                    } else if (gamestate->stalemate) {
    1.77 -                        printw("Stalemate!");
    1.78 -                        clrtoeol();
    1.79 -                        return 1;
    1.80 +        
    1.81 +        if (asyncgetnstr(movestr, buflen)) {
    1.82 +            if (strncmp(movestr, "surr", buflen) == 0) {
    1.83 +                printw("%s surrendered!",
    1.84 +                    gamestate->mycolor==WHITE?"White":"Black");
    1.85 +                clrtoeol();
    1.86 +                refresh();
    1.87 +                return 1;
    1.88 +            } else if (strncmp(movestr, "remis", buflen) == 0) {
    1.89 +                printw("Game ends remis.");
    1.90 +                clrtoeol();
    1.91 +                refresh();
    1.92 +                return 1;
    1.93 +            } else {
    1.94 +                Move move;
    1.95 +                int eval_result = eval_move(gamestate, movestr, &move);
    1.96 +                switch (eval_result) {
    1.97 +                case VALID_MOVE_SYNTAX:
    1.98 +                    if (validate_move(gamestate, &move)) {
    1.99 +                        apply_move(gamestate, &move);
   1.100 +                        if (gamestate->checkmate) {
   1.101 +                            printw("Checkmate!");
   1.102 +                            clrtoeol();
   1.103 +                            return 1;
   1.104 +                        } else if (gamestate->stalemate) {
   1.105 +                            printw("Stalemate!");
   1.106 +                            clrtoeol();
   1.107 +                            return 1;
   1.108 +                        } else {
   1.109 +                            return 0;
   1.110 +                        }
   1.111                      } else {
   1.112 -                        return 0;
   1.113 +                        printw("Invalid move.");
   1.114                      }
   1.115 -                } else {
   1.116 -                    printw("Invalid move.");
   1.117 +                    break;
   1.118 +                default:
   1.119 +                    eval_move_failed_msg(eval_result);
   1.120                  }
   1.121 -                break;
   1.122 -            default:
   1.123 -                eval_move_failed_msg(eval_result);
   1.124 +                clrtoeol();
   1.125              }
   1.126 -            clrtoeol();
   1.127          }
   1.128      }
   1.129  }
   1.130 @@ -346,12 +364,12 @@
   1.131      memset(&gamestate, 0, sizeof(GameState));
   1.132      init_board(&gamestate);
   1.133      gamestate.mycolor = WHITE;
   1.134 -    // TODO: time limit
   1.135 +
   1.136      _Bool running;
   1.137      do {
   1.138          clear();
   1.139          draw_board(&gamestate);
   1.140 -        running = !domove_singlemachine(&gamestate);
   1.141 +        running = !domove_singlemachine(&gamestate, &(settings->gameinfo));
   1.142          gamestate.mycolor = opponent_color(gamestate.mycolor);
   1.143      }  while (running);
   1.144      move(0,0);

mercurial