History loading
This commit is contained in:
@ -1,57 +1,113 @@
|
||||
package state
|
||||
|
||||
func (s *State) CursorCellUp() {
|
||||
if s.cursor.Y >= 1 {
|
||||
s.cursor.Y--
|
||||
if s.cursorCellUp() {
|
||||
s.history += "u"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorCellDown() {
|
||||
if s.cursor.Y < s.size()-1 {
|
||||
s.cursor.Y++
|
||||
if s.cursorCellDown() {
|
||||
s.history += "d"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorCellRight() {
|
||||
if s.cursor.X < s.size()-1 {
|
||||
s.cursor.X++
|
||||
if s.cursorCellRight() {
|
||||
s.history += "r"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorCellLeft() {
|
||||
if s.cursor.X >= 1 {
|
||||
s.cursor.X--
|
||||
if s.cursorCellLeft() {
|
||||
s.history += "l"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorSectionUp() {
|
||||
if s.cursor.Y >= s.cellsPerSection {
|
||||
s.cursor.Y -= s.cellsPerSection
|
||||
if s.cursorSectionUp() {
|
||||
s.history += "U"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorSectionDown() {
|
||||
if s.cursor.Y < s.size()-s.cellsPerSection {
|
||||
s.cursor.Y += s.cellsPerSection
|
||||
if s.cursorSectionDown() {
|
||||
s.history += "D"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorSectionRight() {
|
||||
if s.cursor.X < s.size()-s.cellsPerSection {
|
||||
s.cursor.X += s.cellsPerSection
|
||||
if s.cursorSectionRight() {
|
||||
s.history += "R"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) CursorSectionLeft() {
|
||||
if s.cursor.X >= s.cellsPerSection {
|
||||
s.cursor.X -= s.cellsPerSection
|
||||
if s.cursorSectionLeft() {
|
||||
s.history += "L"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) cursorCellUp() bool {
|
||||
if s.cursor.Y >= 1 {
|
||||
s.cursor.Y--
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *State) cursorCellDown() bool {
|
||||
if s.cursor.Y < s.size()-1 {
|
||||
s.cursor.Y++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *State) cursorCellRight() bool {
|
||||
if s.cursor.X < s.size()-1 {
|
||||
s.cursor.X++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *State) cursorCellLeft() bool {
|
||||
if s.cursor.X >= 1 {
|
||||
s.cursor.X--
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *State) cursorSectionUp() bool {
|
||||
if s.cursor.Y >= s.cellsPerSection {
|
||||
s.cursor.Y -= s.cellsPerSection
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *State) cursorSectionDown() bool {
|
||||
if s.cursor.Y < s.size()-s.cellsPerSection {
|
||||
s.cursor.Y += s.cellsPerSection
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *State) cursorSectionRight() bool {
|
||||
if s.cursor.X < s.size()-s.cellsPerSection {
|
||||
s.cursor.X += s.cellsPerSection
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *State) cursorSectionLeft() bool {
|
||||
if s.cursor.X >= s.cellsPerSection {
|
||||
s.cursor.X -= s.cellsPerSection
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@ -10,10 +10,14 @@ type field struct {
|
||||
|
||||
// newField returns a field of cells, all unset.
|
||||
func newField(size int) *field {
|
||||
return &field{
|
||||
f := &field{
|
||||
cells: make([]cell, size*size),
|
||||
size: size,
|
||||
}
|
||||
for i := 0; i < size*size; i++ {
|
||||
f.cells[i].clear(false)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// fieldFromBytes returns a field of cells given a bytearray; it assumes that the bytearray is a square.
|
||||
|
||||
@ -11,7 +11,7 @@ type Point struct {
|
||||
}
|
||||
|
||||
func (p Point) String() string {
|
||||
return fmt.Sprintf("%d,%d", p.X, p.Y)
|
||||
return fmt.Sprintf("(%d,%d)", p.X, p.Y)
|
||||
}
|
||||
|
||||
type State struct {
|
||||
@ -20,7 +20,8 @@ type State struct {
|
||||
clears, score, factor int
|
||||
blackout []bool
|
||||
|
||||
history string
|
||||
history string
|
||||
historyIndex int
|
||||
|
||||
sectionSize, cellsPerSection int
|
||||
cells, sections *field
|
||||
@ -34,7 +35,7 @@ func New(sectionSize, cellsPerSection int) *State {
|
||||
sections: newField(sectionSize),
|
||||
cursor: &Point{0, 0},
|
||||
}
|
||||
s.history = fmt.Sprintf("g%d,%d", sectionSize, cellsPerSection)
|
||||
s.history = fmt.Sprintf("g(%d,%d)", sectionSize, cellsPerSection)
|
||||
for x := 0; x < sectionSize; x++ {
|
||||
for y := 0; y < sectionSize; y++ {
|
||||
s.initSection(Point{x, y})
|
||||
@ -49,11 +50,6 @@ 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++ {
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
@ -49,24 +48,6 @@ 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(`g2,2(i\d,\d[xo]{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