Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created June 4, 2019 02:18
Show Gist options
  • Save eminetto/81fc59453ecbefa1d56e744dfec14878 to your computer and use it in GitHub Desktop.
Save eminetto/81fc59453ecbefa1d56e744dfec14878 to your computer and use it in GitHub Desktop.
package chess
import "bytes"
type piece struct {
representation string
}
type location struct {
current *piece
}
type board struct {
locations []*location
}
func NewPiece(representation string) *piece {
return &piece{representation: representation}
}
func (p *piece) character() string {
return p.representation[0:1]
}
func (p *piece) addTo(buffer *bytes.Buffer) {
buffer.WriteString(p.character())
}
func NewLocation(piece *piece) *location {
return &location{current: piece}
}
func (l *location) addTo(buffer *bytes.Buffer) {
l.current.addTo(buffer)
}
func NewBoard() *board {
locations := []*location{
NewLocation(NewPiece("London")),
NewLocation(NewPiece("New York")),
NewLocation(NewPiece("Dubai")),
}
return &board{
locations: locations,
}
}
func (b *board) squares() []*location {
return b.locations
}
func (b *board) BoardRepresentation() string {
var buffer = &bytes.Buffer{}
for _, l := range b.squares() {
l.addTo(buffer)
}
return buffer.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment