Created
June 4, 2019 02:18
-
-
Save eminetto/81fc59453ecbefa1d56e744dfec14878 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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