Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 19, 2016
0 parents commit f2f5b35
Show file tree
Hide file tree
Showing 136 changed files with 5,581 additions and 0 deletions.
Binary file added 9781484209967.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,27 @@
Freeware License, some rights reserved

Copyright (c) 2015 Giulio Zambon

Permission is hereby granted, free of charge, to anyone obtaining a copy
of this software and associated documentation files (the "Software"),
to work with the Software within the limits of freeware distribution and fair use.
This includes the rights to use, copy, and modify the Software for personal use.
Users are also allowed and encouraged to submit corrections and modifications
to the Software for the benefit of other users.

It is not allowed to reuse, modify, or redistribute the Software for
commercial use in any way, or for a user�s educational materials such as books
or blog articles without prior permission from the copyright holder.

The above copyright notice and this permission notice need to be included
in all copies or substantial portions of the software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


15 changes: 15 additions & 0 deletions README.md
@@ -0,0 +1,15 @@
#Apress Source Code

This repository accompanies [*Sudoku Programming with C*](http://www.apress.com/9781484209967) by Giulio Zambon (Apress, 2015).

![Cover image](9781484209967.jpg)

Download the files as a zip using the green button, or clone the repository to your machine using Git.

##Releases

Release v1.0 corresponds to the code in the published book, without corrections or updates.

##Contributions

See the file Contributing.md for more information on how you can contribute to this repository.
14 changes: 14 additions & 0 deletions contributing.md
@@ -0,0 +1,14 @@
# Contributing to Apress Source Code

Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers.

## How to Contribute

1. Make sure you have a GitHub account.
2. Fork the repository for the relevant book.
3. Create a new branch on which to make your change, e.g.
`git checkout -b my_code_contribution`
4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted.
5. Submit a pull request.

Thank you for your contribution!
69 changes: 69 additions & 0 deletions sources/Generator/brute.c
@@ -0,0 +1,69 @@
/* brute.c
*
* Copyright (C) 2015 Giulio Zambon - http://zambon.com.au/
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "brute.h"
#include "def.h"
#include "inconsistent_unit.h"

int brute() {
int result = BRUTE_SUCCESSFUL;
unsigned long start_time = clock()/CLOCKS_PER_SEC;
unsigned long this_time;
char initial[9][9];
for (int k = 0; k < 9; k++) {
for (int j = 0; j < 9; j++) {
initial[k][j] = grid[k][j];
}
}
int k = 0;
int j = 0;
do {
do {
if (initial[k][j] == 0) {
int i = grid[k][j] + 1;
if (i > 9) {
grid[k][j] = 0;
do {
do { j--; } while (j >= 0 && initial[k][j] != 0);
if (j < 0) {
k--;
if (k < 0) {
result = BRUTE_IMPOSSIBLE;
goto done; //==>
}
j = 8;
}
} while (initial[k][j] != 0);
} // if (i..
else {
grid[k][j] = i;
int kB = k/3*3+j/3;
if ( !inconsistent_unit("row", k, row[k])
&& !inconsistent_unit("column", j, col[j])
&& !inconsistent_unit("box", kB, box[kB])
) {
j++;
}
} // if (i.. else
} // if (initial[k][j]..
else {
j++;
}
this_time = clock()/CLOCKS_PER_SEC;
if (this_time - start_time > BRUTE_MAX_TIME) {
result = BRUTE_TIMEOUT;
goto done; //==>
}
} while (j < 9);
k++;
j = 0;
} while (k < 9);

done:
return result;
}
21 changes: 21 additions & 0 deletions sources/Generator/brute.h
@@ -0,0 +1,21 @@
/* brute.h
*
* Solves a Sudoku by brute force
*
* See below for the return codes
*
* Copyright (C) 2015 Giulio Zambon - http://zambon.com.au/
*
*/
#ifndef BRUTE
#define BRUTE

#define BRUTE_SUCCESSFUL 0
#define BRUTE_IMPOSSIBLE -1
#define BRUTE_TIMEOUT -2

#define BRUTE_MAX_TIME 10

int brute(void);

#endif
52 changes: 52 additions & 0 deletions sources/Generator/brute_comp.c
@@ -0,0 +1,52 @@
/* brute_comp.c
*
* Copyright (C) 2015 Giulio Zambon - http://zambon.com.au/
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "brute.h"
#include "brute_comp.h"
#include "def.h"
#include "inconsistent_grid.h"

// This messages in clear must match the codes defined in brute_comp.h
const char *brute_comp_err[] = {
/* 0 */ "same as reference",
/* 1 */ "different from reference",
/* 2 */ "timeout",
/* 3 */ "grid inconsistent",
/* 4 */ "puzzle impossible",
/* 5 */ "unknown result"
};

int brute_comp() {
int result = BRUTE_COMP_PROBLEM;
int brute_result = brute();
switch (brute_result) {

case BRUTE_SUCCESSFUL:
result = BRUTE_COMP_OK;
for (int kk = 0; kk < 9 && result == BRUTE_COMP_OK; kk++) {
for (int jj = 0; jj < 9 && result == BRUTE_COMP_OK; jj++) {
if (solved[kk][jj] != grid[kk][jj]) result = BRUTE_COMP_DIFFERENT;
}
}
if (inconsistent_grid()) result = BRUTE_COMP_INCONSISTENT;
break;

case BRUTE_IMPOSSIBLE:
result = BRUTE_COMP_IMPOSSIBLE;
break;

case BRUTE_TIMEOUT:
result = BRUTE_COMP_TIMEOUT;
break;

default:
result = BRUTE_COMP_PROBLEM;
break;
}
return result;
}
26 changes: 26 additions & 0 deletions sources/Generator/brute_comp.h
@@ -0,0 +1,26 @@
/* brute_comp.h
*
* Solves a Sudoku by executing brute() and then compares the result with
* the reference
*
* See below for the return codes
*
* Copyright (C) 2015 Giulio Zambon - http://zambon.com.au/
*
*/
#ifndef BRUTE_COMPARE
#define BRUTE_COMPARE

// If you modify the following list, change brute_comp_err accordingly
#define BRUTE_COMP_OK 0
#define BRUTE_COMP_DIFFERENT 1
#define BRUTE_COMP_TIMEOUT 2
#define BRUTE_COMP_INCONSISTENT 3
#define BRUTE_COMP_IMPOSSIBLE 4
#define BRUTE_COMP_PROBLEM 5

extern const char *brute_comp_err[];

int brute_comp(void);

#endif
19 changes: 19 additions & 0 deletions sources/Generator/count_solved.c
@@ -0,0 +1,19 @@
/* count_solved.c
*
* Copyright (C) 2015 Giulio Zambon - http://zambon.com.au/
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "count_solved.h"
#include "def.h"

int count_solved() {
int result = 0;
for (int k = 0; k < 9; k++) {
for (int j = 0; j < 9; j++) {
if (grid[k][j] != 0) result++;
}
}
return result;
}
13 changes: 13 additions & 0 deletions sources/Generator/count_solved.h
@@ -0,0 +1,13 @@
/* count_solved.h
*
* Counts the number of solved cells.
*
* Copyright (C) 2015 Giulio Zambon - http://zambon.com.au/
*
*/
#ifndef COUNT_SOLVED
#define COUNT_SOLVED

int count_solved(void);

#endif
37 changes: 37 additions & 0 deletions sources/Generator/def.h
@@ -0,0 +1,37 @@
/* def.h
*
* Definitions and declarations
*
* Copyright (C) 2015 Giulio Zambon - http://zambon.com.au/
*
*/
#ifndef DEF
#define DEF

// General definitions
#define FALSE 0
#define TRUE 1

// Used in some strategies for clarity
#define ROW 0
#define COL 1
#define BOX 2
extern char *unit_names[3];

// grid declarations
extern char grid[9][9];
extern char row[9][9][2];
extern char col[9][9][2];
extern char box[9][9][2];
extern char solved[9][9];

// Flags
extern int silent;

// Patch because Windows doesn't recognise srandom() and random()
#ifdef __WIN32__
#define srandom srand
#define random rand
#endif

#endif
42 changes: 42 additions & 0 deletions sources/Generator/display.c
@@ -0,0 +1,42 @@
/* display.c
*
* Copyright (C) 2015 Giulio Zambon - http://zambon.com.au/
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "def.h"
#include "display.h"

void display() {
char *h = " ++---+---+---++---+---+---++---+---+---++";
char *hh = " ++===+===+===++===+===+===++===+===+===++";
int jBase[] = {2, 6, 10, 15, 19, 23, 28, 32, 36};
printf(" 0 1 2 3 4 5 6 7 8\n");
for (int k = 0; k < 9; k++) {
if (k%3 == 0) {
printf("%s\n", hh);
}
else {
printf("%s\n", h);
}
// 000 000 111 111 122 222 223 333 333
// 234 678 012 567 901 345 890 234 678
char top[42] = "|| | | || | | || | | ||";
char mid[42] = "|| | | || | | || | | ||";
char bot[42] = "|| | | || | | || | | ||";
char *displ[42] = {top, mid, bot};
for (int j = 0; j < 9; j++) {
if (grid[k][j] == 0) {
mid[jBase[j]+1] = ' ';
}
else {
mid[jBase[j]+1] = '0' + grid[k][j];
}
} // for (int j..
printf(" %s\n", displ[0]);
printf("%d %s\n", k, displ[1]);
printf(" %s\n", displ[2]);
}
printf("%s\n", hh);
}
13 changes: 13 additions & 0 deletions sources/Generator/display.h
@@ -0,0 +1,13 @@
/* display.h
*
* Displays the sudoku grid.
*
* Copyright (C) 2015 Giulio Zambon - http://zambon.com.au/
*
*/
#ifndef DISPLAY
#define DISPLAY

void display();

#endif
19 changes: 19 additions & 0 deletions sources/Generator/display_string.c
@@ -0,0 +1,19 @@
/* display_string.c
*
* Copyright (C) 2015 Giulio Zambon - http://zambon.com.au/
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "def.h"
#include "display_string.h"

void display_string(char *name) {
for (int k = 0; k < 9; k++) {
for (int j = 0; j < 9; j++) {
printf("%d", grid[k][j]);
}
}
if (name != NULL) printf(" \"%s\"", name);
printf("\n");
}
13 changes: 13 additions & 0 deletions sources/Generator/display_string.h
@@ -0,0 +1,13 @@
/* display_string.h
*
* Displays the sudoku string.
*
* Copyright (C) 2015 Giulio Zambon - http://zambon.com.au/
*
*/
#ifndef DISPLAY_STRING
#define DISPLAY_STRING

void display_string(char *name);

#endif

0 comments on commit f2f5b35

Please sign in to comment.