Skip to content

Commit

Permalink
chore(linter): code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
likeawizard committed Feb 28, 2024
1 parent c029fda commit 836d80e
Show file tree
Hide file tree
Showing 22 changed files with 282 additions and 205 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Code Quality
on:
pull_request:
jobs:
code-quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.22
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.56.2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
/pgn/*
/*.bin
/*.pgn
Expand Down
84 changes: 84 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
# golangci options
run:
timeout: 3m
skip-dirs:
- cmd/playground
output:
format: colored-line-number
print-issued-lines: true
print-linter-name: true
uniq-by-line: true
linters-settings:
errcheck:
check-type-assertions: true
check-blank: false
gci:
custom-order: true
sections:
- standard
- prefix(github.com/toggl/toggl_work_api)
- default
gocognit:
min-complexity: 15
govet:
enable:
- fieldalignment
revive:
ignore-generated-header: false
severity: warning
confidence: 0.8
rules:
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: error-return
- name: error-strings
- name: error-naming
- name: exported
- name: increment-decrement
# - name: var-naming
- name: var-declaration
- name: package-comments
- name: range
- name: receiver-naming
- name: time-naming
- name: indent-error-flow
- name: errorf
- name: empty-block
- name: superfluous-else
- name: unused-parameter
linters:
enable:
# Enable by default, see more in https://golangci-lint.run/usage/linters/#enabled-by-default-linters
- errcheck
- gci
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
# Custom to project, see more in https://golangci-lint.run/usage/linters/#disabled-by-default-linters--e--enable
# - gosec
- godot
# - gocognit
# - gocyclo
- gofmt
- revive
- gofumpt
- asciicheck
- dogsled
- exportloopref
- errcheck
- gocritic
- goimports
- goprintffuncname
- misspell
- nakedret
- nolintlint
- rowserrcheck
- unconvert
- unparam
- whitespace
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ build:
go build -o polyglot-composer cmd/polyglot-composer/main.go

build-texel:
go build -o texel-data cmd/texel-data/main.go
go build -o texel-data cmd/texel-data/main.go

lint:
golangci-lint run

lint-fix:
golangci-lint run --fix
2 changes: 1 addition & 1 deletion cmd/polyglot-composer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/signal"
"sync"

_ "github.com/likeawizard/polyglot-composer/pkg/logger"
"github.com/likeawizard/polyglot-composer/pkg/pgn"
"github.com/likeawizard/polyglot-composer/pkg/polyglot"
)
Expand Down Expand Up @@ -40,7 +41,6 @@ SourceLoop:
default:
}
pp, err := pgn.NewPGNParser(path, true)

if err != nil {
fmt.Printf("could not load pgn file: %s with error: %s\n", path, err)
continue
Expand Down
3 changes: 1 addition & 2 deletions cmd/texel-data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ SourceLoop:
default:
}
pp, err := pgn.NewPGNParser(path, false)

if err != nil {
fmt.Printf("could not load pgn file: %s with error: %s\n", path, err)
continue
Expand Down Expand Up @@ -66,7 +65,7 @@ SourceLoop:

writer := bufio.NewWriter(file)
for fen := range fenChan {
writer.WriteString(fen)
_, _ = writer.WriteString(fen)
}
writer.Flush()
writeWG.Done()
Expand Down
53 changes: 22 additions & 31 deletions pkg/board/bitboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,25 @@ func (bb BBoard) String() string {
return s
}

// Get the bit at position
// Get the bit at position.
func (bb *BBoard) Get(sq int) BBoard {
return *bb >> sq & 1
}

// Set a bit to one at position
// Set a bit to one at position.
func (bb *BBoard) Set(sq int) {
*bb |= SquareBitboards[sq]
}

// Set a bit to zero at position
// Set a bit to zero at position.
func (bb *BBoard) Clear(sq int) {
*bb &= ^SquareBitboards[sq]
}

// Return population count (number of 1's)
func (bb BBoard) Count() int {
return bits.OnesCount64(uint64(bb))
}

// Get the position of the Least Signficant
func (bb BBoard) LS1B() int {
return bits.TrailingZeros64(uint64(bb))
}
Expand All @@ -53,23 +51,23 @@ func (bb *BBoard) PopLS1B() int {
return ls1b
}

// Get bishop attack mask with blocker occupancy
// Get bishop attack mask with blocker occupancy.
func GetBishopAttacks(sq int, occ BBoard) BBoard {
occ &= BishopAttackMasks[sq]
occ *= BishopMagics[sq]
occ >>= 64 - BishopOccBitCount[sq]
return BishopAttacks[sq][occ]
}

// Get Rook attack mask with blocker occupancy
// Get Rook attack mask with blocker occupancy.
func GetRookAttacks(sq int, occ BBoard) BBoard {
occ &= RookAttackMasks[sq]
occ *= RookMagics[sq]
occ >>= 64 - RookOccBitCount[sq]
return RookAttacks[sq][occ]
}

// Get Queen attacks as a Bishop and Rook superposition
// Get Queen attacks as a Bishop and Rook superposition.
func GetQueenAttacks(sq int, occ BBoard) BBoard {
return GetBishopAttacks(sq, occ) | GetRookAttacks(sq, occ)
}
Expand Down Expand Up @@ -150,7 +148,7 @@ func (b *Board) GetChecksBB(side int) (BBoard, bool) {
return checks, numChecks > 1
}

// Determine if a square is attacked by the opposing side
// Determine if a square is attacked by the opposing side.
func (b *Board) IsAttacked(sq, side int, occ BBoard) bool {
var isAttacked bool

Expand All @@ -177,27 +175,27 @@ func (b *Board) IsAttacked(sq, side int, occ BBoard) bool {
return isAttacked
}

// Determine if the king for the given side is in check
// Determine if the king for the given side is in check.
func (b *Board) IsChecked(side int) bool {
king := b.Pieces[side][KINGS].LS1B()

return b.IsAttacked(king, side, b.Occupancy[BOTH])
}

// Get a bitboard of all the squares attacked by the opposition
// Get a bitboard of all the squares attacked by the opposition.
func (b *Board) AttackedSquares(side int, occ BBoard) BBoard {
attacked := BBoard(0)

for sq := 0; sq < 64; sq++ {
if b.IsAttacked(sq, side, occ) {
attacked = attacked | SquareBitboards[sq]
attacked |= SquareBitboards[sq]
}
}

return attacked
}

// Generate all legal moves for the current side to move
// Generate all legal moves for the current side to move.
func (b *Board) MoveGen() []Move {
var from, to int
var pieces, attacks BBoard
Expand Down Expand Up @@ -259,7 +257,6 @@ func (b *Board) MoveGen() []Move {
umake()
}
}

} else {
pieces = b.Pieces[BLACK][PAWNS]
for pieces > 0 {
Expand Down Expand Up @@ -331,7 +328,6 @@ func (b *Board) MoveGen() []Move {
move |= IS_CAPTURE
}
moves = append(moves, move)

}
}

Expand All @@ -352,7 +348,6 @@ func (b *Board) MoveGen() []Move {
move |= IS_CAPTURE
}
moves = append(moves, move)

}
}

Expand All @@ -373,7 +368,6 @@ func (b *Board) MoveGen() []Move {
move |= IS_CAPTURE
}
moves = append(moves, move)

}
}

Expand Down Expand Up @@ -403,7 +397,7 @@ func (b *Board) MoveGen() []Move {
// return b.RemoveIllegal(moves)
}

// Return king moves for the current side to move
// Return king moves for the current side to move.
func (b *Board) MoveGenKing() []Move {
var from, to int
var pieces, attacks, attackedSquares BBoard
Expand All @@ -424,7 +418,6 @@ func (b *Board) MoveGenKing() []Move {
move |= IS_CAPTURE
}
moves = append(moves, move)

}
}

Expand All @@ -436,7 +429,6 @@ func (b *Board) MoveGenKing() []Move {
moves = append(moves, WCastleQueen)
}
}

} else {
attackedSquares = b.AttackedSquares(b.Side, b.Occupancy[BOTH]&^b.Pieces[b.Side][KINGS])
pieces = b.Pieces[BLACK][KINGS]
Expand All @@ -451,7 +443,6 @@ func (b *Board) MoveGenKing() []Move {
move |= IS_CAPTURE
}
moves = append(moves, move)

}
}

Expand All @@ -468,7 +459,7 @@ func (b *Board) MoveGenKing() []Move {
return moves
}

// Generate a function to return the board state the it's current state
// Generate a function to return the board state the it's current state.
func (b *Board) GetUnmake() func() {
copy := b.Copy()
return func() {
Expand All @@ -492,7 +483,7 @@ func (b *Board) MakeMove(move Move) func() {
b.HalfMoveCounter++
}

bitboard := b.GetBitBoard(move.Piece(), move)
bitboard := b.GetBitBoard(move.Piece())
switch {
case move.IsEnPassant():

Expand Down Expand Up @@ -535,7 +526,7 @@ func (b *Board) MakeMove(move Move) func() {
return umove
}

// Attempt to play a UCI move in position. Returns unmake closure and ok
// Attempt to play a UCI move in position. Returns unmake closure and ok.
func (b *Board) MoveUCI(uciMove string) (func(), bool) {
all := b.MoveGen()

Expand All @@ -561,24 +552,24 @@ func (b *Board) PlayMovesUCI(uciMoves string) bool {
return true
}

// Return a pointer to the bitboard of the piece moved
func (b *Board) GetBitBoard(piece int, move Move) *BBoard {
// Return a pointer to the bitboard of the piece moved.
func (b *Board) GetBitBoard(piece int) *BBoard {
side := WHITE
if piece > 6 {
side = BLACK
}
return &b.Pieces[side][(piece-1)%6]
}

// Remove a piece captured by a move from the opposing bitboard
// Remove a piece captured by a move from the opposing bitboard.
func (b *Board) RemoveCaptured(sq int) {
b.Occupancy[b.Side^1].Clear(sq)
for piece := PAWNS; piece <= KINGS; piece++ {
b.Pieces[b.Side^1][piece] &= b.Occupancy[b.Side^1]
}
}

// Make the complimentary rook move when castling
// Make the complimentary rook move when castling.
func (b *Board) CompleteCastling(move Move) {
bitboard := &b.Pieces[b.Side][ROOKS]
var rookMove Move
Expand All @@ -596,7 +587,7 @@ func (b *Board) CompleteCastling(move Move) {
bitboard.Clear(int(rookMove.From()))
}

// Get the piece at square as a collection of values: found, color, piece
// Get the piece at square as a collection of values: found, color, piece.
func (b *Board) PieceAtSquare(sq Square) (bool, int, int) {
for color := WHITE; color <= BLACK; color++ {
for pieceType := PAWNS; pieceType <= KINGS; pieceType++ {
Expand All @@ -609,14 +600,14 @@ func (b *Board) PieceAtSquare(sq Square) (bool, int, int) {
return false, 0, 0
}

// Replace a pawn on the 8th/1st rank with the promotion piece
// Replace a pawn on the 8th/1st rank with the promotion piece.
func (b *Board) Promote(move Move) {
if move.Promotion() == 0 {
return
}

var pawnBitBoard, promotionBitBoard *BBoard
pawnBitBoard = b.GetBitBoard(move.Piece(), move)
pawnBitBoard = b.GetBitBoard(move.Piece())
var pieceIdx int

switch move.Promotion() {
Expand Down
Loading

0 comments on commit 836d80e

Please sign in to comment.