Skip to content

Commit

Permalink
增加自动点击功能,优化识别,现在可以自动续盘了
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincentzyx committed Jul 4, 2022
1 parent 3de29a2 commit 6f9ca5c
Show file tree
Hide file tree
Showing 23 changed files with 19,054 additions and 77 deletions.
70 changes: 63 additions & 7 deletions VinXiangQi/DetectionLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,40 @@ partial class Mainform
Bitmap LastBoardAreaBitmap = null;
// 当失败次数过多时,自动重载
int InvalidCount = 0;
// 是否第一次收到绝杀
bool FirstMate = true;

void AutoClickLoop()
{
while (Running)
{
try
{
if (!Settings.AutoClick)
{
Thread.Sleep(500);
continue;
}
Bitmap screen = Screenshot();
foreach (Bitmap image in AutoClickImageList.ToArray())
{
Point pos = ImageHelper.FindImageFromTop(screen, image);
if (pos.X != -1)
{
Point clickPos = new Point(pos.X + image.Width / 2, pos.Y + image.Height / 2);
MouseLeftClick(clickPos.X, clickPos.Y);
Thread.Sleep(1000);
}
}
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
File.AppendAllText("err.log", DateTime.Now.ToString() + "\r\n" + ex.ToString() + "\r\n\r\n");
}
Thread.Sleep(5000);
}
}

void DetectionLoop()
{
Expand Down Expand Up @@ -55,13 +89,13 @@ void DetectionLoop()
}
Bitmap screenshot = Screenshot();
Size maxSize = screenshot.Size;
int sameCount = 0;
bool reloaded = false;
if (LastBoardAreaBitmap != null && BoardArea.X != -1)
{
Bitmap currentBoard = (Bitmap)ImageHelper.CropImage(screenshot, BoardArea);
if (Settings.KeepDetecting)
{
DisplayStatus("YOLO检测图像");
DisplayStatus("检测图像");
bool result = ModelPredict(currentBoard);
if (result)
{
Expand All @@ -80,13 +114,15 @@ void DetectionLoop()
{
CurrentBoard = (string[,])Board.Clone();
currentFenCount = 1;
reloaded = false;
}
else
{
currentFenCount++;
if (currentFenCount >= 2)
if (currentFenCount >= 2 && !reloaded)
{
ReloadBoard();
reloaded = true;
}
}
}
Expand All @@ -106,7 +142,6 @@ void DetectionLoop()
{
if (!ImageHelper.AreEqual(currentBoard, LastBoardAreaBitmap) || ForceRefresh)
{
sameCount = 0;
if (ForceRefresh) ForceRefresh = false;
HaveUpdated = true;
LastBoardAreaBitmap = currentBoard;
Expand All @@ -117,7 +152,7 @@ void DetectionLoop()
}
else
{
if (sameCount >= 0 && HaveUpdated)
if (HaveUpdated)
{
DisplayStatus("YOLO检测图像");
bool result = ModelPredict(currentBoard);
Expand Down Expand Up @@ -365,7 +400,6 @@ bool ModelPredict(Bitmap image)

void ReloadBoard()
{
RenderDisplayBoard();
var compResult = Utils.CompareBoard(LastBoard, Board);
if (compResult.DiffCount == 0) return;
string opponentSymbol = Settings.RedSide ? "b_" : "r_";
Expand All @@ -385,6 +419,7 @@ void ReloadBoard()
}
if (compResult.DiffCount == 2 && compResult.Chess.Contains(mySymbol))
{
RenderDisplayBoard();
if (Settings.AnalyzingMode)
{
InvalidCount = 0;
Expand All @@ -396,13 +431,16 @@ void ReloadBoard()
}
else
{
DisplayStatus("己方棋子变化,跳过");
InvalidCount = 0;
LastBoard = (string[,])Board.Clone();
DisplayStatus("己方棋子变化,跳过分析");
}
return;
}
var compResultExpected = Utils.CompareBoard(ExpectedBoard, Board);
if (compResultExpected.DiffCount == 0)
{
RenderDisplayBoard();
DisplayStatus("和预期棋盘一样,跳过");
return;
}
Expand All @@ -418,6 +456,7 @@ void ReloadBoard()
}
return;
}
RenderDisplayBoard();
InvalidCount = 0;
EngineAnalyzeCount++;
LastBoard = (string[,])Board.Clone();
Expand All @@ -432,6 +471,23 @@ private void Engine_InfoEvent(string cmd, Dictionary<string, string> infos)
string info_str = "";
if (infos.ContainsKey("score"))
{
if (Settings.StopWhenMate)
{
if (infos["score"].Contains("绝杀") && int.Parse(infos["score"].Split('(').Last().Split(')').First()) > 0)
{
if (FirstMate)
{
FirstMate = false;
Engine.StopAnalyze();
return;
}
}
else
{
FirstMate = true;
}
}

if (infos["score"].Contains("绝杀") && (double.Parse(infos["depth"]) < 40 || double.Parse(infos["depth"]) > 50))
{
return;
Expand Down
120 changes: 120 additions & 0 deletions VinXiangQi/DetectionLogic.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
Loading

0 comments on commit 6f9ca5c

Please sign in to comment.