summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzedarider <ymherklotz@gmail.com>2016-09-08 02:19:06 +0200
committerzedarider <ymherklotz@gmail.com>2016-09-08 02:19:06 +0200
commitfa254e60eeea7b228abf9a5ed9d4e56fd0f34b51 (patch)
tree74c8e4c484289b92fd7559acb6c0e9a9df602ff5
downloadsudoku_solver-fa254e60eeea7b228abf9a5ed9d4e56fd0f34b51.tar.gz
sudoku_solver-fa254e60eeea7b228abf9a5ed9d4e56fd0f34b51.zip
addint all the original files
-rw-r--r--Makefile38
-rwxr-xr-xbin/Sudoku_Solverbin0 -> 87240 bytes
-rw-r--r--build/main.obin0 -> 35560 bytes
-rw-r--r--build/sudoku_solver.obin0 -> 86344 bytes
-rw-r--r--include/sudoku_solver.hpp47
-rw-r--r--resources/grid.txt9
-rw-r--r--src/main.cpp12
-rw-r--r--src/sudoku_solver.cpp107
8 files changed, 213 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..a9372d5
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,38 @@
+CC := g++ # this is the main compiler
+# CC := clange --analyze # and comment out the linker last line
+SRCDIR := src
+BUILDDIR := build
+TARGETDIR := bin
+TARGET := bin/Sudoku_Solver
+
+SRCEXT := cpp
+SOURCES := $(shell find $(SRCDIR) -type f -name "*.$(SRCEXT)")
+OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
+CFLAGS := -g -std=c++14 -Wall
+LIBDIR :=
+LIB :=
+INC := -Iinclude/
+
+$(TARGET): $(OBJECTS)
+ @echo " Linking..."
+ @mkdir -p $(TARGETDIR)
+ @echo " $(CC) $^ -o $(TARGET) $(LIBDIR) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIBDIR) $(LIB)
+
+$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
+ @echo " Building..."
+ @mkdir -p $(BUILDDIR)
+ @echo " $(CC) $< $(CFLAGS) $(INC) -c -o $@"; $(CC) $< $(CFLAGS) $(INC) -c -o $@
+
+clean:
+ @echo " Cleaning..."
+ @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET)
+
+# Tests
+tester:
+ @echo " $(CC) $(CFLAGS) test/tester.cpp $(INC) $(LIB) -o bin/tester"; $(CC) $(CFLAGS) test/tester.cpp $(INC) $(LIB) -o bin/tester
+
+# Spikes
+ticket:
+ @echo " $(CC) $(CFLAGS) spikes/ticket.cpp $(INC) $(LIB) -o bin/ticket"; $(CC) $(CFLAGS) spikes/ticket.cpp $(INC) $(LIB) -o bin/ticket
+
+.PHONY: clean
diff --git a/bin/Sudoku_Solver b/bin/Sudoku_Solver
new file mode 100755
index 0000000..736f887
--- /dev/null
+++ b/bin/Sudoku_Solver
Binary files differ
diff --git a/build/main.o b/build/main.o
new file mode 100644
index 0000000..0edb993
--- /dev/null
+++ b/build/main.o
Binary files differ
diff --git a/build/sudoku_solver.o b/build/sudoku_solver.o
new file mode 100644
index 0000000..7bfebfb
--- /dev/null
+++ b/build/sudoku_solver.o
Binary files differ
diff --git a/include/sudoku_solver.hpp b/include/sudoku_solver.hpp
new file mode 100644
index 0000000..c3a685b
--- /dev/null
+++ b/include/sudoku_solver.hpp
@@ -0,0 +1,47 @@
+/*
+
+ -- -- -- - -- -- -- - -- -- --
+ | 00 01 02 | 03 04 05 | 06 07 08 |
+ | 09 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 |
+ -- -- -- - -- -- -- - -- -- --
+
+ */
+
+#ifndef SUDOKU_SOLVER_HEAD
+#define SUDOKU_SOLVER_HEAD
+
+#define GRIDSIZE 9
+#define SEPSIZE 3
+
+#include <string>
+#include <vector>
+#include <cstdlib>
+#include <fstream>
+#include <iostream>
+
+class SudokuSolver {
+public:
+ SudokuSolver();
+ ~SudokuSolver();
+
+ void printGrid();
+ bool checkBox(unsigned int &grid_location, std::vector<int> &curr_grid);
+ bool checkRow(unsigned int &grid_location, std::vector<int> &curr_grid);
+ bool checkColumn(unsigned int &grid_location, std::vector<int> &curr_grid);
+ int getBox(unsigned int grid_location);
+protected:
+private:
+ std::vector<int> grid;
+ std::ifstream grid_file;
+};
+
+#endif
diff --git a/resources/grid.txt b/resources/grid.txt
new file mode 100644
index 0000000..d888b9d
--- /dev/null
+++ b/resources/grid.txt
@@ -0,0 +1,9 @@
+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
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..23009a7
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,12 @@
+#include "../include/sudoku_solver.hpp"
+
+using namespace std;
+
+int main(int argc, char** argv) {
+ cout << "Sudoku Solver v1.1" << endl << endl;
+
+ SudokuSolver solver;
+
+ solver.printGrid();
+ return 0;
+}
diff --git a/src/sudoku_solver.cpp b/src/sudoku_solver.cpp
new file mode 100644
index 0000000..54888b8
--- /dev/null
+++ b/src/sudoku_solver.cpp
@@ -0,0 +1,107 @@
+#include "../include/sudoku_solver.hpp"
+
+using namespace std;
+
+SudokuSolver::SudokuSolver() {
+ int tmp_int;
+ grid_file.open("/home/yannherklotz/Github/sudoku_solver/resources/grid.txt", ios::in);
+
+ if(grid_file.is_open()) {
+ cout << "File successfully opened..." << endl << endl;
+ } else {
+ cout << "Failed to open file..." << endl << endl;
+ exit(EXIT_FAILURE);
+ }
+
+ while(grid_file >> tmp_int) {
+ grid.push_back(tmp_int);
+ }
+}
+
+SudokuSolver::~SudokuSolver() {
+
+}
+
+void SudokuSolver::printGrid() {
+ int count = 0;
+ int row = 0;
+ int sep_count = 0;
+ for(int box : grid) {
+ cout << box << " ";
+ ++count;
+
+ if(count % SEPSIZE == 0 && count % GRIDSIZE != 0) {
+ cout << "| ";
+ }
+
+ if(count >= GRIDSIZE) {
+ count = 0;
+ cout << endl;
+ ++row;
+ }
+
+ if(row >= SEPSIZE && sep_count < SEPSIZE-1) {
+ for(unsigned int i = 0; i < GRIDSIZE + SEPSIZE-1; ++i) {
+ cout << "- ";
+ }
+ cout << endl;
+
+ row = 0;
+ ++sep_count;
+ }
+ }
+}
+
+bool SudokuSolver::checkBox(unsigned int &grid_location, vector<int> &curr_grid) {
+ int box_location = getBox(grid_location);
+
+ for(unsigned int i = 0; i < curr_grid.size(); ++i) {
+ if(getBox(i) == box_location && grid_location != i) {
+ if(curr_grid[grid_location] == curr_grid[i]) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+bool SudokuSolver::checkRow(unsigned int &grid_location, vector<int> &curr_grid) {
+ int row = grid_location / GRIDSIZE;
+
+ for(unsigned int i = 0; i < curr_grid.size(); ++i) {
+ int tmp_row = i / GRIDSIZE;
+ if(row == tmp_row && grid_location != i) {
+ if(curr_grid[grid_location] == curr_grid[i]) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+bool SudokuSolver::checkColumn(unsigned int &grid_location, vector<int> &curr_grid) {
+ int column = grid_location % GRIDSIZE;
+
+ for(unsigned int i = 0; i < curr_grid.size(); ++i) {
+ int tmp_col = i % GRIDSIZE;
+ if(column == tmp_col && grid_location != i) {
+ if(curr_grid[grid_location] == curr_grid[i]) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+int SudokuSolver::getBox(unsigned int grid_location) {
+ int column = grid_location % GRIDSIZE;
+
+ int box_row = grid_location / (GRIDSIZE * SEPSIZE);
+ int box_column = column / SEPSIZE;
+
+ int box_location = box_row * 3 + box_column;
+ return box_location;
+}