diff -r 41bbfd4d17a3 -r 824c9522ce66 src/game.c --- a/src/game.c Mon Mar 31 14:08:00 2014 +0200 +++ b/src/game.c Mon Mar 31 15:03:25 2014 +0200 @@ -35,12 +35,12 @@ static const uint8_t boardx = 10, boardy = 10; -static void draw_board(Board board, MoveListRoot *movelist, uint8_t mycolor) { +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 = board[y][x] & COLOR_MASK; - uint8_t piece = board[y][x] & PIECE_MASK; + uint8_t col = gamestate->board[y][x] & COLOR_MASK; + uint8_t piece = gamestate->board[y][x] & PIECE_MASK; char piecec; if (piece) { piecec = piece == PAWN ? 'P' : getpiecechr(piece); @@ -51,8 +51,8 @@ attrset((col == WHITE ? A_BOLD : A_DIM) | COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW)); - int cy = mycolor == WHITE ? boardy-y : boardy-7+y; - int cx = mycolor == WHITE ? boardx+x*3 : boardx+21-x*3; + int cy = gamestate->mycolor == WHITE ? boardy-y : boardy-7+y; + int cx = gamestate->mycolor == WHITE ? boardx+x*3 : boardx+21-x*3; mvaddch(cy, cx, ' '); mvaddch(cy, cx+1, piecec); mvaddch(cy, cx+2, ' '); @@ -61,8 +61,8 @@ attrset(A_NORMAL); for (uint8_t i = 0 ; i < 8 ; i++) { - int x = mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3; - int y = mycolor == WHITE ? boardy-i : boardy-7+i; + int x = gamestate->mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3; + int y = gamestate->mycolor == WHITE ? boardy-i : boardy-7+i; mvaddch(boardy+1, x, 'a'+i); mvaddch(y, boardx-2, '1'+i); } @@ -72,14 +72,14 @@ uint8_t logy = 0; const uint8_t logx = boardx + 30; int logi = 1; - MoveList *logelem = movelist->first; + MoveList *logelem = gamestate->movelist; while (logelem) { logi++; if (logi % 2 == 0) { if ((logi - 2) % 4 == 0) { logy++; - wmove(tchess_window, logy, logx); + move(logy, logx); } printw("%d. ", logi / 2); } @@ -108,15 +108,14 @@ } -static int sendmove(Board board, MoveListRoot *movelist, - uint8_t mycolor, int opponent) { +static int sendmove(GameState *gamestate, int opponent) { const size_t buflen = 8; char movestr[buflen]; _Bool remisrejected = FALSE; uint8_t code; - int inputy = getmaxy(tchess_window) - 6; + int inputy = getmaxy(stdscr) - 6; while (1) { move(inputy, 0); if (remisrejected) { @@ -156,7 +155,7 @@ } } else { Move move; - int eval_result = eval_move(board, mycolor, movestr, &move); + int eval_result = eval_move(gamestate, movestr, &move); switch (eval_result) { case VALID_MOVE_SYNTAX: net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move)); @@ -166,8 +165,7 @@ if (code == NETCODE_DECLINE) { printw("Invalid move."); } else { - apply_move(board, &move); - addmove(movelist, &move); + apply_move(gamestate, &move); if (move.checkmate) { printw("Checkmate!"); clrtoeol(); @@ -194,9 +192,9 @@ } } -static int recvmove(Board board, MoveListRoot *movelist, int opponent) { +static int recvmove(GameState *gamestate, int opponent) { - int inputy = getmaxy(tchess_window) - 6; + int inputy = getmaxy(stdscr) - 6; while (1) { move(inputy, 0); printw("Awaiting opponent move..."); @@ -225,9 +223,8 @@ break; case NETCODE_MOVE: net_recieve_data(opponent, &move, sizeof(Move)); - if (validate_move(board, &move)) { - apply_move(board, &move); - addmove(movelist, &move); + if (validate_move(gamestate, &move)) { + apply_move(gamestate, &move); if (move.check) { net_send_code(opponent, NETCODE_CHECK); } else if (move.checkmate) { @@ -243,40 +240,12 @@ } } -void freemovelist(MoveListRoot* list) { - MoveList *elem; - elem = list->first; - while (elem) { - MoveList *cur = elem; - elem = elem->next; - free(cur); - }; - free(list); -} - -void addmove(MoveListRoot* list, Move *move) { - MoveList *elem = malloc(sizeof(MoveList)); - elem->next = NULL; - elem->move = *move; - - if (list->last) { - list->last->next = elem; - list->last = elem; - } else { - list->first = list->last = elem; - } -} - void game_start(Settings *settings, int opponent) { _Bool myturn = is_server(settings) == (settings->gameinfo.servercolor == WHITE); - uint8_t mycolor = myturn ? WHITE:BLACK; - _Bool running; - - MoveListRoot* movelist = calloc(1, sizeof(MoveListRoot)); - - Board board = { + GameState gamestate; + Board initboard = { {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK}, {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN}, {0, 0, 0, 0, 0, 0, 0, 0}, @@ -286,22 +255,26 @@ {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN}, {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK} }; + memcpy(gamestate.board, initboard, sizeof(Board)); + gamestate.mycolor = myturn ? WHITE:BLACK; + gamestate.movelist = gamestate.lastmove = NULL; + _Bool running; do { clear(); - draw_board(board, movelist, mycolor); + draw_board(&gamestate); if (myturn) { - running = !sendmove(board, movelist, mycolor, opponent); + running = !sendmove(&gamestate, opponent); } else { - running = !recvmove(board, movelist, opponent); + running = !recvmove(&gamestate, opponent); flushinp(); // flush any input the user hacked in while waiting } myturn ^= TRUE; } while (running); - freemovelist(movelist); + gamestate_cleanup(&gamestate); - mvaddstr(getmaxy(tchess_window)-1, 0, + mvaddstr(getmaxy(stdscr)-1, 0, "Game has ended. Press any key to leave..."); getch(); }