diff --git a/lines.c b/lines.c index 943ec44..1d8ca29 100644 --- a/lines.c +++ b/lines.c @@ -17,10 +17,10 @@ typedef struct _Cell { } Cell; typedef struct _Game { - Cell CUR_CELL; - Cell CUR_BRICK; - int SCORE; - unsigned int BOARD[9][9]; + Cell cur_cell; + Cell cur_brick; + int score; + unsigned int board[9][9]; // TODO: N_TURNS /* int N_FREE_CELLS = 9 * 9; */ } Game; @@ -62,24 +62,24 @@ void select_cell(Game *G, int y, int x) wrefresh(BOARD_WIN); return; } - deselect_cell(G->CUR_CELL.y, G->CUR_CELL.x); - if (G->CUR_BRICK.y != -1 && G->CUR_BRICK.x != -1) { - draw_cell_border(G->CUR_BRICK.y, G->CUR_BRICK.x, COLOR_PAIR(3)); + deselect_cell(G->cur_cell.y, G->cur_cell.x); + if (G->cur_brick.y != -1 && G->cur_brick.x != -1) { + draw_cell_border(G->cur_brick.y, G->cur_brick.x, COLOR_PAIR(3)); } - G->CUR_CELL.y = y; - G->CUR_CELL.x = x; + G->cur_cell.y = y; + G->cur_cell.x = x; draw_cell_border(y, x, COLOR_PAIR(2)); } // TODO: make it blink? void select_brick(Game *G, int y, int x) { - if (G->CUR_BRICK.y != -1 && G->CUR_BRICK.x != -1) { - deselect_cell(G->CUR_BRICK.y, G->CUR_BRICK.x); + if (G->cur_brick.y != -1 && G->cur_brick.x != -1) { + deselect_cell(G->cur_brick.y, G->cur_brick.x); } draw_cell_border(y, x, COLOR_PAIR(3)); - G->CUR_BRICK.y = y; - G->CUR_BRICK.x = x; + G->cur_brick.y = y; + G->cur_brick.x = x; } void fill_cell(int y, int x, short pair) @@ -98,7 +98,7 @@ void fill_cell(int y, int x, short pair) void put_brick(Game *G, int y, int x, short color) { - G->BOARD[y][x] = color; + G->board[y][x] = color; fill_cell(y, x, COLOR_PAIR(10 + color)); } @@ -131,7 +131,7 @@ Cell** get_adj_cells(Game *G, int y, int x, int* adj_cells_n) } if (adj_y >= 0 && adj_x >= 0 && adj_y < 9 && adj_x < 9 && i * j == 0 && (i != 0 || j != 0) && - G->BOARD[adj_y][adj_x] == 0) + G->board[adj_y][adj_x] == 0) { c_ptr = malloc(sizeof (Cell)); c_ptr->y = adj_y; @@ -156,9 +156,9 @@ int dist(int from_y, int from_x, int to_y, int to_x) int compare_cells(const void *a, const void *b, void *G) { int d_a = dist((*((Cell **) a))->y, (*((Cell **) a))->x, - ((Game *) G)->CUR_CELL.y, ((Game *) G)->CUR_CELL.x); + ((Game *) G)->cur_cell.y, ((Game *) G)->cur_cell.x); int d_b = dist((*((Cell **) b))->y, (*((Cell **) b))->x, - ((Game *) G)->CUR_CELL.y, ((Game *) G)->CUR_CELL.x); + ((Game *) G)->cur_cell.y, ((Game *) G)->cur_cell.x); return d_a - d_b; } @@ -197,7 +197,7 @@ bool path_exists(Game *G, int from_y, int from_x, int to_y, int to_x, bool visit void remove_brick(Game *G, int y, int x, bool draw_border) { - G->BOARD[y][x] = 0; + G->board[y][x] = 0; fill_cell(y, x, COLOR_PAIR(1)); if (draw_border) draw_cell_border(y, x, COLOR_PAIR(4)); @@ -211,12 +211,12 @@ bool move_selected_brick(Game *G, int y, int x) visited[i][j] = false; } } - if (path_exists(G, G->CUR_BRICK.y, G->CUR_BRICK.x, y, x, visited, 0)) { - put_brick(G, y, x, G->BOARD[G->CUR_BRICK.y][G->CUR_BRICK.x]); - remove_brick(G, G->CUR_BRICK.y, G->CUR_BRICK.x, false); - deselect_cell(G->CUR_BRICK.y, G->CUR_BRICK.x); - G->CUR_BRICK.y = -1; - G->CUR_BRICK.x = -1; + if (path_exists(G, G->cur_brick.y, G->cur_brick.x, y, x, visited, 0)) { + put_brick(G, y, x, G->board[G->cur_brick.y][G->cur_brick.x]); + remove_brick(G, G->cur_brick.y, G->cur_brick.x, false); + deselect_cell(G->cur_brick.y, G->cur_brick.x); + G->cur_brick.y = -1; + G->cur_brick.x = -1; select_cell(G, y, x); return true; } else { @@ -229,10 +229,10 @@ bool move_selected_brick(Game *G, int y, int x) // TODO: records db void score(Game *G, int n) { - G->SCORE += (int) pow((double) 10, (double) n - 5); + G->score += (int) pow((double) 10, (double) n - 5); wmove(INFO_WIN, 1, 1); wclrtoeol(INFO_WIN); - wprintw(INFO_WIN, "SCORE: %d", G->SCORE); + wprintw(INFO_WIN, "score: %d", G->score); wrefresh(INFO_WIN); } @@ -242,11 +242,10 @@ void reduce_bricks(Game *G, int at_y, int at_x) // rainbow order! // TODO: slow the animation a bit - int match_n, match_color, y, x, start_i, stop_i; + int match_n, y, x, start_i, stop_i; Cell axes[] = { { 1, 0 }, { 0, 1 }, { 1, 1 }, { 1, -1 } }; int dir[] = { -1, 1 }; - match_color = G->BOARD[at_y][at_x]; match_n = 1; for (int a = 0; a < 4; a++) { @@ -255,7 +254,7 @@ void reduce_bricks(Game *G, int at_y, int at_x) y = at_y + axes[a].y * dir[d] * i; x = at_x + axes[a].x * dir[d] * i; if (y > 9 || y < 0 || x > 9 || x < 0 || - G->BOARD[y][x] != match_color) { + G->board[y][x] != G->board[at_y][at_x]) { if (dir[d] == -1) { start_i = i; } else { @@ -293,7 +292,7 @@ void put_bricks_random(Game *G, int n) int cell_idx = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { - if (G->BOARD[i][j] == 0) { + if (G->board[i][j] == 0) { Cell c = { i, j }; free_cells[n_free_cells++] = c; } @@ -313,7 +312,7 @@ int get_n_free_cells(Game *G) int n_free_cells = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { - n_free_cells += (G->BOARD[i][j] == 0); + n_free_cells += (G->board[i][j] == 0); } } return n_free_cells; @@ -345,11 +344,11 @@ int main() Game G; // FIXME: Initialize via {} - G.CUR_CELL.x = 0; - G.CUR_CELL.y = 0; - G.CUR_BRICK.x = -1; - G.CUR_BRICK.y = -1; - G.SCORE = 0; + G.cur_cell.x = 0; + G.cur_cell.y = 0; + G.cur_brick.x = -1; + G.cur_brick.y = -1; + G.score = 0; board_win_height = CELL_HEIGHT * 9 - 8; board_win_width = CELL_WIDTH * 9 - 8; @@ -395,7 +394,7 @@ int main() // Initialize the board for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { - G.BOARD[i][j] = 0; + G.board[i][j] = 0; } } @@ -411,29 +410,29 @@ int main() switch (ch = wgetch(BOARD_WIN)) { case 'h': case KEY_LEFT: - select_cell(&G, G.CUR_CELL.y, G.CUR_CELL.x - 1); + select_cell(&G, G.cur_cell.y, G.cur_cell.x - 1); break; case 'l': case KEY_RIGHT: - select_cell(&G, G.CUR_CELL.y, G.CUR_CELL.x + 1); + select_cell(&G, G.cur_cell.y, G.cur_cell.x + 1); break; case 'k': case KEY_UP: - select_cell(&G, G.CUR_CELL.y - 1, G.CUR_CELL.x); + select_cell(&G, G.cur_cell.y - 1, G.cur_cell.x); break; case 'j': case KEY_DOWN: - select_cell(&G, G.CUR_CELL.y + 1, G.CUR_CELL.x); + select_cell(&G, G.cur_cell.y + 1, G.cur_cell.x); break; case 'q': die(EXIT_SUCCESS); break; case ' ': - if (G.BOARD[G.CUR_CELL.y][G.CUR_CELL.x] != 0) { - select_brick(&G, G.CUR_CELL.y, G.CUR_CELL.x); - } else if (G.CUR_BRICK.y != -1) { - if (move_selected_brick(&G, G.CUR_CELL.y, G.CUR_CELL.x)) { - reduce_bricks(&G, G.CUR_CELL.y, G.CUR_CELL.x); + if (G.board[G.cur_cell.y][G.cur_cell.x] != 0) { + select_brick(&G, G.cur_cell.y, G.cur_cell.x); + } else if (G.cur_brick.y != -1) { + if (move_selected_brick(&G, G.cur_cell.y, G.cur_cell.x)) { + reduce_bricks(&G, G.cur_cell.y, G.cur_cell.x); end_turn = true; } } @@ -454,7 +453,7 @@ int main() (LINES - msg_height) / 2, (COLS - msg_width) / 2); box(message_win, 0, 0); mvwprintw(message_win, 0, (msg_width - 20) / 2 , " Very well indeed! "); - mvwprintw(message_win, 2, 2, "You scored %d points.", G.SCORE); + mvwprintw(message_win, 2, 2, "You scored %d points.", G.score); mvwprintw(message_win, 4, 2, "Have another try."); wrefresh(message_win); getch();