From fd65298bcf85ccac3bb8c9b9eefb7c468bb0137d Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Fri, 24 Dec 2021 12:59:14 -0500 Subject: [PATCH] get rid of mostly everything --- board/display.go | 120 ------------------------------------- board/display_test.go | 62 ------------------- game/game.go => game.go | 19 ++---- main.go | 3 +- piece/piece.go => piece.go | 2 +- style/style.go | 12 ---- 6 files changed, 6 insertions(+), 212 deletions(-) delete mode 100644 board/display.go delete mode 100644 board/display_test.go rename game/game.go => game.go (73%) rename piece/piece.go => piece.go (93%) delete mode 100644 style/style.go diff --git a/board/display.go b/board/display.go deleted file mode 100644 index 7c911c4..0000000 --- a/board/display.go +++ /dev/null @@ -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 -} diff --git a/board/display_test.go b/board/display_test.go deleted file mode 100644 index d0ac13e..0000000 --- a/board/display_test.go +++ /dev/null @@ -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) - } -} diff --git a/game/game.go b/game.go similarity index 73% rename from game/game.go rename to game.go index 165ba85..98b284a 100644 --- a/game/game.go +++ b/game.go @@ -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: diff --git a/main.go b/main.go index 43cd13a..cfc168e 100644 --- a/main.go +++ b/main.go @@ -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(), ) diff --git a/piece/piece.go b/piece.go similarity index 93% rename from piece/piece.go rename to piece.go index 19a01e5..192bcf2 100644 --- a/piece/piece.go +++ b/piece.go @@ -1,4 +1,4 @@ -package piece +package main var Display = map[string]string{ "": " ", diff --git a/style/style.go b/style/style.go deleted file mode 100644 index 5980e37..0000000 --- a/style/style.go +++ /dev/null @@ -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")) -)