-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboxer.go
45 lines (37 loc) · 1.09 KB
/
boxer.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
package main
import (
"math/rand"
)
type Boxer struct {
Name string `json:"name"`
Health int64 `json:"health"`
AttackedBodyPart BodyPart `json:"attacked_body_part"`
BlockedBodyPart BodyPart `json:"blocked_body_part"`
}
func getTagetBodyPart() BodyPart {
bodyPart := BodyParts[rand.Intn(3)]
return bodyPart
}
func (boxer Boxer) Attack(attackedBoxer *Boxer) (BodyPart, BodyPart) {
attackedBodyPart, attackedPower := boxer.Hit()
blockedBodyPart, blockedPower := attackedBoxer.Block()
powerDiff := max(attackedPower-blockedPower, 0)
if attackedBodyPart != blockedBodyPart {
attackedBoxer.Health = attackedBoxer.Health - attackedPower
} else {
attackedBoxer.Health = attackedBoxer.Health - powerDiff
}
return attackedBodyPart, blockedBodyPart
}
func (boxer Boxer) Hit() (BodyPart, int64) {
bodyPart := getTagetBodyPart()
return bodyPart, 1
}
func (boxer Boxer) Block() (BodyPart, int64) {
bodyPart := getTagetBodyPart()
return bodyPart, 2
}
func CreateBoxer(name string, health int64) Boxer {
newBoxer := Boxer{Name: name, Health: health}
return newBoxer
}