-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (124 loc) · 3.33 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
package main
import (
"fmt"
"math/rand"
"time"
"github.com/briandowns/spinner"
"github.com/evilsocket/islazy/log"
"github.com/manifoldco/promptui"
"github.com/moorada/lelouch/dictionary"
)
const Select = "Seleziona modalità"
const UpdateDictionaries = "Aggiorna i dizionari"
const GameWordToNumber = "Gioco: Converti in numero"
const GameNumberToWord = "Gioco: Converti in parole"
const GameMix = "Gioco: Misto"
const Converter = "Converti numeri o parole"
const simpleLevel = "Sono all'inizio"
const mediumLevel = "Sono pratico con la conversione fonetica"
const extremeLevel = "Sono un convertitore fonetico vivente"
const Stats = "Statistiche"
const MostraTabellaCompleta = "Visualizza tabella completa"
const Continua = "Continua"
const MenuPrincipale = "Torna al menu' principale"
const charsetNumber = "0123456789"
const MostraSoluzioni = "Mostra soluzioni"
const (
simplelevelint = 5
mediumlevelint = 10
extremelevelint = 20
)
var completeDictionary dictionary.AS
var commonDictionary dictionary.AS
var levelGame = simpleLevel
func main() {
rand.Seed(time.Now().UnixNano())
var err1, err2 error
err1, completeDictionary = dictionary.GetCompleteDictionary()
err2, commonDictionary = dictionary.GetCommonDictionary()
if err1 != nil || err2 != nil {
loadingDictionaries()
err1, completeDictionary = dictionary.GetCompleteDictionary()
err2, commonDictionary = dictionary.GetCommonDictionary()
if err1 != nil || err2 != nil {
log.Fatal("Error to get dictionaries")
}
}
start()
}
func start() {
for {
prompt := promptui.Select{
Label: Select,
Items: []string{Converter, GameWordToNumber, GameNumberToWord, GameMix},
}
_, result, err := prompt.Run()
if err != nil {
log.Fatal("Error: %s", err)
return
}
switch result {
case Converter:
convert()
case GameWordToNumber:
gamePhraseWN()
case GameNumberToWord:
gamePhraseNW()
case GameMix:
gameMix()
}
}
}
func loadingDictionaries() {
s := spinner.New(spinner.CharSets[26], 100*time.Millisecond)
s.Prefix = "Aggiornamento dizionari in corso "
s.FinalMSG = "Aggiornamento completato"
s.Start()
dictionary.MakeDictionaries()
s.Stop()
}
func stats() {
var numberOfWords int
for _, ws := range completeDictionary {
numberOfWords += len(ws)
}
fmt.Println("Nel dizionario di tutte le parole ci sono", numberOfWords, "parole")
numberOfWords = 0
for _, ws := range commonDictionary {
numberOfWords += len(ws)
}
fmt.Println("Nel dizionario delle parole comuni ci sono", numberOfWords, "parole")
}
func chooseLevelGame() {
prompt := promptui.Select{
Label: "Seleziona il livello",
Items: []string{simpleLevel, mediumLevel, extremeLevel},
}
_, result, err := prompt.Run()
if err != nil {
log.Fatal("Error: %s", err)
}
levelGame = result
}
func getLevelGame(result string) int {
switch result {
case simpleLevel:
randomNumber := rand.Intn(simplelevelint) + 1
return randomNumber
case mediumLevel:
randomNumber := rand.Intn(mediumlevelint) + simplelevelint
return randomNumber
default:
randomNumber := rand.Intn(extremelevelint) + mediumlevelint
return randomNumber
}
}
func stringWithCharset(length int, charset string) string {
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}