This repository was archived by the owner on Nov 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.go
More file actions
101 lines (92 loc) · 2.5 KB
/
Copy pathplayer.go
File metadata and controls
101 lines (92 loc) · 2.5 KB
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
package main
import (
"fmt"
"slices"
)
type player struct {
dice [][]int // Current decomposition of bones
points int // Total points (current)
name string // Player name (entered at the beginning of the game)
deskPosition bool // Displays in the player console from above or below
}
// calcPoints player score calculation
func (p *player) calcPoints() {
points := 0
for column := range p.dice {
sliceCopy := getUniqueElements(p.dice[column]) // necessary to eliminate repetitions in order to correctly calculate the multipliers
for _, value := range sliceCopy {
factor := countValuesInArray(value, p.dice[column])
switch factor {
case 3:
points += (value * 3) * 3
case 2:
points += (value * 2) * 2
case 1:
points += value
}
}
}
p.points = points
}
// reCalcDice recalculates dice after opponent's move
func (p *player) reCalcDice(column int, opponentColumnDice []int) {
for idx, value := range p.dice[column] {
if slices.Contains(opponentColumnDice, value) {
p.dice[column][idx] = 0
}
}
}
// diceIsFull determines that the game is over -> the player has all fields inside the dice filled after the move
func (p *player) diceIsFull() bool {
for column := range p.dice {
if slices.Contains(p.dice[column], 0) {
return false
}
}
return true
}
// getAvailableColumns get a list of writable columns
func (p *player) getAvailableColumns() (res []int) {
for column := range p.dice {
if slices.Contains(p.dice[column], 0) {
res = append(res, column)
}
}
return
}
// printPlayerFields prints the player's field according to his parameters to the console
func (p *player) printPlayerFields() {
fmt.Printf("%s%s%s\n", Magenta, p.name, Cyan)
for column := range p.dice {
for row := range p.dice[column] {
var state int
if p.deskPosition {
// "2 - column": since we output the matrix of the second player
// in the direction to the matrix of the first player
state = p.dice[row][2-column]
} else {
state = p.dice[row][column]
}
if state != 0 {
fmt.Printf("%d", state)
} else {
fmt.Print(" ")
}
fmt.Print(" ")
}
fmt.Print("\n")
}
fmt.Printf("%s%sScore: %d%s\n", Reset, Magenta, p.points, Reset)
}
// dropDiceNumbers replaces empty bottom cells with dice
func (p *player) dropDiceNumbers() {
for i := 0; i <= 2; i++ {
p.dice[i] = removeZeros(p.dice[i])
}
}
func (p *player) scanNameOfPlayer() {
_, err := fmt.Scanf("%s\n", &p.name)
if err != nil {
fmt.Println("Error: " + err.Error())
}
}