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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
const int length{ 9 };
void showthesudoku(int array[][length]);// just prints the sudoku
int randomnumber(); //generates a random number
int randomrow(); //the same -1 because arrays, to get the coordinates
int randomcol(); //the same just to add a delay
void thanksforplaying(); //prints thank you
void thegame();//another not important menu
void humaninput(int array[][length]);//input with cin
void machineinput(int array[][length]);//input with random numbers. The idea for this brain fart is, that it could populate my array so i could play the game.Also a help tool if you're stuck.
bool doesitfit(int array[][length]);//forces the rules
void calculations(int array[][length]);//Just adds rows,col and 3x3.I know it is redudant but i imagine when i turn
void reset(); //this, into a windowed mode you can click a box and it will tell you the sum in rows col and 3x3
int kingsmove(int array[][length]);
void counter();
struct sudoku
{
int precious[length][length]{ 0 };
int row{ 0 };
int col{ 0 };
int number{ 0 };
int dummy[length][length]{ 0 }; //i used this because somehow it printed the numbers that didnt fullfill
char move; //the conditions on my original array, very annoying
int additionrow{ 0 };
int additioncol{ 0 };
int addition3x3box{ 0 };
int countfilledsquares{ 0 };
}sud;
int main()
{
thegame();
thanksforplaying();
return 0;
}
void thegame()
{
reset();
while (1)
{
cout << "press c to choose numbers or m for the machine to do it for you or q to quit\n";
cin >> sud.move;
if (sud.move == 'c')
{
humaninput(sud.precious);
if (sud.number != 0)
{
sud.precious[sud.row][sud.col] = sud.number;
}
counter();
showthesudoku(sud.precious);
}
else if (sud.move == 'm')
{
machineinput(sud.precious);
if (sud.number != 0)
{
sud.precious[sud.row][sud.col] = sud.number;
}
counter();
showthesudoku(sud.precious);
}
else if (sud.move == 'q')
{
break;
}
else continue;
}
return;
}
bool doesitfit(int array[][length])
{ //spent about a lot of hours here, for this thing
//checks every row for the same number
for (int j = 0, i = sud.row; j < length; j++)
{ //|not needed| | this one | excludes the number we choose
if (((sud.number == sud.precious[i][j]) && ((sud.row == i && sud.col != j) && sud.precious[i][j] != 0)))
{
cout << "not possible, you already have one in the same line\n";
reset();
return true;// not needed
}
}
//checks every column for the same number
for (int i = 0, j = sud.col; i < length; i++)
{
if (((sud.number == sud.precious[i][j]) && ((sud.row != i && sud.col == j) && sud.precious[i][j] != 0)))
{
cout << "not possible, you have one in the same column\n";
reset();
return true;
}
}
//this one i hate. try to exclude the same number in a 3x3 box spend 6 or more hours here. stop laughing
int resizedrow = (sud.row / 3) * 3;
int resizedcolumn = (sud.col / 3) * 3;
for (int i = resizedrow; i < (resizedrow + 3); i++)
{
for (int j = resizedcolumn; j < (resizedcolumn + 3); j++)
{
//Always checks for 4 boxes. the diagonal positions if you choose middle box.A 2x2 if you choose corner box and so on
if (((sud.number == sud.precious[i][j]) && ((sud.row != i && sud.col != j) && sud.precious[i][j] != 0)))
{
cout << "you cant, i wish you could\n";
reset();
return true;
}
}
}
calculations(sud.precious);
if (sud.additionrow > 45)
{
cout << "you exceed the max sum allowed in this row\n";
reset();
return true;
}
else if (sud.additioncol > 45)
{
cout << "you exceed the max sum allowed in this column\n";
reset();
return true;
}
else if (sud.addition3x3box > 45)
{
cout << "you exceed the max sum allowed in this box\n";
reset();
return true;
}
//absolutely worth it
return false;
}
void calculations(int array[][length])
{
sud.additionrow = 0;//calculates the sum of all the numbers in the row the user chose, from his row input
for (int i = sud.row, j = 0; j < length; j++)
{
sud.additionrow += sud.precious[i][j];
}
sud.additionrow += sud.dummy[sud.row][sud.col];
sud.additioncol = 0;//calculates the sum of all the numbers in the column the user chose, from his column selection
for (int i = 0, j = sud.col; i < length; i++)
{
sud.additioncol += sud.precious[i][j];
}
sud.additioncol += sud.dummy[sud.row][sud.col];
sud.addition3x3box = 0;//calculates the sum of all the numbers in a 3x3square with magic. learned that here, ty.
int resizedrow = (sud.row / 3) * 3; //3 possible outcomes each to get the 3x3box.(0,3,6)^2=1 to 9 possible 3x3box
int resizedcolumn = (sud.col / 3) * 3;// i know what it does.I don't know what are the principles behind it.
//anyway it is still amazing!!
for (int i = resizedrow; i < (resizedrow + 3); i++)
{
for (int j = resizedcolumn; j < (resizedcolumn + 3); j++)
{
sud.addition3x3box += sud.precious[i][j];
}
}
sud.addition3x3box += sud.dummy[sud.row][sud.col];
return;
}
void humaninput(int array[][length])
{
while (true)
{
cout << "enter row, column and your number\n";
cin >> sud.row >> sud.col >> sud.number;//if i put a letter all hell breaks loose
if (sud.row < 0 || sud.row >= length)
{
cout << "out of bounds row\n";
}
else if (sud.col < 0 || sud.col >= length)
{
cout << "out of bounds column\n";
}
else if (sud.number < 0 || sud.number > length)
{
cout << "out of bounds number\n";
}
else if (sud.precious[sud.row][sud.col] != 0)
{
cout << "already assigned\n";
}
else if (doesitfit(sud.precious) != 0);
else if (kingsmove(sud.precious) != 0);
else break;
}
sud.dummy[sud.row][sud.col] = sud.number;
return;// i was trying to return sud.dummy[][]. Wrongly thought it's out of scope and gone when i exit but struct are good
}
void machineinput(int array[][length])
{
while (1)
{
reset();
sud.row = randomrow();
sud.col = randomcol();
sud.number = randomnumber();
if (sud.precious[sud.row][sud.col] != 0);
else if (doesitfit(sud.precious) != 0);
else if ((kingsmove(sud.precious)) != 0);
else break;
}
sud.dummy[sud.row][sud.col] = sud.number;
return;
}
void counter()
{
sud.countfilledsquares = 0;
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
if (sud.precious[i][j] != 0)
{
sud.countfilledsquares++;
}
}
}
cout << sud.countfilledsquares << "\n";
}
int kingsmove(int array[][length])
{
int k = 0;
int increasedrow = 0;
int increasedcol = 0;
int decreasedrow = 0;
int decreasedcol = 0;
increasedrow = sud.row + 1;
increasedcol = sud.col + 1;
decreasedrow = sud.row - 1;
decreasedcol = sud.col - 1;
for (int i = decreasedrow; i < increasedrow; i++)
{
for (int j = decreasedcol; j < increasedcol; j++)
{
if (i < 0 || j < 0)
{
cout << "less than0\n";
}
else if (i > 8 || j > 8)
{
cout << "morethan8\n";
}
else if (sud.precious[i][j] == sud.number)
{
sud.precious[i][j];
k = 1;
sud.number = 0;
sud.row = 0;
sud.col = 0;
return k;
}
}
}
return k;
}
int randomnumber()//the numbers follow a pattern unfortunately, i thought the delay will fix it
{
unsigned int k = 0;
k = (rand() % 9) + 1;
return k;
}
int randomrow()
{
unsigned int l = 0;
l = rand() % 9;
return l;
}
int randomcol()
{
unsigned int m = 0;
m = rand() % 9;
return m;
}
void showthesudoku(int array[][length])
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
cout << setw(3) << array[i][j];
}cout << "\n";
}
}
void reset()
{
sud.row = 0;
sud.col = 0;
sud.number = 0;
sud.dummy[sud.row][sud.col] = 0;
return;
}
void thanksforplaying()
{
cout << "thanks for playing";
}
|