FighterInfo.cpp
上传用户:hydatong
上传日期:2022-08-07
资源大小:219k
文件大小:7k
- // FighterInfo.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Fighter.h"
- #include "FighterInfo.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CFighterInfo
- CFighterInfo::CFighterInfo()
- {
- }
- CFighterInfo::~CFighterInfo()
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFighterInfo message handlers
- void CFighterInfo::SetHeadPos(CPoint ptPos)
- {
- m_ptHeadPos = ptPos;
- }
- BOOL CFighterInfo::SetFighterPosition(int nDir)
- {
- switch(nDir){
- case 0:
- m_ptPos[0] = m_ptHeadPos;
- m_ptPos[1] = CPoint(m_ptHeadPos.x-1, m_ptHeadPos.y-2);
- m_ptPos[2] = CPoint(m_ptHeadPos.x-1, m_ptHeadPos.y-1);
- m_ptPos[3] = CPoint(m_ptHeadPos.x-1, m_ptHeadPos.y );
- m_ptPos[4] = CPoint(m_ptHeadPos.x-1, m_ptHeadPos.y+1);
- m_ptPos[5] = CPoint(m_ptHeadPos.x-1, m_ptHeadPos.y+2);
- m_ptPos[6] = CPoint(m_ptHeadPos.x-2, m_ptHeadPos.y );
- m_ptPos[7] = CPoint(m_ptHeadPos.x-3, m_ptHeadPos.y-1);
- m_ptPos[8] = CPoint(m_ptHeadPos.x-3, m_ptHeadPos.y );
- m_ptPos[9] = CPoint(m_ptHeadPos.x-3, m_ptHeadPos.y+1);
- break;
- case 1:
- m_ptPos[0] = m_ptHeadPos;
- m_ptPos[1] = CPoint(m_ptHeadPos.x+2, m_ptHeadPos.y-1);
- m_ptPos[2] = CPoint(m_ptHeadPos.x+1, m_ptHeadPos.y-1);
- m_ptPos[3] = CPoint(m_ptHeadPos.x , m_ptHeadPos.y-1);
- m_ptPos[4] = CPoint(m_ptHeadPos.x-1, m_ptHeadPos.y-1);
- m_ptPos[5] = CPoint(m_ptHeadPos.x-2, m_ptHeadPos.y-1);
- m_ptPos[6] = CPoint(m_ptHeadPos.x , m_ptHeadPos.y-2);
- m_ptPos[7] = CPoint(m_ptHeadPos.x+1, m_ptHeadPos.y-3);
- m_ptPos[8] = CPoint(m_ptHeadPos.x , m_ptHeadPos.y-3);
- m_ptPos[9] = CPoint(m_ptHeadPos.x-1, m_ptHeadPos.y-3);
- break;
- case 2:
- m_ptPos[0] = m_ptHeadPos;
- m_ptPos[1] = CPoint(m_ptHeadPos.x+1, m_ptHeadPos.y+2);
- m_ptPos[2] = CPoint(m_ptHeadPos.x+1, m_ptHeadPos.y+1);
- m_ptPos[3] = CPoint(m_ptHeadPos.x+1, m_ptHeadPos.y );
- m_ptPos[4] = CPoint(m_ptHeadPos.x+1, m_ptHeadPos.y-1);
- m_ptPos[5] = CPoint(m_ptHeadPos.x+1, m_ptHeadPos.y-2);
- m_ptPos[6] = CPoint(m_ptHeadPos.x+2, m_ptHeadPos.y );
- m_ptPos[7] = CPoint(m_ptHeadPos.x+3, m_ptHeadPos.y+1);
- m_ptPos[8] = CPoint(m_ptHeadPos.x+3, m_ptHeadPos.y );
- m_ptPos[9] = CPoint(m_ptHeadPos.x+3, m_ptHeadPos.y-1);
- break;
- case 3:
- m_ptPos[0] = m_ptHeadPos;
- m_ptPos[1] = CPoint(m_ptHeadPos.x-2, m_ptHeadPos.y+1);
- m_ptPos[2] = CPoint(m_ptHeadPos.x-1, m_ptHeadPos.y+1);
- m_ptPos[3] = CPoint(m_ptHeadPos.x , m_ptHeadPos.y+1);
- m_ptPos[4] = CPoint(m_ptHeadPos.x+1, m_ptHeadPos.y+1);
- m_ptPos[5] = CPoint(m_ptHeadPos.x+2, m_ptHeadPos.y+1);
- m_ptPos[6] = CPoint(m_ptHeadPos.x , m_ptHeadPos.y+2);
- m_ptPos[7] = CPoint(m_ptHeadPos.x-1, m_ptHeadPos.y+3);
- m_ptPos[8] = CPoint(m_ptHeadPos.x , m_ptHeadPos.y+3);
- m_ptPos[9] = CPoint(m_ptHeadPos.x+1, m_ptHeadPos.y+3);
- break;
- default:
- break;
- }
- for(int i=0; i<10; i++)
- if(m_ptPos[i].x<0 || m_ptPos[i].y<0 || m_ptPos[i].x>9 || m_ptPos[i].y>9)
- return FALSE;
- return TRUE;
- }
- //////////////////////////////////////////////////////
- //检查m_nSky[10][10]中的布局是否正确
- //
- //
- ///////////////////////////////////////////////////////
- BOOL CFighterInfo::IsFighterValid()
- {
- for(int i=0; i<10; i++){
- if(m_ptPos[i].x<0 || m_ptPos[i].x>9) return FALSE;
- if(m_ptPos[i].y<0 || m_ptPos[i].y>9) return FALSE;
- if(m_nSky[m_ptPos[i].x][m_ptPos[i].y] == 1) return FALSE;
- if(m_nSky[m_ptPos[i].x][m_ptPos[i].y] == 2) return FALSE;
- }
- return TRUE;
- }
- BOOL CFighterInfo::IsSkyValid()
- {
- int i=0, j=0, k=0, l=0;
- int nSky[10][10];
- CPoint ptHead[3];
- int nHead=0;
- for(i=0; i<10; i++)
- for(j=0; j<10; j++)
- {
- if(m_nSky[i][j] == 2)
- ptHead[nHead++] = CPoint(i, j);
- nSky[i][j] = 0;
- }
- if(nHead!=3)
- return FALSE;
- int nTotal=0;
- for(i=0; i<10; i++)
- for(j=0; j<10; j++)
- nTotal += m_nSky[i][j];
- if(nTotal!=33)
- return FALSE;
- int nx=0, ny=0;
- char buf[256];
- CPoint ptPos2[10];//第二层的循环的m_ptPos[10]需要保留,若第三层检验不成功要减去
- BOOL bInterrupted = FALSE;
- for(i=0; i<4; i++)//第一层循环
- {
- for(nx=0; nx<10; nx++)
- for(ny=0; ny<10; ny++)
- nSky[nx][ny]=0;
- SetHeadPos(ptHead[0]);
- if(!SetFighterPosition(i))
- continue;
- int temp = ptHead[0].x;
- temp = ptHead[0].y;
- for(l=0; l<10; l++)
- nSky[m_ptPos[l].x][m_ptPos[l].y] += 1;
- sprintf(buf, "i=%drn", i);
- //PrintSky(nSky, buf);
- for(j=0; j<4; j++)//第二层循环
- {
- SetHeadPos(ptHead[1]);
- if(!SetFighterPosition(j))
- continue;
- for(l=0; l<10; l++)
- {
- nSky[m_ptPos[l].x][m_ptPos[l].y] += 1;
- ptPos2[l] = m_ptPos[l];//保留第二层的m_ptPos
- }
- sprintf(buf, "i=%d j=%drn", i, j);
- //PrintSky(nSky, buf);
- bInterrupted = FALSE;
- for(nx=0; nx<10; nx++)
- {
- for(ny=0; ny<10; ny++)
- if(nSky[nx][ny]==2) //如果为2,说明飞机有重叠,重新第二层循环
- {
- for(l=0; l<10; l++)
- nSky[ptPos2[l].x][ptPos2[l].y] -= 1;
- bInterrupted = TRUE;
- break;
- }
- if(bInterrupted)
- break;
- }
- if(bInterrupted)
- continue;
- for(k=0; k<4; k++)//第三层循环
- {
- SetHeadPos(ptHead[2]);
- if(!SetFighterPosition(k))
- continue;
- for(l=0; l<10; l++)
- nSky[m_ptPos[l].x][m_ptPos[l].y] += 1;
- sprintf(buf, "(before check)i=%d j=%d k=%drn", i, j, k);
- //PrintSky(nSky, buf);
- bInterrupted = FALSE;
- for(nx=0; nx<10; nx++)
- {
- for(ny=0; ny<10; ny++)
- if(nSky[nx][ny]==2) //如果为2,说明飞机有重叠,重新第三层循环
- {
- for(l=0; l<10; l++)
- nSky[m_ptPos[l].x][m_ptPos[l].y] -= 1;
- bInterrupted = TRUE;
- break;
- }
- if(bInterrupted)
- break;
- }
- if(bInterrupted)
- continue;
- sprintf(buf, "(通过测试)i=%d j=%d k=%drn", i, j, k);
- //PrintSky(nSky, buf);
- BOOL bMatched = TRUE;
- for(int nRow=0; nRow<10; nRow++)
- {
- for(int nColumn=0; nColumn<10; nColumn++)
- {
- if((m_nSky[nRow][nColumn]!=2)
- &&(nSky[nRow][nColumn] != m_nSky[nRow][nColumn]))
- {
- bMatched = FALSE;
- break;
- }
- }
- if(!bMatched)
- break;
- }
- if(bMatched)
- return TRUE;
- for(l=0; l<10; l++)
- nSky[m_ptPos[l].x][m_ptPos[l].y] -= 1;
- }
- for(l=0; l<10; l++)
- nSky[ptPos2[l].x][ptPos2[l].y] -= 1;
- }
- }
- sprintf(buf, "leave: i=%d j=%d k=%drn", i, j, k);
- //PrintSky(m_nSky, buf);
- return FALSE;
- }
- void CFighterInfo::SetItInSky()
- {
- m_nSky[m_ptPos[0].x][m_ptPos[0].y] = 2;
- for(int i=1; i<10; i++){
- m_nSky[m_ptPos[i].x][m_ptPos[i].y] = 1;
- }
- }
- CPoint CFighterInfo::GetHeadPos()
- {
- return m_ptHeadPos;
- }
- void CFighterInfo::PrintSky(int nSky[10][10], LPCTSTR buf)
- {
- CStdioFile file;
- if(!file.Open("g:\sky.txt", CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite))
- {
- AfxMessageBox("open sky.txt fail");
- return ;
- }
- CString info;
- int temp;
- file.Seek(0, CFile::end);
- file.WriteString(buf);
- for(int j=0; j<10; j++)
- {
- for(int i=0; i<10; i++)
- {
- temp = nSky[i][j];
- info.Format("%d ", nSky[i][j]);
- file.WriteString(info);
- }
- file.WriteString("rn");
- }
- file.Close();
- }