Esempio n. 1
0
//ListGames lists all games
func (c *Controller) ListGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	collection := c.DB.C("games")
	var games []Game
	if err := collection.Find(nil).All(&games); err != nil {
		c.Error(w, errors.New("something went wrong"))
		return
	}

	states := make([]string, len(games))
	for i, game := range games {
		states[i] = game.PrettyPrint()
	}
	fmt.Fprint(w, states)
}
Esempio n. 2
0
//NewGame intializes a game
func (c *Controller) NewGame(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	var info Info
	if err := c.UnmarshalBody(r, &info); err != nil {
		c.Error(w, err)
		return
	}

	collection := c.DB.C("games")
	game := Game{ID: bson.NewObjectId(), Board: NewBoard(info.BoardDimensions)}
	if err := collection.Insert(&game); err != nil {
		c.Error(w, errors.New("something went wrong"))
		return
	}
	fmt.Fprint(w, game.PrettyPrint())
}
Esempio n. 3
0
//ApplyMove moves the piece according to move given
func (b *Board) ApplyMove(move Move) error {
	if !move.PieceCoords.InBoundsOf(b.Dimensions) {
		return errors.NewOutOfBounds("Board")
	}
	space := &b.Spaces[move.PieceCoords.X][move.PieceCoords.Y]
	if !space.HasPiece {
		return errors.New("No piece at space provided")
	}
	piece := space.Piece
	newLocation := move.PieceCoords.Add(move.Vector)
	if !newLocation.InBoundsOf(b.Dimensions) {
		return errors.NewOutOfBounds("Board")
	}
	newSpace := &b.Spaces[newLocation.X][newLocation.Y]
	newSpace.Piece = piece
	newSpace.HasPiece = true
	space.Piece = Piece{}
	space.HasPiece = false
	return nil
}