-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
183 lines (146 loc) · 3.64 KB
/
Copy pathapp.go
File metadata and controls
183 lines (146 loc) · 3.64 KB
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 (
"fmt"
"math/rand"
"strings"
"time"
"unicode/utf8"
)
var i, j int = 0, 3
var countryData = []string{"ghana", "togo", "benin", "kenya", "uganda"}
const lives = 5
// function that gets user input and returns it
func getUserInput() string {
var userInput string
fmt.Println("")
data, err := fmt.Scanf("%s \n", &userInput)
if err != nil || data != 1 {
return "Error"
}
return userInput
}
func getRandomCountry() string {
rand.Seed(time.Now().UnixNano())
min := 0
max := len(countryData)
return countryData[rand.Intn(max-min)]
}
// Find takes a slice and looks for an element in it. If found it will
// return it's key, otherwise it will return -1 and a bool of false.
func Find(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
func getCharaterPositions(char string, word string) []int {
pos := []int{}
for idx, ch := range word {
if char == fmt.Sprintf("%c", ch) {
pos = append(pos, idx)
}
}
return pos
}
func guessedResult(slice []string, word string) []string {
strlen := utf8.RuneCountInString(word)
guessed := make([]string, strlen)
// fill the entire guessed array with "_"
for idx := range guessed {
guessed[idx] = "_"
}
// for each letter in slice get the positions
for i := 0; i < len(slice); i++ {
postions := getCharaterPositions(slice[i], word)
for j := 0; j < len(postions); j++ {
guessed[postions[j]] = slice[i]
}
}
return guessed
}
func countUnique(word string) int {
characters := []string{}
// create an array to store all values characters
for _, ch := range word {
// convert rune to string
s := fmt.Sprintf("%c", ch)
// check if character exists
k, _ := Find(characters, s)
if k == -1 {
// add if yes
characters = append(characters, s)
} else {
// skip if no
continue
}
}
return len(characters)
}
func main() {
usedChars := []string{}
correctGuess := []string{}
incorrectGuess := []string{}
fmt.Println("Welcome to hangman, type 's' to begin")
// get data to start
data := getUserInput()
// if returned data is not s keep asking
for data != "s" {
data = getUserInput()
}
// if data is s
if data == "s" {
fmt.Println("Hangman started")
}
// choose a the guess
guess := getRandomCountry()
// count the unique items
uc := countUnique(guess)
fmt.Println("Guess the country")
for {
fmt.Println(guessedResult(correctGuess, guess))
// user has to enter a value
value := getUserInput()
k, _ := Find(usedChars, value)
if k == -1 {
fmt.Println("")
usedChars = append(usedChars, value)
fmt.Println("")
} else {
fmt.Println("Value already used")
continue
}
if strings.Contains(guess, value) {
correctGuess = append(correctGuess, value) // if guess is correct store it
fmt.Printf(" - %s - is a character of guess", value)
} else {
incorrectGuess = append(incorrectGuess, value) // if guess is correct store it
fmt.Printf(" - %s - is not character of guess", value)
}
fmt.Println("")
fmt.Println("")
fmt.Print("Number of chances left : ")
fmt.Println(lives - len(incorrectGuess))
fmt.Print("Incorrect guessed letters")
fmt.Println(incorrectGuess)
fmt.Print("Correct guesses letters")
fmt.Println(correctGuess)
if len(incorrectGuess) == lives {
break
}
if len(correctGuess) == uc {
break
}
}
if len(incorrectGuess) == lives {
fmt.Println("")
fmt.Println("----------------You died----------------")
fmt.Printf("The country was %s", guess)
}
if len(correctGuess) == uc {
fmt.Println("")
fmt.Println("----------------Well done----------------")
fmt.Printf("The country was %s", guess)
}
}