Get the basic game working

This commit is contained in:
Jeremy Rand 2014-07-23 11:28:10 -05:00
parent 8111301a14
commit 0902ec0f79
3 changed files with 139 additions and 7 deletions

View File

@ -1,12 +1,121 @@
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#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;
}

29
game.c
View File

@ -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();
}

2
game.h
View File

@ -28,8 +28,6 @@ void initGame(void);
void slideInDirection(tDir dir);
void addRandomTile(void);
tScore currentScore(void);
tScore nextTarget(void);