Skip to content

Commit

Permalink
user input
Browse files Browse the repository at this point in the history
  • Loading branch information
DeyV committed Aug 29, 2013
1 parent 0735f54 commit ae9e5b5
Show file tree
Hide file tree
Showing 4 changed files with 397 additions and 33 deletions.
72 changes: 57 additions & 15 deletions board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import (
"strconv"
)

type playBoard struct {
type PlayBoard struct {
fields [9][9]StoneField
size int
}

func NewBoard() *playBoard {
return &playBoard{size: 9}
func NewBoard() *PlayBoard {
return &PlayBoard{size: 9}
}

func (b *playBoard) Put(x, y int, color StoneColor) bool {
func (b *PlayBoard) Size() int {
return b.size
}

func (b *PlayBoard) Put(x, y int, color StoneColor) bool {
if !b.Get(x, y).IsEmpty() {
// return false
panic(fmt.Sprintf("this field ( %d, %d ) is not empty", x, y))
Expand All @@ -26,6 +30,11 @@ func (b *playBoard) Put(x, y int, color StoneColor) bool {
field := NewStoneField(color, fieldBreath, fieldGroup)

fieldGroup.breath += fieldBreath

if fieldGroup.breath <= 0 {
panic(fmt.Sprintf("You cant put stone on field ( %d, %d ) ", x, y))
}

fieldGroup.size++

b.fields[y][x] = field
Expand All @@ -35,11 +44,11 @@ func (b *playBoard) Put(x, y int, color StoneColor) bool {
return true
}

func (b *playBoard) Get(x, y int) *StoneField {
func (b *PlayBoard) Get(x, y int) *StoneField {
return &b.fields[y][x]
}

func (b *playBoard) String() string {
func (b *PlayBoard) String() string {
var buff bytes.Buffer

for i, row := range b.fields {
Expand All @@ -61,10 +70,10 @@ var neighbors = [...][2]int{
{1, 0},
}

func (b *playBoard) visitNeighbors(x, y int, do func(*StoneField) bool) bool {
func (b *PlayBoard) visitNeighbors(x, y int, do func(*StoneField) bool) bool {
for _, row := range neighbors {
if b.onBoard(row[0]+y, row[1]+x) {
field := b.Get(row[0]+y, row[1]+x)
if b.onBoard(row[0]+x, row[1]+y) {
field := b.Get(row[0]+x, row[1]+y)

toBeCon := do(field)

Expand All @@ -77,7 +86,7 @@ func (b *playBoard) visitNeighbors(x, y int, do func(*StoneField) bool) bool {
return true
}

func (b *playBoard) onBoard(x, y int) bool {
func (b *PlayBoard) onBoard(x, y int) bool {
if x < 0 || x >= b.size {
return false
}
Expand All @@ -88,7 +97,7 @@ func (b *playBoard) onBoard(x, y int) bool {
return true
}

func (b *playBoard) GetFieldBreatch(x, y int) int8 {
func (b *PlayBoard) GetFieldBreatch(x, y int) int8 {
var result int8 = 0

b.visitNeighbors(x, y, func(f *StoneField) bool {
Expand All @@ -101,7 +110,7 @@ func (b *playBoard) GetFieldBreatch(x, y int) int8 {
return result
}

func (b *playBoard) GetNearGroup(x, y int, color StoneColor) *StoneGroup {
func (b *PlayBoard) GetNearGroup(x, y int, color StoneColor) *StoneGroup {
var group *StoneGroup

b.visitNeighbors(x, y, func(f *StoneField) bool {
Expand All @@ -119,18 +128,51 @@ func (b *playBoard) GetNearGroup(x, y int, color StoneColor) *StoneGroup {
return group
}

func (b *playBoard) RemoveNeighborBreath(x, y int) bool {
func (b *PlayBoard) RemoveGroup(gr *StoneGroup) int {
i := 0
for y, row := range b.fields {
for x, field := range row {
if field.Group == gr {
b.clearField(x, y)
i++
}
}
}
return i
}

func (b *PlayBoard) clearField(x, y int) bool {
field := b.Get(x, y)
field.StoneColor = EMPTY

group := field.Group

b.visitNeighbors(x, y, func(f *StoneField) bool {
if f.IsEmpty() {
return true
}

if f.Group == group {
return true // its group removed at this moment
}

f.ChangeBreath(1)
return true
})
return true
}

func (b *PlayBoard) RemoveNeighborBreath(x, y int) bool {
groupLive := true
b.visitNeighbors(x, y, func(f *StoneField) bool {
fmt.Println("group RemoveNeighborBreath", f, x, y)
if f.IsEmpty() {
return true
}

groupLive = f.ChangeBreath(-1)

if !groupLive {
fmt.Println("group deadth")
b.RemoveGroup(f.Group)
}

return true
Expand Down
59 changes: 46 additions & 13 deletions board/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,34 @@ func TestGetFieldBreatch_StoneInCorner_1breath(t *testing.T) {
assert.Equal(t, 1, res)
}

func TestGetFieldBreatch_SecondStoneClose_1breathLess(t *testing.T) {
func TestGetFieldBreatch_SecondStoneClose_1BreathLess(t *testing.T) {
b := NewBoard()

// . .
// . B B .
// . .
b.Put(1, 1, BLACK)
b.Put(1, 2, WHITE)
b.Put(2, 1, BLACK)

res := b.GetFieldBreatch(1, 1)
res := b.GetFieldBreatch(2, 1)

assert.Equal(t, 3, res)
assert.Equal(t, 3, b.Get(2, 1).breath)
}

func TestGetFieldBreatch_TopRowSecondStoneClose_1BreathLess(t *testing.T) {
b := NewBoard()

// x x
// . B B .
// . .
b.Put(1, 0, BLACK)
b.Put(2, 0, BLACK)

res := b.GetFieldBreatch(2, 0)

assert.Equal(t, 2, res)
assert.Equal(t, 2, b.Get(2, 0).breath)
}

func TestStoneField_ToString_String(t *testing.T) {
Expand Down Expand Up @@ -146,21 +162,26 @@ func TestPut_TwoStoneInGroupOnEmptyField_GroupBreath(t *testing.T) {
func TestPut_TwoStoneInGroupOnTop_GroupBreath(t *testing.T) {
b := NewBoard()

// x x
// . .
// . B B .
// . .
b.Put(2, 0, BLACK)
fmt.Println(b)
b.Put(3, 0, BLACK)

res := b.Get(3, 0).Group.breath
b.Put(2, 1, BLACK)
// fmt.Println(b)

fmt.Println(b)
res := b.Get(2, 1).Group.breath

assert.Equal(t, 4, res)

b.Put(3, 1, BLACK)

f2 := b.Get(3, 1)

// fmt.Println(b)

assert.Equal(t, 3, f2.breath)
assert.Equal(t, 6, f2.Group.breath)
}

/*
func TestPut_TwoStoneInCornerWith_GroupBreath(t *testing.T) {
b := NewBoard()

Expand Down Expand Up @@ -206,7 +227,18 @@ func TestPut_ThreeStoneInCornerWithNeightbour_GroupBreath(t *testing.T) {
assert.Equal(t, 3, res)
}

/*
func TestRemoveGroup_OneFieldGroup_EmptyField(t *testing.T) {
b := NewBoard()

b.Put(0, 0, BLACK)
group := b.Get(0, 0).Group
i := b.RemoveGroup(group)

res := b.Get(0, 0).IsEmpty()
assert.True(t, res)
assert.Equal(t, 1, i) // one stone
}

func TestPut_PutStoneAroundOtherInCorner_OtherDisaper(t *testing.T) {
b := NewBoard()

Expand All @@ -224,4 +256,5 @@ func TestPut_PutStoneAroundOtherInCorner_OtherDisaper(t *testing.T) {
}
}

*/
/*
*/
Loading

0 comments on commit ae9e5b5

Please sign in to comment.