-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a4ee180
Showing
7 changed files
with
370 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/.idea |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="GO_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="Go sdk go1.1.1 windows/amd64" jdkType="Google Go SDK" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} | ||
*/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "-" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) |