-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjass.go
347 lines (298 loc) · 7.4 KB
/
jass.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"fmt"
"math/rand"
"sort"
)
type card struct {
color int
figure int
isPlayed bool
}
type player struct {
id int
hand []card
}
type game struct {
points1 int
points2 int
}
type round struct {
trump int
points1 int
points2 int
startingPlayer int
}
type stich struct {
cardsPlayed []card
startingPlayer int
points int
winningPlayer int
}
func createCardStack() [36]card {
var cardStack [36]card
for color := 0; color < 4; color++ {
for figure := 0; figure < 9; figure++ {
cardStack[color*9+figure] = card{color, figure, false}
}
}
return cardStack
}
func shuffleCardStack(cardStack [36]card) [36]card {
for i := 0; i < 1000; i++ {
rand.Shuffle(36, func(i, j int) {
cardStack[i], cardStack[j] = cardStack[j], cardStack[i]
})
}
return cardStack
}
func distributeCards(shuffledStack [36]card) [4]player {
var players [4]player
var j int
var hand []card
for i := 0; i < 4; i++ {
j = i * 9
hand = shuffledStack[0+j : 9+j]
players[i] = player{i, hand}
}
return players
}
func sortCards(players [4]player) {
for i := 0; i < 4; i++ {
sort.Slice(players[i].hand, func(j, k int) bool { return players[i].hand[j].figure < players[i].hand[k].figure })
sort.Slice(players[i].hand, func(j, k int) bool { return players[i].hand[j].color < players[i].hand[k].color })
}
}
func getColor(color int) string {
switch {
case color == 0:
return "Eicheln"
case color == 1:
return "Rosen"
case color == 2:
return "Schellen"
case color == 3:
return "Schilten"
case color == 4:
return "Obe"
case color == 5:
return "Unde"
}
return ""
}
func getFigure(figure int) string {
switch {
case figure == 0:
return "6"
case figure == 1:
return "7"
case figure == 2:
return "8"
case figure == 3:
return "9"
case figure == 4:
return "10"
case figure == 5:
return "Under"
case figure == 6:
return "Ober"
case figure == 7:
return "König"
case figure == 8:
return "Ass"
}
return ""
}
func getCardName(cardInfo card) string {
var name string
name = getColor(cardInfo.color)
return name + " " + getFigure(cardInfo.figure)
}
func showCards(player player) {
fmt.Println("This are the cards of player", player.id, ":")
for i := 0; i < 9; i++ {
if player.hand[i].isPlayed == false {
fmt.Println(i, getCardName(player.hand[i]))
}
}
}
func getTrump(players [4]player, player int, geschoben bool) int {
var trump int
var text string
var max int
showCards(players[player])
trump = -1
text = "What should be trump:\n0:Eicheln\n1:Rosen\n2:Schellen\n3:Schilten\n4:Oben\n5:Unten"
if geschoben {
max = 5
} else {
max = 6
text += "\n6:Schiben"
}
for trump < 0 || trump > max {
fmt.Println(text)
fmt.Scanln(&trump)
}
if trump == 6 {
trump = getTrump(players, (player+2)%4, true)
} else {
fmt.Println(getColor(trump), "is trump")
}
return trump
}
func playCards(currentStich stich, players [4]player) stich {
var currentCardNumb int
var currentCard card
var currentPlayer player
for i := 0; i < 4; i++ {
currentPlayer = players[(currentStich.startingPlayer+i)%4]
showCards(currentPlayer)
currentCardNumb = -1
for currentCardNumb < 0 || currentCardNumb > 8 {
fmt.Println("What card to play: ")
fmt.Scanln(¤tCardNumb)
}
if currentPlayer.hand[currentCardNumb].isPlayed == true {
i -= 1
// TODO also check for undertrumpfe and color ageh
} else {
currentCard = currentPlayer.hand[currentCardNumb]
currentPlayer.hand[currentCardNumb].isPlayed = true
fmt.Println(getCardName(currentCard), "is played")
currentStich.cardsPlayed = append(currentStich.cardsPlayed, currentCard)
}
}
return currentStich
}
func isHigherCard(card1 card, card2 card, trump int, outcardColor int) (card card) {
if card1.color == trump && card2.color == trump {
if card1.figure == 5 {
return card1
} else if card2.figure == 5 {
return card2
} else if card1.figure == 3 {
return card1
} else if card2.figure == 3 {
return card2
} else if card1.figure > card2.figure {
return card1
} else {
return card2
}
} else if card1.color == trump {
return card1
} else if card2.color == trump {
return card2
} else if card1.color == outcardColor && card2.color == outcardColor {
if (card1.figure > card2.figure && trump != 5) || (card1.figure < card2.figure && trump == 5) {
return card1
} else {
return card2
}
} else if card1.color == outcardColor {
return card1
} else if card2.color == outcardColor {
return card2
}
return card1
}
func countCard(currentCard card, trump int) int {
if currentCard.color == trump {
if currentCard.figure == 5 {
return 20
} else if currentCard.figure == 3 {
return 14
}
}
if currentCard.figure == 8 && trump != 5 {
return 11
} else if currentCard.figure == 7 {
return 4
} else if currentCard.figure == 6 {
return 3
} else if currentCard.figure == 5 {
return 2
} else if currentCard.figure == 4 {
return 10
} else if currentCard.figure == 2 && (trump == 5 || trump == 4) {
return 8
} else if currentCard.figure == 0 && trump == 5 {
return 11
}
return 0
}
func countStich(cardsPlayed []card, trump int) int {
var score int
score = 0
for i := 0; i < 4; i++ {
score += countCard(cardsPlayed[i], trump)
}
return score
}
func evalStich(currentStich stich, trump int, lastStich bool) stich {
var outcardColor int
var currentBest card
outcardColor = currentStich.cardsPlayed[0].color
currentBest = currentStich.cardsPlayed[0]
for i := 1; i < 4; i++ {
if isHigherCard(currentBest, currentStich.cardsPlayed[i], trump, outcardColor) != currentBest {
currentBest = currentStich.cardsPlayed[i]
currentStich.winningPlayer = i
}
}
currentStich.points = countStich(currentStich.cardsPlayed, trump)
if lastStich {
currentStich.points += 5
}
fmt.Println("Player", currentStich.winningPlayer, "sticht with", getCardName(currentBest), "counting", currentStich.points, "points")
return currentStich
}
func playRound(currentGame game, players [4]player, currentRound round) game {
var currentStich stich
var startingPlayer int
var points1 int
var points2 int
var lastStich bool
startingPlayer = currentRound.startingPlayer
points1 = 0
points2 = 0
lastStich = false
for i := 0; i < 9; i++ {
if i == 8 {
lastStich = true
}
currentStich = stich{nil, startingPlayer, 0, startingPlayer}
currentStich = playCards(currentStich, players)
currentStich = evalStich(currentStich, currentRound.trump, lastStich)
if currentStich.winningPlayer == 0 || currentStich.winningPlayer == 2 {
points1 += currentStich.points
} else {
points2 += currentStich.points
}
startingPlayer = currentStich.winningPlayer
}
currentGame.points1 += points1
currentGame.points2 += points2
fmt.Println("Team 1 got", points1, "points (", currentGame.points1, ")\nTeam 2 got", points2, "points (", currentGame.points2, ")")
return currentGame
}
func main() {
var startPlayer int
var trump int
var currentRound round
var cards = createCardStack()
var shuffledStack [36]card
var players [4]player
sortCards(players)
var currentGame = game{0, 0}
startPlayer = 0
for currentGame.points1 < 2500 && currentGame.points2 < 2500 {
shuffledStack = shuffleCardStack(cards)
players = distributeCards(shuffledStack)
sortCards(players)
trump = getTrump(players, startPlayer, false)
currentRound = round{trump, 0, 0, startPlayer}
currentGame = playRound(currentGame, players, currentRound)
startPlayer += 1
}
}