Hi, I already got an answer for the following assignment using Visual studios C+
ID: 3692285 • Letter: H
Question
Hi, I already got an answer for the following assignment using Visual studios C++. I will type the code I received from the expert and the errors I get when trying to run the code.
SUDOKU game.
Find an online game book and code up 40 possible Sudoku games. ( use a file to store the 40 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.
Code from Expert:
Errors found in Visual Studios C++: boolearAplha and chartConvert are undefined. How can I fix this problem?
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
class SudokuGame {
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 SudokuGame::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
// choosing difficultyLevel
void SudokuGame::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();
system("pause");
return 0;
}
Explanation / Answer
Please follow the code and comments for the description of the code :
As the code has some bugs and it was difficult for me to analyse I am providing you withe the new one.
CODE :
#include <iostream>
using namespace std;
void readAPuzzle(int grid[][9]);
bool search(int grid[][9]);
int getFreeCellList(const int grid[][9], int freeCellList[][2]);
void printGrid(const int grid[][9]);
bool isValid(int i, int j, const int grid[][9]);
bool isValid(const int grid[][9]);
int main()
{
// Read a Sudoku puzzle
int grid[9][9];
readAPuzzle(grid);
if (!isValid(grid))
cout << "Invalid input" << endl;
else if (search(grid))
{
cout << "The solution is found:" << endl;
printGrid(grid);
}
else
cout << "No solution" << endl;
return 0;
}
// Read a Sudoku puzzle from the keyboard
void readAPuzzle(int grid[][9])
{
cout << "Enter a Sudoku puzzle:" << endl;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
cin >> grid[i][j];
}
// Obtain a list of free cells from the puzzle
int getFreeCellList(const int grid[][9], int freeCellList[][2])
{
// 81 is the maximum number of free cells
int numberOfFreeCells = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[i][j] == 0)
{
freeCellList[numberOfFreeCells][0] = i;
freeCellList[numberOfFreeCells][1] = j;
numberOfFreeCells++;
}
return numberOfFreeCells;
}
// Display the values in the grid
void printGrid(const int grid[][9])
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
cout << grid[i][j] << " ";
cout << endl;
}
}
// Search for a solution
bool search(int grid[][9])
{
int freeCellList[81][2]; // Declare freeCellList
int numberOfFreeCells = getFreeCellList(grid, freeCellList);
if (numberOfFreeCells == 0)
return true; // No free cells
int k = 0; // Start from the first free cell
while (true)
{
int i = freeCellList[k][0];
int j = freeCellList[k][1];
if (grid[i][j] == 0)
grid[i][j] = 1; // Fill the free cell with number 1
if (isValid(i, j, grid))
{
if (k + 1 == numberOfFreeCells)
{ // No more free cells
return true; // A solution is found
}
else
{ // Move to the next free cell
k++;
}
}
else if (grid[i][j] < 9)
{
// Fill the free cell with the next possible value
grid[i][j] = grid[i][j] + 1;
}
else
{ // grid[i][j] is 9, backtrack
while (grid[i][j] == 9)
{
if (k == 0)
{
return false; // No possible value
}
grid[i][j] = 0; // Reset to free cell
k--; // Backtrack to the preceding free cell
i = freeCellList[k][0];
j = freeCellList[k][1];
}
// Fill the free cell with the next possible value,
// search continues from this free cell at k
grid[i][j] = grid[i][j] + 1;
}
}
return true; // A solution is found
}
// Check whether grid[i][j] is valid in the grid
bool isValid(int i, int j, const int grid[][9])
{
// Check whether grid[i][j] is valid at the i's row
for (int column = 0; column < 9; column++)
if (column != j && grid[i][column] == grid[i][j])
return false;
// Check whether grid[i][j] is valid at the j's column
for (int row = 0; row < 9; row++)
if (row != i && grid[row][j] == grid[i][j])
return false;
// Check whether grid[i][j] is valid in the 3-by-3 box
for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
if (row != i && col != j && grid[row][col] == grid[i][j])
return false;
return true; // The current value at grid[i][j] is valid
}
// Check whether the fixed cells are valid in the grid
bool isValid(const int grid[][9])
{
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[i][j] < 0 || grid[i][j] > 9 ||
(grid[i][j] != 0 && !isValid(i, j, grid)))
return false;
return true; // The fixed cells are valid
}
}
Sample Output :
Enter a puzzle:
0 6 0 1 0 4 0 5 0
0 0 8 3 0 5 6 0 0
2 0 0 0 0 0 0 0 1
8 0 0 4 0 7 0 0 6
0 0 6 0 0 0 3 0 0
7 0 0 9 0 1 0 0 4
5 0 0 0 0 0 0 0 2
0 0 7 2 0 6 9 0 0
0 4 0 5 0 8 0 7 0
The solution is found:
9 6 3 1 7 4 2 5 8
1 7 8 3 2 5 6 4 9
2 5 4 6 8 9 7 3 1
8 2 1 4 3 7 5 9 6
4 9 6 8 5 2 3 1 7
7 3 5 9 6 1 8 2 4
5 8 9 7 1 3 4 6 2
3 1 7 2 4 6 9 8 5
6 4 2 5 9 8 1 7 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.