?? aidlg.cpp
字號:
test_tbfs[step].B += p->search->getB();
//a1
p->search = &p->astar1;
p->search->search(begin, end, stop, 1);
test_a1[step].step += p->search->getTotalnode();
test_a1[step].B += p->search->getB();
//a2
p->search = &p->astar2;
p->search->search(begin, end, stop, 1);
test_a2[step].step += p->search->getTotalnode();
test_a2[step].B += p->search->getB();
//ids
p->search = &p->idfs;
p->search->search(begin, end, stop);
test_ids[step].step += p->search->getTotalnode();
test_ids[step].B += p->search->getB();
}
FILE *file = fopen("test.txt", "w");
fprintf(file, "%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n",
"STEP", "TIME", "bfs", "tbfs",
"astar1", "astar2", "idfs", "bfs", "tbfs", "astar1", "astar2", "idfs");
for(int i = 1; i < 32; ++i){
if(!count[i])
continue;
fprintf(file, "%-10d%-10d%-10d%-10d%-10d%-10d%-10d%-10.3lf%-10.3lf%-10.3lf%-10.3lf%-10.3lf\n",
i,
count[i],
test_bfs[i].step / count[i],
test_tbfs[i].step / count[i],
test_a1[i].step / count[i],
test_a2[i].step / count[i],
test_ids[i].step / count[i],
test_bfs[i].B / count[i],
test_tbfs[i].B / count[i],
test_a1[i].B / count[i],
test_a2[i].B / count[i],
test_ids[i].B / count[i]
);
}
fclose(file);
return 0;
}
void CAIDlg::test()
{
HANDLE hThread1 = CreateThread(NULL, 0, TestProc, this, 0, NULL);
CloseHandle(hThread1);
}
#endif
void CAIDlg::OnBnClickedButtonBegin()
{
// TODO: 在此添加控件通知處理程序代碼
#ifdef TEST
test();
#else
gamestart = true; //防止按鈕選擇
CString str;
if(GetDlgItemText(IDC_BUTTON_BEGIN, str), str == "停止搜索"){
stop = 1;
return;
}
if(statusReady() == false){
MessageBox("未設置初未狀態!", "錯誤");
return;
}
SetDlgItemText(IDC_BUTTON_BEGIN, "停止搜索");
( (CButton *)GetDlgItem(IDC_CHECK_HAND) )->EnableWindow(FALSE);
//初始
prevstep.EnableWindow(FALSE);
nextstep.EnableWindow(FALSE);
recover.EnableWindow(FALSE);
display.EnableWindow(FALSE);
//狀態框按鈕
( (CButton *) GetDlgItem(IDC_BUTTON_DEFAULT) )->EnableWindow(FALSE);
( (CButton *) GetDlgItem(IDC_BUTTON_RANDOM) )->EnableWindow(FALSE);
( (CButton *) GetDlgItem(IDC_BUTTON_HAND) )->EnableWindow(FALSE);
( (CButton *) GetDlgItem(IDC_CHECK_BEGIN) )->EnableWindow(FALSE);
( (CButton *) GetDlgItem(IDC_CHECK_END) )->EnableWindow(FALSE);
( (CButton *) GetDlgItem(IDC_CHECK_NOANSWER) )->EnableWindow(FALSE);
HANDLE hThread1 = CreateThread(NULL, 0, SearchProc, this, 0, NULL);
CloseHandle(hThread1);
#endif
}
void CAIDlg::move(int pos)
{
static const int C[]={2, 3, 2, 3, 4, 3, 2, 3, 2};
static const int EP[][4]={{1,3,0,0},{0,2,4,0},{1,5,0,0},{0,4,6,0},{1,3,5,7},{2,4,8,0},{3,7,0,0},{4,6,8,0},{5,7,0,0}};
if(pos_begin_return[pos] == -1)
return;
int i;
for(i = 0; i < C[pos]; ++i){
if(pos_begin_return[ EP[pos][i] ] == -1){
m_selectval = pos_begin_return[pos];
changeBitmap(m_init, select_begin, pos_begin, pos_begin_return, EP[pos][i]);
break;
}
}
if(i >= C[pos]) //不可移的方塊
return;
int status = getStatus(pos_begin_return);
while(path.size() - 1 > top)
path.pop_back();
path.push_back(status);
++top;
if(top == 1){
( (CButton *)GetDlgItem(IDC_BUTTON_PREV))->EnableWindow(TRUE);
( (CButton *)GetDlgItem(IDC_BUTTON_RECOVER))->EnableWindow(TRUE);
}
( (CButton *)GetDlgItem(IDC_BUTTON_NEXT))->EnableWindow(FALSE);
SetNowstep(top);
}
void CAIDlg::OnBnClickedButtonPrev()
{
// TODO: 在此添加控件通知處理程序代碼
SetStatus(m_init, select_begin, pos_begin, pos_begin_return, path[--top]);
( (CButton *)GetDlgItem(IDC_BUTTON_NEXT))->EnableWindow(TRUE);
if(top < 1)
( (CButton *)GetDlgItem(IDC_BUTTON_PREV))->EnableWindow(FALSE);
SetNowstep(top);
}
void CAIDlg::OnBnClickedButtonNext()
{
// TODO: 在此添加控件通知處理程序代碼
SetStatus(m_init, select_begin, pos_begin, pos_begin_return, path[++top]);
if(top >= (path.size() - 1) )
( (CButton *)GetDlgItem(IDC_BUTTON_NEXT))->EnableWindow(FALSE);
( (CButton *)GetDlgItem(IDC_BUTTON_PREV))->EnableWindow(TRUE);
SetNowstep(top);
}
void CAIDlg::OnBnClickedButtonDefault()
{
// TODO: 在此添加控件通知處理程序代碼
if(!path.empty() &&
IDOK != MessageBox("將要丟失原來信息,是否確認", "確認", MB_OKCANCEL) ){
return;
}
static begin_default = 234150768, end_default = 87654321;
SetStatus(m_init, select_begin, pos_begin, pos_begin_return, begin_default);
SetStatus(m_end, select_end, pos_end, pos_end_return, end_default);
path.clear();
GetMenu()->EnableMenuItem(ID_CREATE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
prevstep.EnableWindow(FALSE);
nextstep.EnableWindow(FALSE);
display.EnableWindow(FALSE);
recover.EnableWindow(FALSE);
SetNowstep(0);
}
void CAIDlg::OnBnClickedButtonRecover()
{
// TODO: 在此添加控件通知處理程序代碼
SetStatus(m_init, select_begin, pos_begin, pos_begin_return, path[top = 0]);
if(path.size() - 1 > top)
( (CButton *)GetDlgItem(IDC_BUTTON_NEXT))->EnableWindow(TRUE);
( (CButton *)GetDlgItem(IDC_BUTTON_PREV))->EnableWindow(FALSE);
SetNowstep(0);
}
DWORD WINAPI DisplayProc(LPVOID lpParameter){
CAIDlg *p = (CAIDlg *) lpParameter;
p->prevstep.EnableWindow(FALSE);
p->nextstep.EnableWindow(FALSE);
p->recover.EnableWindow(FALSE);
//狀態框按鈕
( (CButton *) p->GetDlgItem(IDC_BUTTON_DEFAULT) )->EnableWindow(FALSE);
( (CButton *) p->GetDlgItem(IDC_BUTTON_RANDOM) )->EnableWindow(FALSE);
( (CButton *) p->GetDlgItem(IDC_BUTTON_HAND) )->EnableWindow(FALSE);
( (CButton *) p->GetDlgItem(IDC_CHECK_BEGIN) )->EnableWindow(FALSE);
( (CButton *) p->GetDlgItem(IDC_CHECK_END) )->EnableWindow(FALSE);
( (CButton *) p->GetDlgItem(IDC_CHECK_NOANSWER) )->EnableWindow(FALSE);
while(!p->stop && p->top + 1 < p->path.size() ){
/*debug
char str[30];
sprintf(str, "top = %d\r\n, status = %d", p->top, p->path[p->top]);
p->SetDlgItemText(IDC_EDIT_RESULT, str);*/
p->SetStatus( p->m_init, p->select_begin, p->pos_begin, p->pos_begin_return, p->path[++p->top]);
p->SetNowstep(p->top);
Sleep(p->m_sleep);
}
( (CButton *) p->GetDlgItem(IDC_BUTTON_DEFAULT) )->EnableWindow(TRUE);
( (CButton *) p->GetDlgItem(IDC_BUTTON_RANDOM) )->EnableWindow(TRUE);
( (CButton *) p->GetDlgItem(IDC_BUTTON_HAND) )->EnableWindow(TRUE);
( (CButton *) p->GetDlgItem(IDC_CHECK_BEGIN) )->EnableWindow(TRUE);
( (CButton *) p->GetDlgItem(IDC_CHECK_END) )->EnableWindow(TRUE);
( (CButton *) p->GetDlgItem(IDC_CHECK_NOANSWER) )->EnableWindow(TRUE);
if(p->top + 1 < p->path.size() ){
p->nextstep.EnableWindow(TRUE);
}
if(p->top > 0){
p->prevstep.EnableWindow(TRUE);
}
p->recover.EnableWindow(TRUE);
p->SetDlgItemText(IDC_BUTTON_AUTO, "自動演示");
return 0;
}
void CAIDlg::OnBnClickedButtonAuto()
{
// TODO: 在此添加控件通知處理程序代碼
CString str;
if( GetDlgItemText(IDC_BUTTON_AUTO, str), str == "自動演示"){
stop = 0;
if(top == path.size() - 1)
return;
HANDLE hThread1 = CreateThread(NULL, 0, DisplayProc, this, 0, NULL);
CloseHandle(hThread1);
SetDlgItemText(IDC_BUTTON_AUTO, "暫停演示");
}else{
stop = 1;
}
}
void CAIDlg::loadpicture(int start)
{
for(int i = start, j = 1; i <= start + 7; ++i, ++j){
m_bitmap[j].DeleteObject();
m_bitmap[j].LoadBitmap(i);
}
for(int i = 1; i < NUM; ++i){
m_hand[i].SetBitmap(m_bitmap[i]);
m_init[i].SetBitmap(NULL);
m_end[i].SetBitmap(NULL);
}
m_init[0].SetBitmap(NULL);
m_end[0].SetBitmap(NULL);
}
void CAIDlg::OnAbout()
{
// TODO: 在此添加命令處理程序代碼
CAboutDlg about;
about.DoModal();
}
void CAIDlg::OnGirl()
{
// TODO: 在此添加命令處理程序代碼
changeimage(ID_GIRL);
}
void CAIDlg::OnDigits()
{
// TODO: 在此添加命令處理程序代碼
changeimage(ID_DIGITS);
}
void CAIDlg::OnIco()
{
// TODO: 在此添加命令處理程序代碼
changeimage(ID_ICO);
}
void CAIDlg::changeimage(int id)
{
static int image = ID_DIGITS;
struct IMAGE{
int id, int start;
};
static struct IMAGE maps[] = {
{ID_DIGITS, IDB_BITMAP1},
{ID_GIRL, IDB_BITMAP9},
{ID_ICO, IDB_BITMAP17}
};
if(id == image)
return;
path.clear();
memset(pos_begin_return, -1, sizeof(pos_begin_return));
memset(pos_begin, -1, sizeof(pos_begin));
memset(pos_end_return, -1, sizeof(pos_end_return));
memset(pos_end, -1, sizeof(pos_end));
GetMenu()->CheckMenuItem(image, MF_UNCHECKED);
GetMenu()->CheckMenuItem(id, MF_CHECKED);
image = id;
for(int i = 0; i < sizeof(maps) / sizeof(struct IMAGE); ++i){
if(maps[i].id == id){
loadpicture(maps[i].start);
return;
}
}
}
void CAIDlg::SetNowstep(int step)
{
char str[20];
sprintf(str, "當前步數:%d", step);
SetDlgItemText(IDC_NOWSTEP, str);
}
void CAIDlg::SetParameter()
{
// TODO: 在此添加命令處理程序代碼
if(set.DoModal() == 1000)
set.GetParameter(m_heuristic, m_sleep, m_maxstep);
}
void CAIDlg::OnCreate()
{
// TODO: 在此添加命令處理程序代碼
static CGraph graph;
graph.getPath(path);
graph.DoModal();
}
void CAIDlg::OnHelp()
{
// TODO: 在此添加命令處理程序代碼
CHelp help;
help.DoModal();
}
void CAIDlg::OnCompare()
{
// TODO: 在此添加命令處理程序代碼
if(statusReady() == false){
MessageBox("未設置初未狀態!", "錯誤");
return;
}
int begin = getStatus(pos_begin_return);
int end = getStatus(pos_end_return);
if(begin == end){
MessageBox("一樣的還咋比?", "^-^");
return;
}
CCompare compare;
compare.getHer(m_heuristic);
compare.getMaxstep(m_maxstep);
int m_begin, m_end;
while(m_begin = getStatus(pos_begin_return), m_end = getStatus(pos_end_return),
(search->getreverse(m_begin) & 1) != (search->getreverse(m_end) & 1) ){
if(IDOK == MessageBox("此題無解,是否重新選擇?", "確認", MB_OKCANCEL) ){
OnBnClickedButtonRandom();
}
else
return;
}
compare.getStatus(m_begin, m_end);
compare.DoModal();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -