-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.go
More file actions
79 lines (61 loc) · 1.5 KB
/
deck.go
File metadata and controls
79 lines (61 loc) · 1.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
package main
import (
"fmt"
"math/rand"
)
type card struct {
attack int
defence int
}
type deck []card
func (d deck) showDeck() {
fmt.Println()
for i, card := range d {
fmt.Printf("[%d] Attack: [%2d] | Defence: [%2d]\n", i+1, card.attack, card.defence)
}
}
func newCard() card {
attack := rand.Intn(15) + 1
defence := rand.Intn(15) + 1
newCard := card{attack: attack, defence: defence}
return newCard
}
func (d *deck) initializeUltimateDeck() {
for i := 0; i < 50; i++ {
*d = append(*d, newCard())
}
}
func (d deck) generateHand(handSize int) (deck, deck) {
range1 := rand.Intn(50 - handSize)
range2 := rand.Intn(50 - handSize)
return d[range1 : range1+handSize], d[range2 : range2+handSize]
}
func (d deck) removeCardByIndex(index int) (deck, card) {
removed := d[index]
copy(d[index:], d[index+1:])
//d[len(d)-1] =
d = d[:len(d)-1]
return d, removed
}
func playGame(card1 card, card2 card, gameMode string) int {
fmt.Printf("\nYou chose: Attack: [%2d] | Defence: [%2d]\n", card1.attack, card1.defence)
fmt.Printf("Your opponent chose: Attack: [%2d] | Defence: [%2d]\n", card2.attack, card2.defence)
var value1 int
var value2 int
if gameMode == "A" || gameMode == "a" {
value1 = card1.attack
value2 = card2.defence
} else {
value1 = card1.defence
value2 = card2.attack
}
if value1 > value2 {
fmt.Println("\nYou won this round!")
return 1
} else if value1 == value2 {
fmt.Println("\nIt's a draw!")
return 0
}
fmt.Println("\nYou lost this round!")
return -1
}