1
0
mirror of https://github.com/maaslalani/gambit.git synced 2024-12-30 21:19:52 +02:00

move position test

This commit is contained in:
Maas Lalani 2021-12-21 22:36:20 -05:00
parent 77948dbbfc
commit 599b7d4ec5
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000
3 changed files with 111 additions and 9 deletions

69
board/display.go Normal file
View File

@ -0,0 +1,69 @@
package board
import (
"fmt"
"strings"
)
var (
border = []string{
"┌", "┬", "┐",
"├", "┼", "┤",
"└", "┴", "┘",
}
)
const (
vertical = "│"
horizontal = "─"
marginLeft = " "
)
var (
files = []string{"A", "B", "C", "D", "E", "F", "G", "H"}
ranks = []int{7, 6, 5, 4, 3, 2, 1, 0}
)
type Ranks []int
func (b Board) String() string {
var s string
if b.reversed {
ranks = []int{0, 1, 2, 3, 4, 5, 6, 7}
}
for r, row := range ranks {
if r == 0 {
s += buildRow(border[0], border[1], border[2])
s += "\n"
}
for c, cell := range b.grid[row] {
if c == 0 {
s += fmt.Sprintf(" %d ", row+1)
}
s += fmt.Sprintf("%s %s ", vertical, cell)
if c == len(ranks)-1 {
s += vertical
}
}
if r == len(b.grid)-1 {
s += buildRow(border[6], border[7], border[8])
s += "\n "
s += strings.Join(files, " ")
} else {
s += buildRow(border[3], border[4], border[5])
}
s += "\n"
}
return s
}
func buildRow(left, middle, right string) string {
var row []string
row = append(row, left)
for i := 0; i < 7; i++ {
row = append(row, middle)
}
row = append(row, right)
return fmt.Sprintf("\n%s%s", marginLeft, strings.Join(row, horizontal+horizontal+horizontal))
}

33
board/display_test.go Normal file
View File

@ -0,0 +1,33 @@
package board
import (
"testing"
)
func TestDisplay(t *testing.T) {
b := NewBoard()
expected := `
8
7
6
5
4
3
2
1
A B C D E F G H
`
if b.String() != expected {
t.Fatalf("%s\n%s", expected, b)
}
}

View File

@ -4,18 +4,18 @@ import "testing"
func TestPosition(t *testing.T) {
tt := []struct {
s string
row int
col int
s string
}{
{0, 7, "H1"},
{1, 6, "G2"},
{2, 5, "F3"},
{3, 4, "E4"},
{4, 3, "D5"},
{5, 2, "C6"},
{6, 1, "B7"},
{7, 0, "A8"},
{"A8", 7, 0},
{"B7", 6, 1},
{"C6", 5, 2},
{"D5", 4, 3},
{"E4", 3, 4},
{"F3", 2, 5},
{"G2", 1, 6},
{"H1", 0, 7},
}
for i, tc := range tt {