#include<iostream>
#include <Windows.h>
#include <conio.h>
#include <ctime>
#define Black 0
#define Blue1 1
#define Green1 2
#define Cyan1 3
#define Red1 4
#define Magenta1 5
#define Yellow1 6
#define Gray1 7
#define Gray2 8
#define Blue2 11
#define Green2 10
#define Cyan2 11
#define Red2 12
#define Magenta2 13
#define Yellow2 14
#define White 15
using namespace std;
int cnt, n, mode;
int sum[102];
bool visited[2500];
int arr[50][50];
int space[4][2] = {{4, 4},{3, 4},{3, 3},{2, 3}};
int weight[50][50];
int d[9] = {1, 2, -3, -4, 0, 4, 3, -2, -1};
void textColor(int fg_color, int bg_color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
fg_color | bg_color << 4);
}
void removeCursor(void) {
CONSOLE_CURSOR_INFO cursorinfo;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorinfo);
cursorinfo.bVisible = 0;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorinfo);
}
int size(int k) {
int len = 0;
while(k) {
k/=10;
len++;
}
return len;
}
void printTable() {
int idx=0;
for(int i=0; i<6*n-1; i++) {
if(!((i+1)%6)) {
for(int j=0; j<10*n-1; j++) cout << "-";
}
else {
if(!((i+4)%6)) {
for(int k=0; k<n; k++) {
if(arr[idx/n][idx%n]+1) {
for(int l=0; l<space[size(arr[idx/n][idx%n])-1][0]; l++) cout << ' ';
textColor(10, 0);
cout << arr[idx/n][idx%n];
for(int l=0; l<space[size(arr[idx/n][idx%n])-1][1]; l++) cout << ' ';
}
else {
for(int l=0; l<space[size(idx+1)-1][0]-1; l++) cout << ' ';
textColor(11, 0);
cout << '(' << idx+1 << ')';
for(int l=0; l<space[size(idx+1)-1][1]-1; l++) cout << ' ';
}
textColor(15, 0);
if(k != n-1) cout << '|';
idx++;
}
}
else {
for(int i=0; i<n-1; i++) cout << " |";
cout << " ";
}
}
cout << '\n';
}
}
void isRight() {
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) sum[j] += arr[i][j]+((mode-1)?weight[i][j]:0);
for(int j=0; j<n; j++) sum[j+n] += arr[j][i]+((mode-1)?weight[i][j]:0);
sum[2*n] += arr[i][i]+((mode-1)?weight[i][i]:0);
sum[2*n+1] += arr[i][n-1-i]+((mode-1)?weight[i][i]:0);
}
for(int i=1; i<2*n+1; i++) {
if(sum[i] != sum[i-1]) {
textColor(12, 0);
cout << "Wrong Answer";
return;
}
}
textColor(10, 0);
cout << "Great!";
}
int main(void) {
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) weight[i][j] = d[i*3+j];
}
textColor(11, 0);
while(1) {
cout << "Input Mode( 1)Normal, 2)Weight ) : ";
cin >> mode;
if(mode == 1 || mode == 2) break;
else {
system("cls");
textColor(12, 0);
cout << "Error : Invalid Input\n";
textColor(11, 0);
}
}
while(1) {
cout << "Input the height of Magic Square : ";
cin >> n;
if(1 <= n && n <= 49) break;
else {
system("cls");
textColor(12, 0);
cout << "Error : Invalid Input\n";
textColor(11, 0);
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) arr[i][j] = -1;
}
system("cls");
textColor(15, 0);
int a;
printTable();
while(1) {
cout << "Input a number(1~" << n*n << ") : ";
cin >> a;
system("cls");
if(1 <= a && a <= n*n) {
if(!visited[a-1]) arr[(a-1)/n][(a-1)%n] = ++cnt;
printTable();
textColor(12, 0);
if(visited[a-1]) cout << "Error : Already selected\n";
textColor(15, 0);
visited[a-1] = true;
}
else {
printTable();
textColor(12, 0);
cout << "Error : Invalid Input\n";
textColor(15, 0);
}
if(cnt >= n*n) break;
}
isRight();
}
To embed this project on your website, copy the following code and paste it into your website's HTML: