-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.go
189 lines (164 loc) · 4.9 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
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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// StartNewGame initalizes the players, the deck, and deals cards to each
// player.
func StartNewGame(name *string, pScore, AIScore *int) (err error) {
p1 := &Player{*name, Hand{}}
p2 := &Player{"AI", Hand{}}
turn := p1
RummyDeck := InitializeDeck()
RummyStack := RummyDeck.InitializeStack()
RummyDeck.Deal(p1, p2)
knock, draw, gin := false, false, false
// While Knock is true, keep the players in a loop that handle turns.
for {
if turn == p1 {
PlayerActions(p1, &RummyDeck, &RummyStack, &knock, &draw, &gin)
if knock || draw {
break
}
turn = p2
} else {
AIActions(p2, &RummyDeck, &RummyStack, &knock, &draw, &gin)
if knock || draw {
break
}
turn = p1
}
}
if draw {
fmt.Printf("\nThe game was a draw!\n---\n%s score: %d AI score: %d \n Play again? (Y/N) ", *name, *pScore, *AIScore)
reader := bufio.NewReader(os.Stdin)
response, err := reader.ReadString('\n')
response = strings.ToUpper(strings.TrimSpace(response))
if response == "Y" {
StartNewGame(name, pScore, AIScore)
}
return err
}
if turn == p1 {
*pScore += CalculateScore(&p1.Hand, &p2.Hand, gin)
} else {
*AIScore += CalculateScore(&p2.Hand, &p1.Hand, gin)
}
fmt.Printf("\n%s's score: %d AI score: %d \nPlay again? (Y/N) ", *name, *pScore, *AIScore)
reader := bufio.NewReader(os.Stdin)
response, err := reader.ReadString('\n')
response = strings.ToUpper(strings.TrimSpace(response))
if response == "Y" {
StartNewGame(name, pScore, AIScore)
} else {
fmt.Printf("\nGoodbye!\n\nFinal scores:\n%s: %d\nAI: %d", *name, *pScore, *AIScore)
}
return err
}
// PlayerActions - describes what the player is going to do.
func PlayerActions(p *Player, deck *Deck, stack *Stack, knock, draw, gin *bool) {
reader := bufio.NewReader(os.Stdin)
TURN_ACTIONS:
for {
if len(*deck) == 0 {
*draw = true
break TURN_ACTIONS
}
fmt.Printf("\n---\nCard on stack: %s \nYour hand: %s \n", stack.PeekAtStack(), p.PrettyPrintHand())
fmt.Printf("\nWhat would you like to do, %s?\n 1. DRAW CARD FROM DECK\n 2. PICKUP CARD FROM STACK\n 3. CHECK MELDS IN HAND\n 4. CHECK POINTS IN HAND\n", p.name)
response, err := reader.ReadString('\n')
response = strings.TrimRight(response, "\n")
if err != nil {
fmt.Println("Unrecognized command.")
continue
}
response = strings.ToUpper(strings.TrimSpace(response))
switch response {
case "1", "DRAW CARD":
card, _ := p.Hand.DrawCard(deck)
fmt.Printf("\n%s drew %s.", p.name, card.PrettyPrintCard())
break TURN_ACTIONS
case "2", "PICKUP CARD":
card, _ := p.Hand.DrawCard(stack)
fmt.Printf("\n%s drew %s.", p.name, card.PrettyPrintCard())
break TURN_ACTIONS
case "3", "CHECK MELDS":
melds := p.Hand.CheckMelds()
fmt.Printf("\n%s", melds.PrettyPrintMelds())
case "4", "CHECK POINTS":
// Check the total of points in your hand, values not melded
total := p.Hand.CheckTotal()
if total <= 10 {
fmt.Printf("\nYour hand total is: %d. Will you knock? (Y/N) ", total)
response, err := reader.ReadString('\n')
response = strings.ToUpper(strings.TrimRight(response, "\n"))
if err != nil {
fmt.Println("Something went wrong...")
}
if response == "Y" {
if total == 0 {
*gin = true
}
*knock = true
break TURN_ACTIONS
}
}
fmt.Printf("\nYou hand total is: %d", total)
default:
fmt.Printf("\nUnrecognized command. Please pick one of the four options. To knock, check your score first and see if it's under 10.\n\n")
}
}
if *knock {
return
}
if *draw {
return
}
for {
fmt.Printf("\nYou must now discard a card from your hand.\n\nCard on stack: %s \nYour hand: %s \n", stack.PeekAtStack(), p.PrettyPrintHand())
discard, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Something went wrong. Try discarding a different card.")
continue
}
discard = strings.ToUpper(strings.TrimRight(discard, "\n"))
card, err := GetCardFromPrettyPrint(discard)
if err != nil {
fmt.Println("Something went wrong. Try discarding a different card.")
continue
}
discardCard, err := p.Hand.DiscardCard(card, stack)
if err != nil {
fmt.Println("\nSomething went wrong. Try discarding a different card.")
continue
}
fmt.Printf("\n%s discarded %s\n", p.name, discardCard)
break
}
return
}
// CalculateScore - gets the score from the last round.
func CalculateScore(h1, h2 *Hand, gin bool) (score int) {
if gin {
return h2.CheckTotal() - h1.CheckTotal() + 20
}
return h2.CheckTotal() - h1.CheckTotal()
}
func main() {
pScore := 0
AIScore := 0
reader := bufio.NewReader(os.Stdin)
fmt.Println("Enter your name:")
name, err := reader.ReadString('\n')
name = strings.TrimRight(name, "\n")
fmt.Printf("\n")
if err != nil {
fmt.Printf("An error occurred with the name")
os.Exit(0)
}
fmt.Printf("Welcome %s! Lets play a game of Gin Rummy!\n", name)
fmt.Printf("\n")
StartNewGame(&name, &pScore, &AIScore)
}