Code: Select all
Imports System.Threading
Imports System.Management
Imports VB = Microsoft.VisualBasic
Module modFonctions
Public book_processus As System.Diagnostics.Process
Public book_entree As System.IO.StreamWriter
Public book_sortie As System.IO.StreamReader
Public eval_processus As System.Diagnostics.Process
Public eval_entree As System.IO.StreamWriter
Public eval_sortie As System.IO.StreamReader
Public Sub book_chargerMoteur(engine As String, file As String)
Dim ligne As String
book_processus = New System.Diagnostics.Process()
book_processus.StartInfo.RedirectStandardOutput = True
book_processus.StartInfo.UseShellExecute = False
book_processus.StartInfo.RedirectStandardInput = True
book_processus.StartInfo.CreateNoWindow = True
book_processus.StartInfo.WorkingDirectory = My.Application.Info.DirectoryPath
book_processus.StartInfo.FileName = engine
book_processus.Start()
book_processus.PriorityClass = 64 '64 (idle), 16384 (below normal), 32 (normal), 32768 (above normal), 128 (high), 256 (realtime)
book_entree = book_processus.StandardInput
book_sortie = book_processus.StandardOutput
book_entree.WriteLine("setoption name bookfile value " & file)
book_entree.WriteLine("isready")
ligne = ""
While InStr(ligne, "readyok") = 0
ligne = book_sortie.ReadLine
Thread.Sleep(1)
End While
End Sub
Public Sub book_dechargerMoteur()
book_entree.Close()
book_sortie.Close()
book_processus.Close()
book_entree = Nothing
book_sortie = Nothing
book_processus = Nothing
End Sub
Public Function listBookMoves(position As String) As String
Dim tabMoves() As String, tabPoids() As String, ligne As String, i As Integer
Dim lstMoves As String, lstPoids As String
Dim totalPoids As Integer, poids As Integer
book_entree.WriteLine("setoption name OwnBook value true")
book_entree.WriteLine("position startpos moves " & position)
book_entree.WriteLine("bookprobe")
book_entree.WriteLine("isready")
ligne = ""
lstMoves = ""
lstPoids = ""
totalPoids = 0
Do
ligne = book_sortie.ReadLine
If ligne <> "" And ligne <> "readyok" Then
tabMoves = Split(Trim(Replace(ligne, ")", "")), "(")
lstMoves = lstMoves & tabMoves(0) & ":"
poids = CInt(tabMoves(1))
totalPoids = totalPoids + poids
lstPoids = lstPoids & poids & ":"
Thread.Sleep(1)
End If
Loop Until ligne = "readyok"
If lstMoves <> "" Then
tabMoves = Split(lstMoves, ":")
tabPoids = Split(lstPoids, ":")
lstMoves = ""
For i = 0 To UBound(tabPoids)
If tabMoves(i) <> "" Then
If 1 < 100 * (CInt(tabPoids(i)) / totalPoids) Then
lstMoves = lstMoves & tabMoves(i) & ", "
End If
End If
Next
End If
Return lstMoves
End Function
Public Function listLegalMoves(position As String, maxMultiPV As Integer) As String
Dim ligne As String, move As String, liste As String, pos As Integer
'on cherche tous les coups possibles
book_entree.WriteLine("setoption name OwnBook value false")
book_entree.WriteLine("setoption name MultiPV value " & maxMultiPV)
book_entree.WriteLine("position startpos moves " & position)
book_entree.WriteLine("go depth 1")
ligne = ""
liste = ""
While InStr(ligne, "bestmove", CompareMethod.Text) = 0
ligne = book_sortie.ReadLine
If InStr(ligne, " depth 1 ", CompareMethod.Text) > 0 Then
'info depth 1 seldepth 2 multipv 1 time 1 nps 479000 score mate 1 nodes 479 tbhits 0 pv d8h4
If InStr(ligne, "mate", CompareMethod.Text) = 0 Then
'info depth 1 seldepth 1 multipv 1 score cp 29 nodes 210 nps 105000 hashfull 0 tbhits 353 time 2 pv g1f3 e7e5
pos = InStr(ligne, " pv ", CompareMethod.Text)
If pos > 0 Then
move = ligne.Substring(pos + 3)
'g1f3 e7e5
pos = InStr(move, " ", CompareMethod.Text)
If pos > 0 Then
move = move.Substring(0, move.IndexOf(" "))
'g1f3
End If
If InStr(liste, move & ", ", CompareMethod.Text) = 0 Then
liste = liste & move & ", "
End If
End If
End If
End If
Thread.Sleep(1)
End While
book_entree.WriteLine("stop")
book_entree.WriteLine("setoption name MultiPV value 1")
book_entree.WriteLine("isready")
ligne = ""
While InStr(ligne, "readyok") = 0
ligne = book_sortie.ReadLine
Thread.Sleep(1)
End While
Return liste
End Function
'///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Public Sub eval_chargerMoteur(engineEXP As String)
Dim ligne As String
chargement:
eval_processus = New System.Diagnostics.Process()
eval_processus.StartInfo.RedirectStandardOutput = True
eval_processus.StartInfo.UseShellExecute = False
eval_processus.StartInfo.RedirectStandardInput = True
eval_processus.StartInfo.CreateNoWindow = True
eval_processus.StartInfo.WorkingDirectory = My.Application.Info.DirectoryPath
eval_processus.StartInfo.FileName = engineEXP
eval_processus.Start()
eval_processus.PriorityClass = 64 '64 (idle), 16384 (below normal), 32 (normal), 32768 (above normal), 128 (high), 256 (realtime)
eval_entree = eval_processus.StandardInput
eval_sortie = eval_processus.StandardOutput
eval_entree.WriteLine("setoption name Experience File value E:\JEUX\ARENA CHESS 3.5.1\Engines\Eman\work.exp")
eval_entree.WriteLine("setoption name Experience Read Only value false")
eval_entree.WriteLine("setoption name Experience MultiPV value false")
eval_entree.WriteLine("setoption name Experience Book Max Moves value 100")
eval_entree.WriteLine("setoption name NNUE Eval File value E:\JEUX\ARENA CHESS 3.5.1\Engines\Eman\nn-e1fb1ade4432.nnue")
eval_entree.WriteLine("setoption name Book 1 File value <empty>")
eval_entree.WriteLine("setoption name Book 2 File value <empty>")
eval_entree.WriteLine("setoption name SyzygyPath value E:\JEUX\ARENA CHESS 3.5.1\TB\Syzygy;C:\Syzygy")
eval_entree.WriteLine("setoption name Threads value " & Format(cpu() - 1), "0")
eval_entree.WriteLine("setoption name Hash value 2048")
eval_entree.WriteLine("isready")
ligne = ""
While InStr(ligne, "readyok") = 0
ligne = eval_sortie.ReadLine
If eval_processus.HasExited Then
eval_dechargerMoteur()
GoTo chargement
End If
Thread.Sleep(1)
End While
End Sub
Public Sub eval_dechargerMoteur()
eval_entree.Close()
eval_sortie.Close()
eval_processus.Close()
eval_entree = Nothing
eval_sortie = Nothing
eval_processus = Nothing
End Sub
Public Function expListe(position As String, move As String) As String
Dim chaine As String, ligne As String, tabTmp() As String, profondeur As Integer
eval_entree.WriteLine("position startpos moves " & position)
eval_entree.WriteLine("expex")
eval_entree.WriteLine("isready")
ligne = ""
chaine = ""
While InStr(ligne, "readyok") = 0
ligne = eval_sortie.ReadLine
If InStr(ligne, " depth: ", CompareMethod.Text) > 0 Then
If InStr(ligne, ": " & move, CompareMethod.Text) > 0 Then
'formatage
If InStr(ligne, ",", CompareMethod.Text) > 0 Then
While InStr(ligne, " ", CompareMethod.Text) > 0
ligne = Replace(ligne, " ", " ")
End While
ElseIf InStr(ligne, "PV", CompareMethod.Text) > 0 Then
While InStr(ligne, " ", CompareMethod.Text) > 0
ligne = Replace(ligne, " ", ",")
End While
While InStr(ligne, ",,", CompareMethod.Text) > 0
ligne = Replace(ligne, ",,", ",")
End While
End If
tabTmp = Split(Replace(Replace(ligne, " : ", ","), ": ", ","), ",")
profondeur = CInt(Trim(tabTmp(3)))
If minDepth <= profondeur Then
If InStr(tabTmp(5), "cp") > 0 Then
chaine = Format(CInt(Replace(tabTmp(5), "cp ", "")) / 100, "0.00")
ElseIf InStr(tabTmp(5), "mate") > 0 Then
chaine = Format(CInt(Replace(tabTmp(5), "mate ", "")), "0")
End If
chaine = chaine & "/" & profondeur
nbExperiences = nbExperiences + 1
End If
End If
End If
End While
If chaine = "" Then
chaine = searchMove(position, move) & "/" & minDepth
End If
Return chaine
End Function
Public Function searchMove(position As String, move As String) As Single
Dim ligne As String, score As String
ligne = ""
score = "0"
'on cherche un coup spécifique
eval_entree.WriteLine("position startpos moves " & position)
eval_entree.WriteLine("go depth " & minDepth & " searchmoves " & move)
While InStr(ligne, "bestmove", CompareMethod.Text) = 0
ligne = eval_sortie.ReadLine
If InStr(ligne, " depth " & minDepth, CompareMethod.Text) > 0 Then
If InStr(ligne, " pv ", CompareMethod.Text) > 0 Then
If InStr(ligne, " score cp ", CompareMethod.Text) > 0 Then
score = ligne.Substring(ligne.IndexOf(" score cp ") + 10)
score = Format(CSng(score.Substring(0, score.IndexOf(" "))) / 100, "0.00")
ElseIf InStr(ligne, " score mate ", CompareMethod.Text) > 0 Then
score = ligne.Substring(ligne.IndexOf(" score mate ") + 12)
score = score.Substring(0, score.IndexOf(" "))
Else
MsgBox("en travaux")
End
End If
End If
End If
End While
eval_entree.WriteLine("stop")
eval_entree.WriteLine("ucinewgame")
eval_entree.WriteLine("isready")
ligne = ""
While InStr(ligne, "readyok") = 0
ligne = eval_sortie.ReadLine
End While
nbAnalysis = nbAnalysis + 1
Return CSng(score)
End Function
'///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Public Function cpu(Optional reel As Boolean = False) As Integer
Dim collection As New ManagementObjectSearcher("select * from Win32_Processor"), taches As Integer
taches = 0
For Each element As ManagementObject In collection.Get
If reel Then
taches = taches + element.Properties("NumberOfCores").Value 'cores
Else
taches = taches + element.Properties("NumberOfLogicalProcessors").Value 'threads
End If
Next
Return taches
End Function
Public Function droite(texte As String, longueur As Integer) As String
If longueur > 0 Then
Return VB.Right(texte, longueur)
Else
Return ""
End If
End Function
Public Function gauche(texte As String, longueur As Integer) As String
If longueur > 0 Then
Return VB.left(texte, longueur)
Else
Return ""
End If
End Function
Public Function maxMultiPVMoteur(chaine As String) As Integer
maxMultiPVMoteur = 200
If InStr(chaine, "asmfish", CompareMethod.Text) > 0 Then
maxMultiPVMoteur = 224
ElseIf InStr(chaine, "brainfish", CompareMethod.Text) > 0 _
Or InStr(chaine, "brainlearn", CompareMethod.Text) > 0 _
Or InStr(chaine, "stockfish", CompareMethod.Text) > 0 _
Or InStr(chaine, "cfish", CompareMethod.Text) > 0 _
Or InStr(chaine, "sugar", CompareMethod.Text) > 0 _
Or InStr(chaine, "eman", CompareMethod.Text) > 0 _
Or InStr(chaine, "hypnos", CompareMethod.Text) > 0 _
Or InStr(chaine, "judas", CompareMethod.Text) > 0 _
Or InStr(chaine, "aurora", CompareMethod.Text) > 0 Then
maxMultiPVMoteur = 500
ElseIf InStr(chaine, "houdini", CompareMethod.Text) > 0 Then
maxMultiPVMoteur = 220
ElseIf InStr(chaine, "komodo", CompareMethod.Text) > 0 Then
maxMultiPVMoteur = 218
End If
Return maxMultiPVMoteur
End Function
Public Function nbCaracteres(ByVal chaine As String, ByVal critere As String) As Integer
Return Len(chaine) - Len(Replace(chaine, critere, ""))
End Function
Public Function nomFichier(chemin As String) As String
Return My.Computer.FileSystem.GetName(chemin)
End Function
Public Function trierChaine(serie As String, separateur As String, Optional ordre As Boolean = True) As String
Dim tabChaine() As String
tabChaine = Split(serie, separateur)
If tabChaine(UBound(tabChaine)) = "" Then
ReDim Preserve tabChaine(UBound(tabChaine) - 1)
End If
Array.Sort(tabChaine)
If Not ordre Then
Array.Reverse(tabChaine)
End If
Return String.Join(separateur, tabChaine)
End Function
Public Function trierTableau(tableau() As String, Optional ordre As Boolean = True) As String()
Dim tabChaine() As String
ReDim tabChaine(UBound(tableau))
Array.Copy(tableau, tabChaine, tableau.Length)
Array.Sort(tabChaine)
If Not ordre Then
Array.Reverse(tabChaine)
End If
Return tabChaine
End Function
End Module