📄 luffar.cpp
字号:
pt = SearchAll(m_nBoard[m_lastPt.x][m_lastPt.y],1);
if (pt.x != -1) return pt;
}
}
return RandomPoint();
}
CPoint CBoard::Analyse(int x,int y, int count,int param)
{
// Se om n唃on har "count" i rad eller "count-1" i rad
// med 1 till i n剅heten
// eller tv?"count-1" bredvid varandra
CPoint topPt,endPt, tempPt;
BOOL topClosed, endClosed, closed, closed1;
int length;
int length1, sum=0;
Direction temp, temp1, temp2;
if (x != -1 && y != -1)
{
for (temp=LEFT; temp<RIGHT;temp++)
{
length = Calculate(x, y, temp)+
Calculate(x, y, (Direction)(temp+4))+1;
topPt = GetEndPoint(x, y, temp, topClosed);
endPt = GetEndPoint(x, y, (Direction)(temp+4), endClosed);
if (length>=2 && count == 2 )
{
if ((!topClosed) && (topPt.x!=-1) &&
m_nBoard[topPt.x][topPt.y]==0)
{
for (temp1=LEFT; temp1<=DOWNLEFT;temp1++)
{
length1 = Calculate(topPt.x, topPt.y, temp1,
m_nBoard[x][y]);
tempPt = GetEndPoint(topPt.x, topPt.y, temp1, closed);
GetEndPoint(tempPt.x, tempPt.y, temp1, closed);
if ((temp1 != temp) && (temp!=temp1-4) &&
(length1>=2) && (!closed))
return topPt;
if (m_nBoard[tempPt.x][tempPt.y]==0 && (!endClosed)
&& (endPt.x!=-1) && m_nBoard[endPt.x][endPt.y]==0)
{
for (temp2=LEFT; temp2<=DOWNLEFT;temp2++)
{
tempPt = GetEndPoint(tempPt.x, tempPt.y, temp2, closed);
tempPt = GetEndPoint(tempPt.x, tempPt.y, temp2, closed);
length1 = Calculate(tempPt.x, tempPt.y, temp2,
m_nBoard[x][y])+1;
if ((temp2 != temp1) &&
(length1>=2) && (!closed))
return topPt;
}
}
}
}
if ((!endClosed) && (endPt.x!=-1) &&
m_nBoard[endPt.x][endPt.y]==0)
{
for (temp1=LEFT; temp1<DOWNLEFT;temp1++)
{
length1 = Calculate(endPt.x, endPt.y, temp1,
m_nBoard[x][y]);
tempPt = GetEndPoint(endPt.x, endPt.y, temp1, closed);
tempPt = GetEndPoint(tempPt.x, tempPt.y, temp2, closed);
GetEndPoint(tempPt.x, tempPt.y, temp1, closed);
if ((temp !=temp1) && (temp != temp1-4) &&
(length1>=2) && (!closed))
return endPt;
if (m_nBoard[tempPt.x][tempPt.y]==0 && (!topClosed)
&& (topPt.x!=-1) && m_nBoard[topPt.x][topPt.y]==0)
{
for (temp2=LEFT; temp2<=DOWNLEFT;temp2++)
{
tempPt = GetEndPoint(tempPt.x, tempPt.y, temp2, closed);
length1 = Calculate(tempPt.x, tempPt.y, temp2,
m_nBoard[x][y])+1;
if ((temp2 != temp1) &&
(length1>=2) && (!closed))
return topPt;
}
}
}
}
}
// X X
// Det h剅 situationen
// X X
else if (length>=1 && count ==2)
{
if ((!topClosed) && (topPt.x!=-1) &&
m_nBoard[topPt.x][topPt.y]==0)
{
for (temp1=LEFT; temp1<RIGHT;temp1++)
{
length1 = Calculate(topPt.x, topPt.y, temp1,
m_nBoard[x][y]);
tempPt = GetEndPoint(topPt.x, topPt.y, temp1, closed);
tempPt = GetEndPoint(tempPt.x, tempPt.y, temp1, closed);
if ((length1 >= 1) && (!closed))
{
length1 = Calculate(topPt.x, topPt.y, (Direction)(temp1+4),
m_nBoard[x][y]);
tempPt = GetEndPoint(topPt.x, topPt.y, (Direction)(temp1+4), closed);
tempPt = GetEndPoint(tempPt.x, tempPt.y, (Direction)(temp1+4), closed);
if ((length1 >= 1) && (!closed)) sum++;
}
if (sum>=2)
return topPt;
}
sum=0;
}
if ((!endClosed) && (endPt.x!=-1) &&
m_nBoard[endPt.x][endPt.y]==0)
{
for (temp1=LEFT; temp1<RIGHT;temp1++)
{
length1 = Calculate(endPt.x, endPt.y, temp1,
m_nBoard[x][y]);
tempPt = GetEndPoint(endPt.x, endPt.y, temp1, closed);
tempPt = GetEndPoint(tempPt.x, tempPt.y, temp1, closed);
if ((length1 >= 1) && (!closed))
{
length1 = Calculate(endPt.x, endPt.y, (Direction)(temp1+4),
m_nBoard[x][y]);
tempPt = GetEndPoint(endPt.x, endPt.y, (Direction)(temp1+4), closed);
tempPt = GetEndPoint(tempPt.x, tempPt.y, (Direction)(temp1+4), closed);
if ((length1 >= 1) && (!closed)) sum++;
}
if (sum>=2)
return endPt;
}
sum=0;
}
}
if (length>=count && param!=1)
{
if ((!topClosed) && (topPt.x!=-1) &&
(m_nBoard[topPt.x][topPt.y]==0) &&
(!endClosed) && (endPt.x!=-1) &&
m_nBoard[endPt.x][endPt.y]==0)
{
if (length<=3)
{
GetEndPoint(topPt.x, topPt.y, temp, closed);
GetEndPoint(endPt.x, endPt.y, Direction(temp+4), closed1);
if ((!closed) && (!closed1))
return topPt;
}else
return topPt;
}
else if (length>=4 || param==2)
{
if ((!topClosed) && (topPt.x!=-1) &&
m_nBoard[topPt.x][topPt.y]==0)
return topPt;
if ((!endClosed) && (endPt.x!=-1) &&
m_nBoard[endPt.x][endPt.y]==0)
return endPt;
}
}
else if ((!topClosed) && (topPt.x!=-1) &&
m_nBoard[topPt.x][topPt.y]==0 && param!=1)
{
if (Calculate(topPt.x,topPt.y,temp,m_nBoard[x][y])>=count-length)
{
if (count>=4)
return topPt;
else
{
tempPt = GetEndPoint(topPt.x, topPt.y, temp, closed);
tempPt = GetEndPoint(tempPt.x, tempPt.y, temp, closed);
if ((!endClosed) && (endPt.x!=-1) &&
m_nBoard[endPt.x][endPt.y]==0 && (!closed))
return topPt;
}
}
}
else if ((!endClosed) && (endPt.x!=-1) &&
m_nBoard[endPt.x][endPt.y]==0 && param!=0 )
{
if (Calculate(endPt.x,endPt.y,(Direction)(temp+4),m_nBoard[x][y])>=count-length)
{
if (count>=4)
return endPt;
else
{
tempPt = GetEndPoint(endPt.x, endPt.y, temp, closed);
tempPt = GetEndPoint(tempPt.x, tempPt.y, temp, closed);
if ((!topClosed) && (topPt.x!=-1) &&
m_nBoard[topPt.x][topPt.y]==0 && (!closed))
return endPt;
}
}
}
}
}
return CPoint();
}
int CBoard::Calculate(int x,int y,Direction d, int player)
{
if (player==-1)
player=m_nBoard[x][y];
switch (d)
{
case UP:
if (y==0 || m_nBoard[x][y-1]!=player)
return 0;
return 1+Calculate(x,y-1,d,player);
case UPRIGHT:
if (y==0 || x==MAX_X-1 || m_nBoard[x+1][y-1]!=player)
return 0;
return 1+Calculate(x+1,y-1,d,player);
case RIGHT:
if (x==MAX_X-1 || m_nBoard[x+1][y]!=player)
return 0;
return 1+Calculate(x+1,y,d,player);
case DOWNRIGHT:
if (y==MAX_Y-1 || x==MAX_X-1 || m_nBoard[x+1][y+1]!=player)
return 0;
return 1+Calculate(x+1,y+1,d,player);
case DOWN:
if (y==MAX_Y-1 || m_nBoard[x][y+1]!=player)
return 0;
return 1+Calculate(x,y+1,d,player);
case DOWNLEFT:
if (y==MAX_Y-1 || x==0 || m_nBoard[x-1][y+1]!=player)
return 0;
return 1+Calculate(x-1,y+1,d,player);
case LEFT:
if (x==0 || m_nBoard[x-1][y]!=player)
return 0;
return 1+Calculate(x-1,y,d,player);
case UPLEFT:
if (y==0 || x==0 || m_nBoard[x-1][y-1]!=player)
return 0;
return 1+Calculate(x-1,y-1,d,player);
} return 0;
}
CPoint CBoard::FindDangerPt(int player)
{
CPoint pt;
int count=0, closedCount=0;
BOOL closed;
// S攌 genom hela spelet
for (int temp=0;temp<MAX_X;temp++)
{
for (int temp1=0;temp1<MAX_Y;temp1++)
{
if (m_nBoard[temp][temp1]==0)
{
for (Direction tmp=LEFT;tmp<=DOWNLEFT;tmp++)
{
pt = GetEndPoint(temp,temp1,tmp,closed);
if (pt.x!=-1 && pt.y!=-1)
{
if (m_nBoard[pt.x][pt.y]==player)
count++;
if (m_nBoard[pt.x][pt.y]==player && (!closed))
closedCount++;
}
if (count>=3 || closedCount>=2)
return CPoint(temp,temp1);
count=0;
closedCount=0;
}
}
}
}
return CPoint();
}
//return 1 om f鰎sta spelaren vinner
//return 2 om andra spelaren vinner
//return -1 om ingen vinner
int CBoard::GetWinner()
{
if (m_lastPt.x==-1 || m_lastPt.y==-1)
return -1;
int x=m_lastPt.x;
int y=m_lastPt.y;
if ((Calculate(x,y,UP)+Calculate(x,y,DOWN)>=4))
{ ShowWinner(x,y,UP);
ShowWinner(x,y,DOWN); }
else if ((Calculate(x,y,UPLEFT)+Calculate(x,y,DOWNRIGHT)>=4))
{ ShowWinner(x,y,UPLEFT);
ShowWinner(x,y,DOWNRIGHT);}
else if ((Calculate(x,y,LEFT)+Calculate(x,y,RIGHT)>=4))
{ ShowWinner(x,y,LEFT);
ShowWinner(x,y,RIGHT);}
else if ((Calculate(x,y,UPRIGHT)+Calculate(x,y,DOWNLEFT)>=4))
{ ShowWinner(x,y,UPRIGHT);
ShowWinner(x,y,DOWNLEFT);}
else
return -1;
delay(400);
return m_nBoard[x][y];
}
void CBoard::ShowWinner(int x,int y,Direction start,int player)
{
if (player==-1)
player = m_nBoard[x][y];
if (m_nBoard[x][y]!=player)
return;
HidePoint();
/*putimage(x0+x*m_nCellX+1,y0+y*m_nCellY+1,
m_pImage1,NOT_PUT);*/
setcolor(YELLOW);
circle(x0+x*m_nCellX+m_nCellX/2,
y0+y*m_nCellY+m_nCellY/2,
((m_nCellX>m_nCellY)?m_nCellY:m_nCellX)/2);
ShowPoint();
switch (start)
{
case UP:
ShowWinner(x,y-1,UP,player);
break;
case DOWN:
ShowWinner(x,y+1,DOWN,player);
break;
case UPLEFT:
ShowWinner(x-1,y-1,UPLEFT,player);
break;
case DOWNRIGHT:
ShowWinner(x+1,y+1,DOWNRIGHT,player);
break;
case LEFT:
ShowWinner(x-1,y,LEFT,player);
break;
case RIGHT:
ShowWinner(x+1,y,RIGHT,player);
break;
case UPRIGHT:
ShowWinner(x+1,y-1,UPRIGHT,player);
break;
case DOWNLEFT:
ShowWinner(x-1,y+1,DOWNLEFT,player);
break;
}
}
CMessagePad::CMessagePad()
{
x0 = 450;
y0 = 120;
x1 = 620;
y1 = 400;
m_nShadow = 5;
mx0 = x0 + 10;
my0 = y0 + 25;
mx1 = x1 - 10;
my1 = y1 - 22;
m_nLineSpace = 15;
m_nLines = (my1-my0)/m_nLineSpace;
my1 = my0 + m_nLineSpace * m_nLines;
m_nCurrentLine = 0;
}
void CMessagePad::Draw()
{
HidePoint();
CRect rect(x0,y0,x1,y1);
rect.Draw(BROWN,WHITE,TRUE,m_nShadow);
//Titel
CRect title(x0,y0,x1,y0+15);
title.Draw(WHITE,WHITE);
setcolor(BLACK);
outtextxy(title.x0+6,title.y0+6,"Messages");
ShowPoint();
}
void CMessagePad::Message(char* msg)
{
HidePoint();
if (m_nCurrentLine == m_nLines)
{
ScrollMessages();
m_nCurrentLine--;
}
setcolor(BLACK);
outtextxy(mx0+1,my0+m_nCurrentLine*m_nLineSpace+1,msg);
setcolor(WHITE);
outtextxy(mx0,my0+m_nCurrentLine*m_nLineSpace,msg);
m_nCurrentLine++;
ShowPoint();
}
void CMessagePad::ScrollMessages()
{
void *oldImage;
unsigned int size;
/* calculate the size of the image */
size = imagesize(mx0,my0+m_nLineSpace,mx1,my1+m_nLineSpace);
/* allocate memory to hold the image */
oldImage = malloc(size);
/* grab the image */
HidePoint();
getimage(mx0,my0+m_nLineSpace,mx1,my1+m_nLineSpace,oldImage);
putimage(mx0,my0,oldImage,COPY_PUT);
ShowPoint();
/* clean up */
free(oldImage);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -