I need help Fixing this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <windows.h>
#include <unistd.h>

using namespace std;

class SodukoGame{

private:
int gamingBorad[9][9];
int boardOfSolution[9][9];
int openedBoard[9][9];
char difficultyLevel;
int points;
string playFile, solFile, storeLog;
string openedLine, solLine;
int count = 0;
public:

SodukoGame() {

for (int i = 0; i < 9; ++i) {

for (int j = 0; j < 9; ++j) {

this->openedBoard[i][j] = 0; //starting number board
this->gamingBorad[i][j] = 0; // gaming board
this->boardOfSolution[i][j] = j + 1; //board for hidden solutions
}
}
}

void startPlaying() {
int coloumn, row, number;
char game;
cout << boolalpha;

sayWelcomeToPlayer();

this->points = 100;

this->chooseDifficultyLevel();
this->gameInit(this->playFile, this->solFile);


this->displayBoard();


while (true) {

coloumn = this->validColumn();
// break if new game or quit
if (coloumn == 13 || coloumn == 16) {
break;
}


row = this->validRow();
// quit if it is a new game
if (row == 29 || row == 61 || row == 32 || row == 64) {
break;
}


if (this->openATile(coloumn, row)) {

number = this->validNumber();

if (number == 30 || number == 62 || number == 33 || number == 65) {
break;
}
cout << endl;

this->move(coloumn, row, number);
}
// otherwise, display not open
else {
cout << "Input open tile.";
}


if (this->playEnd()) {
this->saveGame(); // save completed game
this->displayScore(); // displays stats
game = this->playGameAgain();
if (game == 'Y') {
this->startPlaying();
}
else {
cout << "Thanks for playing";
}
break;
}
}

if (coloumn == 13 || row == 29 || row == 61 || number == 30 || number == 62) {
cout << endl;
this->startPlaying();
}


if (coloumn==16 || row == 32 || row == 64 || number == 33 || number == 65) {
cout << "Game ended early";
}
}

void displayBoard() {
cout << "N: Play New Game\n Q: Quit Game\n S: Display Stats\n";
cout << " A B C D E F G H I \n";
cout << "===============================================\n";

for (int i = 0; i < 9; ++i) {
cout << i + 1 << " ||";

for (int j = 0; j < 9; ++j) {

if (this->gamingBorad[i][j] == 0) {

if ((j + 1) % 3 == 0) {
cout << " ||";
}

else {
cout << " || ";
}
}
// otherwise, print number
else {
// if opening number, print in blue
if (this->gamingBorad[i][j] == this->openedBoard[i][j] && this->openedBoard[i][j] != 0) {
cout << " " << " " << this->gamingBorad[i][j] << " ";
}
// otherwise
else {
// if correct space, print in green
if (this->gamingBorad[i][j] == this->boardOfSolution[i][j]) {
cout << " " << " " << this->gamingBorad[i][j] << " ";
}
// else, print in red
else {
cout << " " << " " << this->gamingBorad[i][j] << " ";
}
}

if ((j + 1) % 3 == 0) {
cout << " ||";
}

else {
cout << " || ";
}
}
}
if ((i + 1) % 3 == 0) {

if (i == 8) {
cout << "\n ===============================================\n";
}

else {
cout << "\n ||=============||=============||=============||\n";
}
}
else {
cout << "\n =============||=============" <<
"\033[1;0m||\033[1;30m=============\033[1;0m||\n";
}
}
cout << endl;
cout << "points: " << this->points << endl << endl;
}

// choosing difficultyLevel

void chooseDifficultyLevel() {

cout << "Pick a difficultyLevel: \n" << "1.Very Easy\n2.Easy\n3.Medium\n4.Hard\n\n";
cout << "difficultyLevel: ";
cin >> this->difficultyLevel;
while (cin.get() != '\n') {}
cout << endl;

// set files
while (true) {
if (this->difficultyLevel == '1') {
this->playFile = "veryEasy.txt";
this->solFile = "veryEasySolutions.txt";
break;
}

else if (this->difficultyLevel == '2') {
this->playFile = "easy.txt";
this->solFile = "easySolutions.txt";
break;
}

else if (this->difficultyLevel == '3') {
this->playFile = "medium.txt";
this->solFile = "mediumSolutions.txt";
break;
}

else if (this->difficultyLevel == '4') {
this->playFile = "hard.txt";
this->solFile = "hardSolutions.txt";
break;
}

else {
cout << "Sorry that isn't an answer.\n\n";
this->chooseDifficultyLevel();
}
}
}


void gameInit(string openFile, string solFile) {
int randomLine = randomize(); // randomizes a number (0-9) for which SodukoGame board
ifstream openGame(openFile.c_str()); // c.str turns a string a readable format for reading in files
ifstream openSolution(solFile.c_str());
for (int lines = 0; lines <= randomLine; ++lines) { // this loops through every line until the random line is reached - then stores line it's stopped on it
getline(openGame, openedLine);
getline(openSolution, solLine);
}

for (int row = 0; row < 9; ++row) {
for (int coloumn = 0; coloumn < 9; ++coloumn) {
openedBoard[row][coloumn] = charConvert(openedLine[count]);
gamingBorad[row][coloumn] = charConvert(openedLine[count]);
boardOfSolution[row][coloumn] = charConvert(solLine[count]);
++count;
}
}
}


int charConvert(char c) {
return int(c) - 48;
}

int validRow() {
char row; // local variable
while (true) {

cout << "row (1 - 9): ";
cin >> row;
while (cin.get() != '\n') {} // flush


row = charConvert(row) - 1;


if ((row >= 0 && row < 9) || row == 29 || row == 61 || row == 32 || row == 64) {
return row;
}

else if (row == 34 || row == 66) {
this->printStats();
}
else {
cout << "Please enter a valid row (1 - 9).\n\n";
}
}
}

int validColumn() {
char coloumn; // local variable
while (true) {

cout << "column (A - I): ";
cin >> coloumn;
while (cin.get() != '\n') {} // flush


coloumn = int(toupper(coloumn)) - 65;


if ((coloumn >= 0 && coloumn < 9) || coloumn == 13 || coloumn == 16) {
return coloumn;
}
else if (coloumn == 18) {
this->printStats();
}
else {
cout << "Please enter a valid column (A - I).\n\n";
}
}
}

int validNumber() {
char number; // local variable
while (true) {

cout << "Number to enter (1 - 9): ";
cin >> number;
while (cin.get() != '\n') {} // flush


number = charConvert(number);

if ((number > 0 && number <= 9) || number == 30 || number == 62 || number == 33 || number == 65) {
return number;
}
else if (number == 35 || number == 67) {
this->printStats();
}
else {
cout << "Please enter a valid number (1 - 9).\n\n";
}
}
}

bool openATile(int coloumn, int row) {
// if open tile, return true
if (this->gamingBorad[row][coloumn] == 0) {
return true;
}

else {
return false;
}
}

bool correctTile(int coloumn, int row, int number) {
// if number entered for move correct for tile, return true
if (number == this->boardOfSolution[row][coloumn]) {
return true;
}

return false;
}

void move(int coloumn, int row, int number) {

if (this->correctTile(coloumn, row, number)) {
// clear screen
for (int i = 0; i < 50; ++i) {
cout << endl;
}

cout << "Correct!\n\n";
// change game board tile to the number specified
this->gamingBorad[row][coloumn] = number;
this->displayBoard();
}

else {

this->points -= 5;

for (int i = 0; i < 50; ++i) {
cout << endl;
}

cout << "incorrect move";
int temp = this->gamingBorad[row][coloumn];
this->gamingBorad[row][coloumn] = number;
this->displayBoard();
Sleep(4000);

for (int i = 0; i < 50; ++i) {
cout << endl;
}


this->gamingBorad[row][coloumn] = temp;
this->displayBoard();
}
}

void pauseTime(int seconds) {
clock_t temp;
temp = clock() + seconds * CLOCKS_PER_SEC;
while (clock() < temp) {}
}

bool playEnd() {
// rows
for (int i = 0; i < 9; ++i) {
// columns
for (int j = 0; j < 9; ++j) {
if (this->gamingBorad[i][j] == this->boardOfSolution[i][j]) {
continue;
}
else {
return false;
}
}
}
return true;
}

int randomize() {
srand(time(NULL));
int number = rand() % 10;
return number;
}

void sayWelcomeToPlayer(){
cout << "WELCOME!";
}

char playGameAgain() {
char ch;
while (true) {
cout << "play Again ? ([Y]es or [N]o): ";
cin >> ch;
ch = toupper(ch);

if (ch == 'Y' || ch == 'N') {
return ch;
}

else {
cout << "Invalid choice";
}
}
}

//end the SudokuGame class
};

int main(){
SodukoGame S1;
S1.startPlaying();
return 0;
}
Last edited on
Please be very specific about what help your need.

If you're getting compile errors, post the full error text.
If you're not getting the output you expect, post what input you're supplying, what you think the output should be and what output you're receiving.

I get compile errors trying to compile your program.
Line 88: saveGame() is not defined.
Line 89: displayGame() is not defined.
Line 264,288,312: printStats() is not defined.

You're going to have to supply the undefined functions before this will compile.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Last edited on
So im trying to make this For my class and i have the code above i'm stuck trying to write

those saveGame(), DisplaGame() and the printStat() functions I've tried but i just cant figure

it out and when I run the code instead of getting a nice clean board i get some random

numbers ill attach a picture -> https://ibb.co/ctNLy5

This is the prompt on how i'm supposed to make the game

use a file to store the 40 soduko possible games 10 very easy 10 easy 10 medium 10 hard

Have the user pick the level (very easy, easy, medium, hard) and then have the computer

randomly pick one(1) of the ten(10) games for that level.

Print out the game board ( 9 by 9 ), and load it with the initial values for the game, in other

words pre-populate the game board with those initial values and make them a certain color

like BLUE.

The cells where the user is to input values will be blank. Next, have an easy to use method

where a user can select an EMPTY cell and enter a number (Color Green), between 1 and 9.

Be sure to validate that the user is only entering values into an EMPTY cell.. Have the

computer check if the user input is a valid value… Inform the user if the number entered by

them is not the correct number for the cell.. by placing

the incorrect number in the chosen cell for 5 seconds FLASHING, then erase/blank the cell…

Keep track of the number of errors the user makes. Possible scoring… 100 points if game

complete successfully with no errors… subtract 5 points per error. Show current score on

screen. Code for the following winning conditions with these messages.

At end of game if score == 100 then ‘Excellent Player, should try next level’ If score > = 80

and < 100 then ‘ Good player, keep practicing on this level’ If score >= 60 and < 80 then ‘OK

player… keep practicing on this level if you dare’ If score >= 40 and < 60 ‘ Well.. suggest

trying a easier level’ If score < 40 then ‘Watch this video on how to play’….

Have a quit game option available while playing Have a new game option while playing Keep

track of the number of games played at each level and average of score at that level. (hint

files) Have option to allow user see average for all levels. THIS GAME IS to be written using

C++ Classes… The main function must look like this: Int main() { SUDOKU S1;

// instantiate game. S1.startGame(); // start game.. } All classes are to be written using

separate .h interface file and the .cpp implementation file. There will be NO global variables.

This is a character based application, not a GUI.


Last edited on
What is the problem anyway? can't seem to detect it by myself when tried..
If you find it troubling, detecting errors, there are a lot of tools to help you with it. I tend to use checkmarx sometimes, might help.
Anyway, you can try and specify what's not working.
Good luck.
Ben.
Topic archived. No new replies allowed.