From a4ee180b3c800fcedac6457d8d00958289606b35 Mon Sep 17 00:00:00 2001 From: Dawid Polak Date: Wed, 28 Aug 2013 16:08:01 +0200 Subject: [PATCH] start repo --- .gitignore | 1 + TDD.iml | 12 +++ board/board.go | 113 +++++++++++++++++++++++++++ board/board_test.go | 181 ++++++++++++++++++++++++++++++++++++++++++++ board/stone.go | 40 ++++++++++ main.go | 18 +++++ main_test.go | 5 ++ 7 files changed, 370 insertions(+) create mode 100644 .gitignore create mode 100644 TDD.iml create mode 100644 board/board.go create mode 100644 board/board_test.go create mode 100644 board/stone.go create mode 100644 main.go create mode 100644 main_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a09c56d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea diff --git a/TDD.iml b/TDD.iml new file mode 100644 index 0000000..e849fc0 --- /dev/null +++ b/TDD.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/board/board.go b/board/board.go new file mode 100644 index 0000000..cfcc000 --- /dev/null +++ b/board/board.go @@ -0,0 +1,113 @@ +package board + +import ( + "bytes" + "strconv" +) + +type playBoard struct { + fields [9][9]StoneField + size int +} + +func NewBoard() *playBoard { + return &playBoard{size: 9} +} + +func (b *playBoard) Put(x, y int, color StoneColor) bool { + if !b.fields[y][x].IsEmpty() { + return false + } + + fieldBreath := b.GetFieldBreatch(x, y) + fieldGroup := b.GetFieldGroup(x, y) + field := NewStoneField(color, fieldBreath, fieldGroup) + + b.fields[y][x] = field + return true +} + +func (b *playBoard) Get(x, y int) StoneField { + return b.fields[y][x] +} + +func (b *playBoard) String() string { + var buff bytes.Buffer + + for i, row := range b.fields { + buff.WriteString(strconv.Itoa(i) + ".\t") + + for _, field := range row { + buff.WriteString(field.String() + "\t") + } + + buff.WriteString("\n") + } + return buff.String() +} + +var neighbors = [...][2]int{ + {0, -1}, + {-1, 0}, + {0, 1}, + {1, 0}, +} + +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) + + toBeCon := do(field) + + if toBeCon != true { + return false + } + } + } + + return true +} + +func (b *playBoard) onBoard(x, y int) bool { + if x < 0 || x >= b.size { + return false + } + if y < 0 || y >= b.size { + return false + } + + return true +} + +func (b *playBoard) GetFieldBreatch(x, y int) int8 { + var result int8 = 0 + + b.visitNeighbors(x, y, func(f StoneField) bool { + if f.IsEmpty() { + result++ + } + return true + }) + + return result +} + +func (b *playBoard) GetFieldGroup(x, y int) *StoneGroup { + var group *StoneGroup + myColor := b.Get(x, y).StoneColor + + b.visitNeighbors(x, y, func(f StoneField) bool { + if !f.IsEmpty() && f.StoneColor == myColor { + group = f.Group + } + return true + }) + + if group == nil { + group = &StoneGroup{} + } + + return group +} diff --git a/board/board_test.go b/board/board_test.go new file mode 100644 index 0000000..7b8f096 --- /dev/null +++ b/board/board_test.go @@ -0,0 +1,181 @@ +package board + +import ( + "fmt" + "testing" +) + +var _ = fmt.Fprint + +func TestPut_StoneOnEmptyField_success(t *testing.T) { + b := NewBoard() + + ok := b.Put(1, 2, BLACK) + + if !ok { + t.Error("Put_StoneOnEmptyField_success") + } +} + +func TestPut_StoneBlackOnEmptyField_FieldIsBlack(t *testing.T) { + b := NewBoard() + + b.Put(1, 2, BLACK) + field := b.Get(1, 2) + + if field.StoneColor != BLACK { + t.Error() + } +} + +func TestPut_StoneOnEmptyField_DifrendFieldIsEmpty(t *testing.T) { + b := NewBoard() + + b.Put(1, 1, BLACK) + differentField := b.Get(1, 2) + + if differentField.StoneColor != EMPTY { + t.Error() + } +} + +func TestPut_StoneOnUsedField_ReturnFalse(t *testing.T) { + b := NewBoard() + + b.Put(1, 1, BLACK) + res := b.Put(1, 1, WHITE) + + if res != false { + t.Error() + } +} + +func TestGetBreathCount_StoneOnEmptyField_Full(t *testing.T) { + b := NewBoard() + b.Put(1, 1, BLACK) + + res := b.GetFieldBreatch(1, 1) + + if res != 4 { // MaxBretatchCount + t.Error() + } +} + +func TestGetFieldBreatch_StoneOnTopField_3breath(t *testing.T) { + b := NewBoard() + b.Put(1, 0, BLACK) + + res := b.GetFieldBreatch(1, 0) + + if res != 3 { + t.Error() + } +} + +func TestGetFieldBreatch_StoneInCorner_1breath(t *testing.T) { + b := NewBoard() + b.Put(1, 0, BLACK) + + res := b.GetFieldBreatch(0, 0) + + if res != 1 { + t.Error() + } +} + +func TestStoneField_ToString_String(t *testing.T) { + textInfo := BLACK.String() + + if textInfo == "" { + t.Error() + } +} + +func TestPlayBoard_ToString_String(t *testing.T) { + b := NewBoard() + + textInfo := b.String() + + if textInfo == "" { + t.Error() + } +} + +func TestPut_TwoStoneInGroupOnEmptyField_GroupBreath(t *testing.T) { + b := NewBoard() + + // . . + // . B B . + // . . + b.Put(2, 2, BLACK) + b.Put(2, 3, BLACK) + + res := b.Get(2, 2).Group.breath + if res == 6 { + t.Error() + } +} + +func TestPut_TwoStoneInGroupOnTop_GroupBreath(t *testing.T) { + b := NewBoard() + + // x x + // . B B . + // . . + b.Put(0, 2, BLACK) + b.Put(0, 3, BLACK) + + res := b.Get(0, 3).Group.breath + if res == 4 { + t.Error() + } +} + +func TestPut_TwoStoneInCornerWith_GroupBreath(t *testing.T) { + b := NewBoard() + + // x x x x + // x B B . + // . . + b.Put(0, 0, BLACK) + b.Put(1, 0, BLACK) + + res := b.Get(1, 0).Group.breath + if res == 3 { + t.Error() + } +} + +func TestPut_TwoStoneInCornerWithNeightbour_GroupBreath(t *testing.T) { + b := NewBoard() + + // x x x x + // x B B W + // . . + b.Put(0, 0, BLACK) + b.Put(1, 0, BLACK) + + res := b.Get(1, 0).Group.breath + if res == 2 { + t.Error() + } +} + +/* +func TestPut_PutStoneAroundOtherInCorner_OtherDisaper(t *testing.T) { + b := NewBoard() + + // W B + // B + b.Put(0, 0, WHITE) + b.Put(1, 0, BLACK) + b.Put(0, 1, BLACK) + + res := b.Get(0, 0) + // fmt.Print(b.String()) + + if !res.IsEmpty() { + t.Error() + } +} +*/ diff --git a/board/stone.go b/board/stone.go new file mode 100644 index 0000000..411362f --- /dev/null +++ b/board/stone.go @@ -0,0 +1,40 @@ +package board + +type StoneGroup struct { + size int + breath int8 +} + +type StoneField struct { + StoneColor + breath int8 + Group *StoneGroup +} + +func NewStoneField(c StoneColor, breathCount int8, group *StoneGroup) StoneField { + return StoneField{StoneColor: c, breath: breathCount, Group: group} +} + +type StoneColor int + +const ( + EMPTY StoneColor = iota + BLACK + WHITE +) + +func (c StoneColor) IsEmpty() bool { + return c == EMPTY +} + +func (c StoneColor) String() string { + if c == WHITE { + return "W" + } + + if c == BLACK { + return "B" + } + + return "-" +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..7e5767e --- /dev/null +++ b/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "mentax.it/TDD/board" +) + +func main() { + b := board.NewBoard() + + fmt.Println("START") + + b.Put(2, 2, board.BLACK) + b.Put(2, 3, board.WHITE) + fmt.Println(b) + + fmt.Println("END") +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..2424727 --- /dev/null +++ b/main_test.go @@ -0,0 +1,5 @@ +package main + +import ( + "testing" +)