model

package
v0.0.0-...-e9bdced Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2017 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package model contains Go Board and Game specific types

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GameNames

func GameNames(games []Game) (names []string)

GameNames slice with names of games

Types

type Board

type Board struct {
	Size     int
	Move     int
	Last     Point
	KO       *Point
	Groups   Groups          // id > group
	Members  map[Point]int   // point > group
	Stones   map[Point]Color // point > color
	Captures struct {
		Black int // white stones
		White int // black stones
	}
}

Board supports a straight forward Go game with capturing and KO + suicide detection

Example (EndlessPlay)
b := model.NewBoard(model.Size(3))
// setup
for _, a := range endlessPlaySetup {
	_, _ = b.Place(a.c, a.p)
}
fmt.Println(b)
// loop
for _, a := range endlessPlayLoop {
	_, _ = b.Place(a.c, a.p)
}
fmt.Println(b)
Output:

+abc+
a ● |
b●◯●|
c◯ ◯|
{3 3} w0 b0
+abc+
a ●+|
b●◯●|
c◯ ◯|
{3 2} w4 b4
Example (Suicide)
b := model.NewBoard(model.Size(4))

// setup
for _, a := range []struct {
	c model.Color
	p model.Point
}{
	{c: B, p: P(1, 2)},
	{c: B, p: P(1, 4)},
	{c: B, p: P(2, 1)},
	{c: B, p: P(2, 2)},
	{c: B, p: P(2, 3)},
	{c: B, p: P(2, 4)},
	{c: W, p: P(3, 1)},
	{c: W, p: P(3, 2)},
	{c: W, p: P(3, 3)},
	{c: W, p: P(3, 4)},
	{c: W, p: P(4, 1)},
	{c: W, p: P(4, 3)},
} {
	_, _ = b.Place(a.c, a.p)
}

// suicides
for _, a := range []struct {
	c model.Color
	p model.Point
}{
	{c: B, p: P(4, 2)},
	{c: B, p: P(4, 4)},
	{c: W, p: P(1, 1)},
	{c: W, p: P(1, 3)},
} {
	if r, err := b.Place(a.c, a.p); err == nil || len(r) != 0 {
		fmt.Println(fmt.Sprintf("suicide %s %v expected", a.c, a.p))
	}
}

fmt.Println(b)
Output:

+abcd+
a ●◯◯|
b●●◯ |
c ●◯◯|
d●●◯ |
{4 3} w0 b0

func NewBoard

func NewBoard(s Size) Board

NewBoard returns empty board

func (*Board) Place

func (b *Board) Place(c Color, p Point) (Points, error)

Place a stone, return captures

func (*Board) PlaceIGS

func (b *Board) PlaceIGS(c Color, i string) ([]string, error)

PlaceIGS stone

Example
b := model.NewBoard(model.X13)
for _, a := range []struct {
	c model.Color
	i string
}{
	{c: B, i: "L10"},
	{c: W, i: "D4"},
	{c: B, i: "K3"},
	{c: W, i: "D10"},
	{c: B, i: "J11"},
	{c: W, i: "N13"},
	{c: B, i: "A13"},
	{c: W, i: "A1"},
	{c: B, i: "N1"},
} {
	_, err := b.PlaceIGS(a.c, a.i)
	if err != nil {
		panic(err)
	}
}
fmt.Println(b)
_, err := b.PlaceIGS(W, "N1")
fmt.Println(err)
Output:

+abcdefghjklmn+
a●           ◯|
b             |
c        ●    |
d   ◯      ●  |
e             |
f             |
g             |
h             |
j             |
k   ◯         |
l         ●   |
m             |
n◯           ●|
{13 13} w0 b0
{13 13} is occupied

func (*Board) PlaceSGF

func (b *Board) PlaceSGF(c Color, p string) ([]string, error)

PlaceSGF stone

Example
b := model.NewBoard(model.X9)
for _, a := range []struct {
	c model.Color
	i string
}{
	{c: B, i: "gd"},
	{c: W, i: "df"},
	{c: B, i: "gg"},
	{c: W, i: "ec"},
	{c: B, i: "cc"},
	{c: W, i: "jj"},
	{c: B, i: "aj"},
	{c: W, i: "aa"},
	{c: B, i: "ja"},
} {
	_, err := b.PlaceSGF(a.c, a.i)
	if err != nil {
		panic(err)
	}
}
fmt.Println(b)
_, err := b.PlaceSGF(W, "ja")
fmt.Println(err)
Output:

+abcdefghj+
a◯       ●|
b         |
c  ● ◯    |
d      ●  |
e         |
f   ◯     |
g      ●  |
h         |
j●       ◯|
{9 1} w0 b0
{9 1} is occupied

func (Board) String

func (b Board) String() string

type Color

type Color int

Color black or white (are no colors, but how to name it?)

const (
	Black Color = iota
	White
)

color constants

func (Color) String

func (c Color) String() string

type Date

type Date time.Time

Date alias time.Time with JSON date format

func ToDate

func ToDate(s string) Date

ToDate from JSON string

func Today

func Today() Date

Today Date

func (Date) Add

func (d Date) Add(years int, months int, days int) Date

Add delegates to time.Time.AddDate

func (Date) After

func (d Date) After(o Date) bool

After delegates to time.Time.After

Example
package main

import (
	"fmt"

	"github.com/dgf/gotv/model"
)

func main() {
	today := model.Today()
	yesterday := today.Add(0, 0, -1)

	fmt.Printf("> %v\n", today.After(yesterday))
	fmt.Printf("< %v\n", yesterday.After(today))

}
Output:

> true
< false

func (Date) Before

func (d Date) Before(o Date) bool

Before delegates to time.Time.Before

Example
package main

import (
	"fmt"

	"github.com/dgf/gotv/model"
)

func main() {
	today := model.Today()
	tomorrow := today.Add(0, 0, 1)

	fmt.Printf("< %v\n", today.Before(tomorrow))
	fmt.Printf("> %v\n", tomorrow.Before(today))

}
Output:

< true
> false

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

MarshalJSON calls String

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/dgf/gotv/model"
)

func main() {
	date := model.ToDate("2017-07-23")
	data := map[string]model.Date{"date": date}

	if b, err := json.Marshal(data); err != nil {
		panic(err)
	} else {
		fmt.Print(string(b))
	}

}
Output:

{"date":"2017-07-23"}

func (Date) String

func (d Date) String() string

type Filter

type Filter struct {
	// contains filtered or unexported fields
}

Filter games between min and max values

func (Filter) DateMax

func (f Filter) DateMax(d Date) Filter

DateMax filters maximum Date

func (Filter) DateMin

func (f Filter) DateMin(d Date) Filter

DateMin filters minimum Date

func (Filter) HandicapMax

func (f Filter) HandicapMax(h Handicap) Filter

HandicapMax filters maximum Handicap

func (Filter) HandicapMin

func (f Filter) HandicapMin(h Handicap) Filter

HandicapMin filters minimum Handicap

func (*Filter) Matches

func (f *Filter) Matches(g *Game) bool

Matches all active filters

func (Filter) RankMax

func (f Filter) RankMax(r Rank) Filter

RankMax filters maximum Rank

func (Filter) RankMin

func (f Filter) RankMin(r Rank) Filter

RankMin filters minimum Rank

func (Filter) SizeMax

func (f Filter) SizeMax(s Size) Filter

SizeMax filters maximum Size

func (Filter) SizeMin

func (f Filter) SizeMin(s Size) Filter

SizeMin filters minimum Size

type Game

type Game struct {
	Date     Date     `json:"date"`
	Place    string   `json:"place"`
	Name     string   `json:"name"`
	Black    Player   `json:"black"`
	White    Player   `json:"white"`
	Size     Size     `json:"size"`
	Handicap Handicap `json:"handicap"`
	Komi     Komi     `json:"komi"`
	Result   string   `json:"result"`
}

Game details

type GameSorter

type GameSorter []LessGame

GameSorter sort games

func GameOrder

func GameOrder(less ...LessGame) GameSorter

GameOrder returns a Sorter that sorts using the less functions, in order.

func (GameSorter) Sort

func (s GameSorter) Sort(games []Game)

Sort sorts the argument slice according to the less functions passed to OrderedBy.

type Group

type Group struct {
	Color     Color
	Liberties map[Point]struct{}
	Stones    map[Point]struct{}
}

Group of stones on the board

func (Group) String

func (g Group) String() string

String group dump with sorted members and liberties format: (B|W) m[members...] l[liberties...]

type Groups

type Groups map[int]Group

Groups map games by ID

func (Groups) String

func (g Groups) String() string

String groups dump sorted by ID format: ID (B|W) m[members...] l[liberties...]

type Handicap

type Handicap int

Handicap of a game

const (
	H0 Handicap = iota

	H2
	H3
	H4
	H5
	H6
	H7
	H8
	H9
)

handicap constants: 0, 2, .., 9

func (Handicap) String

func (h Handicap) String() string

type Komi

type Komi float64

Komi value

func (Komi) String

