-
Notifications
You must be signed in to change notification settings - Fork 0
/
football-game.go
83 lines (73 loc) · 1.8 KB
/
football-game.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
package main
import (
"fmt"
"math/rand"
"time"
)
const (
MaxAttempts = 3
)
type Team struct {
Name string
Offense int
Defense int
}
func (t *Team) OffenseChance() int {
return rand.Intn(t.Offense) + 1
}
func (t *Team) DefenseChance() int {
return rand.Intn(t.Defense) + 1
}
func main() {
rand.Seed(time.Now().UnixNano())
// Set up teams
homeTeam := &Team{
Name: "Home Team",
Offense: 80,
Defense: 60,
}
awayTeam := &Team{
Name: "Away Team",
Offense: 70,
Defense: 50,
}
// Play game
fmt.Printf("%s vs %s\n", homeTeam.Name, awayTeam.Name)
homeScore, awayScore := 0, 0
for i := 1; i <= 4; i++ {
fmt.Printf("\nQuarter %d:\n", i)
for j := 1; j <= MaxAttempts; j++ {
// Home team's possession
homeOffenseChance := homeTeam.OffenseChance()
awayDefenseChance := awayTeam.DefenseChance()
fmt.Printf("\nAttempt %d: %s offense (%d) vs %s defense (%d)\n", j, homeTeam.Name, homeOffenseChance, awayTeam.Name, awayDefenseChance)
if homeOffenseChance > awayDefenseChance {
fmt.Println("Touchdown!")
homeScore += 7
break
} else {
fmt.Println("No score.")
}
// Away team's possession
awayOffenseChance := awayTeam.OffenseChance()
homeDefenseChance := homeTeam.DefenseChance()
fmt.Printf("\nAttempt %d: %s offense (%d) vs %s defense (%d)\n", j, awayTeam.Name, awayOffenseChance, homeTeam.Name, homeDefenseChance)
if awayOffenseChance > homeDefenseChance {
fmt.Println("Touchdown!")
awayScore += 7
break
} else {
fmt.Println("No score.")
}
}
}
// Game over
fmt.Printf("\nFinal score: %s %d, %s %d\n", homeTeam.Name, homeScore, awayTeam.Name, awayScore)
if homeScore > awayScore {
fmt.Println("Home team wins!")
} else if homeScore < awayScore {
fmt.Println("Away team wins!")
} else {
fmt.Println("It's a tie!")
}
}