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 965d83c
Show file tree
Hide file tree
Showing 21 changed files with 212 additions and 115 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
1 change: 0 additions & 1 deletion cmd/polyglot-composer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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
1 change: 0 additions & 1 deletion 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
47 changes: 20 additions & 27 deletions pkg/board/bitboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ 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)
// Return population count (number of 1's).
func (bb BBoard) Count() int {
return bits.OnesCount64(uint64(bb))
}

// Get the position of the Least Signficant
// Get the position of the Least Signficant.
func (bb BBoard) LS1B() int {
return bits.TrailingZeros64(uint64(bb))
}
Expand All @@ -53,23 +53,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 +150,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,14 +177,14 @@ 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)

Expand All @@ -197,7 +197,7 @@ func (b *Board) AttackedSquares(side int, occ BBoard) BBoard {
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 +259,6 @@ func (b *Board) MoveGen() []Move {
umake()
}
}

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

}
}

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

}
}

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

}
}

Expand Down Expand Up @@ -403,7 +399,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 +420,6 @@ func (b *Board) MoveGenKing() []Move {
move |= IS_CAPTURE
}
moves = append(moves, move)

}
}

Expand All @@ -436,7 +431,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 +445,6 @@ func (b *Board) MoveGenKing() []Move {
move |= IS_CAPTURE
}
moves = append(moves, move)

}
}

Expand All @@ -468,7 +461,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 Down Expand Up @@ -535,7 +528,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,7 +554,7 @@ func (b *Board) PlayMovesUCI(uciMoves string) bool {
return true
}

// Return a pointer to the bitboard of the piece moved
// Return a pointer to the bitboard of the piece moved.
func (b *Board) GetBitBoard(piece int, move Move) *BBoard {

Check warning on line 558 in pkg/board/bitboard.go

View workflow job for this annotation

GitHub Actions / code-quality

unused-parameter: parameter 'move' seems to be unused, consider removing or renaming it as _ (revive)
side := WHITE
if piece > 6 {
Expand All @@ -570,15 +563,15 @@ func (b *Board) GetBitBoard(piece int, move Move) *BBoard {
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 +589,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,7 +602,7 @@ 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
Expand Down
Loading

0 comments on commit 965d83c

Please sign in to comment.