From d8eb32cf50731b1dd8e637c700d03bd7a7b87d3b Mon Sep 17 00:00:00 2001 From: zedarider Date: Thu, 8 Sep 2016 02:28:07 +0200 Subject: adding all initial files --- .gitignore | 2 + Makefile | 38 ++++++++++++++++ include/sudoku_solver.hpp | 47 ++++++++++++++++++++ resources/grid.txt | 9 ++++ src/main.cpp | 12 ++++++ src/sudoku_solver.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 215 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 include/sudoku_solver.hpp create mode 100644 resources/grid.txt create mode 100644 src/main.cpp create mode 100644 src/sudoku_solver.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5697ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +bin/ 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/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 +#include +#include +#include +#include + +class SudokuSolver { +public: + SudokuSolver(); + ~SudokuSolver(); + + void printGrid(); + bool checkBox(unsigned int &grid_location, std::vector &curr_grid); + bool checkRow(unsigned int &grid_location, std::vector &curr_grid); + bool checkColumn(unsigned int &grid_location, std::vector &curr_grid); + int getBox(unsigned int grid_location); +protected: +private: + std::vector 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 &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 &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 &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; +} -- cgit