-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (145 loc) · 4.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"Sudoku-GO/solver"
"fmt"
"html/template"
"net/http"
"os"
"strconv"
"strings"
)
// data structure for the main Sudoku Grid
// Done Boolean value is used for building the HTML table that
// displays the grid
type Sudoku struct {
Done bool
Value [9]int
Solved [9]bool
Solution [9]int
ID [9]string
}
// Data structure to display the value for each cell i.e A1 to I9
type CellID struct {
Row int
Col int
}
// function main starts the application server. An empty puzzle is used to
// build the HTML game template - so that fromatting is correct
func main() {
tmpl := template.Must(template.ParseFiles("template/GUI.html"))
var b = Solver.GetEmptyPuzzle()
sudoku := arrayToData(b)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl.Execute(w, struct{ Sudokus [9]Sudoku }{sudoku})
})
//file server for handling static files
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/favicon.ico", faviconHandler)
http.HandleFunc("/game/", gameHandler)
http.HandleFunc("/killer/", killerHandler)
http.ListenAndServe(":"+os.Getenv("PORT"), nil)
// http.ListenAndServe(":8080", nil)
}
// Simple funtion to serve the favicon
func faviconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "/static/favicons/favicon.ico")
}
// the game handler will recieve information from the server with the requested difficulty level
// as a string
// The handler will then get a newly generated puzzle from the solver package and then
// solve it.
// The template will then be rendered appropriately
func gameHandler(w http.ResponseWriter, r *http.Request) {
var optionSelected string
r.ParseForm()
fmt.Println(r.Form) // print information on server side.
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("val:", strings.Join(v, ""))
optionSelected = strings.Join(v, " ")
}
fmt.Println(optionSelected)
var b = Solver.GetPuzzle(optionSelected)
var solvedArray = Solver.NewSolver(b, true)
sudoku := getUnsolved(b, solvedArray)
t := template.Must(template.ParseFiles("template/easy_game.html"))
t, _ = t.ParseFiles("template/easy_game.html")
t.Execute(w, struct{ Sudokus [9]Sudoku }{sudoku})
}
// Function handler for the Killer template - needs updated
func killerHandler(w http.ResponseWriter, r *http.Request) {
var b = Solver.GetPuzzle("")
fmt.Println(b)
var solvedArray = Solver.NewSolver(b, true)
fmt.Println(solvedArray)
// sudoku := arrayToData(solvedArray)
sudoku := getUnsolved(b, solvedArray)
t := template.Must(template.ParseFiles("template/killer.html"))
t, _ = t.ParseFiles("template/killer.html")
t.Execute(w, struct{ Sudokus [9]Sudoku }{sudoku})
}
func arrayToData(solved [9][9]int) [9]Sudoku {
solvedSudoku := [9]Sudoku{}
for i := 0; i < 9; i++ {
for j := 0; j < 9; j++ {
solvedSudoku[i].Value[j] = solved[i][j]
solvedSudoku[i].Solved[j] = true
if i == 3 || i == 6 {
solvedSudoku[i].Done = true
} else {
solvedSudoku[i].Done = false
}
}
}
return solvedSudoku
}
// This function is to set the variables in the Sudoku data Structure so that
// a Sudoku grid has both the value and solutions
// This can then be used appropriately for the Client side logic to control the game
// This function will also fill the CellId data structure with value coresponding to the Cell
// The method returns a Completed Sudoku array
func getUnsolved(unsolved [9][9]int, solved [9][9]int) [9]Sudoku {
solvedSudoku := [9]Sudoku{}
for i := 0; i < 9; i++ {
for j := 0; j < 9; j++ {
if unsolved[i][j] != 0 {
solvedSudoku[i].Value[j] = solved[i][j]
} else {
solvedSudoku[i].Solution[j] = solved[i][j]
}
var f string
// switch statement to set correct Cell ID
switch i {
case 0:
f = "A"
case 1:
f = "B"
case 2:
f = "C"
case 3:
f = "D"
case 4:
f = "E"
case 5:
f = "F"
case 6:
f = "G"
case 7:
f = "H"
case 8:
f = "I"
}
s := strconv.Itoa(j + 1)
solvedSudoku[i].ID[j] = f + s
// The done variable is used to identify in the template the boundaries of the minigrids for the
// thicker border
if i == 3 || i == 6 {
solvedSudoku[i].Done = true
} else {
solvedSudoku[i].Done = false
}
}
}
return solvedSudoku
}