//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include "global.h"
#include "about.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFrm_main *Frm_main;
//---------------------------------------------------------------------------
__fastcall TFrm_main::TFrm_main(TComponent* Owner)
: TForm(Owner)
{
Paths = new KTList<RULETYPE>;
//初始化
}
//---------------------------------------------------------------------------
void __fastcall TFrm_main::FormClose(TObject *Sender, TCloseAction &Action)
{
delete Paths;
}
//---------------------------------------------------------------------------
int TFrm_main::GetNum(TSpeedButton *btn)
{
if (btn->Caption != "")
return StrToInt(btn->Caption);
else
return 0;
}
//---------------------------------------------------------------------------
//得到起始状态
void TFrm_main::GetBData(int mData[][3])
{
//重新设置起始状态
mData[0][0] = GetNum(b00); mData[0][1] = GetNum(b01); mData[0][2] = GetNum(b02);
mData[1][0] = GetNum(b10); mData[1][1] = GetNum(b11); mData[1][2] = GetNum(b12);
mData[2][0] = GetNum(b20); mData[2][1] = GetNum(b21); mData[2][2] = GetNum(b22);
}
//---------------------------------------------------------------------------
//得到目标状态
void TFrm_main::GetEData(int mData[][3])
{
//重新设置目标状态
mData[0][0] = GetNum(e00); mData[0][1] = GetNum(e01); mData[0][2] = GetNum(e02);
mData[1][0] = GetNum(e10); mData[1][1] = GetNum(e11); mData[1][2] = GetNum(e12);
mData[2][0] = GetNum(e20); mData[2][1] = GetNum(e21); mData[2][2] = GetNum(e22);
}
//---------------------------------------------------------------------------
//自定义函数,根据NData[3][3]设置当前状态
void TFrm_main::ResetNowState()
{
n00->Caption = IntToStr(NData[0][0]);
n01->Caption = IntToStr(NData[0][1]);
n02->Caption = IntToStr(NData[0][2]);
n10->Caption = IntToStr(NData[1][0]);
n11->Caption = IntToStr(NData[1][1]);
n12->Caption = IntToStr(NData[1][2]);
n20->Caption = IntToStr(NData[2][0]);
n21->Caption = IntToStr(NData[2][1]);
n22->Caption = IntToStr(NData[2][2]);
//找到空格位置,设成按下状态
for(int i=0;i<ComponentCount;i++) {
if ( ((TSpeedButton*)Components[i])->Parent->Name == "GB_NData" ) {
if ( ((TSpeedButton*)Components[i])->Caption == "0" ) {
((TSpeedButton*)Components[i])->Caption = "";
((TSpeedButton*)Components[i])->Down = true;
break;
}
}
}
}
//---------------------------------------------------------------------------
//传进按钮名称(a12)空格s1和位置s2,判断位置移动是否合法
bool TFrm_main::IsLegal(AnsiString s1,AnsiString s2)
{
int si,sj,i,j;
si = StrToInt(s1.SubString(2,1));
sj = StrToInt(s1.SubString(3,1));
i = StrToInt(s2.SubString(2,1));
j = StrToInt(s2.SubString(3,1));
if (si==i)
if (sj == j+1 || sj == j-1)
return true;
if (sj==j)
if (si == i+1 || si == i-1)
return true;
return false;
}
//---------------------------------------------------------------------------
void __fastcall TFrm_main::b00Click(TObject *Sender)
{
//找到空格位置,调换按钮标题
for(int i=0;i<ComponentCount;i++) {
if ( ((TSpeedButton*)Components[i])->Parent->Name == "GB_BData" ) {
if ( ((TSpeedButton*)Components[i])->Caption == "" ) { //找到空格位置
if ( IsLegal(((TSpeedButton*)Components[i])->Name,((TSpeedButton*)Sender)->Name) ) {
((TSpeedButton*)Components[i])->Caption = ((TSpeedButton*)Sender)->Caption; ;
((TSpeedButton*)Sender)->Caption = "";
((TSpeedButton*)Sender)->Down = true;
}
else
((TSpeedButton*)Components[i])->Down = true;
break;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TFrm_main::e00Click(TObject *Sender)
{
//找到空格位置,调换按钮标题
for(int i=0;i<ComponentCount;i++) {
if ( ((TSpeedButton*)Components[i])->Parent->Name == "GB_EData" ) {
if ( ((TSpeedButton*)Components[i])->Caption == "" ) {
if ( IsLegal(((TSpeedButton*)Components[i])->Name,((TSpeedButton*)Sender)->Name) ) {
((TSpeedButton*)Components[i])->Caption = ((TSpeedButton*)Sender)->Caption; ;
((TSpeedButton*)Sender)->Caption = "";
((TSpeedButton*)Sender)->Down = true;
}
else
((TSpeedButton*)Components[i])->Down = true;
break;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TFrm_main::Btn_BRandomClick(TObject *Sender)
{
b00->Caption = '1';
b01->Caption = '2';
b02->Caption = '3';
b10->Caption = '4';
b11->Caption = '5';
b12->Caption = '6';
b20->Caption = '7';
b21->Caption = '8';
b22->Caption = '0';
//找到空格位置,设成按下状态
for(int i=0;i<ComponentCount;i++) {
if ( ((TSpeedButton*)Components[i])->Parent->Name == "GB_BData" ) {
if ( ((TSpeedButton*)Components[i])->Caption == "0" ) {
((TSpeedButton*)Components[i])->Caption = "";
((TSpeedButton*)Components[i])->Down = true;
break;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TFrm_main::Btn_ERandomClick(TObject *Sender)
{
int mData[3][3];
GetBData(mData);
e00->Caption = IntToStr(mData[0][0]);
e01->Caption = IntToStr(mData[0][1]);
e02->Caption = IntToStr(mData[0][2]);
e10->Caption = IntToStr(mData[1][0]);
e11->Caption = IntToStr(mData[1][1]);
e12->Caption = IntToStr(mData[1][2]);
e20->Caption = IntToStr(mData[2][0]);
e21->Caption = IntToStr(mData[2][1]);
e22->Caption = IntToStr(mData[2][2]);
//找到空格位置,设成按下状态
for(int i=0;i<ComponentCount;i++) {
if ( ((TSpeedButton*)Components[i])->Parent->Name == "GB_EData" ) {
if ( ((TSpeedButton*)Components[i])->Caption == "0" ) {
((TSpeedButton*)Components[i])->Caption = "";
((TSpeedButton*)Components[i])->Down = true;
break;
}
}
}
}
//---------------------------------------------------------------------------
//重置
void __fastcall TFrm_main::Btn_ResetClick(TObject *Sender)
{
LB_Step->Caption = "0/0";
GB_BData->Enabled = true;
GB_EData->Enabled = true;
Btn_BRandom->Enabled = true;
Btn_ERandom->Enabled = true;
btn_do->Enabled = true;
Btn_Reset->Enabled = false;
Btn_BData->Enabled = false;
Btn_EData->Enabled = false;
Btn_Next->Enabled = false;
Btn_Prior->Enabled = false;
}
//---------------------------------------------------------------------------
//自动计算步骤
void __fastcall TFrm_main::btn_doClick(TObject *Sender)
{
int mData[3][3];
//从界面得到起始状态
GetBData(mData);
//设置起始状态的数据
ResetBData(mData);
KCopy(NData,mData); //当前状态
ResetNowState();
//从界面得到目标状态
GetEData(mData);
//设置目标状态的数据
ResetEData(mData);
Paths->Empty(); //清空规则序列
//调用核心函数,寻找路径
if (BackTrack(NData,0)) {
//成功,设置界面导航
GB_BData->Enabled = false;
GB_EData->Enabled = false;
Btn_BRandom->Enabled = false;
Btn_ERandom->Enabled = false;
btn_do->Enabled = false;
Btn_Reset->Enabled = true;
Btn_BData->Enabled = true;
Btn_EData->Enabled = true;
Btn_Next->Enabled = true;
Btn_Prior->Enabled = true;
Btn_BDataClick(Send