Can you help fix this code #include <iostream> #include <cstdlib> #include <ctim
ID: 3827808 • Letter: C
Question
Can you help fix this code
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
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:
SudokuGame() {
// initialize all boards
for (int i = 0; i < 9; ++i) {
// columns
for (int j = 0; j < 9; ++j) {
// 1 - 9 per row
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 << booleanAlpha;
sayWelcomeToPlayer();
this->points = 100;
this->chooseDifficultyLevel();
this->gameInit(this->playFile, this->solFile);
// display the game
this->displayBoard();
// Start playing
while (true) {
// column input
coloumn = this->validColumn();
// break if new game or quit
if (coloumn == 13 || coloumn == 16) {
break;
}
// input a row
row = this->validRow();
// quit if it is a new game
if (row == 29 || row == 61 || row == 32 || row == 64) {
break;
}
// check if it is an open tile,
if (this->openATile(coloumn, row)) {
// input a number
number = this->validNumber();
// break if new game or quit
if (number == 30 || number == 62 || number == 33 || number == 65) {
break;
}
cout << endl;
// make a game move
this->move(coloumn, row, number);
}
// otherwise, display not open
else {
cout << "Input open tile.";
}
// if game the is over then quit else continue
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;
}
}
// create a new game
if (coloumn == 13 || row == 29 || row == 61 || number == 30 || number == 62) {
cout << endl;
this->startPlaying();
}
// end the game early
if (coloumn==16 || row == 32 || row == 64 || number == 33 || number == 65) {
cout << "Game ended early";
}
}
void displayBoard() {
cout << "N: Play New Game Q: Quit Game S: Display Stats ";
cout << " A B C D E F G H I ";
cout << "=============================================== ";
//select a row for displaying
for (int i = 0; i < 9; ++i) {
cout << i + 1 << " ||";
// print the columns in the row
for (int j = 0; j < 9; ++j) {
// blank if not solved yet
if (this->gamingBorad[i][j] == 0) {
// border
if ((j + 1) % 3 == 0) {
cout << " ||";
}
// within border
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) {
// check if it's bottom row
if (i == 8) {
cout << " =============================================== ";
}
else {
cout << " ||=============||=============||=============|| ";
}
}
else {
cout << " =============||=============" <<
"[1;0m||[1;30m=============[1;0m|| ";
}
}
cout << endl;
cout << "points: " << this->points << endl << endl;
}
// choosing difficultyLevel
void chooseDifficultyLevel() {
// ch
cout << "Pick a difficultyLevel: " << "1.Very Easy 2.Easy 3.Medium 4.Hard ";
cout << "difficultyLevel: ";
cin >> this->difficultyLevel;
while (cin.get() != ' ') {}
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. ";
this->chooseDifficultyLevel();
}
}
}
// reading SudokuGame boards into arrays
void gameInit(string openFile, string solFile) {
int randomLine = randomize(); // randomizes a number (0-9) for which SudokuGame 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 throug 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] = convertChar(openedLine[count]);
gamingBorad[row][coloumn] = convertChar(openedLine[count]);
boardOfSolution[row][coloumn] = convertChar(solLine[count]);
++count;
}
}
}
int convertChar(char c) {
return int(c) - 48;
}
int validRow() {
char row; // local variable
while (true) {
// input
cout << "row (1 - 9): ";
cin >> row;
while (cin.get() != ' ') {} // flush
// convert to int equivalent
row = charConvert(row) - 1;
// check if valid row, if so, break
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). ";
}
}
}
int validColumn() {
char coloumn; // local variable
while (true) {
// input
cout << "column (A - I): ";
cin >> coloumn;
while (cin.get() != ' ') {} // flush
// convert to int equivalent
coloumn = int(toupper(coloumn)) - 65;
// check if valid column, if so, break
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). ";
}
}
}
int validNumber() {
char number; // local variable
while (true) {
// input
cout << "Number to enter (1 - 9): ";
cin >> number;
while (cin.get() != ' ') {} // flush
// convert to int equivalent
number = charConvert(number);
// check if valid number, if so, break
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). ";
}
}
}
bool openATile(int coloumn, int row) {
// if open tile, return true
if (this->gamingBorad[row][coloumn] == 0) {
return true;
}
// otherwise, return false
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;
}
// otherwise return false
return false;
}
void move(int coloumn, int row, int number) {
// if correct tile,
if (this->correctTile(coloumn, row, number)) {
// clear screen
for (int i = 0; i < 50; ++i) {
cout << endl;
}
cout << "Correct! ";
// change game board tile to the number specified
this->gamingBorad[row][coloumn] = number;
this->displayBoard();
}
else {
// deduct 5 points for error
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;
}
// display current board
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(){
SudokuGame S1;
S1.startPlaying();
return 0;
}
Explanation / Answer
Note:
Your program had spelling mistake for the class SodukoGame
Next: You have not defined following methods
1. printStats();
2.saveGame()
Next: you had a speling mistake of convertChar, you had mentioned charConvert
You also had a spelling mistake of Sleep(4000), it is sleep(4000).
So you just need to write the two missing methods
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.