-
Notifications
You must be signed in to change notification settings - Fork 0
/
war.go
178 lines (149 loc) · 4.39 KB
/
war.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"fmt"
"math/rand"
"time"
)
type Card struct {
// a.e. QH = Queen of Hearts
name string
// Numberic value to easily compare cards (ignoring suites and queens and kings etc. converted to integer)
value int
}
type Game struct {
playersDecks [][]Card
// Cards which are on the table
tableDeck []Card
// Next round is dummy (when we take cards, but don't compare them)
dummyRound bool
// Meta-data used for the analysis
roundsCount int
state int
}
var suites = []string{"H", "S", "C", "D"}
var ranks = []string{"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}
var randomSource = rand.New(rand.NewSource(time.Now().Unix()))
const (
Ok = iota
RoundCompletedDraw = iota
GameCompletedFirstPlayerWon = iota
GameCompletedSecondPlayerWon = iota
GameCompletedDraw = iota
)
func generateSortedDeck() []Card {
deck := make([]Card, 0, len(ranks)*len(suites))
for _, suite := range suites {
for index, rank := range ranks {
deck = append(deck, Card{rank + suite, index + 2})
}
}
return deck
}
func shuffleDeck(deck []Card) []Card {
for n := len(deck); n > 0; n-- {
randIndex := randomSource.Intn(n)
deck[n-1], deck[randIndex] = deck[randIndex], deck[n-1]
}
return deck
}
func splitDeck(deck []Card, cardsInFirstDeck int) [][]Card {
decks := make([][]Card, 2)
decks[0] = make([]Card, 0, 54)
decks[1] = make([]Card, 0, 54)
for i := 0; i < len(deck); i++ {
if i < cardsInFirstDeck {
decks[0] = append(decks[0], deck[i])
} else {
decks[1] = append(decks[1], deck[i])
}
}
return decks
}
func createNewGame(cardsForFirstPlayer int) Game {
game := Game{state: Ok, dummyRound: false}
deck := shuffleDeck(generateSortedDeck())
game.playersDecks = splitDeck(deck, cardsForFirstPlayer)
game.tableDeck = make([]Card, 0, len(deck))
return game
}
func pickTopCards(playersDecks [][]Card) (player1Card Card, player2Card Card, gameResult int) {
// Check whether decks are empty
player1DeckIsEmpty := len(playersDecks[0]) == 0
player2DeckIsEmpty := len(playersDecks[1]) == 0
if player1DeckIsEmpty && player2DeckIsEmpty {
return Card{}, Card{}, GameCompletedDraw
}
if player1DeckIsEmpty {
return Card{}, Card{}, GameCompletedSecondPlayerWon
}
if player2DeckIsEmpty {
return Card{}, Card{}, GameCompletedFirstPlayerWon
}
player1Card = playersDecks[0][0]
playersDecks[0] = playersDecks[0][1:]
player2Card = playersDecks[1][0]
playersDecks[1] = playersDecks[1][1:]
return player1Card, player2Card, Ok
}
func winnerTakesAll(game *Game, winnerIndex int) {
// Shuffle cards which we take (so we don't end up with infinite game cycles)
game.playersDecks[winnerIndex] = append(game.playersDecks[winnerIndex], shuffleDeck(game.tableDeck)...)
// Clear table deck
game.tableDeck = game.tableDeck[:0]
}
func isGameComplete(result int) bool {
return result == GameCompletedDraw || result == GameCompletedFirstPlayerWon || result == GameCompletedSecondPlayerWon
}
func playMove(game *Game) {
player1Card, player2Card, result := pickTopCards(game.playersDecks)
if isGameComplete(result) {
game.state = result
return
}
game.tableDeck = append(game.tableDeck, player1Card, player2Card)
if game.dummyRound {
game.dummyRound = false
} else if player1Card.value > player2Card.value {
winnerTakesAll(game, 0)
} else if player2Card.value > player1Card.value {
winnerTakesAll(game, 1)
} else {
game.dummyRound = true
}
}
func playGame(game *Game) {
for true {
playMove(game)
if isGameComplete(game.state) {
break
}
game.roundsCount = game.roundsCount + 1
}
}
func main() {
cardsForFirstPlayerDeck := len(suites) * len(ranks) / 2
gamesCount := 0
totalRounds := 0
totalFirstPlayerWins := 0
for true {
game := createNewGame(cardsForFirstPlayerDeck)
playGame(&game)
// Calculate statistics
gamesCount = gamesCount + 1
totalRounds = totalRounds + game.roundsCount
if game.state == GameCompletedFirstPlayerWon {
totalFirstPlayerWins = totalFirstPlayerWins + 1
}
if game.state == GameCompletedDraw {
fmt.Println("Game ", gamesCount, " Whoaaaa. It's a draw. ")
}
// Print statistics
if gamesCount%100000 == 0 {
fmt.Println()
fmt.Println("Game ", gamesCount, "is done")
fmt.Println("Statistics:")
fmt.Println("Average moves per game:", float64(totalRounds)/float64(gamesCount))
fmt.Println("First player wins in ", float64(totalFirstPlayerWins)/float64(gamesCount))
}
}
}