2016-07-21 00:53:52 +00:00
|
|
|
//
|
|
|
|
// ui.c
|
|
|
|
// a2bejwld
|
|
|
|
//
|
|
|
|
// Created by Jeremy Rand on 2016-07-20.
|
|
|
|
// Copyright © 2016 Jeremy Rand. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include <conio.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "ui.h"
|
2016-07-22 15:43:18 +00:00
|
|
|
#include "anim.h"
|
2016-07-21 00:53:52 +00:00
|
|
|
#include "dbllores.h"
|
|
|
|
#include "game.h"
|
2016-07-24 22:22:37 +00:00
|
|
|
#include "joystick.h"
|
2016-07-21 04:39:06 +00:00
|
|
|
|
|
|
|
|
2016-07-22 17:04:47 +00:00
|
|
|
// Forward delcarations
|
|
|
|
|
2016-07-22 04:45:07 +00:00
|
|
|
static void refreshSquare(tSquare square);
|
|
|
|
static void refreshScore(tScore score);
|
|
|
|
static void refreshLevel(tLevel level);
|
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
static bool joystickChangedCallback(tJoyState *oldState, tJoyState *newState);
|
|
|
|
static bool joystickNoChangeCallback(tJoyState *oldState);
|
|
|
|
|
2016-07-22 04:45:07 +00:00
|
|
|
|
2016-07-22 17:04:47 +00:00
|
|
|
// Globals
|
|
|
|
|
2016-07-21 00:53:52 +00:00
|
|
|
static tSquare gSelectedSquare = 0;
|
2016-07-21 19:33:31 +00:00
|
|
|
static uint8_t gScoreBar = 0;
|
2016-07-21 00:53:52 +00:00
|
|
|
|
2016-07-22 04:45:07 +00:00
|
|
|
static tGameCallbacks gCallbacks = {
|
|
|
|
refreshSquare,
|
|
|
|
refreshScore,
|
2016-07-22 17:04:47 +00:00
|
|
|
refreshLevel,
|
2016-08-17 04:07:00 +00:00
|
|
|
|
2016-07-22 17:04:47 +00:00
|
|
|
beginClearGemAnim,
|
|
|
|
addClearAtSquare,
|
|
|
|
undoClearAtSquare,
|
2016-08-17 04:07:00 +00:00
|
|
|
playSoundForExplodingGem,
|
|
|
|
playSoundForStarringGem,
|
|
|
|
playSoundForSpecialGem,
|
2016-07-22 17:04:47 +00:00
|
|
|
endClearGemAnim,
|
2016-08-17 04:07:00 +00:00
|
|
|
|
2016-07-22 19:48:49 +00:00
|
|
|
swapSquares,
|
2016-08-17 04:07:00 +00:00
|
|
|
|
2016-07-22 21:45:15 +00:00
|
|
|
beginDropAnim,
|
|
|
|
dropSquareFromTo,
|
|
|
|
dropSquareFromOffscreen,
|
|
|
|
endDropAnim
|
2016-07-22 04:45:07 +00:00
|
|
|
};
|
|
|
|
|
2016-07-21 00:53:52 +00:00
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
static tJoyCallbacks gJoyCallbacks = {
|
|
|
|
joystickChangedCallback,
|
|
|
|
joystickNoChangeCallback,
|
|
|
|
25, // Read poll time
|
|
|
|
40, // Initial no change poll time
|
|
|
|
10 // Subsequent no change poll time
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool gShouldSave = false;
|
|
|
|
|
|
|
|
|
2016-07-22 17:04:47 +00:00
|
|
|
// Implementation
|
|
|
|
|
2016-07-22 05:11:36 +00:00
|
|
|
static void showAndClearDblLoRes(void)
|
2016-07-21 00:53:52 +00:00
|
|
|
{
|
|
|
|
showDblLoRes();
|
|
|
|
clearDblLoRes();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void printInstructions(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
int seed = 0;
|
|
|
|
|
|
|
|
unshowDblLoRes();
|
|
|
|
videomode(VIDEOMODE_80x24);
|
|
|
|
clrscr();
|
|
|
|
printf(
|
2016-07-21 19:33:31 +00:00
|
|
|
// 0000000001111111111222222222233333333334444444444555555555566666666667
|
|
|
|
// 1234567890123456789012345678901234567890123456789012345678901234567890
|
|
|
|
" Apple Jeweled\n"
|
|
|
|
" by Jeremy Rand\n"
|
2016-07-21 00:53:52 +00:00
|
|
|
"\n"
|
2016-07-26 05:26:24 +00:00
|
|
|
" Use I-J-K-M, the arrow keys or the joystick to move your selection.\n"
|
|
|
|
" Hold either apple key or joystick button and move your selection to\n"
|
|
|
|
" swap two jewels and match 3 or more jewels. When you match three\n"
|
|
|
|
" jewels, they disappear and new jewels will drop from the top.\n"
|
2016-07-21 00:53:52 +00:00
|
|
|
"\n"
|
2016-07-21 19:33:31 +00:00
|
|
|
" If you match four jewels or three jewels in two directions, then the\n"
|
|
|
|
" jewel does not disappear. Match it again and it explodes taking more\n"
|
2016-07-22 04:45:07 +00:00
|
|
|
" jewels with it. Match five jewels and a special jewel will appear.\n"
|
|
|
|
" Swap it with any other jewel and all jewels of that colour will\n"
|
|
|
|
" disappear.\n"
|
2016-07-21 19:33:31 +00:00
|
|
|
"\n"
|
|
|
|
" When the score bar on the right fills, the board reloads and you level\n"
|
|
|
|
" up. Play ends when no more matches can be made.\n"
|
|
|
|
"\n"
|
|
|
|
" Press Q or escape to quit at any time.\n"
|
2016-07-21 00:53:52 +00:00
|
|
|
" Press R to start a new game.\n"
|
|
|
|
" Press S to toggle sound.\n"
|
2016-07-22 04:45:07 +00:00
|
|
|
" Press H to get a hint.\n"
|
|
|
|
" Press ? to see this info again.\n"
|
2016-07-21 00:53:52 +00:00
|
|
|
"\n"
|
|
|
|
" Press any key to start");
|
|
|
|
|
|
|
|
// The amount of time the user waits to read the in
|
|
|
|
while (!kbhit())
|
|
|
|
seed++;
|
|
|
|
|
|
|
|
cgetc();
|
|
|
|
srand(seed);
|
|
|
|
|
|
|
|
clrscr();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-21 04:39:06 +00:00
|
|
|
static void refreshSquare(tSquare square)
|
|
|
|
{
|
|
|
|
drawBgSquare(square);
|
|
|
|
drawGemAtSquare(square);
|
2016-07-21 04:55:29 +00:00
|
|
|
|
|
|
|
if (gemIsStarredAtSquare(square))
|
|
|
|
starGem(square);
|
2016-07-21 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-21 00:53:52 +00:00
|
|
|
static void drawBoard(void)
|
|
|
|
{
|
|
|
|
tSquare square;
|
|
|
|
|
|
|
|
for (square = MIN_SQUARE; square <= MAX_SQUARE; square++) {
|
2016-07-21 04:39:06 +00:00
|
|
|
refreshSquare(square);
|
2016-07-21 00:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
selectSquare(gSelectedSquare);
|
2016-07-21 19:33:31 +00:00
|
|
|
drawScore(gScoreBar);
|
2016-07-21 00:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void quitGame(void)
|
|
|
|
{
|
|
|
|
unshowDblLoRes();
|
|
|
|
videomode(VIDEOMODE_40x24);
|
|
|
|
clrscr();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void moveUp(void)
|
|
|
|
{
|
|
|
|
tSquare oldSquare = gSelectedSquare;
|
|
|
|
tPos x = SQUARE_TO_X(gSelectedSquare);
|
|
|
|
tPos y = SQUARE_TO_Y(gSelectedSquare);
|
|
|
|
|
|
|
|
if (y == 0)
|
|
|
|
y = BOARD_SIZE - 1;
|
|
|
|
else
|
|
|
|
y--;
|
|
|
|
|
|
|
|
gSelectedSquare = XY_TO_SQUARE(x, y);
|
|
|
|
|
2016-07-21 04:39:06 +00:00
|
|
|
refreshSquare(oldSquare);
|
2016-07-21 00:53:52 +00:00
|
|
|
selectSquare(gSelectedSquare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
static void moveUpLeft(void)
|
|
|
|
{
|
|
|
|
tSquare oldSquare = gSelectedSquare;
|
|
|
|
tPos x = SQUARE_TO_X(gSelectedSquare);
|
|
|
|
tPos y = SQUARE_TO_Y(gSelectedSquare);
|
|
|
|
|
|
|
|
if (y == 0)
|
|
|
|
y = BOARD_SIZE - 1;
|
|
|
|
else
|
|
|
|
y--;
|
|
|
|
|
|
|
|
if (x == 0)
|
|
|
|
x = BOARD_SIZE - 1;
|
|
|
|
else
|
|
|
|
x--;
|
|
|
|
|
|
|
|
gSelectedSquare = XY_TO_SQUARE(x, y);
|
|
|
|
|
|
|
|
refreshSquare(oldSquare);
|
|
|
|
selectSquare(gSelectedSquare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void moveUpRight(void)
|
|
|
|
{
|
|
|
|
tSquare oldSquare = gSelectedSquare;
|
|
|
|
tPos x = SQUARE_TO_X(gSelectedSquare);
|
|
|
|
tPos y = SQUARE_TO_Y(gSelectedSquare);
|
|
|
|
|
|
|
|
if (y == 0)
|
|
|
|
y = BOARD_SIZE - 1;
|
|
|
|
else
|
|
|
|
y--;
|
|
|
|
|
|
|
|
if (x == BOARD_SIZE - 1)
|
|
|
|
x = 0;
|
|
|
|
else
|
|
|
|
x++;
|
|
|
|
|
|
|
|
gSelectedSquare = XY_TO_SQUARE(x, y);
|
|
|
|
|
|
|
|
refreshSquare(oldSquare);
|
|
|
|
selectSquare(gSelectedSquare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-21 00:53:52 +00:00
|
|
|
static void moveDown(void)
|
|
|
|
{
|
|
|
|
tSquare oldSquare = gSelectedSquare;
|
|
|
|
tPos x = SQUARE_TO_X(gSelectedSquare);
|
|
|
|
tPos y = SQUARE_TO_Y(gSelectedSquare);
|
|
|
|
|
|
|
|
if (y == BOARD_SIZE - 1)
|
|
|
|
y = 0;
|
|
|
|
else
|
|
|
|
y++;
|
|
|
|
|
|
|
|
gSelectedSquare = XY_TO_SQUARE(x, y);
|
|
|
|
|
2016-07-21 04:39:06 +00:00
|
|
|
refreshSquare(oldSquare);
|
2016-07-21 00:53:52 +00:00
|
|
|
selectSquare(gSelectedSquare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
static void moveDownLeft(void)
|
|
|
|
{
|
|
|
|
tSquare oldSquare = gSelectedSquare;
|
|
|
|
tPos x = SQUARE_TO_X(gSelectedSquare);
|
|
|
|
tPos y = SQUARE_TO_Y(gSelectedSquare);
|
|
|
|
|
|
|
|
if (y == BOARD_SIZE - 1)
|
|
|
|
y = 0;
|
|
|
|
else
|
|
|
|
y++;
|
|
|
|
|
|
|
|
if (x == 0)
|
|
|
|
x = BOARD_SIZE - 1;
|
|
|
|
else
|
|
|
|
x--;
|
|
|
|
|
|
|
|
gSelectedSquare = XY_TO_SQUARE(x, y);
|
|
|
|
|
|
|
|
refreshSquare(oldSquare);
|
|
|
|
selectSquare(gSelectedSquare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void moveDownRight(void)
|
|
|
|
{
|
|
|
|
tSquare oldSquare = gSelectedSquare;
|
|
|
|
tPos x = SQUARE_TO_X(gSelectedSquare);
|
|
|
|
tPos y = SQUARE_TO_Y(gSelectedSquare);
|
|
|
|
|
|
|
|
if (y == BOARD_SIZE - 1)
|
|
|
|
y = 0;
|
|
|
|
else
|
|
|
|
y++;
|
|
|
|
|
|
|
|
if (x == BOARD_SIZE - 1)
|
|
|
|
x = 0;
|
|
|
|
else
|
|
|
|
x++;
|
|
|
|
|
|
|
|
gSelectedSquare = XY_TO_SQUARE(x, y);
|
|
|
|
|
|
|
|
refreshSquare(oldSquare);
|
|
|
|
selectSquare(gSelectedSquare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-21 00:53:52 +00:00
|
|
|
static void moveLeft(void)
|
|
|
|
{
|
|
|
|
tSquare oldSquare = gSelectedSquare;
|
|
|
|
tPos x = SQUARE_TO_X(gSelectedSquare);
|
|
|
|
tPos y = SQUARE_TO_Y(gSelectedSquare);
|
|
|
|
|
|
|
|
if (x == 0)
|
|
|
|
x = BOARD_SIZE - 1;
|
|
|
|
else
|
|
|
|
x--;
|
|
|
|
|
|
|
|
gSelectedSquare = XY_TO_SQUARE(x, y);
|
|
|
|
|
2016-07-21 04:39:06 +00:00
|
|
|
refreshSquare(oldSquare);
|
2016-07-21 00:53:52 +00:00
|
|
|
selectSquare(gSelectedSquare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void moveRight(void)
|
|
|
|
{
|
|
|
|
tSquare oldSquare = gSelectedSquare;
|
|
|
|
tPos x = SQUARE_TO_X(gSelectedSquare);
|
|
|
|
tPos y = SQUARE_TO_Y(gSelectedSquare);
|
|
|
|
|
|
|
|
if (x == BOARD_SIZE - 1)
|
|
|
|
x = 0;
|
|
|
|
else
|
|
|
|
x++;
|
|
|
|
|
|
|
|
gSelectedSquare = XY_TO_SQUARE(x, y);
|
|
|
|
|
2016-07-21 04:39:06 +00:00
|
|
|
refreshSquare(oldSquare);
|
2016-07-21 00:53:52 +00:00
|
|
|
selectSquare(gSelectedSquare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-22 15:02:25 +00:00
|
|
|
static bool swapUp(void)
|
2016-07-21 04:39:06 +00:00
|
|
|
{
|
2016-07-22 15:02:25 +00:00
|
|
|
bool result = false;
|
2016-07-21 04:39:06 +00:00
|
|
|
tPos y = SQUARE_TO_Y(gSelectedSquare);
|
|
|
|
|
|
|
|
if (y == 0) {
|
|
|
|
badThingHappened();
|
2016-07-22 15:02:25 +00:00
|
|
|
return result;
|
2016-07-21 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
resetStarAnim();
|
2016-07-22 15:02:25 +00:00
|
|
|
result = moveSquareInDir(gSelectedSquare, DIR_UP);
|
2016-07-21 04:39:06 +00:00
|
|
|
selectSquare(gSelectedSquare);
|
2016-07-22 15:02:25 +00:00
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
if (result)
|
|
|
|
gShouldSave = true;
|
|
|
|
|
2016-07-22 15:02:25 +00:00
|
|
|
return result;
|
2016-07-21 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-22 15:02:25 +00:00
|
|
|
static bool swapDown(void)
|
2016-07-21 04:39:06 +00:00
|
|
|
{
|
2016-07-22 15:02:25 +00:00
|
|
|
bool result = false;
|
2016-07-21 04:39:06 +00:00
|
|
|
tPos y = SQUARE_TO_Y(gSelectedSquare);
|
|
|
|
|
|
|
|
if (y == BOARD_SIZE - 1) {
|
|
|
|
badThingHappened();
|
2016-07-22 15:02:25 +00:00
|
|
|
return result;
|
2016-07-21 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
resetStarAnim();
|
2016-07-22 15:02:25 +00:00
|
|
|
result = moveSquareInDir(gSelectedSquare, DIR_DOWN);
|
2016-07-21 04:39:06 +00:00
|
|
|
selectSquare(gSelectedSquare);
|
2016-07-22 15:02:25 +00:00
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
if (result)
|
|
|
|
gShouldSave = true;
|
|
|
|
|
2016-07-22 15:02:25 +00:00
|
|
|
return result;
|
2016-07-21 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-22 15:02:25 +00:00
|
|
|
static bool swapLeft(void)
|
2016-07-21 04:39:06 +00:00
|
|
|
{
|
2016-07-22 15:02:25 +00:00
|
|
|
bool result = false;
|
2016-07-21 04:39:06 +00:00
|
|
|
tPos x = SQUARE_TO_X(gSelectedSquare);
|
|
|
|
|
|
|
|
if (x == 0) {
|
|
|
|
badThingHappened();
|
2016-07-22 15:02:25 +00:00
|
|
|
return result;
|
2016-07-21 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
resetStarAnim();
|
2016-07-22 15:02:25 +00:00
|
|
|
result = moveSquareInDir(gSelectedSquare, DIR_LEFT);
|
2016-07-21 04:39:06 +00:00
|
|
|
selectSquare(gSelectedSquare);
|
2016-07-22 15:02:25 +00:00
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
if (result)
|
|
|
|
gShouldSave = true;
|
|
|
|
|
2016-07-22 15:02:25 +00:00
|
|
|
return result;
|
2016-07-21 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-22 15:02:25 +00:00
|
|
|
static bool swapRight(void)
|
2016-07-21 04:39:06 +00:00
|
|
|
{
|
2016-07-22 15:02:25 +00:00
|
|
|
bool result = false;
|
2016-07-21 04:39:06 +00:00
|
|
|
tPos x = SQUARE_TO_X(gSelectedSquare);
|
|
|
|
|
|
|
|
if (x == BOARD_SIZE - 1) {
|
|
|
|
badThingHappened();
|
2016-07-22 15:02:25 +00:00
|
|
|
return result;
|
2016-07-21 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
resetStarAnim();
|
2016-07-22 15:02:25 +00:00
|
|
|
result = moveSquareInDir(gSelectedSquare, DIR_RIGHT);
|
2016-07-21 04:39:06 +00:00
|
|
|
selectSquare(gSelectedSquare);
|
2016-07-22 15:02:25 +00:00
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
if (result)
|
|
|
|
gShouldSave = true;
|
|
|
|
|
2016-07-22 15:02:25 +00:00
|
|
|
return result;
|
2016-07-21 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool isAppleButtonPressed(void)
|
|
|
|
{
|
2016-07-24 22:22:37 +00:00
|
|
|
return (isButtonPressed(JOY_BUTTON_0) || isButtonPressed(JOY_BUTTON_1));
|
2016-07-21 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void endGame(void)
|
|
|
|
{
|
2016-07-22 04:45:07 +00:00
|
|
|
mixedTextMode();
|
2016-07-21 04:39:06 +00:00
|
|
|
videomode(VIDEOMODE_80x24);
|
2016-07-22 22:18:15 +00:00
|
|
|
cputsxy(0, 20, " No more moves - GAME OVER!!");
|
2016-07-22 04:45:07 +00:00
|
|
|
gotoxy(0,21);
|
2016-07-22 22:18:15 +00:00
|
|
|
cprintf( " You made it to level %u", getLevel());
|
2016-07-22 04:45:07 +00:00
|
|
|
cputsxy(0,23, " Play again (Y/N)?");
|
2016-07-21 04:39:06 +00:00
|
|
|
|
2016-07-22 04:45:07 +00:00
|
|
|
while (true) {
|
|
|
|
switch (cgetc()) {
|
|
|
|
case 'y':
|
|
|
|
case 'Y':
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
case 'N':
|
|
|
|
case CH_ESC:
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
quitGame();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
badThingHappened();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-22 04:45:07 +00:00
|
|
|
static void refreshScore(tScore score)
|
2016-07-21 19:33:31 +00:00
|
|
|
{
|
|
|
|
if (score == gScoreBar)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gScoreBar = score;
|
|
|
|
drawScore(score);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-22 04:45:07 +00:00
|
|
|
static void refreshLevel(tLevel level)
|
|
|
|
{
|
|
|
|
bool waiting = true;
|
|
|
|
|
|
|
|
mixedTextMode();
|
|
|
|
videomode(VIDEOMODE_80x24);
|
|
|
|
gotoxy(0, 20);
|
2016-07-22 22:18:15 +00:00
|
|
|
cprintf( " Completed level %u!!", level);
|
|
|
|
cputsxy(0,22, " Press space to continue to the next level...");
|
2016-07-22 04:45:07 +00:00
|
|
|
|
|
|
|
while (waiting) {
|
|
|
|
switch (cgetc()) {
|
|
|
|
case ' ':
|
|
|
|
waiting = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
badThingHappened();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 05:11:36 +00:00
|
|
|
showAndClearDblLoRes();
|
2016-07-22 04:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void getHint(void)
|
|
|
|
{
|
|
|
|
refreshSquare(gSelectedSquare);
|
|
|
|
|
|
|
|
gSelectedSquare = getHintSquare();
|
|
|
|
selectSquare(gSelectedSquare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-22 05:11:36 +00:00
|
|
|
void initUI(void)
|
|
|
|
{
|
|
|
|
initGameEngine(&gCallbacks);
|
2016-07-22 15:43:18 +00:00
|
|
|
animInit();
|
2016-07-24 22:22:37 +00:00
|
|
|
initJoystick(&gJoyCallbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void joystickMove(tJoyPos position)
|
|
|
|
{
|
|
|
|
switch (position) {
|
|
|
|
case JOY_POS_DOWN:
|
|
|
|
moveDown();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOY_POS_DOWN_LEFT:
|
|
|
|
moveDownLeft();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOY_POS_LEFT:
|
|
|
|
moveLeft();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOY_POS_UP_LEFT:
|
|
|
|
moveUpLeft();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOY_POS_UP:
|
|
|
|
moveUp();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOY_POS_UP_RIGHT:
|
|
|
|
moveUpRight();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOY_POS_RIGHT:
|
|
|
|
moveRight();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOY_POS_DOWN_RIGHT:
|
|
|
|
moveDownRight();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
case JOY_POS_CENTER:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool joystickChangedCallback(tJoyState *oldState, tJoyState *newState)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (oldState->position != JOY_POS_CENTER)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ((newState->button0) ||
|
|
|
|
(newState->button1)) {
|
|
|
|
switch (newState->position) {
|
|
|
|
case JOY_POS_UP:
|
|
|
|
return swapUp();
|
|
|
|
|
|
|
|
case JOY_POS_DOWN:
|
|
|
|
return swapDown();
|
|
|
|
|
|
|
|
case JOY_POS_LEFT:
|
|
|
|
return swapLeft();
|
|
|
|
|
|
|
|
case JOY_POS_RIGHT:
|
|
|
|
return swapRight();
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
joystickMove(newState->position);
|
|
|
|
|
|
|
|
return false;
|
2016-07-22 05:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
static bool joystickNoChangeCallback(tJoyState *oldState)
|
|
|
|
{
|
|
|
|
if (oldState->button0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (oldState->button1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
joystickMove(oldState->position);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool pollKeyboard(void)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
uint8_t ch;
|
|
|
|
|
|
|
|
if (!kbhit())
|
|
|
|
return result;
|
|
|
|
|
|
|
|
ch = cgetc();
|
|
|
|
switch (ch) {
|
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
case CH_CURS_UP:
|
2016-07-26 05:26:24 +00:00
|
|
|
if (!isAppleButtonPressed()) {
|
2016-07-24 22:22:37 +00:00
|
|
|
moveUp();
|
2016-07-26 05:26:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Fallthrough...
|
|
|
|
case 139:
|
|
|
|
result = swapUp();
|
2016-07-24 22:22:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'j':
|
|
|
|
case 'J':
|
|
|
|
case CH_CURS_LEFT:
|
2016-07-26 05:26:24 +00:00
|
|
|
if (!isAppleButtonPressed()) {
|
2016-07-24 22:22:37 +00:00
|
|
|
moveLeft();
|
2016-07-26 05:26:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Fallthrough...
|
|
|
|
case 136:
|
|
|
|
result = swapLeft();
|
2016-07-24 22:22:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'k':
|
|
|
|
case 'K':
|
|
|
|
case CH_CURS_RIGHT:
|
2016-07-26 05:26:24 +00:00
|
|
|
if (!isAppleButtonPressed()) {
|
2016-07-24 22:22:37 +00:00
|
|
|
moveRight();
|
2016-07-26 05:26:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Fallthrough...
|
|
|
|
case 149:
|
|
|
|
result = swapRight();
|
2016-07-24 22:22:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
case CH_CURS_DOWN:
|
2016-07-26 05:26:24 +00:00
|
|
|
if (!isAppleButtonPressed()) {
|
2016-07-24 22:22:37 +00:00
|
|
|
moveDown();
|
2016-07-26 05:26:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Fallthrough...
|
|
|
|
case 138:
|
|
|
|
result = swapDown();
|
2016-07-24 22:22:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CH_ESC:
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
if (gShouldSave) {
|
|
|
|
mixedTextMode();
|
|
|
|
videomode(VIDEOMODE_80x24);
|
|
|
|
gotoxy(0, 20);
|
|
|
|
cprintf("\n\nSaving your game so you can continue\r\n later...");
|
|
|
|
saveGame();
|
|
|
|
}
|
|
|
|
quitGame();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
case 'R':
|
|
|
|
refreshScore(0);
|
|
|
|
startNewGame();
|
|
|
|
gShouldSave = false;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
toggleSound();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
getHint();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
printInstructions();
|
|
|
|
showAndClearDblLoRes();
|
|
|
|
drawBoard();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
badThingHappened();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-21 00:53:52 +00:00
|
|
|
void playGame(void)
|
|
|
|
{
|
2016-07-22 05:11:36 +00:00
|
|
|
bool gameLoaded = false;
|
|
|
|
uint8_t ch;
|
|
|
|
|
2016-07-21 19:33:31 +00:00
|
|
|
gScoreBar = 0;
|
2016-07-24 22:22:37 +00:00
|
|
|
gShouldSave = false;
|
|
|
|
|
|
|
|
printf("\n\nChecking for a saved game...");
|
2016-07-21 19:33:31 +00:00
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
if (loadGame()) {
|
|
|
|
bool gotAnswer = false;
|
2016-07-22 05:11:36 +00:00
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
printf("\n\nYou have a saved game!\n Would you like to continue it (Y/N)");
|
|
|
|
|
|
|
|
while (!gotAnswer) {
|
|
|
|
ch = cgetc();
|
|
|
|
switch (ch) {
|
|
|
|
case 'y':
|
|
|
|
case 'Y':
|
|
|
|
printf("\n\nLoading your saved puzzle");
|
|
|
|
gotAnswer = true;
|
|
|
|
gShouldSave = true;
|
|
|
|
gameLoaded = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
case 'N':
|
|
|
|
gotAnswer = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
badThingHappened();
|
|
|
|
break;
|
2016-07-22 05:11:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 00:53:52 +00:00
|
|
|
|
2016-07-22 05:11:36 +00:00
|
|
|
showAndClearDblLoRes();
|
|
|
|
if (!gameLoaded) {
|
|
|
|
startNewGame();
|
|
|
|
}
|
2016-07-21 00:53:52 +00:00
|
|
|
drawBoard();
|
|
|
|
while (true) {
|
2016-07-24 22:22:37 +00:00
|
|
|
resetStarAnim();
|
2016-07-21 00:53:52 +00:00
|
|
|
|
2016-07-24 22:22:37 +00:00
|
|
|
while (true) {
|
2016-07-22 15:43:18 +00:00
|
|
|
doStarAnim();
|
2016-07-24 22:22:37 +00:00
|
|
|
|
|
|
|
if (pollKeyboard()) {
|
2016-07-21 00:53:52 +00:00
|
|
|
break;
|
2016-07-24 22:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pollJoystick()) {
|
2016-07-21 04:39:06 +00:00
|
|
|
break;
|
2016-07-24 22:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gameIsOver()) {
|
|
|
|
endGame();
|
|
|
|
showAndClearDblLoRes();
|
|
|
|
refreshScore(0);
|
|
|
|
startNewGame();
|
|
|
|
gShouldSave = false;
|
2016-07-21 00:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|