Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ChizhovVadim committed Aug 3, 2019
1 parent 009edad commit 0f88d05
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*.dll
*.so
*.dylib
CounterGo
counter/counter

# Test binary, build with `go test -c`
*.test
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ Chess ratings are available at Computer Chess Rating Lists:

## Commands
Counter supports [UCI protocol](http://www.shredderchess.com/chess-info/features/uci-universal-chess-interface.html) commands and own commands:
+ `arena` - start tournament against themself
+ `epd [/home/vadim/wac.epd]` - analyze collection of [test chess positions](https://www.chessprogramming.org/Win_at_Chess)
+ `eval` - trace evaluation function
+ `move e2e4` - play chess with engine in REPL mode

## Features
Expand Down
20 changes: 20 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"fmt"
"strings"
"unicode"
)
Expand Down Expand Up @@ -234,3 +235,22 @@ func ParseMoveSAN(pos *Position, san string) Move {
}
return MoveEmpty
}

func (si SearchInfo) String() string {
var sScore string
if si.Score.Mate != 0 {
sScore = fmt.Sprintf("M%v", si.Score.Mate)
} else {
sScore = fmt.Sprintf("%v", si.Score.Centipawns)
}
var knps = si.Nodes / (si.Time + 1) // *1000/1000==1
var sb strings.Builder
for i, move := range si.MainLine {
if i > 0 {
sb.WriteString(" ")
}
sb.WriteString(move.String())
}
return fmt.Sprintf("depth: %v score: %v knodes: %v time: %v knps: %v pv: %v",
si.Depth, sScore, si.Nodes/1000, si.Time, knps, sb.String())
}
10 changes: 5 additions & 5 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type Engine struct {
Hash IntUciOption
Threads IntUciOption
ExperimentSettings bool
ExperimentSettings BoolUciOption
evalBuilder func() Evaluator
timeManager TimeManager
transTable TransTable
Expand Down Expand Up @@ -78,7 +78,7 @@ func NewEngine(evalBuilder func() Evaluator) *Engine {
return &Engine{
Hash: IntUciOption{Name: "Hash", Value: 16, Min: 4, Max: 1024},
Threads: IntUciOption{Name: "Threads", Value: 1, Min: 1, Max: numCPUs},
ExperimentSettings: false,
ExperimentSettings: BoolUciOption{Name: "ExperimentSettings", Value: false},
evalBuilder: evalBuilder,
}
}
Expand All @@ -88,12 +88,12 @@ func (e *Engine) GetInfo() (name, version, author string) {
}

func (e *Engine) GetOptions() []UciOption {
return []UciOption{&e.Hash, &e.Threads}
return []UciOption{&e.Hash, &e.Threads, &e.ExperimentSettings}
}

func (e *Engine) Prepare() {
if e.transTable == nil || e.transTable.Megabytes() != e.Hash.Value {
e.transTable = NewTransTable(e.Hash.Value)
e.transTable = newTransTable(e.Hash.Value)
}
if e.lateMoveReduction == nil {
e.lateMoveReduction = lmrByMoveIndex
Expand All @@ -106,7 +106,7 @@ func (e *Engine) Prepare() {
for i := range e.threads {
var t = &e.threads[i]
t.engine = e
t.sortTable = NewSortTable()
t.sortTable = &sortTable{}
t.evaluator = e.evalBuilder()
}
}
Expand Down
4 changes: 0 additions & 4 deletions engine/movesort.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ type sortTable struct {
counter [1024]Move
}

func NewSortTable() *sortTable {
return &sortTable{}
}

func (st *sortTable) Clear() {
for i := range st.killers {
st.killers[i] = MoveEmpty
Expand Down
6 changes: 3 additions & 3 deletions engine/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
. "github.com/ChizhovVadim/CounterGo/common"
)

const PawnValue = 100
const pawnValue = 100

var errSearchTimeout = errors.New("search timeout")

Expand Down Expand Up @@ -271,7 +271,7 @@ func (t *thread) alphaBeta(alpha, beta, depth, height int) int {
continue
}
// futility pruning
if depth <= 3 && lazyEval.Value()+PawnValue*depth <= alpha {
if depth <= 3 && lazyEval.Value()+pawnValue*depth <= alpha {
continue
}
}
Expand Down Expand Up @@ -364,7 +364,7 @@ func (t *thread) quiescence(alpha, beta, depth, height int) int {
}
moveCount++
if !isCheck && !danger && !child.IsCheck() &&
eval+moveValue(move)+2*PawnValue <= alpha {
eval+moveValue(move)+2*pawnValue <= alpha {
continue
}
var score = -t.quiescence(-beta, -alpha, depth-1, height+1)
Expand Down
2 changes: 1 addition & 1 deletion engine/timemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (tm *timeManager) BreakIterativeDeepening(line mainLine) bool {
return true
}
if line.depth >= 5 {
if line.score < tm.lastScore-PawnValue/2 {
if line.score < tm.lastScore-pawnValue/2 {
tm.difficulty = maxDifficulty
} else if line.moves[0] != tm.lastBestMove {
tm.difficulty = math.Max(1.5, tm.difficulty)
Expand Down
2 changes: 1 addition & 1 deletion engine/transtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type transTable struct {

// good test: position fen 8/k7/3p4/p2P1p2/P2P1P2/8/8/K7 w - - 0 1
// good test: position fen 8/pp6/2p5/P1P5/1P3k2/3K4/8/8 w - - 5 47
func NewTransTable(megabytes int) *transTable {
func newTransTable(megabytes int) *transTable {
var size = roundPowerOfTwo(1024 * 1024 * megabytes / 16)
return &transTable{
megabytes: megabytes,
Expand Down
4 changes: 2 additions & 2 deletions engine/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func isPawnEndgame(p *Position, side bool) bool {
}

var (
pieceValues = [...]int{0, PawnValue, 4 * PawnValue,
4 * PawnValue, 6 * PawnValue, 12 * PawnValue, 120 * PawnValue}
pieceValues = [...]int{0, pawnValue, 4 * pawnValue,
4 * pawnValue, 6 * pawnValue, 12 * pawnValue, 120 * pawnValue}

pieceValuesSEE = [...]int{0, 1, 4, 4, 6, 12, 120}
)
Expand Down

0 comments on commit 0f88d05

Please sign in to comment.