Facebook
From Queen Duck, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 125
  1. // SudokuSolver.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include "SudokuFieldProvider.h"
  6.  
  7. int main()
  8. {
  9.     std::cout << "Hello World!\n";
  10.         SudokuFieldProvider provider = SudokuFieldProvider();
  11.         string name = "Test";
  12.         solve(&provider.GetField(name));
  13. }
  14.  
  15. void printTable(SudokuField* field) {
  16.         for (int x = 0; x < sizeof(field->field); x++) {
  17.                 string line = "";
  18.                 for (int y = 0; y < sizeof(field->field); y++) {
  19.                         int num = field->field[x][y];
  20.                         line += " " + std::to_string(num);
  21.                 }
  22.  
  23.                 cout << line;
  24.         }
  25. }
  26.  
  27. void solve(SudokuField* field) {
  28.  
  29.         for (int x = 0; x < sizeof(field->field); x++) {
  30.                 for (int y = 0; y < sizeof(field->field); y++) {
  31.  
  32.                         for (int i = 0; i < sizeof(field->field); i++) {
  33.                                 if (canPlace(field, x, y, i)) {
  34.                                         field->field[x][y] = i;
  35.                                         printTable(field);
  36.                                         break;
  37.                                 }
  38.                         }
  39.  
  40.                 }
  41.         }
  42. }
  43.  
  44. bool canPlace(SudokuField* field, int x, int y, int num)
  45. {
  46.  
  47.         for (int iX = 0; iX < sizeof(field->field); iX++) {
  48.                 if (field->field[iX][y] == num)
  49.                         return false;
  50.         }
  51.         for (int iY = 0; iY < sizeof(field->field); iY++) {
  52.                 if (field->field[x][iY] == num)
  53.                         return false;
  54.         }
  55.         return true;
  56. }
  57.  
  58. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  59. // Debug program: F5 or Debug > Start Debugging menu
  60.  
  61. // Tips for Getting Started:
  62. //   1. Use the Solution Explorer window to add/manage files
  63. //   2. Use the Team Explorer window to connect to source control
  64. //   3. Use the Output window to see build output and other messages
  65. //   4. Use the Error List window to view errors
  66. //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  67. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
  68.