1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int maxPuzzle = 32;
int filled = 0;
const unsigned PUZZLE_WIDTH = 9;
const unsigned PUZZLE_HEIGHT = 9;
int puzzle[PUZZLE_HEIGHT][PUZZLE_WIDTH] = {};
const unsigned REGIONS_PER = 3; // regions per column/row
bool checkBox(int row, int column, int num)
{
const int rows_per_region = PUZZLE_HEIGHT / REGIONS_PER;
const int cols_per_region = PUZZLE_WIDTH / REGIONS_PER;
const int start_row = (row / REGIONS_PER) * rows_per_region;
const int end_row = start_row + rows_per_region;
const int start_col = (column / REGIONS_PER) * cols_per_region;
const int end_col = start_col + cols_per_region;
for (int r = start_row; r < end_row; ++r)
for (int c = start_col; c < end_col; ++c)
if (num == puzzle[r][c])
return false;
return true;
}
bool check(int row, int column, int num)
{
for(int i = 0; i < 9 ; i++)
{
if(num == puzzle[i][column]) return false;
}
for(int j=0; j<9; j++)
{
if(num==puzzle[row][j]) return false;
}
return true;
}
void Show()
{
cout << "-------------------------" << endl;
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
if(j == 0) cout << "|";
cout << " " << puzzle[i][j];
if(j == 2 || j == 5 || j == 8)
{
cout << " |";
}
}
if(i == 2 || i == 5)
{
cout << endl;
cout << "-------------------------";
}
cout << endl;
}
cout << "-------------------------";
cout << endl;
}
void MakePuzzle()
{
int s = 0, x = 0, y = 0, num = 0;
while(s < maxPuzzle)
{
cout << "Type in the position you want to put the number on: ";
cin >> x >> y;
cout << "Type in the number you want to put in: ";
cin >> num;
if(num < 10)
{
if(check(x,y,num) == true && checkBox(x,y,num) == true)
{
if(puzzle[x][y] == 0)
{
s++;
filled ++;
puzzle[x][y] = num;
Show();
cout << endl;
}
else
{
cout << "You already have a number here." << endl;
}
} else cout << "There is that same number in this row/column/box! " << endl;
} else cout << "You must type in numbers from 0-10" << endl;
}
}
void AutoMakePuzzle()
{
int s = 0;
while(s < maxPuzzle)
{
int x = rand() % 9 + 0;
int y = rand() % 9 + 0;
int num = rand() % 9 + 1;
if(check(x,y,num) == true && checkBox(x,y,num) == true)
{
if(puzzle[x][y] == 0)
{
puzzle[x][y] = num;
filled++;
s++;
}
}
}
}
void Clear()
{
int x = 0, y = 0;
cout << "Don't cheat! :) Use it as undo option only :)" << endl;
cout << "What position do you want to clear: ";
cin >> x >> y;
puzzle[x][y] = 0;
filled--;
Show();
}
void PutNum()
{
int x = 0, y = 0, num = 0, s = 0;
char a,b;
while(b != 'y')
{
cout << "Type in the position you want to put the number on: ";
cin >> x >> y;
cout << "Type in the number you want to put in: ";
cin >> num;
if(num < 10)
{
if(check(x,y,num) == true && checkBox(x,y,num) == true)
{
if(puzzle[x][y] == 0)
{
s++;
filled++;
puzzle[x][y] = num;
Show();
cout << endl;
}
else
{
cout << "You already have a number here." << endl;
}
}
else cout << "There is that same number in this row/column/box! " << endl;
}
else cout << "You must type in numbers from 0-10" << endl;
cout << "Do you want to clear some field ?(y/n)" << endl;
cin >> a;
if(a == 'y') Clear();
else
{
cout << "Do you want to stop trying?" << endl;
cin >> b;
}
if(filled == 81)
{
cout << "Good job!!" << endl;
}
if(b == 'y')
{
cout << endl;
cout << "Too bad :( Good luck next time!" << endl;
}
}
}
int main()
{
srand(time(NULL));
string a;
cout << "Do you want puzzle to be made manually or auto ?" << endl;
cin >> a;
if(a == "auto") AutoMakePuzzle();
else if(a == "manually") MakePuzzle();
Show();
cout << "The game now begins!" << endl;
PutNum();
return 0;
}
|