Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <unistd.h>
- #include <termios.h>
- #include <string.h>
- #include <time.h>
- #define BOARDSIZE 4
- #define CELL(board, x, y) ((board)[(x) * BOARDSIZE + (y)])
- enum direction {UP, LEFT, DOWN, RIGHT};
- typedef enum direction dir_t;
- int *init_board() {
- int *board = calloc(BOARDSIZE * BOARDSIZE, sizeof(int));
- return board;
- }
- int disable_newline(struct termios *oldt) {
- struct termios newt;
- tcgetattr(STDIN_FILENO, oldt);
- memcpy(&newt, oldt, sizeof(struct termios));
- newt.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(STDIN_FILENO, TCSANOW, &newt);
- // Actual error checking some day.
- return 0;
- }
- void redraw_board(int *board) {
- int i, j;
- for (i = 0; i < BOARDSIZE; i++) {
- for (j = 0; j < BOARDSIZE; j++) {
- printf(" %4d ", CELL(board, i, j));
- }
- putchar('\n');
- }
- putchar('\n');
- }
- void add_random(int *board) {
- int x, y;
- do {
- x = rand() % BOARDSIZE;
- y = rand() % BOARDSIZE;
- } while (CELL(board, x, y) != 0);
- CELL(board, x, y) = 2;
- }
- int move(int *board, dir_t where) {
- int target = -1;
- int moved = 0;
- switch(where) {
- case RIGHT:
- for (int i = 0; i < BOARDSIZE; i++) {
- target = BOARDSIZE - 1;
- for (int j = BOARDSIZE - 1; j >= 0; j--) {
- if (CELL(board, i, j) == 0) {
- ;
- } else if (target == j) {
- target--;
- } else {
- CELL(board, i, target) = CELL(board, i, j);
- CELL(board, i, j) = 0;
- target--;
- moved = 1;
- }
- }
- }
- break;
- case LEFT:
- for (int i = 0; i < BOARDSIZE; i++) {
- target = 0;
- for (int j = 0; j < BOARDSIZE; j++) {
- if (CELL(board, i, j) == 0) {
- ;
- } else if (target == j) {
- target++;
- } else {
- CELL(board, i, target) = CELL(board, i, j);
- CELL(board, i, j) = 0;
- target++;
- moved = 1;
- }
- }
- }
- break;
- case DOWN:
- for (int j = 0; j < BOARDSIZE; j++) {
- target = BOARDSIZE - 1;
- for (int i = BOARDSIZE - 1; i >= 0; i--) {
- if (CELL(board, i, j) == 0) {
- ;
- } else if (target == i) {
- target--;
- } else {
- CELL(board, target, j) = CELL(board, i, j);
- CELL(board, i, j) = 0;
- target--;
- moved = 1;
- }
- }
- }
- break;
- case UP:
- for (int j = 0; j < BOARDSIZE; j++) {
- target = 0;
- for (int i = 0; i < BOARDSIZE; i++) {
- if (CELL(board, i, j) == 0) {
- ;
- } else if (target == i) {
- target++;
- } else {
- CELL(board, target, j) = CELL(board, i, j);
- CELL(board, i, j) = 0;
- target++;
- moved = 1;
- }
- }
- }
- break;
- default:
- break;
- }
- return moved;
- }
- int merge(int *board, dir_t where) {
- int merged = 0;
- switch(where) {
- case RIGHT:
- for (int i = 0; i < BOARDSIZE; i++) {
- for (int j = BOARDSIZE - 1; j >= 0 + 1; j--) {
- if (CELL(board, i, j) == 0) {
- ;
- } else if (CELL(board, i, j) == CELL(board, i, j - 1)) {
- CELL(board, i, j) *= 2;
- CELL(board, i, j - 1) = 0;
- /* Skip over next cell (one we just merged into) to
- * prevent unexpected double merging) */
- j--;
- merged = 1;
- }
- }
- }
- break;
- case LEFT:
- for (int i = 0; i < BOARDSIZE; i++) {
- for (int j = 0; j <= BOARDSIZE - 1 - 1; j++) {
- if (CELL(board, i, j) == 0) {
- ;
- } else if (CELL(board, i, j) == CELL(board, i, j + 1)) {
- CELL(board, i, j) *= 2;
- CELL(board, i, j + 1) = 0;
- j++;
- merged = 1;
- }
- }
- }
- break;
- case DOWN:
- for (int j = 0; j < BOARDSIZE; j++) {
- for (int i = BOARDSIZE - 1; i >= 0 + 1; i--) {
- if (CELL(board, i, j) == 0) {
- ;
- } else if (CELL(board, i, j) == CELL(board, i - 1, j)) {
- CELL(board, i, j) *= 2;
- CELL(board, i - 1, j) = 0;
- i++;
- merged = 1;
- }
- }
- }
- break;
- case UP:
- for (int j = 0; j < BOARDSIZE; j++) {
- for (int i = 0; i <= BOARDSIZE - 1 - 1; i++) {
- if (CELL(board, i, j) == 0) {
- ;
- } else if (CELL(board, i, j) == CELL(board, i + 1, j)) {
- CELL(board, i, j) *= 2;
- CELL(board, i + 1, j) = 0;
- i--;
- merged = 1;
- }
- }
- }
- break;
- default:
- break;
- }
- return merged;
- }
- int move_possible(int *board) {
- // Check if there's a 0 on the board that allows a move
- for (int i = 0; i < BOARDSIZE * BOARDSIZE; i++) {
- if (!board[i]) {
- return 1;
- }
- }
- // Check if there's a horizontal merge possible
- for (int i = 0; i < BOARDSIZE; i++) {
- for (int j = 0; j < BOARDSIZE - 1; j++) {
- if (CELL(board, i, j) == CELL(board, i, j + 1)) {
- return 1;
- }
- }
- }
- // Check if there's a vertical merge possible
- for (int j = 0; j < BOARDSIZE; j++) {
- for (int i = 0; i < BOARDSIZE - 1; i++) {
- if (CELL(board, i, j) == CELL(board, i + 1, j)) {
- return 1;
- }
- }
- }
- return 0;
- }
- int main(int argc, char *argv[]) {
- srand(time(NULL));
- int *board = init_board();
- if (board == NULL) {
- printf("Something went wrong allocating board space.\n");
- return 1;
- }
- struct termios oldt;
- disable_newline(&oldt);
- add_random(board);
- board[0] = 2;
- board[4] = 2;
- board[8] = 4;
- board[12] = 8;
- while (1) {
- redraw_board(board);
- char ch = getchar();
- int merged, moved1, moved2 = 0;
- dir_t where;
- switch(ch) {
- case 'q':
- tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
- exit(0);
- break;
- case 'w':
- where = UP;
- break;
- case 'a':
- where = LEFT;
- break;
- case 'd':
- where = RIGHT;
- break;
- case 's':
- where = DOWN;
- break;
- default:
- printf("WASD to move tiles, q to quit.\n");
- }
- moved1 = move(board, where);
- merged = merge(board, where);
- moved2 = move(board, where);
- if (moved1 || moved2 || merged) {
- add_random(board);
- } else {
- if (!move_possible(board)) {
- printf("No move possible! :(\n");
- break;
- }
- printf("state hasn't changed, not adding new tile\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement