-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
103 lines (87 loc) · 2.27 KB
/
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"time"
)
// Player is representation for a person and his score
type Player struct {
name string
score int
}
// Game is representation for the game
type Game struct {
name string
round int
Point
}
// Point is representation of the point rule
type Point struct {
plus int
minus int
}
// Activity is the Interface for Games Activity
type Activity interface {
showHeader()
play(playerTurn int) Player
calculateScore(rolledDice int, score *int) int
}
func rollingDice() int {
rand.Seed(time.Now().UnixNano())
var dice = []int{1, 2, 3, 4, 5, 6}
return dice[rand.Intn(len(dice))]
}
func (game Game) showHeader() {
fmt.Printf("====================================\n")
fmt.Printf("Welcome to %s Game\n", game.name)
fmt.Printf("====================================\n\n")
}
func (game Game) calculateScore(rolledDice int, score *int) int {
if rolledDice%2 == 0 {
*score -= game.minus
} else {
*score += game.plus
}
return *score
}
func (game Game) play(playerTurn int) Player {
scanner := bufio.NewScanner(os.Stdin)
var score int = 0
fmt.Printf("\nPlayer %d, Input your name : ", playerTurn)
scanner.Scan()
playerName := scanner.Text()
for currentRound := 1; currentRound <= game.round; currentRound++ {
fmt.Printf("\nRound %d.\nOkay %s, press enter to roll the dice", currentRound, playerName)
scanner.Scan()
rolledDice := rollingDice()
fmt.Printf("You've got %d\n", rolledDice)
game.calculateScore(rolledDice, &score)
}
fmt.Printf("====================================\n")
fmt.Printf("Your score is : %d\n", score)
fmt.Printf("====================================\n")
// Create a Player
var currentPlayer Player
currentPlayer.name = playerName
currentPlayer.score = score
return currentPlayer
}
func viewLeaderboards() {
fmt.Printf("\n----- Leaderboards -----\n")
for i, leaderboard := range leaderboards {
fmt.Printf("%d. %s with Score: %d\n", i+1, leaderboard.name, leaderboard.score)
}
}
func sortingLeaderboards() {
for i := 0; i < len(leaderboards); i++ {
for j := 0; j < len(leaderboards)-i; j++ {
if j < len(leaderboards)-1 && leaderboards[j].score < leaderboards[j+1].score {
temp := leaderboards[j]
leaderboards[j] = leaderboards[j+1]
leaderboards[j+1] = temp
}
}
}
}