Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. /**********************************************************************
  6. * This function asks the user for a filename with a sudoku board on it.
  7. ***********************************************************************/
  8. void getFileName(char fileName[])
  9. {
  10. cout << "Where is your board located? ";
  11. cin >> fileName;
  12. }
  13.  
  14. /**********************************************************************
  15. * This function reads in the sudoku board.
  16. ***********************************************************************/
  17. int readFile(char fileName[], char game[][9])
  18. {
  19. int i = 0;
  20. ifstream input;
  21. input.open(fileName);
  22. if (input.fail())
  23. return -1;
  24.  
  25. for (int i = 0; i < 9; i++)
  26. for (int j = 0; j < 9; j++)
  27. input >> game[i][j];
  28.  
  29. input.close();
  30. return i;
  31. }
  32. void display(char game[][9])
  33. {
  34. cout << " A B C D E F G H I\n";
  35. for (int i = 0; i < 9; i++)
  36. {
  37. cout << i + 1 << " ";
  38. for (int j = 0; j < 9; j++)
  39. if (game[i][j] == '0')
  40. {
  41. cout << " ";
  42. if (j == 2 || j == 5)
  43. cout << "|";
  44. else if (j == 8)
  45. cout << "";
  46. else
  47. cout << " ";
  48. }
  49. else if (j == 2 || j == 5)
  50. cout << game[i][j] << "|";
  51. else if (j == 8)
  52. cout << game[i][j];
  53. else
  54. cout << game[i][j] << " ";
  55.  
  56. if (i == 2 || i == 5)
  57. cout << "\n -----+-----+-----" << endl;
  58. else
  59. cout << endl;
  60.  
  61. }
  62. }
  63. bool checkSquare(int row, int col, char board[][9], int value)
  64. {
  65. for (int i = 0; i < 9; i++)
  66. if (board[row][i] == value)
  67. return false;
  68. for (int i = 0; i < 9; i++)
  69. if (board[i][col] == value)
  70. return false;
  71. int startX = row/3 * 3;
  72. int startY = col/3 * 3;
  73.  
  74. for (int i = startX; i < startX + 3; i++)
  75. for (int j = startY; j < startY + 3; j++)
  76. if (board[i][j] == value)
  77. return false;
  78. return true;
  79.  
  80. }
  81. void editSquare(char game[][9])
  82. {
  83. bool a = 0;
  84. int x;
  85. int y;
  86. char input[3];
  87. char value;
  88. cout << "What are the coordinates of the square: ";
  89. cin >> input;
  90. x = input[1] - '1';
  91. y = input[0] - 'A';
  92.  
  93.  
  94. if (game[x][y] != '0')
  95. {
  96. cout << "ERROR: Square '" << input << "' is filled\n";
  97. // cout << endl;
  98. }
  99. else
  100. {
  101. cout << "What is the value at '" << input << "': ";
  102. cin >> value;
  103. // cout0 << endl;
  104. int i = value;
  105. if (checkSquare(y, x, game, i))
  106. game[x][y] = value;
  107. else
  108. {
  109.  
  110. while(bool a = 0)
  111. cout << "ERROR: Value '" << value << "' in square '" << input[0] << input[1] << "' is invalid\n";
  112. bool a = 1;
  113. }
  114. }
  115. cout << endl;
  116.  
  117. }
  118. void showValues(char board[][9])
  119. {
  120. int x;
  121. int y;
  122. int count = 0;
  123. char input[3];
  124. cout << "What are the coordinates of the square: ";
  125. cin >> input;
  126. x = input[1] - '1';
  127. y = input[0] - 'A';
  128.  
  129. if (board[x][y] != '0')
  130. cout << "Error at coord " << y + 'A' << x + '1' << "is already filled.\n";
  131. else
  132. {
  133. cout << "Possible values are: ";
  134. for (int i = 0; i < 9; i++)
  135. if (checkSquare(x, y, board, i + '0'))
  136. {
  137. if (count == 0)
  138. cout << i;
  139. else
  140. cout << ", " << i;
  141. count++;
  142. }
  143. cout << endl;
  144. }
  145.  
  146. }
  147.  
  148. void gameLoop(char data[][9])
  149. {
  150. cout << endl;
  151. char input;
  152. do
  153. {
  154. cout << "> ";
  155. cin >> input;
  156. switch (input)
  157. {
  158. case '?':
  159. cout << "Options:\n";
  160. cout << " ? Show these instructions\n";
  161. cout << " D Display the board\n";
  162. cout << " E Edit one square\n";
  163. cout << " S Show the possible values for a square\n";
  164. cout << " Q Save and Quit\n";
  165. cout << endl << endl;
  166. break;
  167. case 'D':
  168. display(data);
  169. cout << endl;
  170. break;
  171. case 'E':
  172. editSquare(data);
  173. break;
  174. case 'Q':
  175. break;
  176. case 'S':
  177. showValues(data);
  178. break;
  179. default:
  180. cout << "Incorrect input\n";
  181. break;
  182. }
  183. }
  184. while (input != 'Q');
  185.  
  186. }
  187.  
  188. void getFileName2(char fileName[])
  189. {
  190. cout << "What file would you like to write your board to: ";
  191. cin >> fileName;
  192. }
  193. int writeFile(char fileName[], char game[][9])
  194. {
  195. int i = 0;
  196. ofstream output;
  197. output.open(fileName);
  198. if (output.fail())
  199. return -1;
  200.  
  201. for (int i = 0; i < 9; i++)
  202. for (int j = 0; j < 9; j++)
  203. output << game[i][j];
  204.  
  205. output.close();
  206. return i;
  207. }
  208. int main()
  209. {
  210.  
  211. char fileName[256];
  212. char data[9][9];
  213. char input;
  214.  
  215. getFileName(fileName);
  216. int size = readFile(fileName, data);
  217. if (size == -1)
  218. cout << "Bad file " << fileName << endl;
  219. else
  220. {
  221. cout << "Options:\n";
  222. cout << " ? Show these instructions\n";
  223. cout << " D Display the board\n";
  224. cout << " E Edit one square\n";
  225. cout << " S Show the possible values for a square\n";
  226. cout << " Q Save and Quit\n";
  227. cout << endl;
  228.  
  229. display(data);
  230. }
  231. gameLoop(data);
  232.  
  233. getFileName2(fileName);
  234. writeFile(fileName, data);
  235. cout << "Board written successfully\n";
  236. return 0;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement