Skip to content

Instantly share code, notes, and snippets.

@tommyip
Created April 1, 2019 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommyip/cdb2771874f8a5ceea14f46f5a56fc2a to your computer and use it in GitHub Desktop.
Save tommyip/cdb2771874f8a5ceea14f46f5a56fc2a to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iostream>
#include <cstdlib> // for srand() and rand()
#include <ctime> // for time()
using namespace std;
void fillCubeRandomly(int cube[][3][3]) {
srand(time(nullptr));
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 3; ++y) {
for (int z = 0; z < 3; ++z) {
cube[x][y][z] = rand() % 50;
}
}
}
}
void printCube(int cube[][3][3]) {
cout << "Print the cube:" << endl;
for (int x = 0; x < 3; ++x) {
cout << "Slice " << x << endl;
for (int y = 0; y < 3; ++y) {
for (int z = 0; z < 3; ++z) {
cout << cube[x][y][z] << '\t';
}
cout << endl;
}
}
}
int isMagicCube(int cube[][3][3]) {
int magic_number = 0;
// check all rows
for (int y = 0; y < 3; ++y) {
for (int z = 0; z < 3; ++z) {
int sum = cube[0][y][z] + cube[1][y][z] + cube[2][y][z];
if (magic_number == 0) {
magic_number = sum;
} else if (sum != magic_number) {
return 0;
}
}
}
// check all columns
for (int x = 0; x < 3; ++x) {
for (int z = 0; z < 3; ++z) {
int sum = cube[x][0][z] + cube[x][1][z] + cube[x][2][z];
if (sum != magic_number) {
return 0;
}
}
}
// check all pillars
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 3; ++y) {
int sum = cube[x][y][0] + cube[x][y][1] + cube[x][y][2];
if (sum != magic_number) {
return 0;
}
}
}
// check all diagonal
for (int d = 0; d < 3; ++d) {
int sum1 = cube[0][d][d] + cube[1][d][d] + cube[2][d][d];
int sum2 = cube[0][d][2-d] + cube[1][d][2-d] + cube[2][d][2-d];
if (sum1 != magic_number || sum2 != magic_number) {
return 0;
}
}
// check all trigonal 1
for (int t = 0; t < 3; ++t) {
int sum1 = cube[t][0][t] + cube[t][1][t] + cube[t][2][t];
int sum2 = cube[t][0][2-t] + cube[t][1][2-t] + cube[t][2][2-t];
if (sum1 != magic_number || sum2 != magic_number) {
return 0;
}
}
// check all trigonal 2
for (int t = 0; t < 3; ++t) {
int sum1 = cube[t][t][0] + cube[t][t][1] + cube[t][t][2];
int sum2 = cube[t][2-t][0] + cube[t][2-t][1] + cube[t][2-t][2];
if (sum1 != magic_number || sum2 != magic_number) {
return 0;
}
}
return magic_number;
}
int main()
{
int magicCubeSample[3][3][3] = {
20, 16, 6,
4, 21, 17,
18, 5, 19,
13, 3, 26,
27, 14, 1,
2, 25, 15,
9, 23, 10,
11, 7, 24,
22, 12, 8};
int cube[3][3][3] = {};
int magicNumber = 0;
// Generate a cube randomly and test whether it is magic cube
cout << "Generate a cube randomly." << endl;
fillCubeRandomly(cube);
printCube(cube);
magicNumber = isMagicCube(cube);
if (magicNumber){
cout << "It is a magic cube" << endl;
cout << "The magic number is " << magicNumber << endl;
}
else {
cout << "It is not a magic cube" << endl;
}
cout << endl << endl;
// Since most likely the random generated cube is not magic cube
// Test isMagicCube function with a magic cube sample
cout << "Test isMagicCube function with a magic cube sample." << endl;
printCube(cube);
magicNumber = isMagicCube(magicCubeSample);
if (magicNumber){
cout << "It is a magic cube" << endl;
cout << "The magic number is " << magicNumber << endl;
}
else {
cout << "It is not a magic cube" << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment