2014-07-23 21:26:55 +00:00
|
|
|
/*
|
|
|
|
* File: apple2048.c
|
|
|
|
* Author: Jeremy Rand
|
|
|
|
* Date: July 23, 2014
|
|
|
|
*
|
|
|
|
* This file contains the entry point for the 2048 game.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <apple2enh.h>
|
2014-07-23 16:28:10 +00:00
|
|
|
#include <conio.h>
|
2014-07-23 13:20:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-07-23 16:28:10 +00:00
|
|
|
|
|
|
|
#include "game.h"
|
|
|
|
|
|
|
|
|
2014-07-23 21:40:51 +00:00
|
|
|
#define TILE_WIDTH 10
|
|
|
|
#define TILE_HEIGHT 5
|
|
|
|
|
2014-07-23 19:11:27 +00:00
|
|
|
void printInstructions(void)
|
|
|
|
{
|
|
|
|
int seed = 0;
|
|
|
|
|
|
|
|
clrscr();
|
|
|
|
printf(
|
|
|
|
// 0000000001111111111222222222233333333334
|
|
|
|
// 1234567890123456789012345678901234567890
|
|
|
|
" APPLE 2048\n"
|
|
|
|
"\n"
|
|
|
|
"USE I-J-K-M OR THE ARROW KEYS TO SLIDE\n"
|
|
|
|
"ALL TILES IN A DIRECTION. MATCHING\n"
|
|
|
|
"TILES ARE ADDED TOGETHER TO MAKE A NEW\n"
|
|
|
|
"TILE. ON EVERY MOVE, ONE MORE TILE IS\n"
|
|
|
|
"ADDED WITH A RANDOM VALUE OF EITHER 2\n"
|
|
|
|
"OR 4.\n"
|
|
|
|
"\n"
|
|
|
|
"PLAY ENDS WHEN ALL TILES ARE OCCUPIED\n"
|
|
|
|
"AND NO MORE MOVES ARE POSSIBLE. TRY\n"
|
|
|
|
"TO GET THE LARGEST TILE YOU CAN!\n"
|
|
|
|
"\n"
|
|
|
|
"PRESS ESCAPE OR Q TO QUIT AT ANY TIME.\n"
|
|
|
|
"PRESS R TO START A NEW GAME.\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
" PRESS ANY KEY TO START");
|
|
|
|
|
2014-07-23 21:06:49 +00:00
|
|
|
// The amount of time the user waits to read the in
|
2014-07-23 19:11:27 +00:00
|
|
|
while (!kbhit())
|
|
|
|
seed++;
|
|
|
|
|
|
|
|
cgetc();
|
|
|
|
srand(seed);
|
2014-07-23 21:40:51 +00:00
|
|
|
|
|
|
|
clrscr();
|
2014-07-23 19:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-23 16:28:10 +00:00
|
|
|
void printBoard(void)
|
|
|
|
{
|
|
|
|
tPos x;
|
|
|
|
tPos y;
|
|
|
|
|
|
|
|
for (x = 1; x <= BOARD_SIZE; x++) {
|
2014-07-23 21:40:51 +00:00
|
|
|
for (y = 1; y <= BOARD_SIZE; y++) {
|
|
|
|
textframexy((x - 1) * TILE_WIDTH,
|
|
|
|
(y - 1) * TILE_HEIGHT,
|
|
|
|
TILE_WIDTH,
|
|
|
|
TILE_HEIGHT,
|
|
|
|
TEXTFRAME_WIDE);
|
|
|
|
cputsxy((x - 1) * TILE_WIDTH + 1,
|
|
|
|
(y - 1) * TILE_HEIGHT + 2,
|
|
|
|
tileStringForPos(x, y));
|
|
|
|
}
|
2014-07-23 16:28:10 +00:00
|
|
|
}
|
2014-07-23 21:40:51 +00:00
|
|
|
|
|
|
|
gotoxy(0,20);
|
|
|
|
printf("CURRENT SCORE: %ld\nTRY TO GET THE %ld TILE!", currentScore(),
|
2014-07-23 16:28:10 +00:00
|
|
|
nextTarget());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void gameWon(void)
|
|
|
|
{
|
2014-07-23 21:06:49 +00:00
|
|
|
gotoxy(0, 22);
|
|
|
|
printf("YOU HAVE WON THE GAME! PRESS ANY KEY...\n");
|
2014-07-23 16:28:10 +00:00
|
|
|
cgetc();
|
2014-07-23 21:06:49 +00:00
|
|
|
initGame();
|
2014-07-23 16:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void gameLost(void)
|
|
|
|
{
|
2014-07-23 21:06:49 +00:00
|
|
|
gotoxy(0, 22);
|
|
|
|
printf("SORRY, NO MORE MOVES. PRESS ANY KEY...");
|
2014-07-23 16:28:10 +00:00
|
|
|
cgetc();
|
2014-07-23 21:06:49 +00:00
|
|
|
initGame();
|
2014-07-23 16:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void handleNextEvent(void)
|
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
ch = cgetc();
|
|
|
|
switch (ch) {
|
|
|
|
case 'i':
|
|
|
|
case 'I':
|
2014-07-23 21:26:55 +00:00
|
|
|
case CH_CURS_UP:
|
2014-07-23 16:28:10 +00:00
|
|
|
slideInDirection(DIR_UP);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'j':
|
|
|
|
case 'J':
|
2014-07-23 21:26:55 +00:00
|
|
|
case CH_CURS_LEFT:
|
2014-07-23 16:28:10 +00:00
|
|
|
slideInDirection(DIR_LEFT);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'k':
|
|
|
|
case 'K':
|
2014-07-23 21:26:55 +00:00
|
|
|
case CH_CURS_RIGHT:
|
2014-07-23 16:28:10 +00:00
|
|
|
slideInDirection(DIR_RIGHT);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
2014-07-23 21:26:55 +00:00
|
|
|
case CH_CURS_DOWN:
|
2014-07-23 16:28:10 +00:00
|
|
|
slideInDirection(DIR_DOWN);
|
|
|
|
return;
|
|
|
|
|
2014-07-23 21:26:55 +00:00
|
|
|
case CH_ESC:
|
2014-07-23 16:28:10 +00:00
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
case 'R':
|
|
|
|
initGame();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-23 13:20:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2014-07-23 19:11:27 +00:00
|
|
|
printInstructions();
|
|
|
|
|
2014-07-23 16:28:10 +00:00
|
|
|
initGame();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
printBoard();
|
|
|
|
|
|
|
|
if (isGameWon())
|
|
|
|
gameWon();
|
2014-07-23 21:06:49 +00:00
|
|
|
else if (isGameLost())
|
2014-07-23 16:28:10 +00:00
|
|
|
gameLost();
|
2014-07-23 21:06:49 +00:00
|
|
|
else
|
|
|
|
handleNextEvent();
|
2014-07-23 13:20:45 +00:00
|
|
|
}
|
2014-07-23 16:28:10 +00:00
|
|
|
|
2014-07-23 13:20:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|