Design a program to play the LoShuSquare Game.

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.

javascript:tx('
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "LoShuSquare.h"

using namespace std;

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";
}

return;
}

void LoShuSquare::intro() {
const char* text =
" ___ ________ ________ ___ ___ ___ ___ ________ ________ ___ ___ ________ ________ _______\n"
"|\\ \\ |\\ __ \\|\\ ____\\|\\ \\|\\ \\|\\ \\|\\ \\|\\ ____\\|\\ __ \\|\\ \\|\\ \\|\\ __ \\|\\ __ \\|\\ ___ \\ \n"
"\\ \\ \\ \\ \\ \\|\\ \\ \\ \\___|\\ \\ \\\\\\ \\ \\ \\\\\\ \\ \\ \\___|\\ \\ \\|\\ \\ \\ \\\\\\ \\ \\ \\|\\ \\ \\ \\|\\ \\ \\ __/|\n"
" \\ \\ \\ \\ \\ \\\\\\ \\ \\_____ \\ \\ __ \\ \\ \\\\\\ \\ \\_____ \\ \\ \\\\\\ \\ \\ \\\\\\ \\ \\ __ \\ \\ _ _\\ \\ \\_|/__ \n"
" \\ \\ \\____\\ \\ \\\\\\ \\|____|\\ \\ \\ \\ \\ \\ \\ \\\\\\ \\|____|\\ \\ \\ \\\\\\ \\ \\ \\\\\\ \\ \\ \\ \\ \\ \\ \\\\ \\\\ \\ \\_|\\ \\ \n"
" \\ \\_______\\ \\_______\\____\\_\\ \\ \\__\\ \\__\\ \\_______\\____\\_\\ \\ \\_____ \\ \\_______\\ \\__\\ \\__\\ \\__\\\\ _\\\\ \\_______\\ \n"
" \\|_______|\\|_______|\\_________\\|__|\\|__|\\|_______|\\_________\\|___| \\__\\|_______|\\|__|\\|__|\\|__|\\|__|\\|_______| \n"
" \\|_________| \\|_________| \\|__| \n";

string textStr(text);
cout << textStr;

return;
}

void LoShuSquare::getProcessedInput(int* arguments) {

string input = "";
string i = "";
string j = "";
string num = "";

getline(cin, input);
stringstream inputStream(input);


getline(inputStream, i, ' ');
getline(inputStream, j, ' ');
getline(inputStream, num, ' ');


stringstream iStream(i);
iStream >> arguments[0];

stringstream jStream(j);
jStream >> arguments[1];

stringstream numStream(num);
numStream >> arguments[2];

return;
}

void LoShuSquare::play() {

int* arguments = new int[3];

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);

bool wasFilled = fill(arguments[0], arguments[1], arguments[2]);

if (wasFilled) {
print();
} else {
cout << " Please try again !!\n";
}
}

if (isValid()) {
cout << "\nThe grid is valid!!! :)\n";
} else {
cout << "\nThe final grid is invalid :(\n";
}


return;
}


//////////////////////////////////////////////////
//////// CHECK VALIDITY OF GRID //////////////////
//////////////////////////////////////////////////

bool LoShuSquare::isValidRow(int row) {

int sum = 0;
for (int i = 0; i < SIZE; i++) {
sum += grid[row][i];
}

return (sum == GOAL) ? true : false;
}

bool LoShuSquare::isValidCol(int col) {

int sum = 0;
for (int i = 0; i < SIZE; i++) {
sum += grid[col][i];
}

return (sum == GOAL) ? true : false;
}

bool LoShuSquare::isValidDiagonalUp() {

int sum = 0;

for (int i = 0; i < SIZE; i++) {
sum += grid[i][i];
}

return (sum == GOAL) ? true : false;
}

bool LoShuSquare::areRowsValid() {

for (int i = 0; i < SIZE; i++) {
if(!isValidRow(i)) {
return false;
}
}

return true;
}

bool LoShuSquare::areColumnsValid() {

for (int i = 0; i < SIZE; i++) {
if(!isValidCol(i)) {
return false;
}
}

return true;
}

bool LoShuSquare::areDiagonalsValid() {

bool up = isValidDiagonalUp();
bool down = isValidDiagonalDown();

return up && down;
}

bool LoShuSquare::isValidDiagonalDown() {

int sum = 0;

for (int i = 0; i < SIZE; i++) {
sum += grid[SIZE - 1 - i][i];
}

return (sum == GOAL) ? true : false;
}


bool LoShuSquare::isValid() {

bool rows = areRowsValid();
bool columns = areColumnsValid();
bool diagonals = areDiagonalsValid();

return rows && columns && diagonals;
}
')

javascript:tx('#ifndef LOSHUSQUARE
#define LOSHUSQUARE

#include <vector>

class LoShuSquare {
private:
const int SIZE = 3;
const int GOAL = 15;

int** grid;
std::vector<int> currentNumbers;

bool isRepeated(int num);
void printCurrentNumbers();

bool isValidRow(int row);
bool isValidCol(int col);
bool isValidDiagonalUp();
bool isValidDiagonalDown();

bool areRowsValid();
bool areColumnsValid();
bool areDiagonalsValid();

void intro();
void getProcessedInput(int* arguments);

public:
LoShuSquare();
bool fill(int r, int c, int num);
void print();
bool isValid();
void play();
};

#endif ')


javascript:tx('#include <iostream>
#include "LoShuSquare.h"

int main (int argc, char* argv[]) {

LoShuSquare game;

game.play();

return 0;
}')



. 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.
Topic archived. No new replies allowed.