Commit 285e2a99 authored by Boris Mühmer's avatar Boris Mühmer
Browse files

evening snapshot

parent 2b6ad8b3
Loading
Loading
Loading
Loading
+301 −0
Original line number Diff line number Diff line
package board

import (
	"encoding/json"
	"fmt"
	"strings"
)

type Piece uint

const (
	PieceUnknown Piece = 1 << iota
	PieceEmpty
	PieceBlack
	PieceWhite
)

const (
	textPieceUnknown = "?"
	textPieceEmpty   = "E"
	textPieceBlack   = "B"
	textPieceWhite   = "W"
)

func (p Piece) String() string {
	switch p {
	case PieceEmpty:
		return textPieceEmpty
	case PieceBlack:
		return textPieceBlack
	case PieceWhite:
		return textPieceWhite
	default:
		return textPieceUnknown
	}
}

func ParsePiece(s string) Piece {
	switch strings.ToUpper(s) {
	case textPieceEmpty:
		return PieceEmpty
	case textPieceBlack:
		return PieceBlack
	case textPieceWhite:
		return PieceWhite
	default:
		return PieceUnknown
	}
}

type Point uint

const (
	PointXX Point = 1 << iota
	PointA7
	PointD7
	PointG7
	PointB6
	PointD6
	PointF6
	PointC5
	PointD5
	PointE5
	PointA4
	PointB4
	PointC4
	PointE4
	PointF4
	PointG4
	PointC3
	PointD3
	PointE3
	PointB2
	PointD2
	PointF2
	PointA1
	PointD1
	PointG1
)

const (
	textPointXX = "??"
	textPointA7 = "A7"
	textPointD7 = "D7"
	textPointG7 = "G7"
	textPointB6 = "B6"
	textPointD6 = "D6"
	textPointF6 = "F6"
	textPointC5 = "C5"
	textPointD5 = "D5"
	textPointE5 = "E5"
	textPointA4 = "A4"
	textPointB4 = "B4"
	textPointC4 = "C4"
	textPointE4 = "E4"
	textPointF4 = "F4"
	textPointG4 = "G4"
	textPointC3 = "C3"
	textPointD3 = "D3"
	textPointE3 = "E3"
	textPointB2 = "B2"
	textPointD2 = "D2"
	textPointF2 = "F2"
	textPointA1 = "A1"
	textPointD1 = "D1"
	textPointG1 = "G1"
)

func (p Point) String() string {
	switch p {
	case PointA7:
		return textPointA7
	case PointD7:
		return textPointD7
	case PointG7:
		return textPointG7
	case PointB6:
		return textPointB6
	case PointD6:
		return textPointD6
	case PointF6:
		return textPointF6
	case PointC5:
		return textPointC5
	case PointD5:
		return textPointD5
	case PointE5:
		return textPointE5
	case PointA4:
		return textPointA4
	case PointB4:
		return textPointB4
	case PointC4:
		return textPointC4
	case PointE4:
		return textPointE4
	case PointF4:
		return textPointF4
	case PointG4:
		return textPointG4
	case PointC3:
		return textPointC3
	case PointD3:
		return textPointD3
	case PointE3:
		return textPointE3
	case PointB2:
		return textPointB2
	case PointD2:
		return textPointD2
	case PointF2:
		return textPointF2
	case PointA1:
		return textPointA1
	case PointD1:
		return textPointD1
	case PointG1:
		return textPointG1
	default:
		return textPointXX
	}
}

func ParsePoint(s string) Point {
	switch strings.ToUpper(s) {
	case textPointA7:
		return PointA7
	case textPointD7:
		return PointD7
	case textPointG7:
		return PointG7
	case textPointB6:
		return PointB6
	case textPointD6:
		return PointD6
	case textPointF6:
		return PointF6
	case textPointC5:
		return PointC5
	case textPointD5:
		return PointD5
	case textPointE5:
		return PointE5
	case textPointA4:
		return PointA4
	case textPointB4:
		return PointB4
	case textPointC4:
		return PointC4
	case textPointE4:
		return PointE4
	case textPointF4:
		return PointF4
	case textPointG4:
		return PointG4
	case textPointC3:
		return PointC3
	case textPointD3:
		return PointD3
	case textPointE3:
		return PointE3
	case textPointB2:
		return PointB2
	case textPointD2:
		return PointD2
	case textPointF2:
		return PointF2
	case textPointA1:
		return PointA1
	case textPointD1:
		return PointD1
	case textPointG1:
		return PointG1
	default:
		return PointXX
	}
}

func Points() []Point {
	return []Point{
		PointA7,
		PointD7,
		PointG7,
		PointB6,
		PointD6,
		PointF6,
		PointC5,
		PointD5,
		PointE5,
		PointA4,
		PointB4,
		PointC4,
		PointE4,
		PointF4,
		PointG4,
		PointC3,
		PointD3,
		PointE3,
		PointB2,
		PointD2,
		PointF2,
		PointA1,
		PointD1,
		PointG1,
	}
}

type Mill [3]Point

func Mills() []Mill {
	return []Mill{
		{PointA7, PointD7, PointG7},
		{PointB6, PointD6, PointF6},
		{PointC5, PointD5, PointE5},
		{PointA4, PointB4, PointC4},
		{PointE4, PointF4, PointG4},
		{PointC3, PointD3, PointE3},
		{PointB2, PointD2, PointF2},
		{PointA1, PointD1, PointG1},
		{PointA7, PointA4, PointA1},
		{PointB6, PointB4, PointB2},
		{PointC5, PointC4, PointC3},
		{PointD7, PointD6, PointD5},
		{PointD3, PointD2, PointD1},
		{PointE5, PointE4, PointE3},
		{PointF6, PointF4, PointF2},
		{PointG7, PointG4, PointG1},
	}
}

type Board struct {
	Points map[Point]Piece `json:"points,omitempty"`
}

func New() *Board {
	b := &Board{
		Points: make(map[Point]Piece),
	}

	for _, p := range Points() {
		b.Points[p] = PieceEmpty
	}

	return b
}

func (b *Board) String() string {
	return fmt.Sprintf("%#v", b.Points)
}

func (b *Board) JSON() ([]byte, error) {
	return json.MarshalIndent(b.Points, "", "  ")
}

func FromJSON(bs []byte) (*Board, error) {
	var b Board
	if err := json.Unmarshal(bs, &b.Points); err != nil {
		return nil, err
	}
	return &b, nil
}
+30 −0
Original line number Diff line number Diff line
package board

import (
	"reflect"
	"testing"
)

func TestBoard(t *testing.T) {
	b := New()
	t.Logf("Board: %s", b)
	//for k, v := range b.Points {
	//	t.Logf("%v: %s", k, v)
	//}
	j, err := b.JSON()
	if err != nil {
		t.Fatalf("JSON() failed with: %s", err)
	}
	t.Logf("Board as JSON:\n%s", string(j))
	//t.Logf("Data: %#v", j)

	b2, err := FromJSON(j)
	if err != nil {
		t.Fatalf("FromJSON() failed with: %s", err)
	}
	//t.Logf("Board: %s", b2)

	if !reflect.DeepEqual(b, b2) {
		t.Fatal("boards do not match")
	}
}