Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created July 20, 2019 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eminetto/af2ac89e79f945ff2ace8c71b3299a52 to your computer and use it in GitHub Desktop.
Save eminetto/af2ac89e79f945ff2ace8c71b3299a52 to your computer and use it in GitHub Desktop.
board.go
package main
import (
"fmt"
"bytes"
)
func main() {
b := NewBoard()
fmt.Println(b.BoardRepresentation())
}
type board struct {
Squares []*square
}
type square struct {
current *piece
}
type piece struct {
name string
}
func NewSquare(piece *piece) *square {
return &square{
current: piece,
}
}//código omitido pra ficar legal no slide
func NewPiece(name string) *piece {
return &piece{
name: name,
}
}//código omitido pra ficar legal no slide
func NewBoard() *board {
p1 := NewPiece("king")
p2 := NewPiece("queen")
return &board{
Squares: []*square{NewSquare(p1), NewSquare(p2)},
}
}//código omitido pra ficar legal no slide
func (b *board) BoardRepresentation() string {
var buffer = &bytes.Buffer{}
for _, s := range b.Squares {
buffer.WriteString(s.current.name)
}
return buffer.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment