Hello, I will leave the instructions for this program. I did it in three different files and just called #include "LoShuSquare.h" as a library, but for some reason is not working. I don't know if is my compiler that is not working could someone check the work.
Assignment:
Design a program to play the LoShuSquare (Links to an external site.) Game.
Define a class LoShuSquare with the necessary data and functions to play the game. Use a 2-D int array with three rows and three columns as the game grid. This will be a member variable in your class. Even though it is a 3x3 array, you should define a constant for the size of the square and use it when defining the array and when referring to the size in related functions.
Turn in:
LoShuSquare.h (or .hpp) and LoShuSquare.cpp with the implementation of your class, main.CPP. A screenshot of the rounds of the game ending in a success and one ending a failure.
LoShuSquare::LoShuSquare() {
grid = new int*[SIZE];
for (int i = 0; i < SIZE; i++) {
grid[i] = new int[SIZE];
}
}
//////////////////////////////////////////////////
//////// TO FILL GRID ////////////////////////////
//////////////////////////////////////////////////
bool LoShuSquare::isRepeated(int num) {
bool repeated = false;
for (int i = 0; i < currentNumbers.size(); i++) {
if (currentNumbers[i] == num) {
repeated = true;
break;
}
}
return repeated;
}
bool LoShuSquare::fill(int r, int c, int num) {
if (isRepeated(num)) {
cout << "\n!! " << num << " is repeated.";
return false;
}
if (r < 0 || r >= SIZE) {
cout << "\n!! " << r << " is not a valid row.";
return false;
}
if (c < 0 || c >= SIZE) {
cout << "\n!! " << c << " is not a valid column.";
return false;
}
if (num < 1 || num > 9) {
cout << "\n!! " << num << " is not a valid number.";
return false;
}
int removedNumber = grid[r][c];
grid[r][c] = num;
currentNumbers.push_back(num);
if (removedNumber != 0) {
for (int i = 0; i < currentNumbers.size(); i++) {
if (currentNumbers[i] == removedNumber) {
currentNumbers.erase(currentNumbers.begin() + i);
break;
}
}
}
return true;
}
//////////////////////////////////////////////////
//////// GAME INTERFACE //////////////////////////
//////////////////////////////////////////////////
void LoShuSquare::print() {
cout << "\n-------------\n";
for (int i = 0; i < SIZE; i++) {
cout << "| ";
for (int j = 0; j < SIZE; j++) {
cout << grid[i][j] << " | ";
}
cout << "\n-------------\n";
}
intro();
cout << "\n\nWelcome to a game of LoShuSquare!\n";
cout << "\nTo get started, fill in the grid to try to make a valid square. \n";
cout << "A valid square is filled with the numbers from 1-9 (no repetitions), where \n";
cout << "the sum of its rows, columns, are diagonals equal to 15. \n";
while (currentNumbers.size() != (SIZE * SIZE)) {
cout << "\nEnter a number and its position in the format (row, column, number): " << endl;
cout << "> ";
getProcessedInput(arguments);
. Note: To be shows as I don't create the #include "LoShuSquare.h" library. Could someone run the program and told me why is not working in my compiler. (I am using code blocks.)
"LoShuSquare.h" is not a library. It is a header file shared by two separately compilable .cpp files.
I had no problem compiling and running your program using Visual Studio.
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.