69 } |
69 } |
70 |
70 |
71 return 0; |
71 return 0; |
72 } |
72 } |
73 |
73 |
74 static void draw_board(GameState *gamestate, uint8_t perspective) { |
74 static void draw_board(GameState *gamestate, |
|
75 uint8_t perspective, |
|
76 _Bool unicode) { |
75 char fen[90]; |
77 char fen[90]; |
76 compute_fen(fen, gamestate); |
78 compute_fen(fen, gamestate); |
77 mvaddstr(0, 0, fen); |
79 mvaddstr(0, 0, fen); |
78 |
80 |
79 for (uint8_t y = 0 ; y < 8 ; y++) { |
81 for (uint8_t y = 0 ; y < 8 ; y++) { |
80 for (uint8_t x = 0 ; x < 8 ; x++) { |
82 for (uint8_t x = 0 ; x < 8 ; x++) { |
81 uint8_t col = gamestate->board[y][x] & COLOR_MASK; |
83 uint8_t col = gamestate->board[y][x] & COLOR_MASK; |
82 uint8_t piece = gamestate->board[y][x] & PIECE_MASK; |
84 uint8_t piece = gamestate->board[y][x] & PIECE_MASK; |
83 char piecec; |
85 unsigned char piecestr[5]; |
84 if (piece) { |
86 if (piece) { |
85 piecec = piece == PAWN ? 'P' : getpiecechr(piece); |
87 if (unicode) { |
|
88 unsigned char* uc = getpieceunicode(piece); |
|
89 strncpy(piecestr, uc, 5); |
|
90 } else { |
|
91 piecestr[0] = piece == PAWN ? 'P' : getpiecechr(piece); |
|
92 piecestr[1] = '\0'; |
|
93 } |
86 } else { |
94 } else { |
87 piecec = ' '; |
95 piecestr[0] = ' '; |
|
96 piecestr[1] = '\0'; |
88 } |
97 } |
89 |
98 |
90 _Bool boardblack = (y&1)==(x&1); |
99 _Bool boardblack = (y&1)==(x&1); |
91 attrset((col==WHITE ? A_BOLD : A_DIM)| |
100 attrset((col==WHITE ? A_BOLD : A_DIM)| |
92 COLOR_PAIR(col == WHITE ? |
101 COLOR_PAIR(col == WHITE ? |
95 ) |
104 ) |
96 ); |
105 ); |
97 |
106 |
98 int cy = perspective == WHITE ? boardy-y : boardy-7+y; |
107 int cy = perspective == WHITE ? boardy-y : boardy-7+y; |
99 int cx = perspective == WHITE ? boardx+x*3 : boardx+21-x*3; |
108 int cx = perspective == WHITE ? boardx+x*3 : boardx+21-x*3; |
100 mvaddch(cy, cx, ' '); |
109 mvprintw(cy, cx, " %s ", piecestr); |
101 mvaddch(cy, cx+1, piecec); |
|
102 mvaddch(cy, cx+2, ' '); |
|
103 } |
110 } |
104 } |
111 } |
105 |
112 |
106 attrset(A_NORMAL); |
113 attrset(A_NORMAL); |
107 for (uint8_t i = 0 ; i < 8 ; i++) { |
114 for (uint8_t i = 0 ; i < 8 ; i++) { |
458 } |
465 } |
459 } |
466 } |
460 } |
467 } |
461 } |
468 } |
462 |
469 |
463 static void post_game(GameState *gamestate, GameInfo *gameinfo) { |
470 static void post_game(Settings* settings, GameState *gamestate) { |
|
471 GameInfo *gameinfo = &(settings->gameinfo); |
|
472 |
464 move(0,0); |
473 move(0,0); |
465 draw_board(gamestate, WHITE); |
474 draw_board(gamestate, WHITE, settings->unicode); |
466 |
475 |
467 // TODO: network connection is still open here - think about it! |
476 // TODO: network connection is still open here - think about it! |
468 |
477 |
469 mvaddstr(getmaxy(stdscr)-1, 0, |
478 mvaddstr(getmaxy(stdscr)-1, 0, |
470 "Press 'q' to quit or 's' to save a PGN file..."); |
479 "Press 'q' to quit or 's' to save a PGN file..."); |
518 } |
527 } |
519 |
528 |
520 _Bool running; |
529 _Bool running; |
521 do { |
530 do { |
522 clear(); |
531 clear(); |
523 draw_board(&gamestate, curcol); |
532 draw_board(&gamestate, curcol, settings->unicode); |
524 running = !domove_singlemachine(&gamestate, |
533 running = !domove_singlemachine(&gamestate, |
525 &(settings->gameinfo), curcol); |
534 &(settings->gameinfo), curcol); |
526 curcol = opponent_color(curcol); |
535 curcol = opponent_color(curcol); |
527 } while (running); |
536 } while (running); |
528 |
537 |
529 post_game(&gamestate, &(settings->gameinfo)); |
538 post_game(settings, &gamestate); |
530 } |
539 } |
531 |
540 |
532 void game_continue(Settings *settings, int opponent, GameState *gamestate) { |
541 void game_continue(Settings *settings, int opponent, GameState *gamestate) { |
533 inputy = getmaxy(stdscr) - 6; |
542 inputy = getmaxy(stdscr) - 6; |
534 |
543 |
539 (gamestate->lastmove->move.piece & COLOR_MASK) : BLACK) != mycolor; |
548 (gamestate->lastmove->move.piece & COLOR_MASK) : BLACK) != mycolor; |
540 |
549 |
541 _Bool running; |
550 _Bool running; |
542 do { |
551 do { |
543 clear(); |
552 clear(); |
544 draw_board(gamestate, mycolor); |
553 draw_board(gamestate, mycolor, settings->unicode); |
545 if (myturn) { |
554 if (myturn) { |
546 running = !sendmove(gamestate, &(settings->gameinfo), |
555 running = !sendmove(gamestate, &(settings->gameinfo), |
547 opponent, mycolor); |
556 opponent, mycolor); |
548 } else { |
557 } else { |
549 running = !recvmove(gamestate, &(settings->gameinfo), opponent); |
558 running = !recvmove(gamestate, &(settings->gameinfo), opponent); |
550 } |
559 } |
551 myturn ^= TRUE; |
560 myturn ^= TRUE; |
552 } while (running); |
561 } while (running); |
553 |
562 |
554 post_game(gamestate, &(settings->gameinfo)); |
563 post_game(settings, gamestate); |
555 } |
564 } |
556 |
565 |
557 void game_start(Settings *settings, int opponent) { |
566 void game_start(Settings *settings, int opponent) { |
558 GameState gamestate; |
567 GameState gamestate; |
559 gamestate_init(&gamestate); |
568 gamestate_init(&gamestate); |