fix game output

This commit is contained in:
stryan 2022-03-18 15:21:10 -04:00
parent 48ea6e62a1
commit 677a6e9e4d
6 changed files with 44 additions and 18 deletions

16
api.go
View File

@ -49,7 +49,7 @@ func (a *API) NewGame(res http.ResponseWriter, req *http.Request) {
} }
} }
log.Printf("creating new game %v", a.nextInt) log.Printf("creating new game %v", a.nextInt)
a.games[a.nextInt] = NewSession() a.games[a.nextInt] = NewSession(8)
a.games[a.nextInt].redPlayer.Ready = true a.games[a.nextInt].redPlayer.Ready = true
respondWithJSON(res, http.StatusOK, newGameResp{a.nextInt, "red"}) respondWithJSON(res, http.StatusOK, newGameResp{a.nextInt, "red"})
a.nextInt = a.nextInt + 1 a.nextInt = a.nextInt + 1
@ -81,18 +81,16 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) {
} }
log.Println("sending game state") log.Println("sending game state")
rotatedBoard := s.getBoard(p) board := s.getBoard(p)
rotate := req.Header.Get("Rotate") rotate := req.Header.Get("Rotate")
if rotate == "true" { if rotate == "true" {
i := 0 log.Println("rotating output")
temp := rotatedBoard[0] //rotateBoard(board)
for ; i < len(rotatedBoard)-1; i++ { //rotateBoard(board)
rotatedBoard[i] = rotatedBoard[i+1] //rotateBoard(board)
}
rotatedBoard[i] = temp
} }
respondWithJSON(res, http.StatusOK, gameResp{rotatedBoard}) respondWithJSON(res, http.StatusOK, gameResp{board})
return return
} }

View File

@ -23,7 +23,7 @@ func TestNewAPI(t *testing.T) {
func dummyGame(a *API) int { func dummyGame(a *API) int {
i := a.nextInt i := a.nextInt
a.games[i] = NewSession() a.games[i] = NewSession(8)
a.games[i].redPlayer.Ready = true a.games[i].redPlayer.Ready = true
a.games[i].bluePlayer.Ready = true a.games[i].bluePlayer.Ready = true
a.games[i].simulator.Setup() a.games[i].simulator.Setup()
@ -107,7 +107,7 @@ func TestGetGame(t *testing.T) {
} }
for j := range respStruct.GameBoard { for j := range respStruct.GameBoard {
for i, vt := range respStruct.GameBoard[j] { for i, vt := range respStruct.GameBoard[j] {
curr, err := a.games[gid].simulator.Board.GetPiece(j, i) curr, err := a.games[gid].simulator.Board.GetPiece(i, j)
if err != nil { if err != nil {
t.Fatalf("Strange board position: %v", err) t.Fatalf("Strange board position: %v", err)
} }

View File

@ -10,7 +10,7 @@ type newGameResp struct {
} }
type gameResp struct { type gameResp struct {
GameBoard [8][8]*ViewTile `json:"board"` GameBoard [][]*ViewTile `json:"board"`
} }
type gameStatusResp struct { type gameStatusResp struct {

View File

@ -15,6 +15,7 @@ type Session struct {
bluePlayer *Player bluePlayer *Player
moveNum int moveNum int
moveList []freego.ParsedCommand moveList []freego.ParsedCommand
boardSize int
} }
//Player is a player in a match //Player is a player in a match
@ -34,7 +35,7 @@ func (p *Player) Colour() freego.Colour {
} }
//NewSession creates a new game session //NewSession creates a new game session
func NewSession() *Session { func NewSession(size int) *Session {
sim := freego.NewGame() sim := freego.NewGame()
return &Session{ return &Session{
simulator: sim, simulator: sim,
@ -42,6 +43,7 @@ func NewSession() *Session {
bluePlayer: &Player{false, freego.Blue}, bluePlayer: &Player{false, freego.Blue},
moveNum: 1, moveNum: 1,
moveList: make([]freego.ParsedCommand, 20), moveList: make([]freego.ParsedCommand, 20),
boardSize: size,
} }
} }
@ -78,10 +80,13 @@ func (s *Session) getMove(p *Player, num int) (string, error) {
return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil
} }
func (s *Session) getBoard(p *Player) [8][8]*ViewTile { func (s *Session) getBoard(p *Player) [][]*ViewTile {
var res [8][8]*ViewTile res := make([][]*ViewTile, s.boardSize)
for i := 0; i < 8; i++ { for i := range res {
for j := 0; j < 8; j++ { res[i] = make([]*ViewTile, s.boardSize)
}
for i := 0; i < s.boardSize; i++ {
for j := 0; j < s.boardSize; j++ {
cur := NewViewTile() cur := NewViewTile()
terrain, err := s.simulator.Board.IsTerrain(i, j) terrain, err := s.simulator.Board.IsTerrain(i, j)
if err != nil { if err != nil {
@ -109,7 +114,7 @@ func (s *Session) getBoard(p *Player) [8][8]*ViewTile {
cur.Empty = true cur.Empty = true
} }
} }
res[i][j] = cur res[j][i] = cur
} }
} }
return res return res

10
util.go
View File

@ -21,6 +21,16 @@ func respondWithJSON(res http.ResponseWriter, code int, payload interface{}) {
res.Write(response) res.Write(response)
} }
func rotateBoard(board [][]*ViewTile) {
i := 0
temp := board[0]
for ; i < len(board)-1; i++ {
board[i] = board[i+1]
}
board[i] = temp
}
//TODO remove this when you can actually setup a game //TODO remove this when you can actually setup a game
func initDummy(g *freego.Game) { func initDummy(g *freego.Game) {
//Setup terrain //Setup terrain

View File

@ -12,3 +12,16 @@ type ViewTile struct {
func NewViewTile() *ViewTile { func NewViewTile() *ViewTile {
return &ViewTile{} return &ViewTile{}
} }
func (vt *ViewTile) String() string {
if vt.Piece != "" {
return vt.Piece
}
if vt.Hidden {
return "?"
}
if vt.Terrain {
return "X"
}
return " "
}