Skip to content

Commit 87a97c7

Browse files
committed
sudoku
1 parent d71590f commit 87a97c7

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

done.tar

-10 KB
Binary file not shown.

sudoku/insert.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
func IsValidAnswer() string {
11+
var ans string
12+
flag := false
13+
for !flag {
14+
fmt.Scanln(&ans)
15+
ans = strings.ToLower(ans)
16+
if ans == "yes" || ans == "no" {
17+
return ans
18+
}
19+
fmt.Print("Please enter 'yes' or 'no'.")
20+
}
21+
return ans
22+
}
23+
24+
func IsValidInt() int {
25+
var num string
26+
flag := false
27+
var mynum int
28+
for !flag {
29+
fmt.Scanln(&num)
30+
var err error
31+
mynum, err = strconv.Atoi(num)
32+
if err != nil || mynum < 1 || mynum > 9 {
33+
fmt.Print("Please enter a valid number (1-9)")
34+
continue
35+
}
36+
break
37+
}
38+
return mynum
39+
}
40+
41+
func printBoard(board [][]string) {
42+
fmt.Println(".-=-=--=-=-==-=-=-=-=-=-.")
43+
for i, row := range board {
44+
if i == 3 || i == 6 {
45+
fmt.Println("|-------+-------+-------|")
46+
}
47+
fmt.Print("|", " ")
48+
for j, c := range row {
49+
if j == 3 || j == 6 {
50+
fmt.Print("|", " ")
51+
fmt.Printf("%s", c)
52+
fmt.Print(" ")
53+
} else {
54+
fmt.Printf("%s", c)
55+
fmt.Print(" ")
56+
}
57+
}
58+
fmt.Println("|")
59+
}
60+
}
61+
62+
// Function to check the validity of placing 'c' in board
63+
func isValid(board [][]string, row, col int, c rune) bool {
64+
for i := 0; i < 9; i++ {
65+
if board[i][col] == string(c) || board[row][i] == string(c) ||
66+
board[3*(row/3)+i/3][3*(col/3)+i%3] == string(c) {
67+
return false
68+
}
69+
}
70+
return true
71+
}
72+
73+
// Function to solve Sudoku using backtracking
74+
func solve(board [][]string) bool {
75+
for i := 0; i < 9; i++ {
76+
for j := 0; j < 9; j++ {
77+
if board[i][j] == "." {
78+
for c := '1'; c <= '9'; c++ {
79+
if isValid(board, i, j, c) {
80+
board[i][j] = string(c)
81+
if solve(board) {
82+
return true
83+
}
84+
board[i][j] = "."
85+
}
86+
}
87+
return false
88+
}
89+
}
90+
}
91+
return true
92+
}
93+
94+
func main() {
95+
flag := true
96+
board := make([][]string, 9)
97+
for i := range board {
98+
board[i] = make([]string, 9)
99+
}
100+
for i := 0; i < 9; i++ {
101+
for j := 0; j < 9; j++ {
102+
board[i][j] = "."
103+
}
104+
}
105+
args := os.Args[1:] // Read command-line arguments
106+
// Check for exactly 9 arguments
107+
if len(args) != 9 {
108+
fmt.Println("Error: Please provide exactly 9 rows of the Sudoku puzzle as arguments.")
109+
return
110+
}
111+
for i := range board {
112+
for j, c := range args[i] {
113+
if c != '.' && (c < '1' || c > '9') {
114+
fmt.Printf("Invalid character '%c' in row %d.\n", c, i+1)
115+
return
116+
}
117+
board[i][j] = string(c)
118+
}
119+
}
120+
for flag {
121+
printBoard(board)
122+
fmt.Print("Do you want to enter numbers? (yes/no)")
123+
myans := IsValidAnswer()
124+
if myans == "no" {
125+
if solve(board) {
126+
fmt.Println("Sudoku solved successfully:")
127+
printBoard(board)
128+
return
129+
} else {
130+
fmt.Println("No solution exists for the provided Sudoku puzzle")
131+
return
132+
}
133+
}
134+
fmt.Print("Enter the number.")
135+
mynewnum := IsValidInt()
136+
fmt.Print("Enter the row.")
137+
myrow := IsValidInt()
138+
myrow -= 1
139+
fmt.Print("Enter the col.")
140+
mycol := IsValidInt()
141+
mycol -= 1
142+
myres := strconv.Itoa(mynewnum)
143+
board[myrow][mycol] = myres
144+
}
145+
}

0 commit comments

Comments
 (0)