validateRow.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package flags
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. // Validate if a row is properly set.
  7. // This check for:
  8. // - Correct length
  9. // - Correct numbers
  10. // - Numbers only present once
  11. func (flags *Flags) validateRow(name string, row string) {
  12. var found bool
  13. var double bool
  14. count := make(map[rune]int)
  15. // 1. Make sure the row is 9 in length
  16. if len(row) != 9 {
  17. fmt.Printf("ERROR: Invalid length of %s (%s), must be 9 numbers\n\n", name, row)
  18. flags.printUsage()
  19. os.Exit(1)
  20. }
  21. // 2. Ensure all digits are numbers
  22. for _, value := range row {
  23. found = flags.validChar(value)
  24. }
  25. if !found {
  26. fmt.Printf("ERROR: Invalid character of %s (%s), must be 9 numbers\n\n", name, row)
  27. flags.printUsage()
  28. os.Exit(1)
  29. }
  30. // 3. Ensure all digits (except zero) are there only once
  31. for _, digits := range row {
  32. count[digits] = count[digits] + 1
  33. }
  34. for key, value := range count {
  35. if value > 1 && key != 48 {
  36. double = true
  37. }
  38. }
  39. if double {
  40. fmt.Printf("ERROR: Double character of %s (%s), numbers between 1 and 9 may only be entered once\n\n", name, row)
  41. flags.printUsage()
  42. os.Exit(1)
  43. }
  44. }