Skip to content

Commit ed20c8f

Browse files
authored
Merge pull request #332 from UmairJaved728/umair-hacktoberfest-1
Complete and Comprehensive Sudoku game in C++
2 parents 0f59d51 + 3ce2464 commit ed20c8f

File tree

6 files changed

+790
-0
lines changed

6 files changed

+790
-0
lines changed

Coding/C++/Sudoku/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 3.24)
2+
project(Project C)
3+
4+
set(CMAKE_C_STANDARD 11)
5+
6+
include_directories(.)
7+
8+
add_executable(Project
9+
grid.c
10+
grid.h
11+
sudoku.c
12+
sudoku.h)

Coding/C++/Sudoku/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
OUTPUT=sudoku
2+
CC=gcc
3+
CFLAGS=-std=c11 -Wall -Werror
4+
5+
all: $(OUTPUT)
6+
7+
$(OUTPUT): grid.o sudoku.o
8+
$(CC) $(CFLAGS) grid.o sudoku.o -o $(OUTPUT)
9+
10+
sudoku.o: sudoku.c
11+
$(CC) $(CFLAGS) -c sudoku.c
12+
13+
grid.o: grid.c
14+
$(CC) $(CFLAGS) -c grid.c
15+
16+
clean:
17+
rm -rf *.o $(OUTPUT)

Coding/C++/Sudoku/grid.c

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include "grid.h"
4+
5+
Grid_T grid_init(Grid_T g, int v[9][9])
6+
{
7+
int i, j;
8+
9+
for (i = 0; i < 9; i++)
10+
{
11+
for (j = 0; j < 9; j++)
12+
{
13+
if(v[i][j] != 0)
14+
g = grid_update(g, (Choice_T){i, j, v[i][j]});
15+
}
16+
}
17+
18+
return g;
19+
}
20+
21+
Grid_T grid_update(Grid_T g, Choice_T c) {
22+
int i = c.i;
23+
int j = c.j;
24+
int n = c.n;
25+
26+
// Update the cell (i, j) with the value n
27+
if(g.cells[i][j].choices[n]) {
28+
g.cells[i][j].count--;
29+
}
30+
31+
g.cells[i][j].choices[n] = 0;
32+
g.cells[i][j].choices[0] = n;
33+
34+
// Remove the value n from the possibilities for the row, column, and box
35+
for (int k = 0; k < 9; k++) {
36+
if(g.cells[i][k].choices[n] == 1)
37+
g.cells[i][k].count--;
38+
g.cells[i][k].choices[n] = 0;
39+
40+
if(g.cells[k][j].choices[n] == 1)
41+
g.cells[k][j].count--;
42+
g.cells[k][j].choices[n] = 0;
43+
}
44+
45+
int box_i = (i / 3) * 3;
46+
int box_j = (j / 3) * 3;
47+
for (int x = box_i; x < box_i + 3; x++) {
48+
for (int y = box_j; y < box_j + 3; y++) {
49+
if(g.cells[x][y].choices[n] == 1)
50+
g.cells[x][y].count--;
51+
g.cells[x][y].choices[n] = 0;
52+
}
53+
}
54+
55+
return g;
56+
}
57+
58+
Choice_T grid_iterate(Grid_T g, Choice_T t) {
59+
int i = t.i;
60+
int j = t.j;
61+
int k, r;
62+
63+
if( g.cells[i][j].count <= 0 ) {
64+
return (Choice_T) {0, 0, 0};
65+
}
66+
67+
if(g.cells[i][j].count == 1)
68+
{
69+
for (k = 1; k <= 9; k++)
70+
{
71+
if (g.cells[i][j].choices[k])
72+
{
73+
g.cells[i][j].choices[k] = 0;
74+
g.cells[i][j].count--;
75+
return (Choice_T){i, j, k};
76+
}
77+
}
78+
}
79+
80+
do{
81+
r = rand() % 9 + 1;
82+
if(g.cells[i][j].choices[r]) {
83+
g.cells[i][j].choices[r] = 0;
84+
g.cells[i][j].count--;
85+
return (Choice_T) {i, j, r};
86+
}
87+
}while( !g.cells[i][j].choices[r] );
88+
89+
return (Choice_T){ 0, 0, 0 };
90+
}
91+
92+
int grid_unique(Grid_T g)
93+
{
94+
return g.unique;
95+
}
96+
97+
Choice_T grid_read_value(Grid_T g, Choice_T c)
98+
{
99+
return (Choice_T){c.i, c.j, g.cells[c.i][c.j].choices[0]};
100+
}
101+
102+
Grid_T grid_clear_unique(Grid_T g)
103+
{
104+
g.unique = 0;
105+
return g;
106+
}
107+
108+
Choice_T grid_exist_unique(Grid_T g)
109+
{
110+
int i, j, k;
111+
112+
for (i = 0; i < 9; i++)
113+
{
114+
for (j = 0; j < 9; j++)
115+
{
116+
if (g.cells[i][j].count == 1 && g.cells[i][j].choices[0] == 0)
117+
{
118+
for (k = 1; k <= 9; k++)
119+
{
120+
if (g.cells[i][j].choices[k])
121+
{
122+
return (Choice_T){i, j, k};
123+
}
124+
}
125+
}
126+
}
127+
}
128+
129+
return (Choice_T){0, 0, 0};
130+
}
131+
132+
133+
134+
135+
#ifdef DEBUG
136+
void grid_cell_print(FILE *stream, Grid_T g, Choice_T c)
137+
{
138+
int i;
139+
140+
fprintf(stream, "(%d,%d): ", c.i, c.j);
141+
for (i = 1; i <= 9; i++)
142+
{
143+
if (g.cells[c.i][c.j].choices[i])
144+
{
145+
fprintf(stream, "%d ", i);
146+
}
147+
}
148+
fprintf(stream, "\n");
149+
}
150+
#endif

