Advertisement
Guest User

Untitled

a guest
May 5th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. // Sudoku by @Hanna Sababa
  2.  
  3. // I decided to write my own sudoku from scratch for an extra challenge
  4.  
  5. #include <iostream>
  6. #include <cstdlib>
  7. using namespace std;
  8.  
  9. const int sudokuSize = 9;
  10. const int blockSize = 3;
  11. const int emptyCellValue = 0;
  12.  
  13. int table[sudokuSize][sudokuSize] = {{5,3,0,0,7,0,0,0,0},{6,0,0,1,9,5,0,0,0},{0,9,8,0,0,0,0,6,0},{8,0,0,0,6,0,0,0,3},{4,0,0,8,0,3,0,0,1},{7,0,0,0,2,0,0,0,6},{0,6,0,0,0,0,2,8,0},{0,0,0,4,1,9,0,0,5},{0,0,0,0,8,0,0,7,9}} ;
  14. int solution[sudokuSize] [sudokuSize] = {{5,3,4,8,7,8,9,1,2},{6,7,2,1,9,5,3,4,8},{1,9,8,3,4,2,5,6,7},{8,5,9,7,6,1,4,2,3},{4,2,6,8,5,3,7,9,1},{7,1,3,9,2,4,8,5,6},{9,6,1,5,3,7,2,8,4},{2,8,7,4,1,9,6,3,5},{3,4,5,2,8,6,1,7,9}} ;
  15. //bool isCorrect[sudokuSize][sudokuSize] = {{1,1,0,0,1,0,0,0,0},{1,0,0,1,1,1,0,0,0},{0,1,1,0,0,0,0,1,0},{1,0,0,0,1,0,0,0,1},{1,0,0,1,0,1,0,0,1},{1,0,0,0,1,0,0,0,1},{0,1,0,0,0,0,1,1,0},{0,0,0,1,1,1,0,0,1},{0,0,0,0,1,0,0,1,1}}; // It is justifiable to have these as global variables as there are no alternative arrays the functions can work on
  16.  
  17. enum coordinate
  18. {
  19. A, B, C, D, E, F, G, H, I
  20. };
  21.  
  22. bool validRow(coordinate column, int row, int entry);
  23. bool validColumn (coordinate column, int row, int entry);
  24. bool validBlock (coordinate column, int row, int entry);
  25. template <class firstCell>
  26. firstCell findFirstCell (firstCell num );
  27. bool gameOver ();
  28. void enterNumber(coordinate &column, int &row, int &entry);
  29. void displaySudoku();
  30. void displayMenu();
  31. void playSudoku();
  32. void processEntry(coordinate column, int row, int entry);
  33. void displayInstructions();
  34. bool arraysIdentical2D(int array1[sudokuSize][sudokuSize], int array2[sudokuSize][sudokuSize]);
  35.  
  36. bool validRow(coordinate column, int row, int entry)
  37. {
  38. for(int i = 0; i <sudokuSize; i++)
  39. {
  40. if (i == column) break;
  41. if (table[row][i] == entry) return false;
  42. }
  43. return true;
  44. }
  45.  
  46. bool validColumn (coordinate column, int row, int entry)
  47. {
  48. for(int j = 0; j <sudokuSize; j++)
  49. {
  50. if (j == row) break;
  51. if (table[j][column] == entry) return false;
  52. }
  53. return true;
  54. }
  55.  
  56. bool validBlock (coordinate column, int row, int entry)
  57. {
  58. int rowIndex, columnIndex;
  59. rowIndex = findFirstCell (row);
  60. columnIndex = findFirstCell (column);
  61. for (int x = 0; x < blockSize; x++ )
  62. {
  63. for ( int y = 0; y < blockSize; y++)
  64. {
  65. if ((rowIndex == row) && (columnIndex == column))
  66. break;
  67. if (table[rowIndex][columnIndex] == entry);
  68. return false;
  69. columnIndex++;
  70. }
  71. rowIndex++;
  72. }
  73. return true;
  74. }
  75.  
  76. template <class firstCell>
  77. firstCell findFirstCell (firstCell num)
  78. {
  79. switch(num)
  80. {
  81. case 0:
  82. case 1:
  83. case 2:
  84. return static_cast<firstCell>(0);
  85. case 3:
  86. case 4:
  87. case 5:
  88. return static_cast<firstCell>(3);
  89. case 6:
  90. case 7:
  91. default: // case 8
  92. return static_cast<firstCell>(6);
  93. }
  94. }
  95.  
  96. bool gameOver ()
  97. {
  98. if (arraysIdentical2D(table, solution))
  99. return true;
  100. return false;
  101. }
  102.  
  103. void enterNumber(coordinate &column, int &row, int &entry)
  104. {
  105. char column_temp;
  106. cout << "> ";
  107. cin >> column_temp >> row >> entry;
  108. column_temp -= 65;
  109. column = static_cast<coordinate>(column_temp);
  110. row--;
  111. }
  112.  
  113. void displaySudoku()
  114. {
  115. system("cls");
  116. cout << " A B C D E F G H I\n";
  117. cout << "---------------------------------------\n";
  118. cout << "---------------------------------------\n";
  119. cout << "| |\t |\t | | |\n| | ";
  120. for (int i = 0; i < sudokuSize; i++)
  121. {
  122. for (int j = 0; j < sudokuSize; j++)
  123. {
  124. if (table[i][j] == emptyCellValue) cout << "x";
  125. else
  126. {
  127. cout << table[i][j];
  128. }
  129. if (j==blockSize-1) cout << " |";
  130. if (j==(2*blockSize)-1) cout << " |";
  131. if ((j==(sudokuSize-1))) cout << " | | " << i+1;
  132. else cout << " ";
  133. }
  134. if (i==blockSize-1)
  135. {
  136. cout << "\n| |\t |\t | | | ";
  137. cout << "\n---------------------------------------";
  138. }
  139. if (i==(2*blockSize)-1)
  140. {
  141. cout << "\n| |\t |\t | | | ";
  142. cout << "\n---------------------------------------";
  143. }
  144. if (i==(sudokuSize-1)) cout << endl;
  145. else cout << "\n| |\t |\t | | |\n| | "; //The lines for the sudoku table are very precisely generated. The logic and endlines took my hours
  146. }
  147. cout << "| |\t |\t | | |\n";
  148. cout << "---------------------------------------\n";
  149. cout << "---------------------------------------\n";
  150. }
  151.  
  152. void displayMenu()
  153. {
  154. cout << "Welcome to Extreme Sudoku by Hanna Sababa\n\n";
  155. cout << "1. Start Game\n";
  156. cout << "2. Quit\n\n> ";
  157. }
  158.  
  159. void playSudoku()
  160. {
  161. coordinate column;
  162. int row, entry;
  163. while (!gameOver())
  164. {
  165. displaySudoku();
  166. cout << endl;
  167. enterNumber(column, row, entry);
  168. processEntry(column, row, entry);
  169. }
  170. }
  171.  
  172. void processEntry(coordinate column, int row, int entry)
  173. {
  174. if ((table[row][column] != emptyCellValue))
  175. cout << "\nThe cell you tried to manipulate already has a number. Try again\n\n";
  176. else
  177. {
  178. if (!validColumn(column, row, entry))
  179. cout << "\nThere is a conflict in the column\n\nLook closely and you will find another " << entry << "\n\nIdiot\n\n";
  180. if (!validRow(column, row, entry))
  181. cout << "\nThere is a conflict in the row\n\nLook closely and you will find another " << entry << "\n\nIdiot\n\n";
  182. if (!validBlock(column, row, entry))
  183. cout << "\nThere is a conflict in the block\n\nLook closely and you will find another " << entry << "\n\nIdiot\n\n";
  184. if ((validColumn(column, row, entry) && validRow(column, row, entry) && validBlock(column, row, entry)))
  185. table[row][column] = entry;
  186. }
  187. cout << endl;
  188. system("pause");
  189. playSudoku();
  190. }
  191.  
  192. void displayInstructions()
  193. {
  194. cout << "\t\t\t-INSTRUCTIONS-\n\n\n";
  195. cout << "Use the following format to enter a number to the sudoku grid:\n\n";
  196. cout << "[Column Co-ordinate] [Row Co-Ordinate] [Guess]\n\n";
  197. cout << "For instance, \"A 2 5\" would insert the number 5 into the cell A2\n\n\n";
  198. cout << "In Extreme Sudoku, once an entry has been made, you are not allowed to change it\n";
  199. cout << "The program will notify you if the entry you attempted is illegal\n\n";
  200. cout << "A number which caused no conflicts at time of entry but is incorrect is final\n\n\n";
  201. cout << "NB: Column Co-ordinates should be entered in capital letters\n\n\n";
  202. system("pause");
  203. system ("cls");
  204. }
  205.  
  206. bool arraysIdentical2D(int array1[sudokuSize][sudokuSize], int array2[sudokuSize][sudokuSize])
  207. {
  208. for (int k = 0; k < sudokuSize; k++)
  209. {
  210. for (int m = 0; m < sudokuSize; m++)
  211. {
  212. if ((array1[k][m]) != array2[k][m])
  213. return false;
  214. }
  215. }
  216. return true;
  217. }
  218.  
  219. int main()
  220. {
  221. char selection;
  222. displayMenu();
  223. cin >> selection;
  224. system("cls");
  225. if (selection == '1')
  226. {
  227. displayInstructions();
  228. playSudoku();
  229. }
  230. else if (selection == '2')
  231. {
  232. cout << "Bye\n";
  233. }
  234. else
  235. {
  236. cout << "Seriously re!? Was " << selection << " an option in the menu?\n\n";
  237. cout << "If you can't make a menu selection how the heck will you play sudoku?\n\n";
  238. cout << "BYE\n\n";
  239. }
  240. //system("pause");
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement