Give each function its own file.

This commit is contained in:
2025-01-28 18:12:03 +01:00
parent a314e945ed
commit b83d6bdde4
20 changed files with 411 additions and 386 deletions

View File

@ -2,7 +2,6 @@ package flags
import (
"flag"
"fmt"
"log"
"os"
"runtime"
@ -83,72 +82,3 @@ func (flags *Flags) ParseFlags() {
}
}
// Validate if a row is properly set.
// This check for:
// - Correct length
// - Correct numbers
// - Numbers only present once
func (flags *Flags) validateRow(name string, row string) {
var found bool
var double bool
count := make(map[rune]int)
// 1. Make sure the row is 9 in length
if len(row) != 9 {
log.Printf("ERROR: Invalid length of %s (%s), must be 9 numbers\n\n", name, row)
flags.printUsage()
os.Exit(1)
}
// 2. Ensure all digits are numbers
for _, value := range row {
found = flags.validChar(value)
}
if !found {
log.Printf("ERROR: Invalid character of %s (%s), must be 9 numbers\n\n", name, row)
flags.printUsage()
os.Exit(1)
}
// 3. Ensure all digits (except zero) are there only once
for _, digits := range row {
count[digits] = count[digits] + 1
}
for key, value := range count {
if value > 1 && key != 48 {
double = true
}
}
if double {
log.Printf("ERROR: Double character of %s (%s), numbers between 1 and 9 may only be entered once\n\n", name, row)
flags.printUsage()
os.Exit(1)
}
}
// Validate if the char provided is 0-9
func (flags *Flags) validChar(char rune) (valid bool) {
decvals := [10]int{48, 49, 50, 51, 52, 53, 54, 55, 56, 57}
for _, value := range decvals {
if char == rune(value) {
valid = true
}
}
return
}
// Print help information for the end-user
func (flags *Flags) printUsage() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "\nPut every row of a Sudoku puzzle as paramters.\nUse '0' for what is currently blank in the puzzle you wish to solve.\n\n")
fmt.Fprintf(flag.CommandLine.Output(), "Example: %s -row1 ... -row2 ... -row3 ... (etc)\n\n", os.Args[0])
flag.PrintDefaults()
}

28
flags/printUsage.go Normal file
View File

@ -0,0 +1,28 @@
package flags
import (
"flag"
"fmt"
"os"
)
// Validate if the char provided is 0-9
func (flags *Flags) validChar(char rune) (valid bool) {
decvals := [10]int{48, 49, 50, 51, 52, 53, 54, 55, 56, 57}
for _, value := range decvals {
if char == rune(value) {
valid = true
}
}
return
}
// Print help information for the end-user
func (flags *Flags) printUsage() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "\nPut every row of a Sudoku puzzle as paramters.\nUse '0' for what is currently blank in the puzzle you wish to solve.\n\n")
fmt.Fprintf(flag.CommandLine.Output(), "Example: %s -row1 ... -row2 ... -row3 ... (etc)\n\n", os.Args[0])
flag.PrintDefaults()
}

54
flags/validateRow.go Normal file
View File

@ -0,0 +1,54 @@
package flags
import (
"log"
"os"
)
// Validate if a row is properly set.
// This check for:
// - Correct length
// - Correct numbers
// - Numbers only present once
func (flags *Flags) validateRow(name string, row string) {
var found bool
var double bool
count := make(map[rune]int)
// 1. Make sure the row is 9 in length
if len(row) != 9 {
log.Printf("ERROR: Invalid length of %s (%s), must be 9 numbers\n\n", name, row)
flags.printUsage()
os.Exit(1)
}
// 2. Ensure all digits are numbers
for _, value := range row {
found = flags.validChar(value)
}
if !found {
log.Printf("ERROR: Invalid character of %s (%s), must be 9 numbers\n\n", name, row)
flags.printUsage()
os.Exit(1)
}
// 3. Ensure all digits (except zero) are there only once
for _, digits := range row {
count[digits] = count[digits] + 1
}
for key, value := range count {
if value > 1 && key != 48 {
double = true
}
}
if double {
log.Printf("ERROR: Double character of %s (%s), numbers between 1 and 9 may only be entered once\n\n", name, row)
flags.printUsage()
os.Exit(1)
}
}