Maintain history; use state
This commit is contained in:
@ -3,47 +3,55 @@ package state
|
||||
func (s *State) CursorCellUp() {
|
||||
if s.cursor.Y >= 1 {
|
||||
s.cursor.Y--
|
||||
s.history += "u"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorCellDown() {
|
||||
if s.cursor.Y < s.size()-1 {
|
||||
s.cursor.Y++
|
||||
s.history += "d"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorCellRight() {
|
||||
if s.cursor.X < s.size()-1 {
|
||||
s.cursor.X++
|
||||
s.history += "r"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorCellLeft() {
|
||||
if s.cursor.X >= 1 {
|
||||
s.cursor.X--
|
||||
s.history += "l"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorSectionUp() {
|
||||
if s.cursor.Y >= s.cellsPerSection {
|
||||
s.cursor.Y -= s.cellsPerSection
|
||||
s.history += "U"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorSectionDown() {
|
||||
if s.cursor.Y < s.size()-s.cellsPerSection {
|
||||
s.cursor.Y += s.cellsPerSection
|
||||
s.history += "D"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorSectionRight() {
|
||||
if s.cursor.X < s.size()-s.cellsPerSection {
|
||||
s.cursor.X += s.cellsPerSection
|
||||
s.history += "R"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorSectionLeft() {
|
||||
if s.cursor.X >= s.cellsPerSection {
|
||||
s.cursor.X -= s.cellsPerSection
|
||||
s.history += "L"
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,6 +38,11 @@ func (f *field) String() string {
|
||||
return string(f.cells)
|
||||
}
|
||||
|
||||
// cell returns the raw cell (a byte) at the given point.
|
||||
func (f *field) cell(p Point) cell {
|
||||
return f.cells[f.i(p)]
|
||||
}
|
||||
|
||||
// state returns whether or not the cell is alive.
|
||||
func (f *field) state(p Point) bool {
|
||||
return f.cells[f.i(p)].state()
|
||||
|
||||
@ -2,6 +2,7 @@ package state
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
@ -9,9 +10,18 @@ type Point struct {
|
||||
X, Y int
|
||||
}
|
||||
|
||||
func (p Point) String() string {
|
||||
return fmt.Sprintf("(%d,%d)", p.X, p.Y)
|
||||
}
|
||||
|
||||
type State struct {
|
||||
cursor *Point
|
||||
|
||||
clears, score, factor int
|
||||
blackout []bool
|
||||
|
||||
history string
|
||||
|
||||
sectionSize, cellsPerSection int
|
||||
cells, sections *field
|
||||
}
|
||||
@ -24,11 +34,13 @@ func New(sectionSize, cellsPerSection int) *State {
|
||||
sections: newField(sectionSize),
|
||||
cursor: &Point{0, 0},
|
||||
}
|
||||
s.history = fmt.Sprintf("gogogogogram %d %d:\n", sectionSize, cellsPerSection)
|
||||
for x := 0; x < sectionSize; x++ {
|
||||
for y := 0; y < sectionSize; y++ {
|
||||
s.initSection(Point{x, y})
|
||||
}
|
||||
}
|
||||
// TODO reveal 1 row per section, 1 col per sections/2
|
||||
return s
|
||||
}
|
||||
|
||||
@ -37,6 +49,11 @@ func (s *State) size() int {
|
||||
return s.sectionSize * s.cellsPerSection
|
||||
}
|
||||
|
||||
// History returns the gameplay history for replays
|
||||
func (s *State) History() string {
|
||||
return s.history
|
||||
}
|
||||
|
||||
func (s *State) String() string {
|
||||
var buf bytes.Buffer
|
||||
for x := 0; x < s.sectionSize*s.cellsPerSection; x++ {
|
||||
@ -68,14 +85,17 @@ func (s *State) String() string {
|
||||
}
|
||||
|
||||
func (s *State) Mark() []bool {
|
||||
s.history += "m"
|
||||
return s.mark(*s.cursor)
|
||||
}
|
||||
|
||||
func (s *State) Flag() []bool {
|
||||
s.history += "f"
|
||||
return s.flag(*s.cursor)
|
||||
}
|
||||
|
||||
func (s *State) Clear() {
|
||||
s.history += "c"
|
||||
s.clear(*s.cursor, true)
|
||||
}
|
||||
|
||||
@ -83,17 +103,20 @@ func (s *State) view(cursor []int) {
|
||||
}
|
||||
|
||||
func (s *State) initSection(p Point) {
|
||||
s.history += fmt.Sprintf("i%s", p)
|
||||
s.sections.clear(p, false)
|
||||
startX := p.X * s.cellsPerSection
|
||||
startY := p.Y * s.cellsPerSection
|
||||
for x := 0; x < s.cellsPerSection; x++ {
|
||||
for y := 0; y < s.cellsPerSection; y++ {
|
||||
s.cells.clear(Point{x + startX, y + startY}, true)
|
||||
currPoint := Point{x + startX, y + startY}
|
||||
s.cells.clear(currPoint, true)
|
||||
if rand.Int()%2 == 0 {
|
||||
s.cells.kill(Point{x + startX, y + startY})
|
||||
s.cells.kill(currPoint)
|
||||
} else {
|
||||
s.cells.vivify(Point{x + startX, y + startY})
|
||||
s.cells.vivify(currPoint)
|
||||
}
|
||||
s.history += fmt.Sprintf("%c", byte(s.cells.cell(currPoint)))
|
||||
}
|
||||
}
|
||||
s.update(p)
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
@ -48,6 +49,24 @@ func TestState(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
Convey("You can maintain a history of all actions", func() {
|
||||
s.Flag()
|
||||
s.Mark()
|
||||
s.Clear()
|
||||
s.CursorSectionRight()
|
||||
s.CursorSectionLeft()
|
||||
s.CursorSectionDown()
|
||||
s.CursorSectionUp()
|
||||
s.CursorCellRight()
|
||||
s.CursorCellLeft()
|
||||
s.CursorCellDown()
|
||||
s.CursorCellUp()
|
||||
|
||||
matches, err := regexp.Match("gogogogogram 2 2:\n(i\\(\\d,\\d\\)[\b\x01]{4}){4}fmcRLDUrldu", []byte(s.History()))
|
||||
So(matches, ShouldBeTrue)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("You can complete sections and clear portions of the board", func() {
|
||||
s.cells.vivify(Point{0, 0})
|
||||
s.cells.vivify(Point{2, 0})
|
||||
|
||||
Reference in New Issue
Block a user