Solitaire for the TI-84 Plus CE!
1// Calculation Solitaire / CALCSLTR for the TI-84 Plus CE
2// Copyright (C) 2025 euphory
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#include "save.h"
18
19#include <sys/rtc.h>
20#include <fileioc.h>
21#include <stdlib.h>
22
23#include "variables.h"
24#include "drawing.h"
25#include "ops.h"
26
27unsigned char deck[7];
28unsigned char deckCards;
29
30unsigned int *numWins;
31unsigned char *saveData;
32
33void deal()
34{
35 progress = 0;
36 deckCards = 52;
37
38 for (unsigned char i = 0; i < 7; i++)
39 {
40 deck[i] = 0x00;
41 }
42
43 for (unsigned char i = 0; i < NUM_TABLSLOTS; i++)
44 {
45 for (unsigned char j = 0; j < TABL_STACK_SIZE; j++)
46 {
47 tableau[i][j] = CARD_EMPTY;
48 }
49 }
50
51 for (unsigned char i = 0; i < NUM_FREECELLS; i++)
52 {
53 freeCells[i] = CARD_EXISTS | i | (unsigned char)(rand() & CARD_SUIT);
54 removeFromDeck(freeCells[i]);
55 }
56
57 animateDeal();
58}
59
60void loadWins()
61{
62 unsigned char winsHandle = ti_Open(WINS_VAR_NAME, "r+");
63
64 if (winsHandle == 0)
65 {
66 // create a new blank file
67 winsHandle = ti_Open(WINS_VAR_NAME, "w");
68 ti_PutC(0, winsHandle);
69 ti_Seek(0, SEEK_SET, winsHandle);
70 }
71
72 // is there already an entry for this game?
73 unsigned char *length = ti_GetDataPtr(winsHandle);
74 highscore_t *entries = (highscore_t *)length + sizeof(char);
75 for (unsigned char i = 0; i < *length; i++)
76 {
77 if (entries[i].gameId == GAME_ID)
78 {
79 numWins = &(entries[i].score);
80 goto found_wins;
81 }
82 }
83
84 // add a new blank entry
85 (*length)++;
86 ti_Seek(sizeof(char) + *length * sizeof(highscore_t), SEEK_SET, winsHandle);
87 const highscore_t newHighScore = { GAME_ID, 0 };
88 highscore_t *entry = (highscore_t *)ti_GetDataPtr(winsHandle);
89 ti_Write(&newHighScore, sizeof(highscore_t), 1, winsHandle);
90 numWins = &(entry->score);
91
92found_wins:
93 ti_Close(winsHandle);
94}
95
96void load()
97{
98 unsigned char saveHandle = ti_Open(SAVE_VAR_NAME, "r");
99
100 if (saveHandle == 0)
101 {
102 // no save present
103 deal();
104 }
105 else
106 {
107 unsigned char magicNumber = ti_GetC(saveHandle);
108 if (magicNumber != GAME_ID)
109 {
110 // this is from a different solitaire game
111 deal();
112 }
113 else
114 {
115 // load save
116 ti_Read(freeCells, 1, NUM_FREECELLS, saveHandle);
117 ti_Read(tableau, 1, NUM_TABLSLOTS * TABL_STACK_SIZE, saveHandle);
118
119 // how far along does that make us?
120 progress = 0;
121 for (unsigned char i = 0; i < NUM_FREECELLS; i++)
122 {
123 if ((freeCells[i] & CARD_NUMBER) == CARD_KING) progress++;
124 }
125
126 ti_Read(&deckCards, 1, 1, saveHandle);
127 ti_Read(deck, 1, 7, saveHandle);
128 ti_Read(&selectedCard, 1, 1, saveHandle);
129 }
130 }
131
132 ti_Close(saveHandle);
133}
134
135void save()
136{
137 unsigned char saveHandle = ti_Open(SAVE_VAR_NAME, "w");
138
139 ti_PutC(GAME_ID, saveHandle);
140 ti_Write(freeCells, 1, NUM_FREECELLS, saveHandle);
141 ti_Write(tableau, 1, NUM_TABLSLOTS * TABL_STACK_SIZE, saveHandle);
142 ti_Write(&deckCards, 1, 1, saveHandle);
143 ti_Write(deck, 1, 7, saveHandle);
144 ti_Write(&selectedCard, 1, 1, saveHandle);
145
146 ti_Close(saveHandle);
147}
148
149void deleteSave()
150{
151 ti_Delete(SAVE_VAR_NAME);
152}