Clean devel src dir
This commit is contained in:
parent
32a4b483ee
commit
a116923c69
387
src/2048.c
387
src/2048.c
|
@ -1,387 +0,0 @@
|
|||
/*
|
||||
* 2048.c - Main version that should be worked on.
|
||||
*
|
||||
* Things to do:
|
||||
* - Finish cleaning up code.
|
||||
* - Ask user if they would like to play again instead of exiting immediately.
|
||||
* - Add an actual goal (what happens when we reach 2048?).
|
||||
* - Keep track list of past scores, and store in a better fashion independent
|
||||
* of cwd. Also, store in not in plaintext.
|
||||
* */
|
||||
|
||||
#include <stdlib.h> /* for malloc */
|
||||
#include <time.h> /* for time */
|
||||
#include <unistd.h> /* for getopts */
|
||||
#include <ncurses.h>
|
||||
#include "2048.h"
|
||||
|
||||
int **grid; /* grid pointer */
|
||||
int grid_size; /* grid size */
|
||||
int score; /* score */
|
||||
int score_last; /* Score for last move */
|
||||
int score_high; /* Hiscore */
|
||||
int printwidth; /* maximum length of any value in grid, for printing */
|
||||
|
||||
/* Merges adjacent squares of the same value together in a certain direction */
|
||||
int merge(dir_t d)
|
||||
{
|
||||
int moved = 0;
|
||||
int i, j;
|
||||
|
||||
if (d == DIR_LEFT) {
|
||||
/* Move from upper left, across rows until second to second-last elem each row */
|
||||
for (i = 0; i < grid_size; i++) {
|
||||
for (j = 0; j < grid_size - 1; j++) {
|
||||
if (grid[i][j] == grid[i][j + 1]) {
|
||||
grid[i][j] <<= 1;
|
||||
grid[i][j + 1] = 0;
|
||||
score_last += grid[i][j];
|
||||
score += grid[i][j];
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_UP) {
|
||||
/* Move from upper left, across rows until final row which is skipped */
|
||||
for (i = 0; i < grid_size - 1; i++) {
|
||||
for (j = 0; j < grid_size; j++) {
|
||||
if (grid[i][j] == grid[i + 1][j]) {
|
||||
grid[i][j] <<= 1;
|
||||
grid[i + 1][j] = 0;
|
||||
score_last += grid[i][j];
|
||||
score += grid[i][j];
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_RIGHT) {
|
||||
/* Move from lower right, backwards across rows until first elem each row */
|
||||
for (i = grid_size - 1; i >= 0; i--) {
|
||||
for (j = grid_size - 1; j > 0; j--) {
|
||||
if (grid[i][j] == grid[i][j - 1]) {
|
||||
grid[i][j] <<= 1;
|
||||
grid[i][j - 1] = 0;
|
||||
score_last += grid[i][j];
|
||||
score += grid[i][j];
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_DOWN) {
|
||||
/* Move from lower right, across rows until first row which is skipped */
|
||||
for (i = grid_size - 1; i > 0; i--) {
|
||||
for (j = grid_size - 1; j >= 0; j--) {
|
||||
if (grid[i][j] == grid[i - 1][j]) {
|
||||
grid[i][j] <<= 1;
|
||||
grid[i - 1][j] = 0;
|
||||
score_last += grid[i][j];
|
||||
score += grid[i][j];
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return moved;
|
||||
}
|
||||
|
||||
|
||||
/* move all values in the grid to the edge given by the direction pressed */
|
||||
/* would be nice to generalize this code a little so didn't need four loops */
|
||||
/* if animations are wanted, then need to alter this so it moves each square one at a time */
|
||||
int gravitate(dir_t d)
|
||||
{
|
||||
int moved = 0;
|
||||
int i, j;
|
||||
|
||||
if (d == DIR_LEFT) {
|
||||
for (i = 0; i < grid_size; i++) {
|
||||
for (j = 0; j < grid_size - 1; j++) {
|
||||
if (grid[i][j]) continue;
|
||||
int st = 1;
|
||||
while (j + st < grid_size && !grid[i][j + st]) st++;
|
||||
if (j + st < grid_size) {
|
||||
grid[i][j] = grid[i][j + st];
|
||||
grid[i][j + st] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_UP) {
|
||||
for (i = 0; i < grid_size; i++) {
|
||||
for (j = 0; j < grid_size - 1; j++) {
|
||||
if (grid[j][i]) continue;
|
||||
int st = 1;
|
||||
while (j + st < grid_size && !grid[j + st][i]) st++;
|
||||
if (j + st < grid_size) {
|
||||
grid[j][i] = grid[j + st][i];
|
||||
grid[j + st][i] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_RIGHT) {
|
||||
for (i = 0; i < grid_size; i++) {
|
||||
for (j = grid_size - 1; j > 0; j--) {
|
||||
if (grid[i][j]) continue;
|
||||
int st = 1;
|
||||
while (j - st >= 0 && !grid[i][j - st]) st++;
|
||||
if (j - st >= 0) {
|
||||
grid[i][j] = grid[i][j - st];
|
||||
grid[i][j - st] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_DOWN) {
|
||||
for (i = 0; i < grid_size; i++) {
|
||||
for (j = grid_size - 1; j > 0; j--) {
|
||||
if (grid[j][i]) continue;
|
||||
int st = 1;
|
||||
while (j - st >= 0 && !grid[j - st][i]) st++;
|
||||
if (j - st >= 0) {
|
||||
grid[j][i] = grid[j - st][i];
|
||||
grid[j - st][i] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return moved;
|
||||
}
|
||||
|
||||
/* Return the current highscore */
|
||||
int get_score_high() {
|
||||
int s = 0;
|
||||
FILE *fd = fopen(HISCORE_FILE, "r");
|
||||
if (fd == NULL)
|
||||
fd = fopen(HISCORE_FILE, "w+");
|
||||
|
||||
if (fscanf(fd, "%d", &s) == 1) {};
|
||||
fclose(fd);
|
||||
return s;
|
||||
}
|
||||
|
||||
/* saves hiscore, but only if playing on standard size grid */
|
||||
void save_score_high() {
|
||||
if (score > score_high && grid_size == 4) {
|
||||
score_high = score;
|
||||
FILE *fd = fopen(HISCORE_FILE, "w+");
|
||||
fprintf(fd, "%d", score_high);
|
||||
fclose(fd);
|
||||
}
|
||||
}
|
||||
|
||||
/* returns if there are any possible moves */
|
||||
int moves_available()
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < grid_size; i++)
|
||||
for (j = 0; j < grid_size; j++)
|
||||
if (!grid[i][j]
|
||||
|| ((i + 1 < grid_size) && (grid[i][j] == grid[i + 1][j]))
|
||||
|| ((j + 1 < grid_size) && (grid[i][j] == grid[i][j + 1])))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* places a random block onto the grid - either a 4, or a 2 with a chance of 1:3 respectively */
|
||||
/* could do this in a much smarter fashion by finding which spaces are free */
|
||||
void rand_block()
|
||||
{
|
||||
int x, y;
|
||||
while (grid[x = rand() % grid_size][y = rand() % grid_size]);
|
||||
grid[x][y] = (rand() & 3) ? 2 : 4;
|
||||
}
|
||||
|
||||
/* quick floor log2(n) */
|
||||
int flog2(unsigned int n)
|
||||
{
|
||||
int k = 0;
|
||||
while (n)
|
||||
k++, n >>= 1;
|
||||
return k;
|
||||
}
|
||||
|
||||
/* draws the grid and fills it with the current values */
|
||||
/* colors just rotate around, works for now, can be confusing when you have some fairly high values on the board */
|
||||
void draw_grid(WINDOW *gamewin)
|
||||
{
|
||||
// mvwprintw will sometimes have a useless arg, this is warned, but doesn't affect the program
|
||||
char *scr = score_last ? "SCORE: %d (+%d)\n" : "SCORE: %d\n";
|
||||
mvwprintw(gamewin, 0, 0, scr, score, score_last);
|
||||
mvwprintw(gamewin, 1, 0, "HISCR: %d\n", score_high);
|
||||
|
||||
ITER(grid_size*(printwidth + 2) + 1, waddch(gamewin, '-'));
|
||||
int i, j, xps = 0, yps = 3;
|
||||
for (i = 0; i < grid_size; i++, xps = 0, yps++) {
|
||||
mvwprintw(gamewin, yps, xps++, "|");
|
||||
for (j = 0; j < grid_size; j++) {
|
||||
if (grid[i][j]) {
|
||||
wattron(gamewin, COLOR_PAIR(flog2(grid[i][j]) % 7));
|
||||
mvwprintw(gamewin, yps, xps, "%*d", printwidth, grid[i][j]);
|
||||
wattroff(gamewin, COLOR_PAIR(flog2(grid[i][j]) % 7));
|
||||
mvwprintw(gamewin, yps, xps + printwidth, " |");
|
||||
}
|
||||
else {
|
||||
ITER(printwidth + 1, waddch(gamewin, ' '));
|
||||
waddch(gamewin, '|');
|
||||
}
|
||||
xps += (printwidth + 2);
|
||||
}
|
||||
}
|
||||
ITER(grid_size*(printwidth + 2) + 1, waddch(gamewin, '-'));
|
||||
wrefresh(gamewin);
|
||||
}
|
||||
|
||||
/* entry point for the program */
|
||||
/* parses options and stores the main game loop */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
/* init ncurses environment */
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
curs_set(FALSE);
|
||||
|
||||
/* init variables */
|
||||
score = 0;
|
||||
score_last = 0;
|
||||
score_high = get_score_high();
|
||||
grid_size = DEFAULT_GRID_SIZE;
|
||||
printwidth = DEFAULT_GRID_SIZE;
|
||||
|
||||
int n_blocks = 1;
|
||||
|
||||
/* parse options */
|
||||
int c;
|
||||
int enable_color = has_colors();
|
||||
while ((c = getopt(argc, argv, "rcChs:b:")) != -1) {
|
||||
switch (c) {
|
||||
/* Color support */
|
||||
case 'c':
|
||||
enable_color = 1;
|
||||
break;
|
||||
case 'C':
|
||||
enable_color = 0;
|
||||
break;
|
||||
// different board sizes
|
||||
case 's':;
|
||||
int optint = strtol(optarg, NULL, 10);
|
||||
grid_size = optint > DEFAULT_GRID_SIZE ? optint : DEFAULT_GRID_SIZE;
|
||||
break;
|
||||
// different block spawn rate
|
||||
case 'b':
|
||||
n_blocks = strtol(optarg, NULL, 10);
|
||||
break;
|
||||
// reset hiscores
|
||||
case 'r':
|
||||
endwin();
|
||||
printf("Are you sure you want to reset your highscores? (Y)es or (N)o\n");
|
||||
int response;
|
||||
if ((response = getchar()) == 'y' || response == 'Y') {
|
||||
FILE *fd = fopen(HISCORE_FILE, "w+");
|
||||
fclose(fd);
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
// help menu
|
||||
case 'h':
|
||||
endwin();
|
||||
printf(USAGE_STR);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
if (enable_color) {
|
||||
if (!has_colors()) {
|
||||
fprintf(stderr, "Terminal does not support color\n");
|
||||
} else {
|
||||
start_color();
|
||||
init_pair(0, 1, 0);
|
||||
init_pair(1, 2, 0);
|
||||
init_pair(2, 3, 0);
|
||||
init_pair(3, 4, 0);
|
||||
init_pair(4, 5, 0);
|
||||
init_pair(5, 6, 0);
|
||||
init_pair(6, 7, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Allocate memory once we actually know amount */
|
||||
CALLOC2D(grid, grid_size);
|
||||
|
||||
int width = grid_size * (printwidth + 2) + 1;
|
||||
int height = grid_size * (printwidth + 2) + 3;
|
||||
|
||||
// might center in middle of screen
|
||||
WINDOW *gamewin = newwin(height, width, 1, 1);
|
||||
keypad(gamewin, TRUE);
|
||||
|
||||
/* random seed */
|
||||
srand((unsigned int)time(NULL));
|
||||
ITER(2, rand_block());
|
||||
draw_grid(gamewin);
|
||||
|
||||
int key, moved;
|
||||
while (1) {
|
||||
/* will goto this if we didn't get a valid keypress */
|
||||
retry:;
|
||||
moved = 0;
|
||||
score_last = 0;
|
||||
key = wgetch(gamewin);
|
||||
|
||||
/* should check if anything changed during merge and if not retry */
|
||||
switch (key) {
|
||||
case 'h':
|
||||
case 'a':
|
||||
case KEY_LEFT:
|
||||
moved = TURN(DIR_LEFT);
|
||||
break;
|
||||
case 'l':
|
||||
case 'd':
|
||||
case KEY_RIGHT:
|
||||
moved = TURN(DIR_RIGHT);
|
||||
break;
|
||||
case 'j':
|
||||
case 's':
|
||||
case KEY_DOWN:
|
||||
moved = TURN(DIR_DOWN);
|
||||
break;
|
||||
case 'k':
|
||||
case 'w':
|
||||
case KEY_UP:
|
||||
moved = TURN(DIR_UP);
|
||||
break;
|
||||
case 'q':
|
||||
FREE2D(grid, grid_size);
|
||||
erase();
|
||||
refresh();
|
||||
endwin();
|
||||
save_score_high();
|
||||
exit(EXIT_SUCCESS);
|
||||
default:
|
||||
goto retry;
|
||||
}
|
||||
|
||||
if (!moves_available()) {
|
||||
endwin();
|
||||
printf("\n"
|
||||
"YOU LOSE! - Your score was %d\n", score);
|
||||
save_score_high();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (moved) {
|
||||
ITER(n_blocks, rand_block());
|
||||
draw_grid(gamewin);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
65
src/2048.h
65
src/2048.h
|
@ -1,65 +0,0 @@
|
|||
#ifndef _2048_H_
|
||||
#define _2048_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef enum {
|
||||
DIR_UP,
|
||||
DIR_RIGHT,
|
||||
DIR_DOWN,
|
||||
DIR_LEFT
|
||||
} dir_t;
|
||||
|
||||
#define DATADIR_NAME "2048"
|
||||
#define HIGHSCORE_FILE_NAME "highscore"
|
||||
|
||||
const char* get_highscore_file();
|
||||
|
||||
// Repeat an expression y, x times */
|
||||
#define ITER(x, expr)\
|
||||
do {\
|
||||
int i;\
|
||||
for (i = 0; i < (x); i++){ expr;}\
|
||||
} while (0)
|
||||
|
||||
/* Allocates a square pointer of array of arrays onto heap */
|
||||
#define CALLOC2D(ptr, sz)\
|
||||
do {\
|
||||
int i;\
|
||||
ptr = calloc((sz), sizeof(*ptr));\
|
||||
for (i = 0; i < (sz); i++)\
|
||||
ptr[i] = calloc((sz), sizeof(*ptr));\
|
||||
} while (0)
|
||||
|
||||
/* Frees a square pointer of arrays to arrays */
|
||||
#define FREE2D(ptr, sz)\
|
||||
do {\
|
||||
int i;\
|
||||
for (i = 0; i < (sz); i++)\
|
||||
free(ptr[i]);\
|
||||
free(ptr);\
|
||||
} while (0)
|
||||
|
||||
/* What occurs during a 'turn' of execution */
|
||||
#define TURN(x) (gravitate(x) + merge(x) + gravitate(x))
|
||||
|
||||
/* Constants */
|
||||
#define DEFAULT_GRID_SIZE 4
|
||||
#define HISCORE_FILE get_highscore_file()
|
||||
#define USAGE_STR\
|
||||
"Usage:\n"\
|
||||
" ./2048 [options]\n"\
|
||||
"\n"\
|
||||
"Controls:\n"\
|
||||
" hjkl, wasd Movement\n"\
|
||||
" q Quit\n"\
|
||||
"\n"\
|
||||
"Options:\n"\
|
||||
" -s <size> Set the grid border length\n"\
|
||||
" -b <rate> Set the block spawn rate\n"\
|
||||
" -c Enables color support (ncurses version only)\n"\
|
||||
" -C Disables color support (ncurses version only)\n"
|
||||
|
||||
#endif
|
|
@ -1,326 +0,0 @@
|
|||
/*
|
||||
* 2048_no_curses.c - Non-curses version that can be updated at a later time
|
||||
**/
|
||||
|
||||
#include <stdio.h> /* for printf */
|
||||
#include <stdlib.h> /* for malloc */
|
||||
#include <termios.h>/* */
|
||||
#include <time.h> /* for time */
|
||||
#include <unistd.h> /* for getopts */
|
||||
#include "2048.h"
|
||||
|
||||
int **g; /* grid pointer */
|
||||
int SZ; /* grid size */
|
||||
int s; /* Current score */
|
||||
int sl; /* Score for last turn */
|
||||
int hs; /* Highscore */
|
||||
|
||||
/* Merges adjacent squares of the same value together in a certain direction */
|
||||
int merge(int d)
|
||||
{
|
||||
int moved = 0;
|
||||
if (d == DIR_LEFT) {
|
||||
int i, j;
|
||||
for (i = 0; i < SZ; i++) {
|
||||
for (j = 0; j < SZ; j++) {
|
||||
if (j + 1 < SZ && g[i][j] && g[i][j] == g[i][j + 1]) {
|
||||
g[i][j] <<= 1;
|
||||
sl += g[i][j];
|
||||
s += g[i][j];
|
||||
g[i][j++ + 1] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_UP) {
|
||||
int i, j;
|
||||
for (i = 0; i < SZ; i++) {
|
||||
for (j = 0; j < SZ; j++) {
|
||||
if (j + 1 < SZ && g[j][i] && g[j][i] == g[j + 1][i]) {
|
||||
g[j][i] <<= 1;
|
||||
sl += g[j][i];
|
||||
s += g[j][i];
|
||||
g[j++ + 1][i] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_RIGHT) {
|
||||
int i, j;
|
||||
for (i = SZ - 1; i >= 0; i--) {
|
||||
for (j = SZ - 1; j >= 0; j--) {
|
||||
if (j > 0 && g[i][j] && g[i][j] == g[i][j - 1]) {
|
||||
g[i][j] <<= 1;
|
||||
sl += g[i][j];
|
||||
s += g[i][j];
|
||||
g[i][j-- - 1] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_DOWN) {
|
||||
int i, j;
|
||||
for (i = SZ - 1; i >= 0; i--) {
|
||||
for (j = SZ - 1; j >= 0; j--) {
|
||||
if (j > 0 && g[j][i] && g[j][i] == g[j - 1][i]) {
|
||||
g[j][i] <<= 1;
|
||||
sl += g[j][i];
|
||||
s += g[j][i];
|
||||
g[j-- - 1][i] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return moved;
|
||||
}
|
||||
|
||||
|
||||
/* move all values in the grid to the edge given by the direction pressed */
|
||||
/* would be nice to generalize this code a little so didn't need four loops */
|
||||
int gravitate(int d)
|
||||
{
|
||||
int moved = 0;
|
||||
if (d == DIR_LEFT) {
|
||||
int i, j;
|
||||
for (i = 0; i < SZ; i++) {
|
||||
for (j = 0; j < SZ - 1; j++) {
|
||||
if (g[i][j]) continue;
|
||||
int st = 1;
|
||||
while (j + st < SZ && !g[i][j + st]) st++;
|
||||
if (j + st < SZ) {
|
||||
g[i][j] = g[i][j + st];
|
||||
g[i][j + st] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_UP) {
|
||||
int i, j;
|
||||
for (i = 0; i < SZ; i++) {
|
||||
for (j = 0; j < SZ - 1; j++) {
|
||||
if (g[j][i]) continue;
|
||||
int st = 1;
|
||||
while (j + st < SZ && !g[j + st][i]) st++;
|
||||
if (j + st < SZ) {
|
||||
g[j][i] = g[j + st][i];
|
||||
g[j + st][i] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_RIGHT) {
|
||||
int i, j;
|
||||
for (i = 0; i < SZ; i++) {
|
||||
for (j = SZ - 1; j > 0; j--) {
|
||||
if (g[i][j]) continue;
|
||||
int st = 1;
|
||||
while (j - st >= 0 && !g[i][j - st]) st++;
|
||||
if (j - st >= 0) {
|
||||
g[i][j] = g[i][j - st];
|
||||
g[i][j - st] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (d == DIR_DOWN) {
|
||||
int i, j;
|
||||
for (i = 0; i < SZ; i++) {
|
||||
for (j = SZ - 1; j > 0; j--) {
|
||||
if (g[j][i]) continue;
|
||||
int st = 1;
|
||||
while (j - st >= 0 && !g[j - st][i]) st++;
|
||||
if (j - st >= 0) {
|
||||
g[j][i] = g[j - st][i];
|
||||
g[j - st][i] = 0;
|
||||
moved = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return moved;
|
||||
}
|
||||
|
||||
/* loads hiscore */
|
||||
void load_score() {
|
||||
FILE *fd = fopen(HISCORE_FILE, "r");
|
||||
if (fd == NULL) fd = fopen(HISCORE_FILE, "w+");
|
||||
if (fscanf(fd, "%d", &hs) == EOF) hs = 0;
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
/* saves hiscore, but only if playing on standard size grid */
|
||||
void save_score() {
|
||||
if (s > hs && SZ == 4) {
|
||||
hs = s;
|
||||
FILE *fd = fopen(HISCORE_FILE, "w+");
|
||||
fprintf(fd, "%d", hs);
|
||||
fclose(fd);
|
||||
}
|
||||
}
|
||||
|
||||
/* returns if there are any possible moves */
|
||||
int moves_available()
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < SZ; i++)
|
||||
for (j = 0; j < SZ; j++)
|
||||
if (!g[i][j] || ((i + 1 < SZ) && (g[i][j] == g[i + 1][j])) || ((j + 1 < SZ) && (g[i][j] == g[i][j + 1])))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* places a random block onto the grid - either a 4, or a 2 with a ratio of 1:3 respectively */
|
||||
/* do this in a smarter fashion */
|
||||
void rand_block()
|
||||
{
|
||||
int x_p, y_p;
|
||||
while (g[x_p = rand() % SZ][y_p = rand() % SZ]);
|
||||
g[x_p][y_p] = (rand() & 3) ? 2 : 4;
|
||||
}
|
||||
|
||||
/* draws the grid and fills it with the current values */
|
||||
void draw_grid()
|
||||
{
|
||||
printf("HISCORE: %d |", hs);
|
||||
printf("| SCORE: %d ", s);
|
||||
if (sl) printf("(+%d)", sl);
|
||||
printf("\n");
|
||||
|
||||
// alter this SZ + 1 to match abitrary grid size
|
||||
ITER(SZ, printf("------"));
|
||||
printf("-\n");
|
||||
int i, j;
|
||||
for (i = 0; i < SZ; i++) {
|
||||
printf("|");
|
||||
for (j = 0; j < SZ; j++) {
|
||||
if (g[i][j])
|
||||
printf("%*d |", 4, g[i][j]);
|
||||
else
|
||||
printf(" |");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
ITER(SZ, printf("------"));
|
||||
printf("-\n\n");
|
||||
}
|
||||
|
||||
/* store the terminal settings and call this function on exit to restore */
|
||||
struct termios sattr;
|
||||
void reset_term()
|
||||
{
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &sattr);
|
||||
}
|
||||
|
||||
/* entry point for the program */
|
||||
/* parses options and stores the main game loop */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
/* init variables */
|
||||
hs = 0;
|
||||
s = 0;
|
||||
sl = 0;
|
||||
SZ = 4;
|
||||
CALLOC2D(g, SZ);
|
||||
|
||||
load_score();
|
||||
int n_blocks = 1;
|
||||
|
||||
/* parse options */
|
||||
int c;
|
||||
while ((c = getopt(argc, argv, "rhs:b:")) != -1) {
|
||||
switch (c) {
|
||||
// different board sizes
|
||||
case 's':
|
||||
FREE2D(g, SZ);
|
||||
int optint = atoi(optarg);
|
||||
SZ = optint > 4 ? optint : 4;
|
||||
CALLOC2D(g, SZ);
|
||||
break;
|
||||
// different block spawn rate
|
||||
case 'b':
|
||||
n_blocks = atoi(optarg);
|
||||
break;
|
||||
// reset hiscores
|
||||
case 'r':
|
||||
printf("Are you sure you want to reset your highscores? (Y)es or (N)o\n");
|
||||
int response;
|
||||
if ((response = getchar()) == 'y' || response == 'Y') {
|
||||
FILE *fd = fopen(HISCORE_FILE, "w+");
|
||||
fclose(fd);
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
case 'h':
|
||||
printf(USAGE_STR);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
/* random seed */
|
||||
srand((unsigned int)time(NULL));
|
||||
ITER(2, rand_block());
|
||||
draw_grid();
|
||||
|
||||
/* store term settings so we can restore on exit */
|
||||
tcgetattr(STDIN_FILENO, &sattr);
|
||||
atexit(reset_term);
|
||||
|
||||
/* alters terminal stdin to not echo and doesn't need \n before reading getchar */
|
||||
struct termios tattr;
|
||||
tcgetattr(STDIN_FILENO, &tattr);
|
||||
tattr.c_lflag &= ~(ICANON | ECHO);
|
||||
tcsetattr(STDOUT_FILENO, TCSANOW, &tattr);
|
||||
|
||||
int key, moved;
|
||||
while (1) {
|
||||
/* will goto this if we didn't get a valid keypress */
|
||||
retry:;
|
||||
moved = 0;
|
||||
key = getchar();
|
||||
sl = 0;
|
||||
|
||||
/* should check if anything changed during merge and if not retry */
|
||||
switch (key) {
|
||||
case 'h':
|
||||
case 'a':
|
||||
moved = TURN(DIR_LEFT);
|
||||
break;
|
||||
case 'l':
|
||||
case 'd':
|
||||
moved = TURN(DIR_RIGHT);
|
||||
break;
|
||||
case 'j':
|
||||
case 's':
|
||||
moved = TURN(DIR_DOWN);
|
||||
break;
|
||||
case 'k':
|
||||
case 'w':
|
||||
moved = TURN(DIR_UP);
|
||||
break;
|
||||
case 'q':
|
||||
save_score();
|
||||
exit(EXIT_SUCCESS);
|
||||
default:
|
||||
goto retry;
|
||||
}
|
||||
|
||||
if (!moves_available()) {
|
||||
printf("\n"
|
||||
"YOU LOSE! - Your score was %d\n", s);
|
||||
save_score();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (moved) {
|
||||
ITER(n_blocks, rand_block());
|
||||
draw_grid();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
#include "2048.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
const char* get_highscore_file() {
|
||||
static char buffer[4096];
|
||||
if (getenv("XDG_DATA_HOME") != NULL) {
|
||||
snprintf(buffer, sizeof(buffer), "%s/%s/%s", getenv("XDG_DATA_HOME"), DATADIR_NAME, HIGHSCORE_FILE_NAME);
|
||||
} else if (getenv("HOME") != NULL) {
|
||||
snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", getenv("HOME"), DATADIR_NAME, HIGHSCORE_FILE_NAME);
|
||||
} else {
|
||||
return HIGHSCORE_FILE_NAME;
|
||||
}
|
||||
|
||||
// create hierarrchy of directories up to highscore file location
|
||||
char* sep = strrchr(buffer, '/');
|
||||
while (sep != NULL) {
|
||||
*sep = '\0';
|
||||
if (strlen(buffer) != 0)
|
||||
mkdir(buffer, 0777);
|
||||
char* tmpsep = sep;
|
||||
sep = strrchr(buffer, '/');
|
||||
*tmpsep = '/';
|
||||
}
|
||||
return buffer;
|
||||
}
|
Loading…
Reference in New Issue
Block a user