-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (122 loc) · 2.42 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
package main
import (
"fmt"
"math/rand"
"regexp"
"strconv"
"strings"
"time"
"unicode"
)
var (
words []string
board []string
posBoard int
chance int
selectWord string
userWrite string
wrongWord string
isLooser bool
out []string
)
const clearTerminal = "\033[2J"
func createBoard() {
re := regexp.MustCompile(`[\d]`)
steps := []string{"O", "|", "/", "\\", "/", "\\"}
base := `
+----+
| |
1 |
324 |
5 6 |
|
==========`
board = make([]string, len(steps)+1)
board[0] = re.ReplaceAllString(base, " ")
for i, s := range steps {
base = strings.Replace(base, strconv.Itoa(i+1), s, 1)
currentBase := re.ReplaceAllString(base, " ")
board[i+1] = currentBase
}
}
func sortWord() {
words = []string{"bird", "happy", "soccer", "computer"}
rand.Seed(time.Now().UnixNano())
selectWord = words[rand.Intn(len(words)-1)+1]
}
func showBoard(idx int) {
if idx < len(board) {
print(clearTerminal)
fmt.Println(board[idx])
} else {
isLooser = true
}
}
func usedLetter(word, opt string) bool {
for _, v := range word {
if v == []rune(opt)[0] {
return true
}
}
return false
}
func operations(ch chan int, opt string) {
countWord := strings.Count(selectWord, opt)
if strings.Contains(selectWord, opt) && (strings.Count(wrongWord, opt) >= countWord ||
strings.Count(userWrite, opt) >= countWord) || usedLetter(wrongWord, opt) || usedLetter(userWrite, opt) {
chance--
posBoard++
wrongWord = wrongWord + opt
} else {
for i, v := range selectWord {
if v == []rune(opt)[0] {
out[i] = opt
}
}
}
userWrite = userWrite + opt
showBoard(posBoard)
ch <- 1
}
func userLooser() bool {
return (chance == 0 || isLooser == true) && strings.Join(out, "") != selectWord
}
func run() {
var input string
ch := make(chan int, 1)
chance = 6
posBoard = 0
isLooser = false
out = make([]string, len(selectWord))
for i := range selectWord {
out[i] = "_"
}
fmt.Println(selectWord, "\n", board[posBoard])
for {
fmt.Println("Word: ", out)
if userLooser() {
fmt.Println("You are a loser...")
return
}
if strings.Join(out, "") == selectWord {
fmt.Println("You are a winner...")
return
}
fmt.Scanln(&input)
if len(input) == 1 && unicode.IsLetter([]rune(input)[0]) {
operations(ch, input)
<-ch
} else {
if input == "exit" {
return
}
fmt.Println("parameter not permission")
}
input = ""
}
}
func main() {
sortWord()
createBoard()
run()
}