Skip to content

Instantly share code, notes, and snippets.

@leohmoraes
Forked from eminetto/board.go
Created August 5, 2019 12:07
Show Gist options
  • Save leohmoraes/85791370b960e122710398036d1ad09f to your computer and use it in GitHub Desktop.
Save leohmoraes/85791370b960e122710398036d1ad09f 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