📄 chessdlg.cpp
字号:
}
else if ( tempi > 0 && tempj > 0 && array[tempi][tempj] == 1)
{
beyond = true;
}
tempi = i,tempj = j;
while ( ++ tempi<20 && ++ tempj < 20 && array[tempi][tempj] == side )
{
count ++;
}
if ( tempi < 20 && tempj <20 && array[tempi][tempj] == 0)
{
alivetwo = true;
}
else if ( tempi < 20 && tempj < 20 && array[tempi][tempj] == 1 )
{
beyond = true;
}
if ( count >= 5)
{
return 8 ;
}
if (count == 1 && aliveone == true && alivetwo == true )
{
return 0;
}
if ( count == 1 && beyond == true )
{
return 1 ;
}
if ( count > 1 && aliveone == true && alivetwo == true )
{
return count + 3;
}
if ( count > 1 && (aliveone == true || alivetwo == true ))
{
return count ;
}
return 0;
}
int CChessDlg::LeftDownToRightUp_Status(int i, int j ,int array[][20])
{
int tempi,tempj,count;
tempi = i,tempj = j;
count = 1;
bool aliveone = false;
bool alivetwo = false;
bool beyond = false;
int side = array[i][j];
while ( ++tempi < 20 && --tempj > 0 && array[tempi][tempj] == side)
{
count ++;
}
if ( tempi < 20 && tempj > 0 && array[tempi][tempj] == 0)
{
aliveone = true;
}
else if ( tempi < 20 && tempj > 0 && array[tempi][tempj] == 1)
{
beyond = true ;
}
tempi = i,tempj = j;
while ( -- tempi > 0 && ++ tempj < 20 && array[tempi][tempj] == side )
{
count ++;
}
if ( tempi > 0 && tempj < 20 && array[tempi][tempj] == 0 )
{
alivetwo = true;
}
else if ( tempi > 0 && tempj < 20 && array[tempi][tempj] == 1)
{
beyond = true;
}
if ( count >= 5)
{
return 8 ;
}
if (count == 1 && aliveone == true && alivetwo == true )
{
return 0;
}
if ( count == 1 && beyond == true )
{
return 1 ;
}
if ( count > 1 && aliveone == true && alivetwo == true )
{
return count + 3;
}
if ( count > 1 && (aliveone == true || alivetwo == true ))
{
return count ;
}
return 0;
}
//人机对战
void CChessDlg::OnMachineAndPeson()
{
if ( MessageBox(" 要重新开始吗?","友情提示",MB_YESNO) == IDYES)
{
m_and_p_flag = true ;
is_myturn = true;
StepList.RemoveAll();
for (int i = 0 ;i < 20 ; i++ )
for (int j=0;j<20;j++ )
Fivearray[i][j] = 0;
Invalidate();
}
}
int CChessDlg::MachineSearchGoodPlace(Step &st)
{
CList<GameStatus,GameStatus&>GameStatusList;
GameStatus temp;
temp.is_machine = true;
temp.deep = 0;
for (int i = 0 ; i < 20 ; i++)
for (int jj = 0 ; jj< 20 ;jj++)
temp.fivearray[i][jj] = Fivearray[i][jj];
for ( i = 1 ; i < 20 ; i++)
for(int j =1; j < 20 ; j++)
{
if ( Fivearray[i][j] == 0)
{
temp.fivearray[i][j] = 3;
temp.st.side = 3;
temp.st.x = i ;
temp.st.y = j;
GetCurrentScore(temp);
if ( temp.score > 0 )
{
if ( GameStatusList.GetCount() > 0 )
{
POSITION ps = GameStatusList.GetHeadPosition();
GameStatus temp1 = GameStatusList.GetAt(ps);
while (ps != NULL && temp.score > temp1.score )
{
temp1 = GameStatusList.GetNext(ps);
}
if ( ps == NULL)
{
ps = GameStatusList.GetTailPosition();
temp1 = GameStatusList.GetAt(ps);
if (temp1.score > temp.score )
{
GameStatusList.InsertBefore(ps,temp);
}
else
GameStatusList.AddTail(temp);
}
else
{
GameStatusList.InsertBefore(ps,temp);
}
}
else
{
GameStatusList.AddTail(temp);
}
}
temp.fivearray[i][j] = 0;
}
}
/*
POSITION ps = GameStatusList.GetTailPosition();
if ( ps == NULL )
return 0;
//初级
if ( m_combox.GetCurSel() == 0 )
{
temp = GameStatusList.GetPrev(ps);
st = temp.st ;
GameStatusList.RemoveAll();
return temp.score;
}
//中级
else
{
int maxscore = 0;
for (int i = 0;i < 3; i++ )
{
GameStatus ts = GameStatusList.GetAt(ps);
int sc = DeepSearch(ts);
return sc;
}
}
/*
StepList.AddTail( st );
Fivearray[st.x][st.y] = 3 ;
DrawChess(temp.st.x,temp.st.y);
/*
int ss = temp.score ;
while ( ps != NULL )
{
temp = GameStatusList.GetPrev(ps);
ss = temp.score ;
}
*/
return 0 ;
}
int CChessDlg::MachineSearchBadPlace(Step &stt)
{
CList<GameStatus,GameStatus&>GameStatusList;
GameStatus temp;
temp.deep = 0;
temp.is_machine = false;
for (int i = 0 ; i < 20 ; i++)
for (int jj = 0 ; jj < 20 ;jj++)
temp.fivearray[i][jj] = Fivearray[i][jj];
for ( i = 1 ; i < 20 ; i++)
for(int j =1; j < 20 ; j++)
{
if ( Fivearray[i][j] == 0)
{
temp.fivearray[i][j] = 1;
temp.st.side = 1;
temp.st.x = i ;
temp.st.y = j;
temp.score = 0 ;
GetCurrentScore(temp);
if ( temp.score < 0 )
{
if ( GameStatusList.GetCount() > 0 )
{
POSITION ps = GameStatusList.GetHeadPosition();
GameStatus temp1 = GameStatusList.GetAt(ps);
while (ps != NULL && temp.score < temp1.score )
{
temp1 = GameStatusList.GetNext(ps);
}
if ( ps == NULL)
{
ps = GameStatusList.GetTailPosition();
temp1 = GameStatusList.GetAt(ps);
if (temp1.score < temp.score )
{
GameStatusList.InsertBefore(ps,temp);
}
else
GameStatusList.AddTail(temp);
}
else
{
GameStatusList.InsertBefore(ps,temp);
}
}
else
{
GameStatusList.AddTail(temp);
}
}
temp.fivearray[i][j] = 0;
}
}
/*
POSITION ps = GameStatusList.GetTailPosition();
//初级
if ( m_combox.GetCurSel() == 0 )
{
temp = GameStatusList.GetPrev(ps);
stt = temp.st ;
GameStatusList.RemoveAll();
return -temp.score;
}
/*
StepList.AddTail( st );
Fivearray[st.x][st.y] = 3 ;
DrawChess(temp.st.x,temp.st.y);
int ss = temp.score ;
while ( ps != NULL )
{
temp = GameStatusList.GetPrev(ps);
ss = temp.score ;
}
*/
return 0 ;
}
int CChessDlg::SearchMaxValue(Step &st ,int array[][20])
{
int max_score = 0;
int score = 0;
GameStatus temp;
temp.is_machine = true;
temp.deep = 0;
for (int i = 0 ; i < 20 ; i++)
for (int jj = 0 ; jj< 20 ;jj++)
temp.fivearray[i][jj] = array[i][jj];
for ( i = 1 ; i < 20 ; i++)
for(int j =1; j < 20 ; j++)
{
if ( array[i][j] == 0)
{
temp.st.x = i ;
temp.st.y = j;
score = 0;
int score1 = 0,score2 = 0;
temp.fivearray[i][j] = 3;
temp.st.side = 3;
temp.is_machine = true;
temp.score = 0;
GetCurrentScore(temp);
score1 = temp.score ;
temp.fivearray[i][j] = 1;
temp.st.side = 1 ;
temp.is_machine = false ;
temp.score = 0;
GetCurrentScore(temp);
score2 = -temp.score ;
//机器
if ( score1 >= 10000 )
{
temp.fivearray[i][j] = 3;
temp.st.side = 3;
temp.is_machine = true;
temp.score = 0;
Step tempst1;
int tempscore = SearchValue(temp.fivearray,tempst1,false);
if ( score1 < tempscore)//如果当前盘面还有比现在分数大的,则防守
{
st.x = tempst1.x;
st.y = tempst1.y ;
return tempscore;
}
else
{
st.x = i;
st.y = j;
return score1;
}
}
if ( score2 >= 100000 )
{
temp.fivearray[i][j] = 1;
temp.st.side = 1 ;
temp.is_machine = false ;
temp.score = 0;
Step tempst1;
int tempscore = SearchValue(temp.fivearray,tempst1,true);
if ( score2 <= tempscore)//
{
st.x = tempst1.x;
st.y = tempst1.y ;
return tempscore;
}
else
{
st.x = i;
st.y = j;
return score1;
}
}
score = score1 + score2 ;
if ( score > max_score )
{
max_score = score ;
st.x = i;
st.y = j;
}
temp.fivearray[i][j] = 0;
}
}
return max_score ;
}
//返回分数
int CChessDlg::DeepSearch(GameStatus ts,Step &st,bool machine,int value)
{
ts.deep++;
st.deep ++ ;
int score =0 ;
if ( ts.deep == 3 )
{
score = SearchValue ( ts.fivearray,st,machine);
return score;
}
int min_score = 0;
score = 0;
//选出几局分较高的
CList<GameStatus,GameStatus&>GameStatusList;
for ( int i = 1 ; i < 20 ; i++)
for(int j =1; j < 20 ; j++)
{
if ( ts.fivearray[i][j] == 0)
{
ts.st.x = i ;
ts.st.y = j;
score = 0;
if ( machine )
{
ts.fivearray[i][j] = 3;
ts.st.side = 3;
ts.is_machine = true ;
ts.score = 0;
GetCurrentScore( ts );
if ( score == 100000)
{
st.x = i;
st.y = j;
return score;
}
score = ts.score ;
}
else
{
ts.fivearray[i][j] = 1;
ts.st.side = 1 ;
ts.is_machine = false ;
ts.score = 0;
GetCurrentScore(ts);
score = - ts.score ;
ts.score = score;
if ( score == 100000)
{
st.x = i;
st.y = j;
return score;
}
}
ts.score = score ;
if ( score == 0)
{
ts.fivearray[i][j] = 0;
continue;
}
if ( GameStatusList.GetCount() == 0)
{
min_score = ts.score ;
GameStatusList.AddTail(ts);
}
else
{
if ( score <= min_score )
{
min_score = score ;
GameStatusList.AddTail(ts);
}
else
{
POSITION ps = GameStatusList.GetTailPosition();
while ( ps != NULL )
{
GameStatus temp = GameStatusList.GetPrev(ps);
if ( temp.score >= score )
{
GameStatusList.InsertAfter(ps,ts);
break;
}
}
if ( ps == NULL )
{
GameStatusList.AddHead( ts );
}
}
}
ts.fivearray[i][j] = 0;
}
}//for
POSITION ps = GameStatusList.GetHeadPosition();
Step tempstep = st;
int tempscore = 0 ;
int max = 0;
for ( i = 0 ;i < 3; i ++ )
{
GameStatus temp = GameStatusList.GetNext(ps);
value = temp.score ;
tempscore = DeepSearch(temp,tempstep,! machine,temp.score );
if ( tempscore > max )
{
max = tempscore;
st = temp.st;
st.deep = tempstep.deep ;
}
if ( ps == NULL )
break;
}
return max ;
}
int CChessDlg::SearchValue(int array[][20],Step & st,bool machine)
{
int max_score = 0;
int score = 0;
GameStatus temp;
temp.is_machine = machine;
temp.deep = 0;
for (int i = 0 ; i < 20 ; i++)
for (int jj = 0 ; jj< 20 ;jj++)
temp.fivearray[i][jj] = array[i][jj];
for ( i = 1 ; i < 20 ; i++)
for(int j =1; j < 20 ; j++)
{
if ( array[i][j] == 0)
{
temp.st.x = i ;
temp.st.y = j;
score = 0;
if ( machine)
{
temp.fivearray[i][j] = 3;
temp.st.side = 3;
temp.is_machine = true;
temp.score = 0;
GetCurrentScore(temp);
score = temp.score ;
}
else
{
temp.fivearray[i][j] = 1;
temp.st.side = 1 ;
temp.is_machine = false ;
temp.score = 0;
GetCurrentScore(temp);
score = score - temp.score ;
}
if ( score == 100000 )
{
st.x = i;
st.y = j;
return score;
}
if ( score > max_score )
{
max_score = score ;
st.x = i;
st.y = j;
}
temp.fivearray[i][j] = 0;
}
}
return max_score ;
}
void CChessDlg::OnTimer(UINT nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
DrawChess(oldstep.x,oldstep.y,false);
oldstep = m_currentstep;
flag = !flag;
DrawChess(m_currentstep.x,m_currentstep.y,flag);
CDialog::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -