TakeGame.cpp
上传用户:bsw_2008
上传日期:2013-07-09
资源大小:2446k
文件大小:12k
- // TakeGame.cpp: implementation of the TakeGame class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CGameHallFrame.h"
- #include "TakeGame.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- int RAILWAY[17][17]={
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
- {0,0,0,0,0,1,1,3,1,3,1,1,0,0,0,0,0},
- {0,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1,0},
- {0,1,0,0,0,2,2,0,2,0,2,2,0,0,0,1,0},
- {0,1,0,0,0,1,1,3,1,3,1,1,0,0,0,1,0},
- {0,1,0,0,0,2,2,0,2,0,2,2,0,0,0,1,0},
- {0,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1,0},
- {0,0,0,0,0,1,1,3,1,3,1,1,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- };
- int d[4][2]={
- {0,1},
- {1,0},
- {0,-1},
- {-1,0},
- };
- TakeGame::TakeGame()
- {
- war_kind=FALSE;
- }
- TakeGame::~TakeGame()
- {
- }
- BOOL TakeGame::CanMove(CPoint from, CPoint to)
- {
- int i,a;
- engineer=FALSE;
- if(board[from.x][from.y]>0&&board[from.x][from.y]<12)
- {
- if(board[from.x][from.y]==11)
- return FALSE;
- if(from.x==to.x&&from.y==to.y)
- return FALSE;
- if(IsSameSide(to))
- return FALSE;
- if(RAILWAY[from.x][from.y]==1&&RAILWAY[to.x][to.y]==1)
- {
- if(from.x==to.x)
- {
- a=(from.y<to.y)?1:-1;top=0;
- for(i=from.y+a;i!=to.y;i=i+a)
- {
- stack[top].x=from.x;
- if(a==1)
- stack[top].d=0;
- else
- stack[top].d=2;
- stack[top++].y=i;
- }
- return IsRouteCan();
- }else if(from.y==to.y)
- {
- a=(from.x<to.x)?1:-1;top=0;
- for(i=from.x+a;i!=to.x;i=i+a)
- {
- stack[top].y=from.y;
- if(a==1)
- stack[top].d=1;
- else
- stack[top].d=3;
- stack[top++].x=i;
- }
- return IsRouteCan();
- }else
- {
- if(from.x==10&&from.y<6&&to.x>10&&to.y==6)
- {
- top=0;
- for(i=from.y+1;i<6;i++)
- {
- stack[top].x=from.x;
- stack[top].d=0;
- stack[top++].y=i;
- }
- stack[top].x=11;stack[top].d=0;stack[top++].y=5;
- for(i=11;i<to.x;i++)
- {
- stack[top].x=i;
- stack[top].y=6;
- stack[top++].d=1;
- }
- return IsRouteCan();
- }
- if(to.x==10&&to.y<6&&from.x>10&&from.y==6)
- {
- top=0;
- for(i=from.x-1;i>10;i--)
- {
- stack[top].x=i;
- stack[top].d=3;
- stack[top++].y=6;
- }
- stack[top].x=11;stack[top].d=2;stack[top++].y=5;
- for(i=5;i>to.y;i--)
- {
- stack[top].x=10;
- stack[top].y=i;
- stack[top++].d=2;
- }
- return IsRouteCan();
- }
- if(to.x==10&&to.y>10&&from.x>10&&from.y==10)
- {
- top=0;
- for(i=from.x-1;i>10;i--)
- {
- stack[top].x=i;
- stack[top].y=10;
- stack[top++].d=3;
- }
- stack[top].x=11;stack[top].d=0;stack[top++].y=11;
- for(i=11;i<to.y;i++)
- {
- stack[top].x=10;
- stack[top].y=i;
- stack[top].d=0;
- }
- return IsRouteCan();
- }
- if(to.x>10&&to.y==10&&from.x==10&&from.y>10)
- {
- top=0;
- for(i=from.y-1;i>10;i--)
- {
- stack[top].x=10;
- stack[top].y=i;
- stack[top++].d=2;
- }
- stack[top].x=11;stack[top].d=2;stack[top++].y=11;
- for(i=11;i<to.x;i++)
- {
- stack[top].x=i;
- stack[top].y=10;
- stack[top++].d=1;
- }
- return IsRouteCan();
- }
- if(to.x==6&&to.y<6&&from.x<6&&from.y==6)
- {
- top=0;
- for(i=from.x+1;i<6;i++)
- {
- stack[top].x=i;
- stack[top].y=6;
- stack[top++].d=1;
- }
- stack[top].x=5;stack[top].y=5;stack[top++].d=1;
- for(i=5;i>to.y;i--)
- {
- stack[top].x=6;
- stack[top].d=2;
- stack[top].y=i;
- }
- return IsRouteCan();
- }
- if(to.x<6&&to.y==6&&from.x==6&&from.y<6)
- {
- top=0;
- for(i=from.y+1;i<6;i++)
- {
- stack[top].x=6;
- stack[top].y=i;
- stack[top++].d=0;
- }
- stack[top].x=5;stack[top].y=5;stack[top++].d=0;
- for(i=5;i>to.x;i--)
- {
- stack[top].x=i;
- stack[top].y=6;
- stack[top++].d=3;
- }
- return IsRouteCan();
- }
- if(to.x<6&&to.y==10&&from.x==6&&from.y>10)
- {
- top=0;
- for(i=from.y-1;i>10;i--)
- {
- stack[top].x=6;
- stack[top].y=i;
- stack[top++].d=2;
- }
- stack[top].x=5;stack[top].y=11;stack[top++].d=3;
- for(i=5;i>to.x;i--)
- {
- stack[top].x=i;
- stack[top].y=10;
- stack[top++].d=3;
- }
- return IsRouteCan();
- }
- if(to.x==6&&to.y>10&&from.x<6&&from.y==10)
- {
- top=0;
- for(i=from.x+1;i<6;i++)
- {
- stack[top].x=i;
- stack[top].y=10;
- stack[top++].d=1;
- }
- stack[top].x=5;stack[top].y=11;stack[top++].d=1;
- for(i=11;i<to.y;i++)
- {
- stack[top].x=6;
- stack[top].y=i;
- stack[top++].d=0;
- }
- return IsRouteCan();
- }
- }
- }
- if(IsCamp(to))
- {
- if((abs(from.x-to.x)==1||abs(from.y-to.y)==1)&&board[to.x][to.y]==0)
- return TRUE;
- else return FALSE;
- }else if(IsCamp(from))
- {
- if(abs(from.x-to.x)==1||abs(from.y-to.y)==1)
- return TRUE;
- else return FALSE;
- }else
- {
- if((abs(from.x-to.x)==1)&&(from.y==to.y)||(abs(from.y-to.y)==1)&&(from.x==to.x))
- return TRUE;
- if(board[from.x][from.y]==1)
- {
- //AfxMessageBox("zhou");
- return TakeEngineer(from,to);
- }
- }
- }
- AfxMessageBox("zhou");
- return FALSE;
- }
- BOOL TakeGame::IsSameSide(CPoint to)
- {
- if(board[to.x][to.y]==13&&!war_kind)
- return TRUE;
- if(board[to.x][to.y]>0&&board[to.x][to.y]<13)
- return TRUE;
- return FALSE;
- }
- BOOL TakeGame::TakeEngineer(CPoint from, CPoint to)
- {
- POS p;int x,y;engineer=TRUE;
- p.d=0;p.x=from.x;p.y=from.y;top=0;
- if(RAILWAY[p.x][p.y]==2)
- p.d=1;
- stack[top]=p;int f[17][17];
- while(top>=0)
- {
- p=stack[top];
- if(p.x==to.x&&p.y==to.y)
- return TRUE;
- if(p.d<4)
- {
- x=p.x+d[p.d][0];y=p.y+d[p.d][1];
- if(x==to.x&&p.y==to.y)
- return TRUE;
- if(board[x][y]==0&&RAILWAY[x][y]!=0&&f[x][y]!=1)
- {
- if(RAILWAY[p.x][p.y]==2||RAILWAY[p.x][p.y]==3)
- stack[top].d+=2;
- else
- stack[top].d+=1;
- if(RAILWAY[x][y]==2)
- {
- f[stack[top].x][stack[top].y]=1;
- p.x=x;p.y=y;p.d=1;
- stack[++top]=p;
- }else
- {
- f[stack[top].x][stack[top].y]=1;
- p.x=x;p.y=y;p.d=0;
- stack[++top]=p;
- }
- }
- else
- {
- if(RAILWAY[p.x][p.y]==2||RAILWAY[p.x][p.y]==3)
- stack[top].d+=2;
- else
- stack[top].d+=1;
- }
- }else
- top--;
- }
- return FALSE;
- }
- BOOL TakeGame::IsCamp(CPoint pos)
- {
- if(pos.x==2&&pos.y==7)
- return TRUE;
- if(pos.x==2&&pos.y==9)
- return TRUE;
- if(pos.x==3&&pos.y==8)
- return TRUE;
- if(pos.x==4&&pos.y==7)
- return TRUE;
- if(pos.x==4&&pos.y==9)
- return TRUE;
- if(pos.x==12&&pos.y==7)
- return TRUE;
- if(pos.x==12&&pos.y==9)
- return TRUE;
- if(pos.x==13&&pos.y==8)
- return TRUE;
- if(pos.x==14&&pos.y==7)
- return TRUE;
- if(pos.x==14&&pos.y==9)
- return TRUE;
- if(pos.x==7&&pos.y==2)
- return TRUE;
- if(pos.x==9&&pos.y==2)
- return TRUE;
- if(pos.x==8&&pos.y==3)
- return TRUE;
- if(pos.x==7&&pos.y==4)
- return TRUE;
- if(pos.x==9&&pos.y==4)
- return TRUE;
- if(pos.x==7&&pos.y==12)
- return TRUE;
- if(pos.x==7&&pos.y==14)
- return TRUE;
- if(pos.x==8&&pos.y==13)
- return TRUE;
- if(pos.x==9&&pos.y==12)
- return TRUE;
- if(pos.x==9&&pos.y==14)
- return TRUE;
- return FALSE;
- }
- BOOL TakeGame::IsRouteCan()
- {
- int i;CString s;
- for(i=0;i<top;i++)
- {
- if(board[stack[i].x][stack[i].y]>0)
- {
- //s.Format("%d %d %d",stack[i].x,stack[i].y,board[stack[i].x][stack[i].y]);
- //AfxMessageBox(s);
- return FALSE;
- }
- }
- return TRUE;
- }
- BOOL TakeGame::MoveLine(CPoint from, CPoint to)
- {
- int i,a;top=0;engineer=FALSE;
- if(board[from.x][from.y]>12&&board[from.x][from.y]<16)
- {
- if(board[from.x][from.y]==11)
- return FALSE;
- if(from.x==to.x&&from.y==to.y)
- return FALSE;
- if(IsSameSide(to))
- return FALSE;
- if(RAILWAY[from.x][from.y]==1&&RAILWAY[to.x][to.y]==1)
- {
- if(from.x==to.x)
- {
- a=(from.y<to.y)?1:-1;top=0;
- for(i=from.y+a;i!=to.y;i=i+a)
- {
- stack[top].x=from.x;
- if(a==1)
- stack[top].d=0;
- else
- stack[top].d=2;
- stack[top++].y=i;
- }
- return IsRouteCan();
- }else if(from.y==to.y)
- {
- a=(from.x<to.x)?1:-1;top=0;
- for(i=from.x+a;i!=to.x;i=i+a)
- {
- stack[top].y=from.y;
- if(a==1)
- stack[top].d=1;
- else
- stack[top].d=3;
- stack[top++].x=i;
- }
- return IsRouteCan();
- }else
- {
- if(from.x==10&&from.y<6&&to.x>10&&to.y==6)
- {
- top=0;
- for(i=from.y+1;i<6;i++)
- {
- stack[top].x=from.x;
- stack[top].d=0;
- stack[top++].y=i;
- }
- stack[top].x=11;stack[top].d=0;stack[top++].y=5;
- for(i=11;i<to.x;i++)
- {
- stack[top].x=i;
- stack[top].y=6;
- stack[top++].d=1;
- }
- return IsRouteCan();
- }
- if(to.x==10&&to.y<6&&from.x>10&&from.y==6)
- {
- top=0;
- for(i=from.x-1;i>10;i--)
- {
- stack[top].x=i;
- stack[top].d=3;
- stack[top++].y=6;
- }
- stack[top].x=11;stack[top].d=2;stack[top++].y=5;
- for(i=5;i>to.y;i--)
- {
- stack[top].x=10;
- stack[top].y=i;
- stack[top++].d=2;
- }
- return IsRouteCan();
- }
- if(to.x==10&&to.y>10&&from.x>10&&from.y==10)
- {
- top=0;
- for(i=from.x-1;i>10;i--)
- {
- stack[top].x=i;
- stack[top].y=10;
- stack[top++].d=3;
- }
- stack[top].x=11;stack[top].d=0;stack[top++].y=11;
- for(i=11;i<to.y;i++)
- {
- stack[top].x=10;
- stack[top].y=i;
- stack[top].d=0;
- }
- return IsRouteCan();
- }
- if(to.x>10&&to.y==10&&from.x==10&&from.y>10)
- {
- top=0;
- for(i=from.y-1;i>10;i--)
- {
- stack[top].x=10;
- stack[top].y=i;
- stack[top++].d=2;
- }
- stack[top].x=11;stack[top].d=2;stack[top++].y=11;
- for(i=11;i<to.x;i++)
- {
- stack[top].x=i;
- stack[top].y=10;
- stack[top++].d=1;
- }
- return IsRouteCan();
- }
- if(to.x==6&&to.y<6&&from.x<6&&from.y==6)
- {
- top=0;
- for(i=from.x+1;i<6;i++)
- {
- stack[top].x=i;
- stack[top].y=6;
- stack[top++].d=1;
- }
- stack[top].x=5;stack[top].y=5;stack[top++].d=1;
- for(i=5;i>to.y;i--)
- {
- stack[top].x=6;
- stack[top].d=2;
- stack[top].y=i;
- }
- return IsRouteCan();
- }
- if(to.x<6&&to.y==6&&from.x==6&&from.y<6)
- {
- top=0;
- for(i=from.y+1;i<6;i++)
- {
- stack[top].x=6;
- stack[top].y=i;
- stack[top++].d=0;
- }
- stack[top].x=5;stack[top].y=5;stack[top++].d=0;
- for(i=5;i>to.x;i--)
- {
- stack[top].x=i;
- stack[top].y=6;
- stack[top++].d=3;
- }
- return IsRouteCan();
- }
- if(to.x<6&&to.y==10&&from.x==6&&from.y>10)
- {
- top=0;
- for(i=from.y-1;i>10;i--)
- {
- stack[top].x=6;
- stack[top].y=i;
- stack[top++].d=2;
- }
- stack[top].x=5;stack[top].y=11;stack[top++].d=3;
- for(i=5;i>to.x;i--)
- {
- stack[top].x=i;
- stack[top].y=10;
- stack[top++].d=3;
- }
- return IsRouteCan();
- }
- if(to.x==6&&to.y>10&&from.x<6&&from.y==10)
- {
- top=0;
- for(i=from.x+1;i<6;i++)
- {
- stack[top].x=i;
- stack[top].y=10;
- stack[top++].d=1;
- }
- stack[top].x=5;stack[top].y=11;stack[top++].d=1;
- for(i=11;i<to.y;i++)
- {
- stack[top].x=6;
- stack[top].y=i;
- stack[top++].d=0;
- }
- return IsRouteCan();
- }
- }
- }
- if(IsCamp(to))
- {
- if((abs(from.x-to.x)==1||abs(from.y-to.y)==1)&&board[to.x][to.y]==0)
- return TRUE;
- else return FALSE;
- }else if(IsCamp(from))
- {
- if(abs(from.x-to.x)==1||abs(from.y-to.y)==1)
- return TRUE;
- else return FALSE;
- }else
- {
- if((abs(from.x-to.x)==1)&&(from.y==to.y)||(abs(from.y-to.y)==1)&&(from.x==to.x))
- return TRUE;
-
- return TakeEngineer(from,to);
- }
- }
- return FALSE;
- }