Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created Jul 20, 2019
Embed
What would you like to do?
package main
import (
"fmt"
"bytes"
)
func main() {
b := NewBoard()
fmt.Println(b.BoardRepresentation())
}
type board struct {
Squares []*square
}
type square struct {
current *piece
}
func (s *square) addTo(buffer *bytes.Buffer) {
s.current.addTo(buffer)
}
type piece struct {
name string
}
func (p *piece) addTo(buffer *bytes.Buffer) {
buffer.WriteString(p.name)
}
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 {
s.addTo(buffer)
}
return buffer.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment