diff --git a/board/bishop.go b/board/bishop.go index 8d891b4..6ed3928 100644 --- a/board/bishop.go +++ b/board/bishop.go @@ -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", @@ -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)...) @@ -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() {} diff --git a/board/board.go b/board/board.go index 5c8a4c3..3e3b50b 100644 --- a/board/board.go +++ b/board/board.go @@ -9,6 +9,7 @@ import ( const boardSize = 8 +//Board represents a game board type Board struct { size int squares [boardSize][boardSize]Square @@ -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 @@ -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 @@ -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() { @@ -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 @@ -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' @@ -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 } diff --git a/board/cursor.go b/board/cursor.go index 04b1e06..dc5fc2d 100644 --- a/board/cursor.go +++ b/board/cursor.go @@ -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 diff --git a/board/king.go b/board/king.go index 7e08b17..1931c06 100644 --- a/board/king.go +++ b/board/king.go @@ -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", @@ -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{} @@ -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 } diff --git a/board/knight.go b/board/knight.go index bb90332..945b482 100644 --- a/board/knight.go +++ b/board/knight.go @@ -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", @@ -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) @@ -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() {} diff --git a/board/pawn.go b/board/pawn.go index 5e1c71a..3b9ddfb 100644 --- a/board/pawn.go +++ b/board/pawn.go @@ -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", @@ -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) @@ -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 } diff --git a/board/piece.go b/board/piece.go index 7b66260..9c60d8c 100644 --- a/board/piece.go +++ b/board/piece.go @@ -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 diff --git a/board/queen.go b/board/queen.go index af0c46f..7c0f09a 100644 --- a/board/queen.go +++ b/board/queen.go @@ -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", @@ -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)...) @@ -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() {} diff --git a/board/rook.go b/board/rook.go index 230be0d..f5101ba 100644 --- a/board/rook.go +++ b/board/rook.go @@ -1,10 +1,12 @@ package board +//Rook implements the Piece interface for rooks type Rook struct { core *piece moved bool } +//NewRook returns a new Rook func NewRook(side Color) Piece { c := &piece{ name: "Rook", @@ -15,26 +17,31 @@ func NewRook(side Color) Piece { } } +//Name returns the name of this piece func (p *Rook) Name() string { return p.core.name } -func (p *Rook) Ascii() string { +//ASCII returns the Ascii representation for this piece +func (p *Rook) ASCII() string { if p.core.side == White { return string('r') - } else { - return string('R') } + return string('R') } +//Unicode returns the unicode rune for this piece func (p *Rook) Unicode() string { return string(FillRook) } +//Side return's this piece's side func (p *Rook) 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 *Rook) ValidMoves(board Board, loc *Square) []Square { moves := []Square{} @@ -45,6 +52,7 @@ func (p *Rook) ValidMoves(board Board, loc *Square) []Square { return moves } +//Move returns whether this piece has moved at least once func (p *Rook) Move() { p.moved = true } diff --git a/board/square.go b/board/square.go index 810a4e7..8055aa7 100644 --- a/board/square.go +++ b/board/square.go @@ -1,5 +1,7 @@ package board +//Square enapsulates information about a square on a board, including +//a reference to any occupant type Square struct { x int y int @@ -8,34 +10,41 @@ type Square struct { color Color } +//Indices returns the array indices of a square func (s *Square) Indices() (int, int) { return s.x, s.y } +//Name returns the name of a square in standard chess annotation func (s *Square) Name() string { return s.name } +//SetOccupant sets a square to contain a reference to a piece func (s *Square) SetOccupant(p *Piece) { if p == nil { s.occupant = nil - } else { - s.occupant = *p } + s.occupant = *p } +//Occupant returns an occupying piece, if any func (s *Square) Occupant() Piece { return s.occupant } +//Occupied returns whether a square is occupied func (s *Square) Occupied() bool { return s.occupant != nil } +/* func (s *Square) Unoccupied() bool { return s.occupant != nil } +*/ +//Color returns the background color of a square func (s *Square) Color() Color { return s.color } diff --git a/board/update.go b/board/update.go index bbfbca7..3764fb3 100644 --- a/board/update.go +++ b/board/update.go @@ -8,8 +8,10 @@ import ( "github.com/gdamore/tcell/v2" ) +//Input enumerates the different actions that a keystroke might represent type Input int +//enumeration of actions const ( Left Input = iota Up @@ -20,34 +22,40 @@ const ( Noop ) +//SortDirection enumerates whether a sort should be ascending or descending type SortDirection int +//SortAxis enumerates whether a sort should be performed Rank-wise or File-wise type SortAxis int +//enumeration of axes const ( Rank SortAxis = iota File ) +//enumeration of directions const ( Forward SortDirection = iota Reverse ) +//Move encapsualtes the data needed to execute a move on an existing Board type Move struct { Loc string Tgt string Seq int } +//ProcessEvent takes a tcell keypress event, interprets it as input, and +//applies it according to cursor mode func (b *Board) ProcessEvent(ev *tcell.EventKey) Move { util.Write(fmt.Sprintf("Active Cursor: %v", b.activeCursor())) input := inputFromKeypress(ev) if b.activeCursor().mode == Insert { return b.applyInsert(input) - } else { - return b.applySelect(input) } + return b.applySelect(input) } func (b *Board) applyInsert(i Input) Move { diff --git a/display/display.go b/display/display.go index 3de400f..2bc470e 100644 --- a/display/display.go +++ b/display/display.go @@ -16,6 +16,7 @@ const ( squareWidth = 2 ) +//Loading displays a loading scrren func Loading(s tcell.Screen) { w, h := s.Size() s.Clear() @@ -26,6 +27,7 @@ func Loading(s tcell.Screen) { s.Show() } +//Greeting displays a ready greeting func Greeting(s tcell.Screen) { w, h := s.Size() s.Clear() @@ -73,6 +75,7 @@ func drawSquare(s tcell.Screen, x1, y1, x2, y2 int, boardStyle tcell.Style, piec drawText(s, hcenter-offset, vcenter, hcenter+offset, vcenter, pieceStyle, text) } +//Render updates a board drawing from a game board func Render(b *board.Board, s tcell.Screen) { s.Clear() defStyle := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset) diff --git a/game/game.go b/game/game.go index abb6a13..c1e707e 100644 --- a/game/game.go +++ b/game/game.go @@ -9,30 +9,35 @@ import ( "0x539.lol/rtc/util" ) +//Game encapsulates data about a game session type Game struct { board *board.Board peerID string client *rpc.Client } -func New(b *board.Board) (*Game, *GameRPC) { +//New returns a new Game struct and GameRPC client +func New(b *board.Board) (*Game, *RPC) { g := &Game{ board: b, } - gr := &GameRPC{ + gr := &RPC{ game: g, } return g, gr } +//SetClient updates the rpc client for a game func (g *Game) SetClient(c *rpc.Client) { g.client = c } -type GameRPC struct { +//RPC is an RPC wrapper for the Game structure +type RPC struct { game *Game } +//SendMove sends a move to a remote peer func (g *Game) SendMove(m board.Move) error { var exit int err := g.client.Call("GameRPC.DoMove", m, &exit) @@ -43,11 +48,13 @@ func (g *Game) SendMove(m board.Move) error { return err } +//Peered returns whether a game session has an active peer func (g *Game) Peered() bool { return g.peerID != "" } -func (gr *GameRPC) DoMove(m board.Move, exit *int) error { +//DoMove executes a move against a local game board +func (gr *RPC) DoMove(m board.Move, exit *int) error { util.Write(fmt.Sprintf("Local move: %v", m)) loc := gr.game.board.Position(m.Loc) tgt := gr.game.board.Position(m.Tgt) @@ -67,7 +74,8 @@ func (gr *GameRPC) DoMove(m board.Move, exit *int) error { return nil } -func (gr *GameRPC) Register(clientID string, exit *int) error { +//Register updates the peerID for a game session +func (gr *RPC) Register(clientID string, exit *int) error { gr.game.peerID = clientID return nil } diff --git a/main.go b/main.go index dd8bf5e..1397dc4 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "0x539.lol/rtc/util" ) +//Config encapsulates runtime config of RTC application for use by envconfig type Config struct { HostA string `default:"localhost"` PortA string `default:"1234"` diff --git a/util/config.go b/util/config.go index 9278b3f..b81edc1 100644 --- a/util/config.go +++ b/util/config.go @@ -1,3 +1,4 @@ package util +//Debug determines whether calls to Write() are executed var Debug bool = false diff --git a/util/util.go b/util/util.go index a134d4b..e6d291f 100644 --- a/util/util.go +++ b/util/util.go @@ -7,6 +7,8 @@ import ( "reflect" ) +//ItemExists takes two interfaces, and returns true if the slice interface can +//be interpreted as a slice of items, containing the item. func ItemExists(slice interface{}, item interface{}) bool { s := reflect.ValueOf(slice) @@ -23,12 +25,14 @@ func ItemExists(slice interface{}, item interface{}) bool { return false } +//Check panics if err is not nil func Check(err error) { if err != nil { panic(err) } } +//Write writes a string to a predetermined log file func Write(str string) { if Debug { log := "log.txt"