📄 mainform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Eight_Num_Fengart
{
public partial class MainForm : Form
{
private TextBox[,] txt_Begin, txt_End;
private TextBox[,] txt_Begin2, txt_End2;
private const int n = 3;
private readonly string initbegin = "124365 87", initend = "1238 4765";
private FengartAI AI;
private Direction[] Result;
private int[] board_Start;
private int[] board_End;
private long code_Start;
private long code_Current;
private long code_End;
private int index_show;
public MainForm()
{
InitializeComponent();
txt_Begin = new TextBox[n, n];
txt_End = new TextBox[n, n];
Font font = new Font(new FontFamily(this.Font.Name), 15f, FontStyle.Bold);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
txt_Begin[i, j] = new TextBox();
txt_Begin[i, j].Multiline = true;
txt_Begin[i, j].Size = new Size(30, 30);
txt_Begin[i, j].Location = new Point(i * 32 + 25, j * 32 + 30);
txt_Begin[i, j].Text = Convert.ToString(initbegin[j * n + i]).Trim();
txt_Begin[i, j].BorderStyle = BorderStyle.FixedSingle;
txt_Begin[i, j].Font = font;
txt_Begin[i, j].TextAlign = HorizontalAlignment.Center;
txt_End[i, j] = new TextBox();
txt_End[i, j].Multiline = true;
txt_End[i, j].Size = new Size(30, 30);
txt_End[i, j].Location = new Point(i * 32 + 200, j * 32 + 30);
txt_End[i, j].Text = Convert.ToString(initend[j * n + i]).Trim();
txt_End[i, j].BorderStyle = BorderStyle.FixedSingle;
txt_End[i, j].Font = font;
txt_End[i, j].TextAlign = HorizontalAlignment.Center;
pnl_Input.Controls.Add(txt_Begin[i, j]);
pnl_Input.Controls.Add(txt_End[i, j]);
}
txt_Begin2 = new TextBox[n, n];
txt_End2 = new TextBox[n, n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
txt_Begin2[i, j] = new TextBox();
txt_Begin2[i, j].Multiline = true;
txt_Begin2[i, j].Size = new Size(30, 30);
txt_Begin2[i, j].Location = new Point(i * 32 + 25, j * 32 + 10);
txt_Begin2[i, j].BorderStyle = BorderStyle.FixedSingle;
txt_Begin2[i, j].BackColor = Color.Green;
txt_Begin2[i, j].ForeColor = Color.White;
txt_Begin2[i, j].ReadOnly = true;
txt_Begin2[i, j].Font = font;
txt_Begin2[i, j].TextAlign = HorizontalAlignment.Center;
txt_End2[i, j] = new TextBox();
txt_End2[i, j].Multiline = true;
txt_End2[i, j].Size = new Size(30, 30);
txt_End2[i, j].Location = new Point(i * 32 + 200, j * 32 + 10);
txt_End2[i, j].BorderStyle = BorderStyle.FixedSingle;
txt_End2[i, j].BackColor = Color.Green;
txt_End2[i, j].ForeColor = Color.White;
txt_End2[i, j].ReadOnly = true;
txt_End2[i, j].Font = font;
txt_End2[i, j].TextAlign = HorizontalAlignment.Center;
pnl_Output.Controls.Add(txt_Begin2[i, j]);
pnl_Output.Controls.Add(txt_End2[i, j]);
}
cmb_AI.SelectedIndex = cmb_AI.Items.Count - 1;
cmb_SpanTime.SelectedIndex = 2;
board_Start = new int[n * n];
board_End = new int[n * n];
AI = new FengartAI();
}
private void btn_Go_Click(object sender, System.EventArgs e)
{
btn_Display.Enabled = false;
btn_Front.Enabled = false;
btn_Next.Enabled = false;
int empty = 0;
int i;
rtb_result.Text = "";
for (i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (txt_Begin[j, i].Text.Trim() == "")
board_Start[i * n + j] = empty;
else
board_Start[i * n + j] = int.Parse(txt_Begin[j, i].Text);
if (txt_End[j, i].Text.Trim() == "")
board_End[i * n + j] = empty;
else
board_End[i * n + j] = int.Parse(txt_End[j, i].Text);
}
Answer ans = AI.Compute(board_Start, board_End, int.Parse(txt_MaxDepth.Text), cmb_AI.SelectedIndex);
if (ans == Answer.Exist)
{
rtb_result.Text = cmb_AI.SelectedItem + ":\n[以下是代表空格的移动方向]\n";
Result = AI.Result;
if (Result == null || Result.Length == 0)
{
rtb_result.Text = "无需移动任何空格!";
return;
}
rtb_result.Text += ("需要移动的步数为:" + AI.Result.Length + "\n");
for (i = 0; i < Result.Length; i++)
{
if (AI.Result[i] != Direction.None)
rtb_result.Text += ("第" + (i + 1) + "步移向: " + AI.Result[i] + "\n");
}
btn_Display.Enabled = true;
btn_Front.Enabled = true;
btn_Next.Enabled = true;
Initial_Display();
}
else if (ans == Answer.NotExistInDepth)
{
rtb_result.Text += "在深度为\"" + txt_MaxDepth.Text + "\"的范围内找不到解!\n";
}
else
{
MessageBox.Show("经判断,初始局面无法达到目标局面!");
return;
}
rtb_result.Text += ("搜索结点数:" + AI.Nodes + "\n");
if (cmb_AI.SelectedIndex != 0)
rtb_result.Text += ("命中相同局面:" + AI.Same + "个!\n");
rtb_result.Text += ("搜索的时间:" + AI.Time + "秒\n");
if (AI.Time != 0)
rtb_result.Text += String.Format("搜索的速度:{0:f2}kn/s\n", (AI.Nodes + AI.Same) / AI.Time / 1000);
}
private void Initial_Display()
{
if (Result == null || Result.Length == 0)
return;
index_show = 0;
code_Start = AI.ToIntBoard(board_Start);
code_End = AI.ToIntBoard(board_End);
code_Current = code_Start;
setBoard(txt_Begin2, FengartAI.getBoard(code_Start));
setBoard(txt_End2, FengartAI.getBoard(code_End));
}
private void btn_Display_Click(object sender, EventArgs e)
{
setBoard(txt_Begin2, FengartAI.getBoard(code_Start));
setBoard(txt_End2, FengartAI.getBoard(code_End));
code_Current = code_Start;
index_show = 0;
rtb_result.Text = ("演示开始!\n");
tmr_Display.Interval = int.Parse(cmb_SpanTime.SelectedItem.ToString());
tmr_Display.Enabled = true;
}
private void setBoard(TextBox[,] box, int board)
{
int emp = board % 10;
int temp = board;
int num;
for (int j = n - 1; j >= 0; j--)
for (int i = n - 1; i >= 0; i--)
{
if ((j * n + i) != (emp - 1))
{
temp /= 10;
num = temp % 10;
box[i, j].Text = num.ToString();
box[i, j].BackColor = Color.Green;
}
else
{
box[i, j].Text = "";
box[i, j].BackColor = Color.White;
}
}
}
private void btn_Front_Click(object sender, EventArgs e)
{
if (index_show > 0 && index_show <= Result.Length)
{
index_show--;
code_Current = AI.unchange(code_Current, AI.Result[index_show]);
setBoard(txt_Begin2, FengartAI.getBoard(code_Current));
}
}
private void btn_Next_Click(object sender, EventArgs e)
{
if (index_show >= 0 && index_show < Result.Length - 1)
{
code_Current = FengartAI.change(code_Current, Result[index_show]);
setBoard(txt_Begin2, FengartAI.getBoard(code_Current));
index_show++;
}
else if (index_show == Result.Length - 1)
{
setBoard(txt_Begin2, FengartAI.getBoard(code_End));
code_Current = code_End;
index_show++;
}
}
private void txt_MaxDepth_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
btn_Go_Click(sender, e);
}
private void tmr_Display_Tick(object sender, EventArgs e)
{
if (index_show >= 0 && index_show < Result.Length)
{
code_Current = FengartAI.change(code_Current, Result[index_show]);
if (index_show < Result.Length - 1)
setBoard(txt_Begin2, FengartAI.getBoard(code_Current));
else
{
setBoard(txt_Begin2, FengartAI.getBoard(code_End));
code_Current = code_End;
}
if (Result[index_show] != Direction.None)
rtb_result.Text += ("第" + (index_show + 1) + "步移向: " + Result[index_show] + "\n");
index_show++;
}
else
tmr_Display.Enabled = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -