57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
)
|
|
|
|
func setupGhostTypes() {
|
|
var c []Spookster
|
|
confFile, _ := ioutil.ReadFile("ghosts.json")
|
|
err := json.Unmarshal([]byte(confFile), &c)
|
|
if err != nil {
|
|
return
|
|
}
|
|
ghostTypes = c
|
|
fmt.Printf("Loaded %+v ghost types\n", len(ghostTypes))
|
|
}
|
|
|
|
func (g *Game) EliminateGhosts(newEvidence int) {
|
|
var types []Spookster
|
|
var evidence []int
|
|
for _, v := range g.PossibleGhosts {
|
|
possible := false
|
|
for _, e := range v.Evidence {
|
|
if e == newEvidence {
|
|
possible = true
|
|
}
|
|
}
|
|
if possible {
|
|
types = append(types, v)
|
|
evidence = append(evidence, v.Evidence...)
|
|
}
|
|
}
|
|
g.PossibleGhosts = types
|
|
g.PossibleEvidence = removeDuplicateValues(evidence)
|
|
|
|
games[g.Identifier] = *g
|
|
}
|
|
|
|
// https://www.geeksforgeeks.org/how-to-remove-duplicate-values-from-slice-in-golang/
|
|
func removeDuplicateValues(intSlice []int) []int {
|
|
keys := make(map[int]bool)
|
|
list := []int{}
|
|
|
|
// If the key(values of the slice) is not equal
|
|
// to the already present value in new slice (list)
|
|
// then we append it. else we jump on another element.
|
|
for _, entry := range intSlice {
|
|
if _, value := keys[entry]; !value {
|
|
keys[entry] = true
|
|
list = append(list, entry)
|
|
}
|
|
}
|
|
return list
|
|
}
|