-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_objects.go
177 lines (155 loc) · 4.63 KB
/
game_objects.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
package main
import (
"errors"
"math/rand"
"time"
uuid "github.com/satori/go.uuid"
)
// Various constants for use throughout the game
const (
Flat string = "flat"
Wall string = "wall"
Capstone string = "capstone"
Black string = "black"
White string = "white"
NorthSouth string = "NS"
WestEast string = "WE"
)
// Piece is the most basic element of a Tak game. One of two colors; one of three types.
type Piece struct {
// one of "black" or "white"
Color string `json:"color"`
// Type can be one of "flat", "wall", or "capstone"
Orientation string `json:"orientation"`
}
var (
whiteFlat = Piece{"white", "flat"}
blackFlat = Piece{"black", "flat"}
whiteCap = Piece{"white", "capstone"}
blackCap = Piece{"black", "capstone"}
whiteWall = Piece{"white", "wall"}
blackWall = Piece{"black", "wall"}
)
// Stack is just a slice of Pieces.
type Stack struct {
// Note: the "top" of the stack, for game purposes, is at [0]
Pieces []Piece
}
// GameBoard is a representation of the Tak game board, containing Stacks
type GameBoard [][]Stack
// WinningPath shows the game coords making up a roadWin
type WinningPath []Coords
// Placement describes an action that places a new piece on the board
type Placement struct {
Piece Piece `json:"piece"`
Coords string `json:"coords"`
}
// Movement describes an action that moves a stack.
type Movement struct {
Coords string `json:"coords"`
Direction string `json:"direction"`
Carry int `json:"carry"`
Drops []int `json:"drops"`
}
// TakPlayer describes a human player
type TakPlayer struct {
Username string `json:"username"`
PlayerID uuid.UUID `json:"playerID"`
PlayedGames []uuid.UUID `json:"playedGames"`
passwordHash []byte
Password string `json:"password"`
}
// TakGame is the general object representing an entire game, including a board, an id, and some metadata.
type TakGame struct {
// the id for this game
GameID uuid.UUID `json:"gameID"`
// the gameboard for this game, represented as stacks of Pieces
GameBoard GameBoard `json:"gameBoard"`
// Boolean indicator of whose turn it is
IsBlackTurn bool `json:"isBlackTurn"`
BlackWinner bool `json:"blackWinner"`
WhiteWinner bool `json:"whiteWinner"`
RoadWin bool `json:"roadWin"`
FlatWin bool `json:"flatWin"`
DrawGame bool `json:"drawGame"`
GameOver bool `json:"gameOver"`
GameWinner string `json:"gameWinner"`
WinningPath WinningPath `json:"winningPath"`
StartTime time.Time `json:"startTime"`
WinTime time.Time `json:"winTime"`
BlackPlayer string `json:"blackPlayer"`
WhitePlayer string `json:"whitePlayer"`
GameOwner string `json:"gameOwner"`
IsPublic bool `json:"isPublic"`
HasStarted bool `json:"hasStarted"`
Size int `json:"size"`
MoveCount int `json:"moveCount"`
TurnHistory []interface{} `json:"turnHistory"`
}
// PieceLimits is a map of gridsize to piece limits per player
// note there's no limit listed for 7x7 games, which are "rarely played"
var PieceLimits = map[int]int{
3: 10,
4: 15,
5: 21,
6: 30,
8: 50,
}
// LetterMap converts Tak x-values (letters) to their start-at-zero grid index value. 8x8 games are the max size.
var LetterMap = map[string]int{
"a": 0,
"b": 1,
"c": 2,
"d": 3,
"e": 4,
"f": 5,
"g": 6,
"h": 7,
}
// NumberToLetter converts grid index values back to Tak x-values (letters)
var NumberToLetter = map[int]string{
0: "a",
1: "b",
2: "c",
3: "d",
4: "e",
5: "f",
6: "g",
7: "h",
}
// TakJWT is a simple struct to return JWTs in JSON
type TakJWT struct {
JWT string `json:"jwt"`
Message string `json:"message"`
}
// StackTops is a slice of rows that displays a top-down view of the game (mostly useful for debugging)
type StackTops struct {
TopView []string `json:"topView"`
}
// MakeGame takes an integer size and returns a TakGame with a board that size
func MakeGame(size int) (*TakGame, error) {
if size < 3 || size > 8 {
return nil, errors.New("board size must be in the range 3 to 8 squares")
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// each game gets a guid
newUUID := uuid.NewV4()
newGameBoard := makeGameBoard(size)
newTakGame := TakGame{
GameID: newUUID,
GameBoard: newGameBoard,
Size: size,
// randomly select a first player with a bool
IsBlackTurn: (r.Intn(2) == 0),
}
return &newTakGame, nil
}
func makeGameBoard(s int) [][]Stack {
newBoard := make([][]Stack, s, s)
// ... then populate with the columns of spaces
for x := 0; x < s; x++ {
column := make([]Stack, s, s)
newBoard[x] = column
}
return newBoard
}