Advertisement
Guest User

Untitled

a guest
May 4th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 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. 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
  15.  
  16. enum coordinate
  17. {
  18. A, B, C, D, E, F, G, H, I
  19. };
  20.  
  21. bool checkRow(int cellRow, int cellColumn);
  22. bool checkColumn (int cellRow, int cellColumn);
  23. bool checkBlock (int cellRow, int cellColumn);
  24. void updateConflicts (int cellRow, int cellColumn);
  25. bool checkVictory ();
  26. void enterNumber(coordinate &column, int &row, int &entry);
  27. void displaySudoku();
  28. void displayMenu();
  29. void playSudoku();
  30. void processEntry(coordinate column, int row, int entry);
  31.  
  32.  
  33. bool checkRow(int cellRow, int cellColumn)
  34. {
  35.  
  36. }
  37.  
  38. bool checkColumn (int cellRow, int cellColumn)
  39. {
  40.  
  41. }
  42.  
  43. bool checkBlock (int cellRow, int cellColumn)
  44. {
  45.  
  46. }
  47.  
  48. void updateConflicts (int cellRow, int cellColumn) //calls checkRow(), checkColumn() and checkBlock()whenever a change is made to the sudoku grid
  49. {
  50.  
  51. }
  52.  
  53. bool checkVictory ()
  54. {
  55. return false;
  56. }
  57.  
  58. void enterNumber(coordinate &column, int &row, int &entry)
  59. {
  60. char column_temp;
  61. cout << "> ";
  62. cin >> column_temp >> row >> entry;
  63. column_temp -= 65;
  64. column = static_cast<coordinate>(column_temp);
  65. row--;
  66. }
  67.  
  68. void displaySudoku()
  69. {
  70. system("cls");
  71. cout << " A B C D E F G H I\n";
  72. cout << "---------------------------------------\n";
  73. cout << "---------------------------------------\n";
  74. cout << "| |\t |\t | | |\n| | ";
  75. for (int i = 0; i < sudokuSize; i++)
  76. {
  77. for (int j = 0; j < sudokuSize; j++)
  78. {
  79. if (table[i][j] == emptyCellValue) cout << "x";
  80. else
  81. {
  82. cout << table[i][j];
  83. }
  84. if (j==blockSize-1) cout << " |";
  85. if (j==(2*blockSize)-1) cout << " |";
  86. if ((j==(sudokuSize-1))) cout << " | | " << i+1;
  87. else cout << " ";
  88. }
  89. if (i==blockSize-1)
  90. {
  91. cout << "\n| |\t |\t | | | ";
  92. cout << "\n---------------------------------------";
  93. }
  94. if (i==(2*blockSize)-1)
  95. {
  96. cout << "\n| |\t |\t | | | ";
  97. cout << "\n---------------------------------------";
  98. }
  99. if (i==(sudokuSize-1)) cout << endl;
  100. else cout << "\n| |\t |\t | | |\n| | "; //The lines for the sudoku table are very precisely generated. The logic and endlines took my hours
  101. }
  102. cout << "| |\t |\t | | |\n";
  103. cout << "---------------------------------------\n";
  104. cout << "---------------------------------------\n";
  105. }
  106.  
  107. void displayMenu()
  108. {
  109. cout << "Welcome to Extreme Sudoku by Hanna Sababa\n\n";
  110. cout << "1. Start Game\n";
  111. cout << "2. Quit\n\n> ";
  112. }
  113.  
  114. void playSudoku()
  115. {
  116. coordinate column;
  117. int row, entry;
  118. while (!checkVictory())
  119. {
  120. displaySudoku();
  121. cout << endl;
  122. enterNumber(column, row, entry);
  123. processEntry(column, row, entry);
  124. }
  125. }
  126.  
  127. void processEntry(coordinate column, int row, int entry)
  128. {
  129. if ((table[row][column] != emptyCellValue) && isCorrect[row][column])
  130. {
  131. cout << "The cell you tried to manipulate already has a correct number. Try again\n\n";
  132. enterNumber(column, row, entry);
  133. }
  134. else table[row][column] = entry;
  135. playSudoku();
  136. }
  137.  
  138.  
  139. int main()
  140. {
  141. char selection;
  142. displayMenu();
  143. cin >> selection;
  144. system("cls");
  145. cout << "-INSTRUCTIONS-\n\n\n";
  146. cout << "Use the following format to enter a number to the sudoku grid:\n\n";
  147. cout << "[Column Co-ordinate] [Row Co-Ordinate] [Guess]\n\n";
  148. cout << "For instance, \"A 2 5\" would insert the number 5 into the cell A2\n\n\n";
  149. cout << "In Extreme Sudoku, once an entry has been made, you are not allowed to change it\n";
  150. cout << "The program will notify you if the entry you attempted is illegal\n\n";
  151. cout << "A number which caused no conflicts at time of entry but is incorrect is final\n\n";
  152. cout << "NB: Column Co-ordinates should be entered in capital letters\n\n";
  153. system("pause");
  154. system ("cls");
  155. if (selection == '1')
  156. {
  157. playSudoku();
  158. }
  159. else if (selection == '2')
  160. {
  161. cout << "Bye\n";
  162. }
  163. else
  164. {
  165. cout << "Seriously re? Was " << selection << " an option in the menu?\n\n";
  166. cout << "If you can't make a menu selection how the heck will you play sudoku?\n\n";
  167. cout << "BYE\n\n";
  168. }
  169. //system("pause");
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement