-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.go
More file actions
336 lines (283 loc) · 8.03 KB
/
game.go
File metadata and controls
336 lines (283 loc) · 8.03 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
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
// Package mahjong is a (riichi) mahjong game client.
package main
import (
"fmt"
"math/rand"
"time"
)
// Tile constants
const (
// The # of tiles in each suit
Winds int = 16
Dragons int = 12
Man, Sou, Pin int = 36, 36, 36
// The start and End of each suit in the slice. - 1 used for indice purposes.
WindsStart = 0
WindsEnd = WindsStart + Winds - 1
DragonsStart = WindsEnd + 1
DragonsEnd = DragonsStart + Dragons - 1
ManStart = DragonsEnd + 1
ManEnd = ManStart + Man - 1
SouStart = ManEnd + 1
SouEnd = SouStart + Sou - 1
PinStart = SouEnd + 1
PinEnd = PinStart + Pin - 1
// Define Unicode mahjong tiles
// Winds
East = '🀀'
South = '🀁'
West = '🀂'
North = '🀃'
// Dragons
RedDragon = '🀄'
GreenDragon = '🀅'
WhiteDragon = '🀆'
// Man
OneMan = '🀇'
TwoMan = '🀈'
ThreeMan = '🀉'
FourMan = '🀊'
FiveMan = '🀋'
SixMan = '🀌'
SevenMan = '🀍'
EightMan = '🀎'
NineMan = '🀏'
// Sou
OneSou = '🀐'
TwoSou = '🀑'
ThreeSou = '🀒'
FourSou = '🀓'
FiveSou = '🀔'
SixSou = '🀕'
SevenSou = '🀖'
EightSou = '🀗'
NineSou = '🀘'
// Pin
OnePin = '🀙'
TwoPin = '🀚'
ThreePin = '🀛'
FourPin = '🀜'
FivePin = '🀝'
SixPin = '🀞'
SevenPin = '🀟'
EightPin = '🀠'
NinePin = '🀡'
// Red Fives (First five in each suit)
MAN_RED_FIVE = 44
SOU_RED_FIVE = 80
PIN_RED_FIVE = 116
)
type Tile struct {
value int
}
type Hand struct {
tiles []int
open bool
}
type DiscardPile struct {
ownersName string
tiles map[int]Tile
}
type Player struct {
hand []int
discard DiscardPile
seat string
}
// SimpleSet compiles slices of Mahjong tiles into a basic 'one of each tile' set.
// It returns the set, which allows an unmutated set to be worked with where necessary.
func SimpleSet() []rune {
var (
// Build a set of unicode tiles
WindTiles = append((make([]rune, 0)), East, South, West, North)
DragonTiles = append((make([]rune, 0)), RedDragon, GreenDragon, WhiteDragon)
ManTiles = append((make([]rune, 0)), OneMan, TwoMan, ThreeMan, FourMan, FiveMan, SixMan, SevenMan, EightMan, NineMan)
SouTiles = append((make([]rune, 0)), OneSou, TwoSou, ThreeSou, FourSou, FiveSou, SixSou, SevenSou, EightSou, NineSou)
PinTiles = append((make([]rune, 0)), OnePin, TwoPin, ThreePin, FourPin, FivePin, SixPin, SevenPin, EightPin, NinePin)
Partial1 = append(WindTiles, DragonTiles...)
Partial2 = append(Partial1, ManTiles...)
Partial3 = append(Partial2, SouTiles...)
SetTiles = append(Partial3, PinTiles...)
)
return SetTiles
}
// FullSet compiles slices of Mahjong tiles into a complete 'four of each tile' set.
// It returns the set, which allows an unmutated set to be worked with where necessary.
func FullSet() []int {
FullSet := make([]int, 136)
for i := 0; i < len(FullSet)/4; i++ {
for j := 0; j <= 3; j++ {
i := i * 4
FullSet[i+j] = i + j
}
}
return FullSet
}
// GetUnicodeTile takes an integer from a full set (0-135) and returns the associated tile rune (a unicode mahjong tile).
func GetUnicodeTile(tile int) rune {
SetTiles := SimpleSet()
tileValue := tile / 4
tileCode := SetTiles[tileValue]
return tileCode
}
// ShuffleSet takes set (a slice of integers) and shuffles it.
// It returns the ShuffledSet.
func ShuffleSet(set []int) []int {
ShuffledSet := set
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(ShuffledSet), func(i, j int) { ShuffledSet[i], ShuffledSet[j] = ShuffledSet[j], ShuffledSet[i] })
return ShuffledSet
}
// AssignSeats takes set (a slice of strings) and shuffles it.
// It returns the Seats.
func AssignSeats(set []string) []string {
Seats := set
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(Seats), func(i, j int) { Seats[i], Seats[j] = Seats[j], Seats[i] })
return Seats
}
func Deal() ([]int, []int, []int, []int, []int, []int, []int) {
Tiles := ShuffleSet(FullSet())
var DeadWall []int
var EastHand, SouthHand, WestHand, NorthHand []int
var DrawWall, DoraIndicators, UraDoraIndicators []int
var EastStart, SouthStart, WestStart, NorthStart = 0, 34, 68, 102
DiceRoll := RollDice(2)
WallChoice := DiceRoll % 4
WallBreak := 0
if WallChoice == 1 {
WallBreak = EastStart + (DiceRoll * 2)
} else if WallChoice == 2 {
WallBreak = SouthStart + (DiceRoll * 2)
} else if WallChoice == 3 {
WallBreak = WestStart + (DiceRoll * 2)
} else if WallChoice == 4 {
WallBreak = NorthStart + (DiceRoll * 2)
}
DrawWall = append(Tiles[WallBreak:], Tiles[:WallBreak]...)
for i := 1; i <= 3; i++ {
for w := 1; w <= 4; w++ {
StartDraw := ((i - 1) * w * 4)
EndDraw := StartDraw + 4
Dealt := Tiles[StartDraw:EndDraw]
if w == 1 {
EastHand = append(EastHand, Dealt...)
} else if w == 2 {
SouthHand = append(SouthHand, Dealt...)
} else if w == 3 {
WestHand = append(WestHand, Dealt...)
} else if w == 4 {
NorthHand = append(NorthHand, Dealt...)
}
}
}
DrawWall = Tiles[47:]
if WallBreak >= 14 {
DeadWall = Tiles[WallBreak-14 : WallBreak]
} else {
remainder := 14 - WallBreak
DeadWall = append(Tiles[:WallBreak], Tiles[(len(Tiles)-remainder):]...)
}
DoraIndicators = append(DoraIndicators, DeadWall[4], DeadWall[6], DeadWall[8], DeadWall[10], DeadWall[12])
UraDoraIndicators = append(UraDoraIndicators, DeadWall[5], DeadWall[7], DeadWall[9], DeadWall[11], DeadWall[13])
return DrawWall, DoraIndicators, UraDoraIndicators, EastHand, SouthHand, WestHand, NorthHand
//Deal Pseudocode
//Four Walls: 17x2 Tiles
//Roll Dice: 1:East 2:South 3:West 4:North
//Roll Dice: Count to dice count.
//Begin Deal: 2x2 to East, 2x2 to South, 2x2 to West, 2x2 to North, repeat 3 times (12 tiles)
//Dead wall: 7x2, starting from where the deal began.
//Dora Indicator: top tile, 3 from the right of the dead wall. (ooooXoo)
}
func RollDice(num int) int {
var dice = []int{1, 2, 3, 4, 5, 6}
value := 0
rand.Seed(time.Now().UnixNano())
for i := 0; i < num; i++ {
value += dice[rand.Intn(len(dice))]
}
return value
}
func Draw(tiles []int) ([]int, []int) {
drawn := tiles[:1]
tiles = tiles[1:]
return drawn, tiles
}
func Discard() {
}
func CallChi() {
}
func CallPon() {
}
func CallKan() {
}
func ClosedKan() {
}
func CallRon() {
}
func CallTsumo() {
}
func main() {
SimpleSet()
east := Player{seat: "East"}
south := Player{seat: "South"}
west := Player{seat: "West"}
north := Player{seat: "North"}
var drawWall, doraIndicators, uraDoraIndicators []int
//DrawWall, DoraIndicators, UraDoraIndicators, EastHand, SouthHand, WestHand, NorthHand
drawWall, doraIndicators, uraDoraIndicators, east.hand, south.hand, west.hand, north.hand = Deal()
// DEEBUG / OUTPUT FOR VERIFICATION
fmt.Println("Dealt Game")
fmt.Printf("drawWall: ")
for _, c := range drawWall {
fmt.Printf("%q", GetUnicodeTile(c))
}
fmt.Printf("\n")
fmt.Printf("doraIndicators: ")
for _, c := range doraIndicators {
fmt.Printf("%q", GetUnicodeTile(c))
}
fmt.Printf("\n")
fmt.Printf("uraDoraIndicators: ")
for _, c := range uraDoraIndicators {
fmt.Printf("%q", GetUnicodeTile(c))
}
fmt.Printf("\n")
fmt.Printf("east: ")
for _, c := range east.hand {
fmt.Printf("%q", GetUnicodeTile(c))
}
fmt.Printf("\n")
fmt.Printf("south: ")
for _, c := range south.hand {
fmt.Printf("%q", GetUnicodeTile(c))
}
fmt.Printf("\n")
fmt.Printf("west: ")
for _, c := range west.hand {
fmt.Printf("%q", GetUnicodeTile(c))
}
fmt.Printf("\n")
fmt.Printf("north: ")
for _, c := range north.hand {
fmt.Printf("%q", GetUnicodeTile(c))
}
fmt.Printf("\nEnd Dealt Game\n")
fmt.Println("SimpleSet")
for _, c := range SimpleSet() {
fmt.Printf("%q", c)
}
fmt.Println("\nEnd Simpleset")
fmt.Println("FullSet")
for _, c := range FullSet() {
fmt.Printf("%q", GetUnicodeTile(c))
}
fmt.Println("\nEnd FullSet")
fmt.Println("ShuffledSet")
for _, c := range ShuffleSet(FullSet()) {
fmt.Printf("%q", GetUnicodeTile(c))
}
fmt.Println("\nEnd ShuffledSet")
fmt.Printf("Man Red Five: %q ", GetUnicodeTile(MAN_RED_FIVE))
fmt.Printf("Pin Red Five: %q ", GetUnicodeTile(PIN_RED_FIVE))
fmt.Printf("Sou Red Five: %q\n", GetUnicodeTile(SOU_RED_FIVE))
}