Add support for printing the time to solve the puzzle if there is a real time clock in the system.

This commit is contained in:
Jeremy Rand 2015-08-19 00:35:18 -04:00
parent c14863bc85
commit 8c4afe748e
4 changed files with 57 additions and 9 deletions

View File

@ -38,6 +38,7 @@ typedef struct tGame {
struct tPuzzle *puzzle;
tUpdatePosCallback callback;
bool undoValid;
time_t startTime;
} tGame;
@ -85,6 +86,7 @@ void restartGame(void)
}
theGame.undoValid = false;
theGame.startTime = _systime();
}
@ -107,6 +109,8 @@ void startGame(tDifficulty difficulty, tUpdatePosCallback callback)
}
}
}
theGame.startTime = _systime();
}
@ -116,6 +120,12 @@ void saveGame(void)
if (saveFile != NULL) {
bool isValid = true;
fwrite(&isValid, sizeof(isValid), 1, saveFile);
// Change the start time into an elapsed time before saving...
if (theGame.startTime != 0xffffffff) {
theGame.startTime = _systime() - theGame.startTime;
}
fwrite(&theGame, sizeof(theGame), 1, saveFile);
savePuzzle(theGame.puzzle, saveFile);
fclose(saveFile);
@ -176,6 +186,11 @@ bool loadGame(tUpdatePosCallback callback)
theGame.callback = callback;
// The saved start time is an elapsed time. Convert it back into a start time relative to now.
if (theGame.startTime != 0xffffffff) {
theGame.startTime = _systime() - theGame.startTime;
}
theGame.puzzle = loadPuzzle(saveFile);
fclose(saveFile);
@ -219,6 +234,15 @@ bool isPuzzleSolved(void)
}
time_t timeToSolve(void)
{
if (theGame.startTime == 0xffffffff)
return theGame.startTime;
return _systime() - theGame.startTime;
}
bool isSquareInvalid(tPos col, tPos row)
{
tSquareVal value = SQUARE_XY(col, row).value;

View File

@ -7,6 +7,7 @@
//
#include <time.h>
#include "puzzles.h"
#ifndef __a2sudoku__game__
@ -34,6 +35,8 @@ extern void refreshPos(tPos x, tPos y);
extern bool isPuzzleSolved(void);
extern time_t timeToSolve(void);
// Returns false if unable to set the value for some reason
extern bool setValueAtPos(tPos x, tPos y, tSquareVal val);

Binary file not shown.

View File

@ -10,6 +10,7 @@
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tgi.h>
#include <tgi/tgi-mode.h>
@ -45,9 +46,6 @@ extern char a2e_hi;
((BOARD_SIZE + 1) * THIN_LINE_HEIGHT) + \
(((BOARD_SIZE / SUBSQUARE_SIZE) + 1) * (THICK_LINE_HEIGHT - THIN_LINE_HEIGHT)))
#define WON_WIDTH 100
#define WON_HEIGHT 55
#define SCRATCH_WIDTH 9
#define SCRATCH_HEIGHT 6
@ -497,18 +495,41 @@ void updatePos(tPos x, tPos y, tSquareVal val, tScratchValues scratch, bool corr
void youWon(void)
{
int wonX = (TOTAL_WIDTH - WON_WIDTH) / 2;
int wonY = (TOTAL_HEIGHT - WON_HEIGHT) / 2;
char *line1 = "You solved it!";
int line1Width;
int line2Width;
int wonWidth;
int wonHeight;
int wonX;
int wonY;
static char line1[80];
char *line2 = "Press any key";
time_t solutionTime = timeToSolve();
int textHeight = tgi_textheight(line1);
if (solutionTime == 0xffffffff) {
snprintf(line1, sizeof(line1), "You solved it!");
} else {
snprintf(line1, sizeof(line1), "You solved it in %lu minutes!", solutionTime / 60);
}
line1Width = strlen(line1) * 6;
line2Width = strlen(line2) * 6;
wonHeight = textHeight * 7;
wonWidth = line1Width;
if (wonWidth < line2Width)
wonWidth = line2Width;
wonWidth += 20;
wonX = (TOTAL_WIDTH - wonWidth) / 2;
wonY = (TOTAL_HEIGHT - wonHeight) / 2;
tgi_setcolor(COLOR_BLACK);
tgi_bar(wonX, wonY, wonX + WON_WIDTH, wonY + WON_HEIGHT);
tgi_bar(wonX, wonY, wonX + wonWidth, wonY + wonHeight);
tgi_setcolor(COLOR_WHITE);
tgi_outtextxy(wonX + 10, wonY + (textHeight * 2), line1);
tgi_outtextxy(wonX + 7, wonY + (textHeight * 4), line2);
tgi_outtextxy((TOTAL_WIDTH - line1Width) / 2, wonY + (textHeight * 2), line1);
tgi_outtextxy((TOTAL_WIDTH - line2Width) / 2, wonY + (textHeight * 4), line2);
cgetc();
}