-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
126 lines (112 loc) · 2.8 KB
/
loader.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
package words
import (
"errors"
"fmt"
"math/rand"
"time"
)
type Word struct {
Word string
Sylabes []*Sylabe
AudioFile string
}
type Sylabe struct {
Sylabe string
AudioFile string
}
type Loader interface {
Load() (*GameData, error)
}
type GameData struct {
Words []*Word
Sylabes map[int]*Sylabe
}
type GameDataAccessor interface {
GetRandomWord(exclude []*Word) (*Word, error)
GetUnusedSylabes(amount int, word *Word) ([]*Sylabe, error)
}
func isExcluded(tmpWord *Word, exclude []*Word) bool {
is := false
for i := 0; i < len(exclude) && !is; i++ {
if exclude[i] == tmpWord {
is = true
}
}
return is
}
func (data *GameData) GetRandomWord(exclude []*Word) (*Word, error) {
rand.Seed(time.Now().UnixNano())
var word *Word = nil
length := len(data.Words)
// Atenti: Esto puede ser un loop infinito si todas las palabras están excluídas.
for word == nil {
index := rand.Int() % length
tmpWord := data.Words[index]
is := false
for i := 0; i < len(exclude) && !is; i++ {
if exclude[i] == tmpWord {
is = true
}
}
if !is {
word = tmpWord
}
}
return word, nil
}
// func (data *GameData) GetRandomWord(exclude []*Word) (*Word, error) {
// rand.Seed(time.Now().UnixNano())
// var word *Word = nil
// length := len(data.Words)
// // Atenti: Esto puede ser un loop infinito si todas las palabras están excluídas.
// for word == nil {
// index := rand.Int() % length
// tmpWord := data.Words[index]
// is := false
// for i := 0; i < len(exclude) && !is; i++ {
// if exclude[i] == tmpWord {
// is = true
// }
// }
// if !is {
// word = tmpWord
// }
// }
// return word, nil
// }
func (data *GameData) GetUnusedSylabes(amount int, word *Word) ([]*Sylabe, error) {
// Atenti: Verificar que hay suficientes sílabas
var sylabes []*Sylabe = make([]*Sylabe, amount)
count := 0
length := len(data.Sylabes)
if len(word.Sylabes)+amount > length {
return nil, errors.New(fmt.Sprintf("Not enough sylabes to display. Needed %d, have %d", len(word.Sylabes)+amount, length))
}
for k, v := range data.Sylabes {
fmt.Println(fmt.Sprintf("Sylabe k: %d = %s", k, v.Sylabe))
}
sectionSize := length / amount
fmt.Println(fmt.Sprintf("Section size: %d", sectionSize))
rand.Seed(time.Now().UnixNano())
for count < amount {
offset := sectionSize*count + 1
fmt.Println(fmt.Sprintf("Offset: %d", offset))
rnd := rand.Int() % sectionSize
fmt.Println(fmt.Sprintf("Random number: %d", rnd))
index := offset + rnd
fmt.Println(fmt.Sprintf("Index: %d", index))
tmpSylabe := data.Sylabes[index]
is := false
for i := 0; i < len(word.Sylabes); i++ {
if word.Sylabes[i] == tmpSylabe {
is = true
}
}
if !is {
fmt.Println(fmt.Sprintf("Adding sylabe: %v", tmpSylabe))
sylabes[count] = tmpSylabe
count = count + 1
}
}
return sylabes, nil
}