func (k Komi) String() string

type LessGame

type LessGame func(g1, g2 *Game) bool

LessGame partial game comparison

var ByDate LessGame = func(g1, g2 *Game) bool {
	return g1.Date.After(g2.Date)
}

ByDate newest first

var ByDateDesc LessGame = func(g1, g2 *Game) bool {
	return g1.Date.Before(g2.Date)
}

ByDateDesc oldest first

var ByHandicap LessGame = func(g1, g2 *Game) bool {
	return g1.Handicap < g2.Handicap
}

ByHandicap lowest first

var ByHandicapDesc LessGame = func(g1, g2 *Game) bool {
	return g1.Handicap > g2.Handicap
}

ByHandicapDesc highest first

var ByRank LessGame = func(g1, g2 *Game) bool {
	return MaxRank(g1.Black, g1.White) > MaxRank(g2.Black, g2.White)
}

ByRank highest first

var ByRankDesc LessGame = func(g1, g2 *Game) bool {
	return MinRank(g1.Black, g1.White) < MinRank(g2.Black, g2.White)
}

ByRankDesc lowest first

var BySize LessGame = func(g1, g2 *Game) bool {
	return g1.Size > g2.Size
}

BySize greatest first

var BySizeDesc LessGame = func(g1, g2 *Game) bool {
	return g1.Size < g2.Size
}

BySizeDesc smallest first

type Player

type Player struct {
	Name string `json:"name"`
	Rank Rank   `json:"rank"`
}

Player name and rank

func (Player) String

func (g Player) String() string

String player name and rank

type Point

type Point struct{ X, Y int }

Point intersection on the board

type Points

type Points []Point

Points sortable point slice

func DiffPoints

func DiffPoints(A, B Points) (d Points)

DiffPoints of two point slices

Example
package main

import (
	"fmt"

	"github.com/dgf/gotv/model"
)

func P(x, y int) model.Point {
	return model.Point{X: x, Y: y}
}

func main() {
	a := model.Points{P(1, 2), P(3, 4)}
	b := model.Points{P(4, 3), P(1, 2)}
	fmt.Println(model.DiffPoints(a, b))
}
Output:

[{3 4} {4 3}]

func (Points) Len

func (p Points) Len() int

Len is part of sort.Interface

func (Points) Less

func (p Points) Less(i, j int) bool

Less is part of sort.Interface

func (Points) Swap

func (p Points) Swap(i, j int)

Swap is part of sort.Interface

type Rank

type Rank int

Rank sorted as int

const (
	NR Rank = iota
	Kyu30
	Kyu29
	Kyu28
	Kyu27
	Kyu26
	Kyu25
	Kyu24
	Kyu23
	Kyu22
	Kyu21
	Kyu20
	Kyu19
	Kyu18
	Kyu17
	Kyu16
	Kyu15
	Kyu14
	Kyu13
	Kyu12
	Kyu11
	Kyu10
	Kyu9
	Kyu8
	Kyu7
	Kyu6
	Kyu5
	Kyu4
	Kyu3
	Kyu2
	Kyu1
	Dan1
	Dan2
	Dan3
	Dan4
	Dan5
	Dan6
	Dan7
	Dan8
	Dan9
	Pro1
	Pro2
	Pro3
	Pro4
	Pro5
	Pro6
	Pro7
	Pro8
	Pro9
)

rank constants NR = 0, 30 Kyu = 1, ...

func MaxRank

func MaxRank(b Player, w Player) Rank

MaxRank of black or white

func MinRank

func MinRank(b Player, w Player) Rank

MinRank of black or white

func ToRank

func ToRank(r string) Rank

ToRank decodes rank

Example
package main

import (
	"fmt"

	"github.com/dgf/gotv/model"
)

func main() {
	fmt.Printf("%s\n", model.ToRank("unknown"))
	fmt.Printf("%s\n", model.ToRank("5 Kyu"))
	fmt.Printf("%s\n", model.ToRank("4 Dan"))
	fmt.Printf("%s\n", model.ToRank("3 Pro"))
	fmt.Printf("%s\n", model.ToRank("10 Pro"))
}
Output:

NR
5 Kyu
4 Dan
3 Pro
NR

func (Rank) MarshalJSON

func (r Rank) MarshalJSON() ([]byte, error)

MarshalJSON encodes rank

func (Rank) String

func (r Rank) String() string

String rank, unknown returns NR

type Size

type Size int

Size of game or board

const (
	X9  Size = 9
	X13 Size = 13
	X19 Size = 19
)

size constants

func (Size) String

func (s Size) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL