-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattles.go
102 lines (97 loc) · 2.07 KB
/
battles.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
package main
import (
"math/rand"
"sort"
)
func runBattle(players Players) {
sort.Sort(players)
Output("red", players)
round := 1
numAlive := players.Len()
playerAction := 0
for {
for x := 0; x < players.Len(); x++ {
players[x].Evasion = 0
}
Output("green", "Combat round ", round, " begins...")
for x := 0; x < players.Len(); x++ {
if players[x].Alive != true {
continue
}
playerAction = 0
if !players[x].Npc {
Output("cyan", "What Do you want to do?")
Output("cyan", "\t1 - Run")
Output("cyan", "\t2 - Evade")
Output("cyan", "\t3 - Attack")
UserInput(&playerAction)
}
if playerAction == 2 {
players[x].Evasion = rand.Intn(15)
Output("green", "Evasion set to:", players[x].Evasion)
}
tgt := selectTarget(players, x)
if tgt != -1 {
Output("red", "player: ", x, ", target: ", tgt)
attp1 := players[x].Attack() - players[tgt].Evasion
if attp1 < 0 {
attp1 = 0
}
players[tgt].Health = players[tgt].Health - attp1
if players[tgt].Health <= 0 {
players[tgt].Alive = false
numAlive--
}
Output("green", players[x].Name+" attacks and does ", attp1, " points of damage with his ", Weaps[players[x].Weap].Name, " to the ennemy.")
}
}
if endBattle(players) || playerAction == 1 {
break
} else {
Output("green", players)
round++
}
}
Output("black", players)
Output("green", "Combat is over...")
for x := 0; x < players.Len(); x++ {
if players[x].Alive == true {
Output("cyan", players[x].Name+" is still alive!!!")
}
}
}
func selectTarget(players []Character, x int) int {
y := x
for {
y = y + 1
if y >= len(players) {
y = 0
}
if (players[y].Npc != players[x].Npc) && players[y].Alive {
return y
}
if y == x {
return -1
}
}
return -1
}
func endBattle(players []Character) bool {
count := make([]int, 2)
count[0] = 0
count[1] = 0
for _, pla := range players {
if pla.Alive {
if pla.Npc == false {
count[0]++
} else {
count[1]++
}
}
}
if count[0] == 0 || count[1] == 0 {
return true
} else {
return false
}
}