• 大小: 4.25MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-09-20
  • 语言: 其他
  • 标签: 算法  VC  MFC  人工智能  

资源简介

我最近写了一个拼图游戏,给大家看看 、、、、

资源截图

代码片段和文件信息

//#include 
#include “stdafx.h“
#include “jg.h“

using namespace std;

CSloveJG::CSloveJG(string state)
{
m_initalstate = state;
m_filename = state;
m_vopen.clear();
m_vclosed.clear();
m_vG.clear();
m_vslove.clear();
}

CSloveJG::~CSloveJG()
{

}

bool CSloveJG::iscanslove()
{
string strTmp = “123456780“;
string s0 = m_initalstate;
int i0igij;
i0 = ig = 0;

for( i = 0;i <= 8; i++)
{
if (s0[i] == ‘0‘) 
continue;
for( j = 0;j < i; j++)
{
if (s0[j]==‘0‘) 
continue;
if ((s0[j]-‘0‘)<(s0[i]-‘0‘))
i0++;
}
}

for( i = 0;i <= 8; i++)
{
if (strTmp[i] == ‘0‘) 
continue;
for( j = 0;j < i; j++)
{
if (strTmp[j] == ‘0‘) 
continue;
if ((strTmp[j]-‘0‘) < (strTmp[i]-‘0‘))
ig++;
}
}
return((i0-ig)%2 == 0);
}

bool computer_f(pJGState s1 pJGState s2)
{
if((s1->g+s1->h) > (s2->g+s2->h))
return true;
else
return false;
}

int CSloveJG::computer_h(pJGState js)
{
char* ps = js->state;
string s = “012345678“;
string sg = “123456780“; //目标状态序列
char* psg = (char*)sg.c_str();
int h = 0;
int temptempg;
for(int i = 0;i <= 8;i++)
{
if(s[i] == ‘0‘) 
continue; //0的距离已经包含在其他数字的移动过程中,所以应该舍去
temp = strchr(pss[i])-ps;
tempg = strchr(psgs[i])-psg;
h += abs(temp/3-tempg/3) + abs(temp%3-tempg%3);
}
return h;
}

void CSloveJG::copystate(pJGState s1 pJGState s2)
{
s1->father = s2->father;
s1->g = s2->g;
s1->h = s2->h;
strcpy(s1->state s2->state);
return;
}

pJGState CSloveJG::BornState(pJGState ps int r int father)
{
int position = strchr(ps->state‘0‘)-ps->state;
char * pTmpstr = new char[10];
strcpy(pTmpstr ps->state);
string sn = pTmpstr;
pJGState pSon = new JGState[sizeof(pSon)]; 
pSon->state = new char[10];

switch (r)
{
case 1: //下
if (position/3 == 2 )  

delete [] pSon->state;
delete [] pSon;
return NULL;
}
sn[position] = sn[position+3];
sn[position+3] =‘0‘;
strcpy(pSon->state(char*)sn.c_str());
pSon->father = father;//vclosed.size()-1;
pSon->h = computer_h(pSon);
pSon->g  =ps->g+1;
delete [] pTmpstr;
return pSon;

case 2:  //下
if (position/3 == 0 ) 

delete [] pSon->state;
delete [] pSon;
return NULL;
}
sn[position] = sn[position-3];
sn[position-3] = ‘0‘;
strcpy(pSon->state(char*)sn.c_str());
pSon->father = father;//vclosed.size()-1;
pSon->h = computer_h(pSon);
pSon->g = ps->g+1;
delete [] pTmpstr;
return pSon;

case 3:  //左
if (position%3 == 2 ) 

delete [] pSon->state;
delete [] pSon;
return NULL;
}
sn[position] = sn[position+1];
sn[position+1] =‘0‘;
strcpy(pSon->state(char*)sn.c_str());
pSon->father = father;//vclosed.size()-1;
pSon->h = computer_h(pSon);
pSon->g = ps->g+1;
delete [] pTmpstr;
return pSon;

case 4: //右
if (position%3 == 0 ) 

delete [] pSon->state;
delete [

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        242  2009-04-27 21:59  jiugong\435071268

     文件      68013  2009-04-01 14:44  jiugong\Debug\jg.obj

     文件          0  2009-04-01 14:44  jiugong\Debug\jg.sbr

     文件    4162560  2009-04-01 15:21  jiugong\Debug\jiugong.bsc

     文件     614465  2009-04-01 15:21  jiugong\Debug\jiugong.exe

     文件     276660  2009-04-01 15:21  jiugong\Debug\jiugong.ilk

     文件      28848  2009-04-01 14:44  jiugong\Debug\jiugong.obj

     文件    7079500  2009-04-01 14:39  jiugong\Debug\jiugong.pch

     文件     484352  2009-04-01 15:21  jiugong\Debug\jiugong.pdb

     文件     407528  2009-04-01 14:39  jiugong\Debug\jiugong.res

     文件          0  2009-04-01 14:44  jiugong\Debug\jiugong.sbr

     文件      48603  2009-04-01 15:21  jiugong\Debug\jiugongDlg.obj

     文件          0  2009-04-01 15:21  jiugong\Debug\jiugongDlg.sbr

     文件     105915  2009-04-01 14:39  jiugong\Debug\StdAfx.obj

     文件    1374561  2009-04-01 14:39  jiugong\Debug\StdAfx.sbr

     文件     271360  2009-04-27 21:59  jiugong\Debug\vc60.idb

     文件     471040  2009-04-01 15:21  jiugong\Debug\vc60.pdb

     文件       5379  2009-04-01 14:44  jiugong\jg.cpp

     文件       1015  2009-04-01 14:39  jiugong\jg.h

     文件     440604  2009-04-01 14:36  jiugong\jiugong.aps

     文件       1058  2009-04-27 21:59  jiugong\jiugong.clw

     文件       2077  2009-03-29 20:41  jiugong\jiugong.cpp

     文件       4294  2009-04-01 14:48  jiugong\jiugong.dsp

     文件        522  2009-03-29 20:41  jiugong\jiugong.dsw

     文件       1335  2009-03-29 20:41  jiugong\jiugong.h

     文件      66560  2009-04-27 22:00  jiugong\jiugong.ncb

     文件     274944  2009-04-27 22:00  jiugong\jiugong.opt

     文件        248  2009-04-27 21:59  jiugong\jiugong.plg

     文件       5324  2009-04-01 14:36  jiugong\jiugong.rc

     文件       6748  2009-04-01 15:21  jiugong\jiugongDlg.cpp

............此处省略15个文件信息

评论

共有 条评论