Ejemplo n.º 1
0
func TestEvaluateMaterial(t *testing.T) {
	board := gmc.MustParseFen("r4rk1/2pp1ppp/8/8/5P2/8/PPPPP1PP/RNBQKBNR b KQ c3 0 12")
	v := evaluateMaterial(board)
	if v < 23.5 || v > 24.5 {
		t.Errorf("Expected material advantage for white to be around 24. Actual value: %v", v)
	}
}
Ejemplo n.º 2
0
func ExampleEngine() {
	var e Engine
	defer e.Quit()

	e.SetPosition(chess.MustParseFen(""))
	for info := range e.SearchDepth(6) {
		if err := info.Err(); err != nil {
			log.Fatal(err)
		} else if move, ok := info.BestMove(); ok {
			log.Print("the best move is", move)
		} else {
			log.Print(info.Pv(), info.Stats())
		}
	}
}
Ejemplo n.º 3
0
func TestStockfish(t *testing.T) {
	if true {
		return
	}

	var logger *log.Logger //= log.New(stdout, "", log.LstdFlags)

	e, err := Run("stockfish", nil, logger)
	if err != nil {
		t.Fatalf("%s", err)
	}
	defer e.Quit()

	opt := e.Options()
	w := tabwriter.NewWriter(stdout, 1, 8, 0, ' ', 0)
	for k, v := range opt {
		fmt.Fprintln(w, k, "\t", v)
	}
	w.Flush()

	board := chess.MustParseFen("5r2/pqN1Qpk1/2r3pp/2n1R3/5R2/6P1/4PPKP/8 w - - 0 1")
	e.SetPosition(board)
	for info := range e.SearchDepth(18) {
		if info.Err() != nil {
			t.Fatalf("%s", info.Err())
		}
		if m, ok := info.BestMove(); ok {
			log.Println("bestmove:", m.San(board))
		} else if pv := info.Pv(); pv != nil {
			log.Println("pv:", pv)
			log.Println("stats:", info.Stats())
		} else {
			log.Println("stats:", info.Stats(), info.(Info).line)
		}
	}
}
Ejemplo n.º 4
0
func main() {
	board := gmc.MustParseFen("r4rk1/2pp1ppp/8/8/5P2/8/PPPPP1PP/RNBQKBNR b KQ c3 0 12")
	moves := board.LegalMoves()
	fmt.Printf(moves[0].San(board))
	fmt.Printf("\n")
}
Ejemplo n.º 5
0
func TestEngine(t *testing.T) {
	var logger *log.Logger //= log.New(stdout, "", log.LstdFlags)

	r0, w0 := io.Pipe()
	r1, w1 := io.Pipe()
	go fakeEngine(r1, w0)
	e, err := initialise(r0, w1, w1, logger)
	if err != nil {
		log.Fatal("engine initialisation failed:", err)
	}
	defer e.Quit()

	// test options
	opts := e.Options()
	if opts == nil {
		t.Fatal("no options returned")
	}
	for _, o := range optionTests {
		opt := opts[o.name]
		if opt == nil {
			t.Errorf("option %q not found", o.name)
			continue
		}
		if o.set != "" {
			opt.Set(o.set)
		}
		switch want := o.value.(type) {
		case string:
			s := opt.(*StringOption)
			if got := s.String(); got != want {
				t.Errorf("option %q: want %q, got %q", o.name, want, got)
			}
		case int:
			i := opt.(*IntOption)
			if got := i.Int(); got != want {
				t.Errorf("option %q: want %d, got %d", o.name, want, got)
			}
		case bool:
			b := opt.(*BoolOption)
			if got := b.Bool(); got != want {
				t.Errorf("option %q: want %v, got %v", o.name, want, got)
			}
		}
	}

	// test search
	board := chess.MustParseFen("")
	board = board.MakeMove(chess.Move{chess.E2, chess.E4, 0})
	e.SetPosition(board)

	infoc := e.SearchDepth(1)
	for _, i := range infoTests {
		info := <-infoc
		if info == nil {
			t.Fatal("got nil info instead of:", i.line)
		}
		if err := info.Err(); err != nil {
			t.Fatal("search returned error:", err)
		}
		if move, ok := info.BestMove(); ok {
			if move != *i.bestmove {
				t.Errorf("bestmove mismatch: got %v, want %v: %s",
					move, *i.bestmove, info.(Info).line)
			}
		} else if pv := info.Pv(); pv != nil {
			if pv.Score != i.pvscore {
				t.Errorf("score mismatch: got %d, want %d: %s",
					pv.Score, i.pvscore, info.(Info).line)
			}
		} else {
			stats := info.Stats()
			if *stats != *i.stats {
				t.Errorf("stats mismatch: got %v, want %v: %s",
					stats, i.stats, info.(Info).line)
			}
		}
	}
	if info := <-infoc; info != nil {
		t.Error("spurious info:", info.(Info))
	}
}