src/game.c

changeset 51
84f2e380a434
parent 50
41017d0a72c5
child 53
78b580bfde33
     1.1 --- a/src/game.c	Mon Jun 16 13:45:31 2014 +0200
     1.2 +++ b/src/game.c	Mon Jun 16 15:41:06 2014 +0200
     1.3 @@ -174,7 +174,7 @@
     1.4      
     1.5      char filename[64];
     1.6      int y = getcury(stdscr);
     1.7 -    if (getnstr(filename, 64) == OK) {
     1.8 +    if (getnstr(filename, 64) == OK && filename[0] != '\0') {
     1.9          move(y, 0);
    1.10          FILE *file = fopen(filename, "w");
    1.11          if (file) {
    1.12 @@ -322,8 +322,7 @@
    1.13                  int eval_result = eval_move(gamestate, movestr, &move, mycolor);
    1.14                  switch (eval_result) {
    1.15                  case VALID_MOVE_SYNTAX:
    1.16 -                    net_send_data(opponent, NETCODE_MOVE, &move,
    1.17 -                        sizeof(Move)-8);
    1.18 +                    net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
    1.19                      code = net_recieve_code(opponent);
    1.20                      move.check = code == NETCODE_CHECK ||
    1.21                          code == NETCODE_CHECKMATE;
    1.22 @@ -423,13 +422,11 @@
    1.23                  }
    1.24                  break;
    1.25              case NETCODE_MOVE:
    1.26 -                net_recieve_data(opponent, &move, sizeof(Move)-8);
    1.27 +                net_recieve_data(opponent, &move, sizeof(Move));
    1.28                  code = validate_move(gamestate, &move);
    1.29                  if (code == VALID_MOVE_SEMANTICS) {
    1.30                      apply_move(gamestate, &move);
    1.31 -                    if (move.check) {
    1.32 -                        net_send_code(opponent, NETCODE_CHECK);
    1.33 -                    } else if (gamestate->checkmate) {
    1.34 +                    if (gamestate->checkmate) {
    1.35                          net_send_code(opponent, NETCODE_CHECKMATE);
    1.36                          printw("\rCheckmate!");
    1.37                          clrtoeol();
    1.38 @@ -439,6 +436,8 @@
    1.39                          printw("\rStalemate!");
    1.40                          clrtoeol();
    1.41                          return 1;
    1.42 +                    } else if (move.check) {
    1.43 +                        net_send_code(opponent, NETCODE_CHECK);
    1.44                      } else {
    1.45                          net_send_code(opponent, NETCODE_ACCEPT);
    1.46                      }
    1.47 @@ -523,28 +522,34 @@
    1.48      post_game(&gamestate, &(settings->gameinfo));
    1.49  }
    1.50  
    1.51 -void game_start(Settings *settings, int opponent) {
    1.52 +void game_continue(Settings *settings, int opponent, GameState *gamestate) {
    1.53      inputy = getmaxy(stdscr) - 6;
    1.54      
    1.55 -    _Bool myturn = is_server(settings) ==
    1.56 -        (settings->gameinfo.servercolor == WHITE);
    1.57 -    uint8_t mycolor = myturn ? WHITE : BLACK;
    1.58 +    uint8_t mycolor = is_server(settings) ? settings->gameinfo.servercolor :
    1.59 +        opponent_color(settings->gameinfo.servercolor);
    1.60      
    1.61 -    GameState gamestate;
    1.62 -    gamestate_init(&gamestate);
    1.63 +    _Bool myturn = (gamestate->lastmove ?
    1.64 +        (gamestate->lastmove->move.piece & COLOR_MASK) : WHITE) != mycolor;
    1.65      
    1.66      _Bool running;
    1.67      do {
    1.68          clear();
    1.69 -        draw_board(&gamestate, mycolor);
    1.70 +        draw_board(gamestate, mycolor);
    1.71          if (myturn) {
    1.72 -            running = !sendmove(&gamestate, &(settings->gameinfo),
    1.73 +            running = !sendmove(gamestate, &(settings->gameinfo),
    1.74                  opponent, mycolor);
    1.75          } else {
    1.76 -            running = !recvmove(&gamestate, &(settings->gameinfo), opponent);
    1.77 +            running = !recvmove(gamestate, &(settings->gameinfo), opponent);
    1.78          }
    1.79          myturn ^= TRUE;
    1.80      }  while (running);
    1.81      
    1.82 -    post_game(&gamestate, &(settings->gameinfo));
    1.83 +    post_game(gamestate, &(settings->gameinfo));
    1.84  }
    1.85 +
    1.86 +void game_start(Settings *settings, int opponent) {
    1.87 +    GameState gamestate;
    1.88 +    gamestate_init(&gamestate);
    1.89 +    
    1.90 +    game_continue(settings, opponent, &gamestate);
    1.91 +}

mercurial