Skip to content

Commit

Permalink
lint and comment on exported values
Browse files Browse the repository at this point in the history
  • Loading branch information
imw committed Feb 15, 2022
1 parent da33ba0 commit a179ab1
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 35 deletions.
14 changes: 11 additions & 3 deletions board/bishop.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package board

//Bishop implements the Piece interface for bishops
type Bishop struct {
core *piece
}

//NewBishop returns a new Bishop
func NewBishop(side Color) Piece {
c := &piece{
name: "Bishop",
Expand All @@ -14,26 +16,31 @@ func NewBishop(side Color) Piece {
}
}

//Name returns the name of this piece
func (p *Bishop) Name() string {
return p.core.name
}

func (p *Bishop) Ascii() string {
//ASCII returns the Ascii representation for this piece
func (p *Bishop) ASCII() string {
if p.core.side == White {
return string('b')
} else {
return string('B')
}
return string('B')
}

//Unicode returns the unicode rune for this piece
func (p *Bishop) Unicode() string {
return string(FillBishop)
}

//Side return's this piece's side
func (p *Bishop) Side() Color {
return p.core.side
}

//ValidMoves returns a slice of squares representing valid moves for this
//piece, given board and location
func (p *Bishop) ValidMoves(board Board, loc *Square) []Square {
moves := []Square{}
moves = append(moves, seekForwardL(board, loc, boardSize)...)
Expand All @@ -43,4 +50,5 @@ func (p *Bishop) ValidMoves(board Board, loc *Square) []Square {
return moves
}

//Move is a noop for Bishops
func (p *Bishop) Move() {}
20 changes: 14 additions & 6 deletions board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

const boardSize = 8

//Board represents a game board
type Board struct {
size int
squares [boardSize][boardSize]Square
Expand All @@ -17,8 +18,10 @@ type Board struct {
toMove Color
}

//Color enumerates the opposing sides
type Color int

//possible colors are White and Black
const (
White Color = iota
Black
Expand All @@ -27,12 +30,11 @@ const (
func invertColor(c Color) Color {
if c == White {
return Black
} else {
return White
}
return White
}

//create and color squares
//New returns a new, initialized game board
func New(c Color) *Board {
b := new(Board)
b.size = boardSize
Expand Down Expand Up @@ -71,9 +73,8 @@ func New(c Color) *Board {
func (b *Board) activeCursor() *Cursor {
if b.toMove == White {
return &b.whiteCursor
} else {
return &b.blackCursor
}
return &b.blackCursor
}

func (b *Board) switchCursor() {
Expand Down Expand Up @@ -102,27 +103,33 @@ func (b *Board) resetCursor() {
}
}

//Moves returns the valid next moves for the currently active cursor
func (b *Board) Moves() []Square {
c := b.activeCursor()
return c.choices(*b)
}

//Target returns the target of the currently active cursor
func (b *Board) Target() *Square {
return b.activeCursor().target
}

//Loc returns the location of the currently active cursor
func (b *Board) Loc() *Square {
return b.activeCursor().loc
}

//SetLoc moves the currently active cursor's location to s
func (b *Board) SetLoc(s *Square) {
b.activeCursor().loc = s
}

//Side returns which side's cursor is active
func (b *Board) Side() Color {
return b.activeCursor().color
}

//Flatten returns a sorted 1-d array of pointers to board squares
func (b *Board) Flatten() [boardSize * boardSize]*Square {
sqs := [boardSize * boardSize]*Square{}
k := 0
Expand All @@ -135,7 +142,7 @@ func (b *Board) Flatten() [boardSize * boardSize]*Square {
return sqs
}

//decode file rune to i, convert rank to j and invert
//Position returns a pointer to a square given the name of that square
func (b *Board) Position(name string) *Square {
file := name[0]
fileidx := file - 'A'
Expand Down Expand Up @@ -172,6 +179,7 @@ func (b *Board) setup() {
b.Position("H8").occupant = NewRook(Black)
}

//Size returns size of board
func (b *Board) Size() int {
return b.size
}
3 changes: 3 additions & 0 deletions board/cursor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package board

//CursorMode enumerates whether cursor is set to select or insert
type CursorMode int

//Select chooses a piece to move, Insert chooses a target
const (
Select CursorMode = iota
Insert
)

//Cursor encapsulates information about move selection
type Cursor struct {
loc *Square
target *Square
Expand Down
14 changes: 11 additions & 3 deletions board/king.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package board

//King implements the Piece interface for kings
type King struct {
core *piece
moved bool
}

//NewKing returns a new King
func NewKing(side Color) Piece {
c := &piece{
name: "King",
Expand All @@ -15,26 +17,31 @@ func NewKing(side Color) Piece {
}
}

//Name returns the name of this piece
func (p *King) Name() string {
return p.core.name
}

func (p *King) Ascii() string {
//ASCII returns the Ascii representation for this piece
func (p *King) ASCII() string {
if p.core.side == White {
return string('k')
} else {
return string('K')
}
return string('K')
}

//Unicode returns the unicode rune for this piece
func (p *King) Unicode() string {
return string(FillKing)
}

//Side return's this piece's side
func (p *King) Side() Color {
return p.core.side
}

//ValidMoves returns a slice of squares representing valid moves for this
//piece, given board and location
//TODO: Castling
func (p *King) ValidMoves(board Board, loc *Square) []Square {
moves := []Square{}
Expand All @@ -49,6 +56,7 @@ func (p *King) ValidMoves(board Board, loc *Square) []Square {
return moves
}

//Move marks piece as having moved at least once
func (p *King) Move() {
p.moved = true
}
15 changes: 11 additions & 4 deletions board/knight.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package board

//Knight implements the Piece interface for knights
type Knight struct {
core *piece
}

//NewKnight returns a new Knight
func NewKnight(side Color) Piece {
c := &piece{
name: "Knight",
Expand All @@ -14,26 +16,31 @@ func NewKnight(side Color) Piece {
}
}

//Name returns the name of this piece
func (p *Knight) Name() string {
return p.core.name
}

func (p *Knight) Ascii() string {
//ASCII returns the Ascii representation for this piece
func (p *Knight) ASCII() string {
if p.core.side == White {
return string('n')
} else {
return string('N')
}
return string('N')
}

//Unicode returns the unicode rune for this piece
func (p *Knight) Unicode() string {
return string(FillKnight)
}

//Side return's this piece's side
func (p *Knight) Side() Color {
return p.core.side
}

//ValidMoves returns a slice of squares representing valid moves for this
//piece, given board and location
func (p *Knight) ValidMoves(board Board, loc *Square) []Square {
moves := []Square{}
moves = append(moves, *loc)
Expand Down Expand Up @@ -62,5 +69,5 @@ func (p *Knight) ValidMoves(board Board, loc *Square) []Square {
return moves
}

//NOOP
//Move is a noop for Knights
func (p *Knight) Move() {}
14 changes: 11 additions & 3 deletions board/pawn.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package board

//Pawn implements the Piece interface for Pawns
type Pawn struct {
core *piece
moved bool
}

//NewPawn returns a new Pawn
func NewPawn(side Color) Piece {
c := &piece{
name: "Pawn",
Expand All @@ -15,26 +17,31 @@ func NewPawn(side Color) Piece {
}
}

//Name returns the name of this piece
func (p *Pawn) Name() string {
return p.core.name
}

func (p *Pawn) Ascii() string {
//ASCII returns the Ascii representation for this piece
func (p *Pawn) ASCII() string {
if p.core.side == White {
return string('P')
} else {
return string('p')
}
return string('p')
}

//Unicode returns the unicode rune for this piece
func (p *Pawn) Unicode() string {
return string(FillPawn)
}

//Side return's this piece's side
func (p *Pawn) Side() Color {
return p.core.side
}

//ValidMoves returns a slice of squares representing valid moves for this
//piece, given board and location
func (p *Pawn) ValidMoves(board Board, loc *Square) []Square {
moves := []Square{}
moves = append(moves, *loc)
Expand Down Expand Up @@ -96,6 +103,7 @@ func removeSquare(sqs []Square, index int) []Square {
return append(sqs[:index], sqs[index+1:]...)
}

//Move returns whether this piece has moved at least once
func (p *Pawn) Move() {
p.moved = true
}
3 changes: 2 additions & 1 deletion board/piece.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package board

//Piece is an universal interface for difference chess pieces
type Piece interface {
Name() string
Ascii() string
ASCII() string
Unicode() string
Side() Color
ValidMoves(Board, *Square) []Square
Expand Down
14 changes: 11 additions & 3 deletions board/queen.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package board

//Queen implements the Piece interface for queens
type Queen struct {
core *piece
}

//NewQueen returns a new queen
func NewQueen(side Color) Piece {
c := &piece{
name: "Queen",
Expand All @@ -14,26 +16,31 @@ func NewQueen(side Color) Piece {
}
}

//Name returns the name of this piece
func (p *Queen) Name() string {
return p.core.name
}

func (p *Queen) Ascii() string {
//ASCII returns the Ascii representation for this piece
func (p *Queen) ASCII() string {
if p.core.side == White {
return string('q')
} else {
return string('Q')
}
return string('Q')
}

//Unicode returns the unicode rune for this piece
func (p *Queen) Unicode() string {
return string(FillQueen)
}

//Side return's this piece's side
func (p *Queen) Side() Color {
return p.core.side
}

//ValidMoves returns a slice of squares representing valid moves for this
//piece, given board and location
func (p *Queen) ValidMoves(board Board, loc *Square) []Square {
moves := []Square{}
moves = append(moves, seekForward(board, loc, boardSize)...)
Expand All @@ -47,4 +54,5 @@ func (p *Queen) ValidMoves(board Board, loc *Square) []Square {
return moves
}

//Move is a noop for queens
func (p *Queen) Move() {}
Loading

0 comments on commit a179ab1

Please sign in to comment.