?? chess.cpp
字號(hào):
#include "Chess.h"
#include <iostream.h>
#include <afxtempl.h>
Chess::Chess()
{
for( int i=0;i<15;i++ )
for( int j=0;j<15;j++ )
FiveArray[i][j]=0;
StepList.RemoveAll();
}
Chess::~Chess()
{
}
int Chess::UpDown(int i, int j, int side) //查詢出從上到下相同的棋子數(shù)
{
int tempi,count;
count=1;
tempi=i;
while( --tempi>=0 && FiveArray[tempi][j]==side )
{
count++;
}
tempi=i;
while( ++tempi<15 && FiveArray[tempi][j]==side )
{
count++;
}
return count;
}
int Chess::LeftRight(int i, int j, int side) //查詢出從左到右相同的棋子數(shù)
{
int tempj,count;
count=1;
tempj=j;
while( --tempj>=0 && FiveArray[i][tempj]==side )
{
count++;
}
tempj=j;
while( ++tempj<15 && FiveArray[i][tempj]==side )
{
count++;
}
return count;
}
int Chess::LUptoDown(int i, int j, int side) //查詢出從左上到右下相同的棋子數(shù)
{
int tempi,tempj,count;
count=1;
tempi=i;
tempj=j;
while( --tempi>=0 && --tempj>=0 && FiveArray[tempi][tempj]==side )
{
count++;
}
tempi=i;
tempj=j;
while( ++tempi<15 && ++tempj<15 && FiveArray[tempi][tempj]==side )
{
count++;
}
return count;
}
int Chess::RUptoDown(int i, int j, int side) //查詢出從右上到左下相同的棋子數(shù)
{
int tempi,tempj,count;
count=1;
tempi=i;
tempj=j;
while( --tempi>=0 && ++tempj<15 && FiveArray[tempi][tempj]==side )
{
count++;
}
tempi=i;
tempj=j;
while( ++tempi<15 && --tempj>=0 && FiveArray[tempi][tempj]==side )
{
count++;
}
return count;
}
int Chess::LeftToRight_Status(int i, int j, int array[][15]) //查詢當(dāng)前所走步的棋盤(pán)狀態(tài)
{
int tempj,count;
bool aliveone=false;
bool alivetwo=false;
bool beyond=false;
int side=array[i][j];
count=1;
tempj=j;
while( --tempj>=0 && array[i][tempj]==side )
count++;
if( tempj>=0 && array[i][tempj]==0 )
aliveone=true;
else if( tempj>=0 && array[i][tempj]==1 )
beyond=true;
tempj=j;
while( ++tempj<15 && array[i][tempj]==side )
count++;
if( tempj<15 && array[i][tempj]==0 )
alivetwo=true;
else if( tempj<15 && array[i][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 Chess::UpToDown_Status(int i, int j, int array[][15])
{
int tempi,count;
bool aliveone=false;
bool alivetwo=false;
bool beyond=false;
int side=array[i][j];
count=1;
tempi=i;
while( --tempi>=0 && array[tempi][j]==side )
count++;
if( tempi>=0 && array[tempi][j]==0 )
aliveone=true;
else if( tempi>=0 && array[tempi][j]==1 )
beyond=true;
tempi=i;
while( ++tempi<15 && array[tempi][j]==side )
count++;
if( tempi<15 && array[tempi][j]==0 )
alivetwo=true;
else if( tempi<15 && array[tempi][j]==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 Chess::LeftUpToRightDown_Status(int i, int j, int array[][15])
{
int tempi,tempj,count;
bool aliveone=false;
bool alivetwo=false;
bool beyond=false;
int side=array[i][j];
count=1;
tempi=i;
tempj=j;
while( --tempi>=0 && --tempj>=0 && array[tempi][tempj]==side )
count++;
if( tempi>=0 && tempj>=0 && array[tempi][tempj]==0 )
aliveone=true;
else if( tempi>=0 && tempj>=0 && array[tempi][tempj]==1 )
beyond=true;
tempi=i;
tempj=j;
while( ++tempi<15 && ++tempj<15 && array[tempi][tempj]==side)
count++;
if( tempi<15 && tempj<15 && array[tempi][tempj]==0 )
alivetwo=true;
else if( tempi<15 && tempj<15 && 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 Chess::LeftDownToRightUp_Status(int i, int j, int array[][15])
{
int tempi,tempj,count;
bool aliveone=false;
bool alivetwo=false;
bool beyond=false;
int side=array[i][j];
count=1;
tempi=i;
tempj=j;
while( ++tempi<15 && --tempj>=0 && array[tempi][tempj]==side )
count++;
if( tempi<15 && tempj>=0 && array[tempi][tempj]==0 )
aliveone=true;
else if( tempi<15 && tempj>=0 && array[tempi][tempj]==1 )
beyond=true;
tempi=i;
tempj=j;
while( --tempi>=0 && ++tempj<15 && array[tempi][tempj]==side )
count++;
if( tempi>0 && tempj<14 && array[tempi][tempj]==0 )
alivetwo=true;
else if( tempi>=0 && tempj<15 && 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 Chess::SearchValue(int array[][15], 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<15 ;i++)
for (int jj=0 ; jj<15;jj++)
temp.fivearray[i][jj] = array[i][jj];
for ( i =0; i <15;i++)
for(int j=0;j<15;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 ;
}
//狀態(tài)
//返回8 成5:five
//返回7 活4:alivefour
//返回6 活3:alivethree
//返回5 活2:alivetwo
//返回4 死4:deadfour
//返回3 死3:deadthree
//返回2 死2:deadtwo
//返回0
void Chess::GetCurrentScore(GameStatus &board_situation)
{
int i=board_situation.st.x;
int j=board_situation.st.y;
board_situation.score=0;
//搜索出左到右的狀態(tài)
int lr=LeftToRight_Status(i,j,board_situation.fivearray);
if( lr==8 )
{
if( board_situation.is_machine )
board_situation.score=100000;
else
board_situation.score=-100000;
return;
}
//搜索出上到下的狀態(tài)
int ud=UpToDown_Status(i,j,board_situation.fivearray);
if( ud==8 )
{
if(board_situation.is_machine)
board_situation.score=100000;
else
board_situation.score=-100000;
return;
}
//搜索出左上到右下的狀態(tài)
int lutrd=LeftUpToRightDown_Status(i,j,board_situation.fivearray);
if( lutrd==8 )
{
if( board_situation.is_machine )
board_situation.score=100000;
else
board_situation.score=-100000;
return;
}
//搜索從左下到右上
int ldtru=LeftDownToRightUp_Status(i,j,board_situation.fivearray);
if( ldtru==8 )
{
if( board_situation.is_machine )
board_situation.score=100000;
else
board_situation.score=-100000;
return;
}
//是否活4
if( lr==7 || ud==7 || lutrd==7 || ldtru==7 )
{
if( board_situation.is_machine )
board_situation.score=10000;
else
board_situation.score=-10000;
return;
}
//雙死4
if(
( lr==4 && (ud==4 || lutrd==4 || ldtru==4) )
|| ( ud==4 && ( lutrd==4 || ldtru==4 ) )
|| ( lutrd==4 && ldtru==4 )
)
{
if( board_situation.is_machine )
board_situation.score=10000;
else
board_situation.score=-10000;
return;
}
//死4活3
if(
( lr==4 && ( ud==6 || lutrd==6 || ldtru==6 ) )
||( ud==4 && (lr==6 || lutrd==6 || ldtru==6) )
||( lutrd==4 && (lr==6 || ud==6 || ldtru==6) )
||( ldtru==4 && (lr==6 || ud==6 || lutrd==6) )
)
{
if( board_situation.is_machine )
board_situation.score=10000;
else
board_situation.score=-10000;
return;
}
//雙活3
if(
( lr==6 && (ud==6 || lutrd==6 || ldtru==6) )
|| ( ud==6 && (lutrd==6 || ldtru==6) )
|| ( lutrd==6 && ldtru==6 )
)
{
if( board_situation.is_machine )
board_situation.score=5000;
else
board_situation.score=-5000;
return;
}
//活3雙活2
if(
( lr==6 || ud==6 || lutrd==6 || ldtru==6 ) &&
( (lr==5 && (ud==5 || lutrd==5 || ldtru==5))
|| (ud==5 && (lutrd==5 || ldtru==5))
|| (lutrd==5 && ldtru==5) )
)
{
if( board_situation.is_machine )
board_situation.score=5000;
else
board_situation.score=-5000;
return;
}
//活3死3
if(
( lr==3 && (ud==6 || lutrd==6 || ldtru==6) )
|| ( ud==3 && (lr==6 || lutrd==6 || ldtru==6) )
|| ( lutrd==3 && (lr==6 || ud==6 || ldtru==6) )
|| ( ldtru==3 && (lr==6 || ud==6 || lutrd==6) )
)
{
if( board_situation.is_machine )
board_situation.score=1000;
else
board_situation.score=-1000;
return;
}
//死4
if( lr==4 || ud==4 || lutrd==4 || ldtru==4 )
{
if( board_situation.is_machine )
board_situation.score=500;
else
board_situation.score=-500;
return;
}
//單活3
if( lr==6 || ud==6 || lutrd==6 || ldtru==6 )
{
if( board_situation.is_machine )
board_situation.score=200;
else
board_situation.score=-200;
return;
}
//雙活2
if(
( lr==5 && (ud==5 || lutrd==5 || ldtru==5) )
|| (ud==5 && (lutrd==5 || ldtru==5))
|| (lutrd==5 && ldtru==5)
)
{
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -