-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_manager.go
77 lines (62 loc) · 1.42 KB
/
game_manager.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
package game_manager
import (
"fmt"
"github.com/Frezzle/noxes/tictactoe"
)
type Game struct {
id int
tictactoeGame tictactoe.Game
}
func (g Game) GetId() int {
return g.id
}
func (g Game) GetBoard() string {
return g.tictactoeGame.GetBoard()
}
func (g Game) GetNextTurn() tictactoe.Cell {
return g.tictactoeGame.GetNextTurn()
}
func (g Game) IsGameOver() bool {
return g.tictactoeGame.IsGameOver()
}
func (g Game) GetWinner() tictactoe.Cell {
return g.tictactoeGame.GetWinner()
}
type GameManager struct {
games []Game
nextGameId int
}
func NewGameManager() GameManager {
return GameManager{
games: make([]Game, 0),
nextGameId: 0,
}
}
func (gm *GameManager) CreateNewGame() (gameId int) {
gameId = gm.nextGameId
gm.games = append(gm.games, Game{
id: gameId,
tictactoeGame: tictactoe.NewGame(),
})
gm.nextGameId++
return gameId
}
func (gm GameManager) GetAllGames() []Game {
return gm.games
}
func (gm *GameManager) GetGameById(id int) (Game, error) {
for i := range gm.games {
if gm.games[i].id == id {
return gm.games[i], nil
}
}
return Game{}, fmt.Errorf("Game with ID %d not found", id)
}
func (gm *GameManager) MakeMove(gameId int, cell tictactoe.Cell, loc tictactoe.Location) error {
for i := range gm.games {
if gm.games[i].id == gameId {
return gm.games[i].tictactoeGame.MakeMove(cell, loc)
}
}
return fmt.Errorf("Game with ID %d not found", gameId)
}