Revert "feat: Implement UCI engine with command processing loop" (#2)

* Revert "feat: Implement UCI engine with command processing loop"

This reverts commit f83f9dd036.

* Run CodeQL
This commit is contained in:
Isaac
2025-03-24 12:18:30 -07:00
committed by GitHub
parent 612be866b2
commit 1b3df458a6
6 changed files with 31 additions and 129 deletions

1
.gitignore vendored
View File

@@ -1,2 +1 @@
chess
.vscode

View File

@@ -163,24 +163,6 @@ func (board Game) CreateMoveAlgebra(algebra string) Move {
return board.createMoveFromTarget(piece, algebra[1:])
}
func (board Game) CreateMoveUCI(uci string) Move {
from := CreateCoordAlgebra(uci[:2])
to := CreateCoordAlgebra(uci[2:])
move := board.CreateMove(from, to)
if len(uci) == 5 {
piece, err := GetPiece(rune(uci[4]))
if err != nil {
panic(err)
}
move.promotionTo = piece
}
return move
}
func (board Game) createMoveFromTarget(piece Piece, algebra string) Move {
if len(algebra) == 3 {
target := CreateCoordAlgebra(algebra[1:])

39
main.go
View File

@@ -1,23 +1,46 @@
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/msws/chess/uci"
"github.com/msws/chess/board"
)
func main() {
reader := bufio.NewReader(os.Stdin)
engine := uci.NewEngine(reader, os.Stdout)
board, err := board.FromFEN("r3k2r/p1pPqpb1/1n3np1/1b2N3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R b KQkq - 0 2")
if err != nil {
panic(err)
}
for {
cmd, err := reader.ReadString('\n')
baseMoves := board.GetMoves()
depth := 1
total := 0
println(strings.Join(os.Args, ","))
if len(os.Args) == 2 {
val, err := strconv.Atoi(os.Args[1])
if err != nil {
panic(err)
}
go engine.Print(cmd)
depth = val
}
// fmt.Printf("Depth: %d\n", depth)
for _, move := range baseMoves {
board.MakeMove(move)
perft := board.Perft(depth - 1)
total += perft
fmt.Printf("%v%v: %d\n", move.From.GetAlgebra(), move.To.GetAlgebra(), perft)
board.UndoMove()
}
fmt.Printf("Depth: %d, (%d)", depth, total)
}

View File

@@ -1 +0,0 @@
package main

View File

@@ -1,67 +0,0 @@
package uci
import (
"io"
"github.com/msws/chess/board"
)
type Engine interface {
Debug(bool bool)
Init()
CopyProtection()
SetOption(name string, id string, value string)
Print(message string)
Position(fen string, moves []string)
}
type MyEngine struct {
in io.Reader
out io.Writer
game *board.Game
debug bool
}
// CopyProtection implements Engine.
func (e *MyEngine) CopyProtection() {
e.Println("copyprotection ok")
}
func (e *MyEngine) Init() {
e.Println("id name GoChess2")
e.Println("id author MS")
e.Println("uciok")
}
func NewEngine(in io.Reader, out io.Writer) Engine {
return &MyEngine{
in: in,
out: out,
}
}
func (e *MyEngine) Print(message string) {
e.out.Write([]byte(message))
}
func (e *MyEngine) Println(message string) {
e.Print(message + "\n")
}
func (e *MyEngine) Debug(bool bool) {
e.debug = bool
}
func (e *MyEngine) SetOption(name string, id string, value string) {
}
func (e *MyEngine) Position(fen string, moves []string) {
game, err := board.FromFEN(fen)
if err != nil {
if e.debug {
e.Print(err.Error())
}
return
}
e.game = game
}

View File

@@ -1,34 +0,0 @@
package uci
import (
"io"
"testing"
)
func base() (Engine, io.Reader, io.Writer) {
in, out := io.Pipe()
e := NewEngine(in, out)
return e, in, out
}
func TestInit(t *testing.T) {
e, in, _ := base()
go e.Init()
buf := make([]byte, 1024)
n, err := in.Read(buf)
if err != nil {
t.Error(err)
}
if n == 0 {
t.Error("No data read")
}
if string(buf[:n]) != "id name GoChess2\n" {
t.Error("Unexpected output, got:", string(buf[:n]))
}
}