From 0902ec0f7931f0959208499911f0628663e7aabb Mon Sep 17 00:00:00 2001 From: Jeremy Rand Date: Wed, 23 Jul 2014 11:28:10 -0500 Subject: [PATCH] Get the basic game working --- apple2048.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++-- game.c | 29 ++++++++++++- game.h | 2 - 3 files changed, 139 insertions(+), 7 deletions(-) diff --git a/apple2048.c b/apple2048.c index 150228c..51cf1dd 100644 --- a/apple2048.c +++ b/apple2048.c @@ -1,12 +1,121 @@ +#include #include #include -#include + +#include "game.h" + + +void printBoard(void) +{ + tPos x; + tPos y; + + clrscr(); + + for (y = 1; y <= BOARD_SIZE; y++) { + printf("+"); + for (x = 1; x <= BOARD_SIZE; x++) { + printf("--------+"); + } + printf("\n!"); + for (x = 1; x <= BOARD_SIZE; x++) { + printf(" !"); + } + printf("\n!"); + for (x = 1; x <= BOARD_SIZE; x++) { + printf("%s!", tileStringForPos(x, y)); + } + printf("\n!"); + for (x = 1; x <= BOARD_SIZE; x++) { + printf(" !"); + } + printf("\n"); + } + printf("+"); + for (x = 1; x <= BOARD_SIZE; x++) { + printf("--------+"); + } + printf("\n\nCURRENT SCORE: %ld\nTRY TO GET THE %ld TILE!", currentScore(), + nextTarget()); +} + + +void gameWon(void) +{ + printf("\nCONGRATULATIONS, YOU HAVE WON THE GAME!\n"); + cgetc(); + exit(0); +} + + +void gameLost(void) +{ + printf("\nSORRY, NO MORE MOVES.\n"); + cgetc(); + exit(0); +} + + +void handleNextEvent(void) +{ + char ch; + + while (true) { + ch = cgetc(); + switch (ch) { + case 'i': + case 'I': + case 11: // Up arrow + slideInDirection(DIR_UP); + return; + + case 'j': + case 'J': + case 8: // Left arrow + slideInDirection(DIR_LEFT); + return; + + case 'k': + case 'K': + case 21: // Right arrow + slideInDirection(DIR_RIGHT); + return; + + case 'm': + case 'M': + case 10: // Up arrow + slideInDirection(DIR_DOWN); + return; + + case 27: // Escape + case 'q': + case 'Q': + exit(0); + + case 'r': + case 'R': + initGame(); + return; + } + } +} int main(void) { - printf("DOES THIS WORK...\n"); - while (!kbhit()) { + initGame(); + + while (true) { + printBoard(); + + if (isGameWon()) + gameWon(); + + if (isGameLost()) + gameLost(); + + handleNextEvent(); } + return 0; } diff --git a/game.c b/game.c index dad387b..081dacd 100644 --- a/game.c +++ b/game.c @@ -76,6 +76,9 @@ static tTileValue gNextTarget = 11; static uint8_t gNumEmptyTiles; +void addRandomTile(void); + + void initGame(void) { tPos pos; @@ -133,8 +136,10 @@ void slideInDirection(tDir dir) tPos pos; tPos destPos; int8_t incr; + tTileValue tileValue; + bool addNewTile = false; - if (dir > 0) { + if (dir < 0) { pos = 0; incr = 1; } else { @@ -149,14 +154,34 @@ void slideInDirection(tDir dir) if (destPos == pos) continue; - if (gTileValues[destPos] > 0) { + addNewTile = true; + + tileValue = gTileValues[destPos]; + if (tileValue > 0) { + tileValue++; gTileValues[destPos]++; gNumEmptyTiles++; + gCurrentScore += gValueScores[tileValue]; + + // This is a hack to prevent multiple merges from happening to + // the same tile in a single turn. We set the value to a high + // negative (< -1) and then flip the sign bit later. + gTileValues[destPos] = -tileValue; } else { gTileValues[destPos] = gTileValues[pos]; } gTileValues[pos] = 0; // Empty the old position } + + for (pos = 0; pos < NUM_TILES; pos++) { + tileValue = gTileValues[pos]; + if (tileValue < BLOCKED_TILE_VALUE) { + gTileValues[pos] = -tileValue; + } + } + + if (addNewTile) + addRandomTile(); } diff --git a/game.h b/game.h index aa137d8..dd79a82 100644 --- a/game.h +++ b/game.h @@ -28,8 +28,6 @@ void initGame(void); void slideInDirection(tDir dir); -void addRandomTile(void); - tScore currentScore(void); tScore nextTarget(void);