Skip to content

Commit 5f5ebad

Browse files
committed
lab 9
1 parent 15c07ca commit 5f5ebad

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed

lab9/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
all: sudoku
2+
3+
sudoku: sudoku.o main.cpp
4+
g++ -g -Wall main.cpp sudoku.o -o sudoku
5+
6+
sudoku.o: sudoku.cpp sudoku.h
7+
g++ -g -Wall -c sudoku.cpp -o sudoku.o
8+
9+
clean:
10+
-@rm sudoku
11+
-@rm sudoku.o

lab9/main.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "sudoku.h"
2+
#include <iostream>
3+
4+
int main() {
5+
int puzzle[9][9] = {{1,0,0,0,0,7,0,9,0},
6+
{0,3,0,0,2,0,0,0,8},
7+
{0,0,9,6,0,0,5,0,0},
8+
{0,0,5,3,0,0,9,0,0},
9+
{0,1,0,0,8,0,0,0,2},
10+
{6,0,0,0,0,4,0,0,0},
11+
{3,0,0,0,0,0,0,1,0},
12+
{0,4,0,0,0,0,0,0,7},
13+
{0,0,7,0,0,0,3,0,0}};
14+
Sudoku s(puzzle);
15+
s.solve();
16+
s.print();
17+
std::cout << "PUZZLE 1: ";
18+
s.verify();
19+
20+
int puzzle2[9][9] = {{0,0,0,0,0,3,2,9,0},
21+
{0,8,6,5,0,0,0,0,0},
22+
{0,2,0,0,0,1,0,0,0},
23+
{0,0,3,7,0,5,1,0,0},
24+
{9,0,0,0,0,0,0,0,8},
25+
{0,0,2,9,0,8,3,0,0},
26+
{0,0,0,4,0,0,0,8,0},
27+
{0,0,0,0,0,6,4,7,0},
28+
{0,4,7,1,0,0,0,0,0}};
29+
Sudoku s2(puzzle2);
30+
s2.solve();
31+
s2.print();
32+
std::cout << "PUZZLE 2: ";
33+
s2.verify();
34+
35+
int puzzle3[9][9] = {{0,9,0,3,0,0,0,0,1},
36+
{0,0,0,0,8,0,0,4,6},
37+
{0,0,0,0,0,0,8,0,0},
38+
{4,0,5,0,6,0,0,3,0},
39+
{0,0,3,2,7,5,6,0,0},
40+
{0,6,0,0,1,0,9,0,4},
41+
{0,0,1,0,0,0,0,0,0},
42+
{5,8,0,0,2,0,0,0,0},
43+
{2,0,0,0,0,7,0,6,0}};
44+
Sudoku s3(puzzle3);
45+
s3.solve();
46+
s3.print();
47+
std::cout << "PUZZLE 3: ";
48+
s3.verify();
49+
50+
return 0;
51+
}

lab9/sudoku.cpp

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include "sudoku.h"
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
Sudoku::Sudoku(int puzzle[9][9]) {
7+
for(int i = 0; i < 9; i++) {
8+
for(int j = 0; j < 9; j++) {
9+
board[i][j] = puzzle[i][j];
10+
}
11+
}
12+
}
13+
14+
Sudoku::~Sudoku() {
15+
}
16+
17+
void Sudoku::verify() {
18+
for(int i = 0; i < 9; i++) {
19+
for(int j = 0; j < 9; j++) {
20+
if(!isValid(i, j)) {
21+
cout << "INVALID PUZZLE" << endl;
22+
return;
23+
}
24+
}
25+
}
26+
cout << "VALID PUZZLE" << endl;
27+
}
28+
29+
void Sudoku::print() {
30+
for (int row=0; row<9; row++) {
31+
if (row % 3== 0) {
32+
std::cout << "-------------------------------" << std::endl;
33+
}
34+
35+
for (int col=0; col<9; col++) {
36+
if (col % 3 == 0) {
37+
std::cout << "|";
38+
}
39+
40+
if (board[row][col] != 0) {
41+
std::cout << " " << board[row][col] << " ";
42+
} else {
43+
std::cout << " . ";
44+
}
45+
46+
}
47+
48+
std::cout << "|" << std::endl;
49+
}
50+
std::cout << "-------------------------------" << std::endl;
51+
}
52+
53+
bool Sudoku::isValid(int row, int col) {
54+
55+
56+
int value = board[row][col];
57+
58+
for (int i=0; i<9; i++) {
59+
if (i == row) {
60+
continue;
61+
}
62+
63+
int temp = board[i][col];
64+
if (temp == value) {
65+
return false;
66+
}
67+
}
68+
69+
for (int i=0; i<9; i++) {
70+
if (i == col) {
71+
continue;
72+
}
73+
74+
int temp = board[row][i];
75+
if (temp == value) {
76+
return false;
77+
}
78+
}
79+
80+
int box_row = row / 3;
81+
int box_col = col / 3;
82+
83+
for (int i=box_row * 3; i < box_row * 3 + 3; i++) {
84+
for (int j=box_col * 3; j < box_col * 3 + 3; j++) {
85+
if (i == row && j == col) {
86+
continue;
87+
}
88+
89+
int temp = board[i][j];
90+
if (temp == value) {
91+
return false;
92+
}
93+
}
94+
}
95+
96+
return true;
97+
}
98+
99+
void Sudoku::solve(){
100+
solveHelper(0, 0);
101+
}
102+
103+
104+
bool Sudoku::solveHelper(int row, int col) {
105+
// TODO: IMPLEMENT THIS
106+
}

lab9/sudoku.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef Sudoku_H
2+
#define Sudoku_H
3+
4+
class Sudoku {
5+
6+
public:
7+
Sudoku(int puzzle[9][9]);
8+
~Sudoku();
9+
10+
void solve(); // solves the puzzle
11+
void print(); // prints the puzzle
12+
void verify(); // verifies the puzzle is valid
13+
14+
private:
15+
bool isValid(int row, int col);
16+
bool solveHelper(int row, int col);
17+
int board[9][9];
18+
19+
// add extra helper functions here
20+
};
21+
22+
#endif

0 commit comments

Comments
 (0)