1
0
mirror of https://github.com/maaslalani/gambit.git synced 2024-12-26 20:54:07 +02:00

get rid of mostly everything

This commit is contained in:
Maas Lalani 2021-12-24 12:59:14 -05:00
parent c3744d422c
commit fd65298bcf
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000
6 changed files with 6 additions and 212 deletions

View File

@ -1,120 +0,0 @@
package board
import (
"strings"
"github.com/maaslalani/gambit/position"
"github.com/maaslalani/gambit/style"
)
const (
top int = iota
middle
bottom
)
var (
border = map[int][]rune{
top: []rune("┌┬┐"),
middle: []rune("├┼┤"),
bottom: []rune("└┴┘"),
}
files = []string{"A", "B", "C", "D", "E", "F", "G", "H"}
ranks = []int{7, 6, 5, 4, 3, 2, 1, 0}
)
const (
vertical = "│"
horizontal = "─"
)
// String prints the board in a human readable format.
// The left and bottom sides have labels for ranks and files respectively
// All pieces are surrounded with borders.
func (b Board) String() string {
var s string
if b.Reversed {
ranks = reverse(ranks)
}
for r, row := range ranks {
if isFirstRow(r) {
s += buildRow(border[top]) + "\n"
}
for c, cell := range b.Grid[row] {
// Rank labels
if isFirstColumn(c) {
s += " " + style.Faint.Render(position.RowToRank(row)) + " "
}
s += vertical + " "
cellStyle := cell.Style()
if b.Selected != position.NoPosition && row == b.Selected.Row && c == b.Selected.Col {
cellStyle = style.Selected
}
s += cellStyle.Render(cell.String()) + " "
if isLastColumn(c) {
s += vertical
}
}
if !isLastRow(r) {
s += buildRow(border[middle]) + "\n"
} else {
s += buildRow(border[bottom]) + "\n"
// File labels
s += " " + style.Faint.Render(strings.Join(files, " ")) + "\n"
}
}
return s
}
// isLastRow returns whether or not the given row is the last row of the
// board based on the number of ranks on the board.
func isLastRow(i int) bool {
return i == len(ranks)-1
}
// isLastColumn returns whether or not the given column is the last column of
// the board based on the number of files on the board.
func isLastColumn(i int) bool {
return i == len(files)-1
}
// isFirstRow returns whether or not the given row is the first row
func isFirstRow(i int) bool {
return i == 0
}
// isFirstColumn returns whether or not the given column is the first column
func isFirstColumn(i int) bool {
return i == 0
}
// buildRow builds a row string based on the given borders for the left and
// right side and correctly pads the middle with the given character adjusted
// to the number of rows on the board.
func buildRow(border []rune) string {
var row [9]string
row[0] = string(border[0])
row[8] = string(border[2])
for i := 1; i < len(ranks); i++ {
row[i] = string(border[1])
}
return "\n " + strings.Join(row[:], strings.Repeat(horizontal, 3))
}
// reverse reverses the given slice of ints.
func reverse(ranks []int) []int {
var reversed []int
for i := len(ranks) - 1; i >= 0; i-- {
reversed = append(reversed, ranks[i])
}
return reversed
}

View File

@ -1,62 +0,0 @@
package board
import (
"testing"
)
func TestDisplay(t *testing.T) {
b := New()
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)
}
}
func TestDisplayReversed(t *testing.T) {
b := New()
b.Reversed = true
expected := `
1
2
3
4
5
6
7
8
A B C D E F G H
`
if b.String() != expected {
t.Fatalf("%s\n%s", expected, b)
}
}

View File

@ -1,11 +1,10 @@
package game
package main
import (
"strings"
tea "github.com/charmbracelet/bubbletea"
dt "github.com/dylhunn/dragontoothmg"
"github.com/maaslalani/gambit/piece"
)
type model struct {
@ -13,7 +12,7 @@ type model struct {
}
func Model() tea.Model {
board := dt.ParseFen("rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 1")
board := dt.ParseFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
return model{
board: board,
}
@ -28,10 +27,10 @@ func (m model) View() string {
for _, c := range r {
if c >= '1' && c <= '8' {
for i := 0; i < int(c-'0'); i++ {
s.WriteString(piece.Display[""])
s.WriteString(Display[""])
}
} else {
s.WriteString(piece.Display[string(c)])
s.WriteString(Display[string(c)])
}
}
s.WriteRune('\n')
@ -39,16 +38,6 @@ func (m model) View() string {
return s.String()
}
const (
cellHeight = 2
cellWidth = 4
marginLeft = 4
marginTop = 2
maxCol = 7
maxRow = 7
)
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.MouseMsg:

View File

@ -4,12 +4,11 @@ import (
"log"
tea "github.com/charmbracelet/bubbletea"
"github.com/maaslalani/gambit/game"
)
func main() {
p := tea.NewProgram(
game.Model(),
Model(),
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
)

View File

@ -1,4 +1,4 @@
package piece
package main
var Display = map[string]string{
"": " ",

View File

@ -1,12 +0,0 @@
package style
import (
. "github.com/charmbracelet/lipgloss"
)
var (
Faint = NewStyle().Foreground(Color("8"))
White = NewStyle().Foreground(Color("7"))
Black = NewStyle().Foreground(Color("4"))
Selected = NewStyle().Foreground(Color("2"))
)