Coding/C++/Sudoku/grid.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/********************************************************************
2+
grid.h
3+
Definition of grid ADT
4+
*********************************************************************/
5+
#include <stdio.h>
6+
typedef struct cell_s {
7+
int count; /* count of valid (1) choices in the choices array */
8+
int choices[10]; /* choices[k]=1 if k is a valid choice for element i,j, else choices[k]=0.
9+
choices[0] is the value 1..9 assigned to the cell, when resolved */
10+
} Cell_T;
11+
12+
typedef struct grid_s {
13+
Cell_T cells[9][9]; /* sudoku puzzle cells; 0<=i,j<9 */
14+
int unique; /* if 1 after solving, puzzle has unique-choice solution */
15+
} Grid_T;
16+
17+
typedef struct choice_s {
18+
int i,j,n; /* n is the value 1..9 of a choice for cell i,j. 0 means the cell is empty. */
19+
} Choice_T;
20+
21+
Grid_T grid_init(Grid_T g, int v[9][9]); /* init g with values from array v */
22+
Grid_T grid_update(Grid_T g, Choice_T c); /* update value of c.i,c.j to c.n and eliminate c from choices in grid */
23+
Choice_T grid_iterate(Grid_T g, Choice_T t); /* iterate over all choices in all cells starting from (t.i,t.j,t.n+1).If no choice is left, return (0,0,0) */
24+
25+
int grid_unique(Grid_T g); /* return unique flag for g */
26+
Choice_T grid_exist_unique(Grid_T g); /* return a cell with a unique choice, if one exists, otherwise return (0,0,0) */
27+
Grid_T grid_clear_unique(Grid_T g); /* clear unique flag */
28+
29+
Choice_T grid_read_value(Grid_T g, Choice_T c); /* return value of i,j */
30+
31+
#ifdef DEBUG
32+
void grid_cell_print(FILE *stream, Grid_T g, Choice_T c);
33+
#endif

0 commit comments

Comments
 (0)