-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnd.go
286 lines (232 loc) · 6.42 KB
/
dnd.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"fmt"
"time"
"math/rand"
"os/exec"
"log"
)
//enemy object, conatins health, and name values.
type enemy struct {
name string
health int
level int
weapon string
}
//here is the stuff for the player object
type player struct {
name string
level int
health int
weapon string
}
//also we dont need to use the constructor for making a new object
//here is the constructor for making a new player, we only need one player.
func newPlayer(name string) *player {
p := player{name: name}
p.level = 1
p.health = 100
p.weapon = "sword"
return &p
}
//clears screen
func clear() {
cmd := exec.Command("bash", "clear.sh")
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
//to declare a new player object you just declare a new one like so:
//playerObject := player{name: name, health: 100, level: 100, xp: 10}
//to set the parameters of the player object just call it like so:
//playerObject.health = num
//playerObject.level = num
//we only need one player so we should only need to make one of these objects.
func main () {
clear()
var dicknballs string
//playerObject := player{name: "cory", health: 30, level: 1, weapon: "Great Sword"}
enemyObject := enemy{name: "Goblin", health: 30, level: 1,weapon: "mace"}
playerObject := makePlayerSheet()
loop: for {
fmt.Println("dungeons and dragons demo\n0: break loop\n1: combat demo\n")
fmt.Scanln(&dicknballs)
switch dicknballs {
case "0", "break":
break loop
case "1", "combat":
combat(playerObject, enemyObject)
case "2", "scenario":
scenarioOne(playerObject)
}
}
}
func scenarioOne(pc player) {
var choice string
goblinOne := enemy{name: "Cammy the duck faced HobGoblin", health: 30, level: 1,weapon: "mace"}
goblinTwo := enemy{name: "Myles the Goblin", health: 30, level: 1,weapon: "mace"}
fmt.Println("You've been on the triboar trail for about half a day. As you come around a bed,\nyou spot two dead horses, spralwed infront of you, Each being looted by goblins.\nThe woods press close to trail here, dense brush on either side.")
fmt.Println("You have four options:")
fmt.Println("A: Sneak around them (stealth)")
fmt.Println("B: Attack the goblins")
fmt.Println("C: Try to reason with the goblins")
fmt.Println("D: Run away and bail (coward)")
fmt.Scanln(&choice)
switch choice {
case "a", "sneak", "A":
stealthCheck := diceRoll(4)
if (stealthCheck == 1) {
fmt.Println("You successfully sneak around the goblins")
fmt.Println("sadly this is just a demo, so it ends here, :( im lazy")
return
} else {
combat(pc, goblinOne)
combat(pc, goblinTwo)
}
case "b":
fmt.Println("You attack the goblins")
combat(pc, goblinOne)
combat(pc, goblinTwo)
case "c":
fmt.Println("You attempt to reason with the goblins")
fmt.Println("But they're goblins, and you can't reason with halfwits like these. So you are forced into combat")
combat(pc, goblinOne)
combat(pc, goblinTwo)
case "d":
fmt.Println("you fled to safety")
return
}
}
func makePlayerSheet() player {
var name string
var weapon string
var health int = 30
var level int = 1
var wChoice string
fmt.Println("Character name?")
fmt.Scanln(&name)
fmt.Println("weapon of choice:\n1: sword\n2: axe\n3: dagger")
fmt.Scanln(&wChoice)
switch wChoice {
case "1":
weapon = "sword"
case "2":
weapon = "axe"
case "3":
weapon = "dagger"
}
playerObject := player{name: name, health: health, level: level, weapon: weapon}
return playerObject
}
func printPlayerStats(pObject player) {
fmt.Println("name:", pObject.name)
fmt.Println("health:", pObject.health)
fmt.Println("level:", pObject.level)
fmt.Println("weapon:", pObject.weapon)
}
func printEnemyStats(pObject enemy) {
fmt.Println("name:", pObject.name)
fmt.Println("health:", pObject.health)
fmt.Println("level:", pObject.level)
fmt.Println("weapon:", pObject.weapon)
}
func combat(pObject player, eObject enemy) {
clear()
var a string
looper: for {
fmt.Println("0:break\n1:attack\n2:dodge")
fmt.Scanln(&a)
switch a {
case "break", "0", "b":
break looper
case "attack", "a", "1":
fmt.Println("your turn")
//roll the dice
dmg := diceRoll(20)
//does damage
eObject.health -= dmg
//print results
fmt.Println("rolled", dmg)
fmt.Println(eObject.name, ":", eObject.health)
if (eObject.health <= 0) {
fmt.Println("you win, combat over")
break looper
}
//enemies turn right after you attack
fmt.Println("enemy turn")
//roll the dice
enemyDmg := diceRoll(20)
//do damage
pObject.health -= enemyDmg
//print results
fmt.Println("enemie rolled", enemyDmg)
fmt.Println(pObject.name, ":", pObject.health)
if (pObject.health <= 0) {
fmt.Println("You've fallen, darkness consumes you")
break looper
}
//dodging can either result in the enemy missing their attack or you taking 10 dmg
//I belive this 50/50 gamble will make combat more exciting
case "dodge", "2":
roll := diceRoll(20)
if (roll > 10) {
fmt.Println(eObject.name ,"misses")
} else {
pObject.health -= 10
fmt.Println(eObject.name ,"hits you for 10 pts of health")
}
}
}
//fmt.Println("test successful")
}
//abiltycheck will roll the dice and apply the modifier score accordingly.
//to use the function you put your ability score as the modifier int.
func abilityCheck(modifier int) int {
roll := diceRoll(20)
switch modifier {
case 1:
return roll - 5
case 2, 3:
return roll - 4
case 4, 5:
return roll - 3
case 6, 7:
return roll - 2
case 8, 9:
return roll - 1
case 10, 11:
return roll
case 12, 13:
return roll + 1
case 14, 15:
return roll + 2
case 16, 17:
return roll + 3
case 18, 19:
return roll + 4
case 20, 21:
return roll + 5
case 22, 23:
return roll + 6
case 24, 25:
return roll + 7
case 26, 27:
return roll + 8
case 28, 29:
return roll + 9
case 30:
return roll + 10
}
return roll
}
//dice roll will roll a dice of any sides. or in other words, generate a number between 1 and the number of sides.
func diceRoll(sides int) int {
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
a := r1.Intn(sides)
if (a == 0) {
a++
}
return a
}