mirror of
https://github.com/jeremysrand/a2sudoku.git
synced 2024-06-06 12:29:29 +00:00
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:
parent
c14863bc85
commit
8c4afe748e
|
@ -38,6 +38,7 @@ typedef struct tGame {
|
||||||
struct tPuzzle *puzzle;
|
struct tPuzzle *puzzle;
|
||||||
tUpdatePosCallback callback;
|
tUpdatePosCallback callback;
|
||||||
bool undoValid;
|
bool undoValid;
|
||||||
|
time_t startTime;
|
||||||
} tGame;
|
} tGame;
|
||||||
|
|
||||||
|
|
||||||
|
@ -85,6 +86,7 @@ void restartGame(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
theGame.undoValid = false;
|
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) {
|
if (saveFile != NULL) {
|
||||||
bool isValid = true;
|
bool isValid = true;
|
||||||
fwrite(&isValid, sizeof(isValid), 1, saveFile);
|
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);
|
fwrite(&theGame, sizeof(theGame), 1, saveFile);
|
||||||
savePuzzle(theGame.puzzle, saveFile);
|
savePuzzle(theGame.puzzle, saveFile);
|
||||||
fclose(saveFile);
|
fclose(saveFile);
|
||||||
|
@ -176,6 +186,11 @@ bool loadGame(tUpdatePosCallback callback)
|
||||||
|
|
||||||
theGame.callback = 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);
|
theGame.puzzle = loadPuzzle(saveFile);
|
||||||
|
|
||||||
fclose(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)
|
bool isSquareInvalid(tPos col, tPos row)
|
||||||
{
|
{
|
||||||
tSquareVal value = SQUARE_XY(col, row).value;
|
tSquareVal value = SQUARE_XY(col, row).value;
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
#include "puzzles.h"
|
#include "puzzles.h"
|
||||||
|
|
||||||
#ifndef __a2sudoku__game__
|
#ifndef __a2sudoku__game__
|
||||||
|
@ -34,6 +35,8 @@ extern void refreshPos(tPos x, tPos y);
|
||||||
|
|
||||||
extern bool isPuzzleSolved(void);
|
extern bool isPuzzleSolved(void);
|
||||||
|
|
||||||
|
extern time_t timeToSolve(void);
|
||||||
|
|
||||||
// Returns false if unable to set the value for some reason
|
// Returns false if unable to set the value for some reason
|
||||||
extern bool setValueAtPos(tPos x, tPos y, tSquareVal val);
|
extern bool setValueAtPos(tPos x, tPos y, tSquareVal val);
|
||||||
|
|
||||||
|
|
Binary file not shown.
|
@ -10,6 +10,7 @@
|
||||||
#include <conio.h>
|
#include <conio.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <tgi.h>
|
#include <tgi.h>
|
||||||
#include <tgi/tgi-mode.h>
|
#include <tgi/tgi-mode.h>
|
||||||
|
|
||||||
|
@ -45,9 +46,6 @@ extern char a2e_hi;
|
||||||
((BOARD_SIZE + 1) * THIN_LINE_HEIGHT) + \
|
((BOARD_SIZE + 1) * THIN_LINE_HEIGHT) + \
|
||||||
(((BOARD_SIZE / SUBSQUARE_SIZE) + 1) * (THICK_LINE_HEIGHT - 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_WIDTH 9
|
||||||
#define SCRATCH_HEIGHT 6
|
#define SCRATCH_HEIGHT 6
|
||||||
|
|
||||||
|
@ -497,18 +495,41 @@ void updatePos(tPos x, tPos y, tSquareVal val, tScratchValues scratch, bool corr
|
||||||
|
|
||||||
void youWon(void)
|
void youWon(void)
|
||||||
{
|
{
|
||||||
int wonX = (TOTAL_WIDTH - WON_WIDTH) / 2;
|
int line1Width;
|
||||||
int wonY = (TOTAL_HEIGHT - WON_HEIGHT) / 2;
|
int line2Width;
|
||||||
char *line1 = "You solved it!";
|
int wonWidth;
|
||||||
|
int wonHeight;
|
||||||
|
int wonX;
|
||||||
|
int wonY;
|
||||||
|
static char line1[80];
|
||||||
char *line2 = "Press any key";
|
char *line2 = "Press any key";
|
||||||
|
time_t solutionTime = timeToSolve();
|
||||||
int textHeight = tgi_textheight(line1);
|
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_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_setcolor(COLOR_WHITE);
|
||||||
tgi_outtextxy(wonX + 10, wonY + (textHeight * 2), line1);
|
tgi_outtextxy((TOTAL_WIDTH - line1Width) / 2, wonY + (textHeight * 2), line1);
|
||||||
tgi_outtextxy(wonX + 7, wonY + (textHeight * 4), line2);
|
tgi_outtextxy((TOTAL_WIDTH - line2Width) / 2, wonY + (textHeight * 4), line2);
|
||||||
|
|
||||||
cgetc();
|
cgetc();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user