COCOS2D-X象棋代码(无AI)

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::CCLayer { public:     virtual bool init();       static cocos2d::CCScene* scene();  void menuBeginCallback(CCObject* pSender);  void menuCloseCallback(CCObject* pSender);     CREATE_FUNC(HelloWorld); }; #endif 

HelloWorldScene.cpp

#include "HelloWorldScene.h"
#include "proj.win32\Mygame.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
 HelloWorld *layer = HelloWorld::create();
 scene->addChild(layer);
    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }

 CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

  CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
    "End.png",
    "End1.png",
    this,
   menu_selector(HelloWorld::menuCloseCallback));  
  pCloseItem->setPosition(SCREENX/2,SCREENY*2/5);

 CCMenuItemImage *pBeginItem = CCMenuItemImage::create(
  "Begin.png",
  "Begin1.png",
  this,
  menu_selector(HelloWorld::menuBeginCallback));
 pBeginItem->setPosition(SCREENX/2,SCREENY*3/5);
 CCMenu* piMenu = CCMenu::create(pBeginItem,pCloseItem, NULL);
 piMenu->setPosition(CCPointZero);
 this->addChild(piMenu, 1);

 CCSprite* Chinesechess = CCSprite::create("Chinesechess.png");
 Chinesechess->setPosition(ccp(SCREENX/2,SCREENY*4/5));
 this->addChild(Chinesechess,1);

 CCSprite* background = CCSprite::create("background.jpg");
 background->setPosition(ccp(SCREENX/2,SCREENY/2));
 this->addChild(background,0);
    return true;
}


void HelloWorld::menuBeginCallback(CCObject* pSender)
{
 CCDirector::sharedDirector()->replaceScene(Mygame::scene()); 
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
 CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}

Gamesence.h

#ifndef MYGAME_
#define MYGAME_
#include "cocos2d.h"
#include "Qizi.h"
#define SCREENX 360
#define  SCREENY 600
USING_NS_CC;
class Mygame : public cocos2d::CCLayer 
{
public:
 int Map[10][9],
  Undo[1000][5];
 CCSprite* mylock;
 CCSprite* player;
 CCSprite* BK;
 CCSprite* rwin;
 CCSprite* rmycheckmate;
 CCSprite* bmycheckmate;
 Qizi* qizi[32];
 CCSprite* chess[32];
 CCSprite* myhint[18];
 int chessplayer,bposx,bposy,rbposx,rbposy,
  aposx,aposy,raposx,raposy,click,icheckmate,
  Gamenotover,ichessNumber;
 void Move(int x1,int y1,int Bossx,int Bossy);
 void newchess();
 void Result();
 void BossToking();
 void checkmate(int color);
 void hint(int x1,int y1,int Bossx,int Bossy);
 void hintesc();
 void redblack();
 virtual bool init();
 bool ccTouchBegan( CCTouch *pTouch, CCEvent *pEvent );
 void ccTouchEnded( CCTouch *pTouch, CCEvent *pEvent );
 static cocos2d::CCScene* scene();
 void menuredCallback(CCObject* pSender);
 void menublackCallback(CCObject* pSender);
 void menuBackCallback(CCObject* pSender);
 void menuNewCallback(CCObject* pSender);
 void menuUndoCallback(CCObject* pSender);
 CREATE_FUNC(Mygame);
};
#endif

Gamesence.cpp

#include "cocos2d.h"
#include "HelloWorldScene.h"
#include "Mygame.h"
#include "CCDirector.h"
USING_NS_CC;
CCScene* Mygame::scene()
{
 CCScene *scene = CCScene::create();
 Mygame *layer = Mygame::create();
 scene->addChild(layer);
 return scene;
}

void Mygame::newchess()            //初始化棋盘
{
 for(int i=0;i<10;++i)
 {
  for(int j=0;j<9;++j)
  {
   Map[i][j] = 0;
  }
 }
 for (int i=1;i<33;i++)
 {
  qizi[i-1] = new Qizi;
  qizi[i-1]->ID = i;
  qizi[i-1]->life = 1;
 }

 for (int i=0;i<16;i++)
 {
  qizi[i]->color = -1;
 }

 for (int i=16;i<32;i++)
 {
  qizi[i]->color = 1; 
 }

 int itype[32] = {0,1,2,3,4,3,2,1,0,5,5,6,6,6,6,6,6,6,6,6,6,5,5,0,1,2,3,4,3,2,1,0};
 int iposx[32] = {0,1,2,3,4,5,6,7,8,1,7,0,2,4,6,8,0,2,4,6,8,1,7,0,1,2,3,4,5,6,7,8};
 int iposy[32] = {0,0,0,0,0,0,0,0,0,2,2,3,3,3,3,3,6,6,6,6,6,7,7,9,9,9,9,9,9,9,9,9};
 for(int i=0;i<32;i++)
 {
  qizi[i]->posx = iposx[i];
  qizi[i]->posy = iposy[i];
  qizi[i]->type = itype[i];
  Map[qizi[i]->posy][qizi[i]->posx] = qizi[i]->ID;
 }

 for (int i = 0;i<32;i++)
 {
  char* a = new char[20];
  sprintf(a,"chess\\%d.png",i);
  chess[i]=CCSprite::create(a);
  chess[i]->setPosition(ccp((qizi[i]->posx*77+52)/2,(1148-qizi[i]->posy*77)/2));
  this->addChild(chess[i],1);
  chess[i]->setScale(1);
  delete[] a;
 }

}

bool Mygame::init()
{
 if ( !CCLayer::init() )
 {
  return false;
 }

 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,kCCMenuHandlerPriority,true);

 CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
 CCMenuItemImage *pBackItem = CCMenuItemImage::create(
  "chess\\back.png",
  "chess\\back0.png",
  this,
  menu_selector(Mygame::menuBackCallback));

 pBackItem->setPosition(SCREENX/2,SCREENY/8);
 CCMenu* pMenu = CCMenu::create(pBackItem, NULL);
 pMenu->setPosition(ccp(0,0));
 this->addChild(pMenu,2);

 CCMenuItemImage *pUndoItem = CCMenuItemImage::create(
  "chess\\undo.png",
  "chess\\undo1.png",
  this,
  menu_selector(Mygame::menuUndoCallback));

 pUndoItem->setPosition(SCREENX*1/6,SCREENY*7/24);
 CCMenu* pUndoMenu = CCMenu::create(pUndoItem, NULL);
 pUndoMenu->setPosition(ccp(0,0));
 this->addChild(pUndoMenu,2);

 CCMenuItemImage *pNewItem = CCMenuItemImage::create(
  "chess\\New.png",
  "chess\\New1.png",
  this,
  menu_selector(Mygame::menuNewCallback));

 pNewItem->setPosition(SCREENX*5/6,SCREENY*7/24);
 CCMenu* pNMenu = CCMenu::create(pNewItem, NULL);
 pNMenu->setPosition(ccp(0,0));
 this->addChild(pNMenu,2);

 CCMenuItemImage *redgiveup = CCMenuItemImage::create(
  "chess\\redgiveup.png",
  "chess\\redgiveup1.png",
  this,
  menu_selector(Mygame::menuredCallback));

 redgiveup->setPosition(SCREENX*1/6,SCREENY*5/24);
 CCMenu* redlose = CCMenu::create(redgiveup, NULL);
 redlose->setPosition(ccp(0,0));
 this->addChild(redlose,2);

 CCMenuItemImage *blackgiveup = CCMenuItemImage::create(
  "chess\\blackgiveup.png",
  "chess\\blackgiveup1.png",
  this,
  menu_selector(Mygame::menublackCallback));

 blackgiveup->setPosition(SCREENX*5/6,SCREENY*5/24);
 CCMenu* blacklose = CCMenu::create(blackgiveup, NULL);
 blacklose->setPosition(ccp(0,0));
 this->addChild(blacklose,2);

 CCSprite* Chinesechess = CCSprite::create("chess\\backdrop.png");
 Chinesechess->setPosition(ccp(SCREENX/2,SCREENY/2));
 this->addChild(Chinesechess,1);
 
 CCSprite* ChessBoard = CCSprite::create("chess\\ChessBoard.png");
 ChessBoard->setPosition(ccp((SCREENX)/2,SCREENY/2+100));
 this->addChild(ChessBoard,1);
 ChessBoard->setScale(0.5);

 mylock=CCSprite::create("chess\\lock.png");
 mylock->setPosition(ccp(-500,-500));
 this->addChild(mylock,1);
 mylock->setScale(0.5);

 BK=CCSprite::create("chess\\see.png");
 BK->setPosition(ccp(-1000,1000));
 addChild(BK,1);

 rmycheckmate=CCSprite::create("chess\\redcheckmate.png");
 rmycheckmate->setPosition(ccp(1500,SCREENY/2));
 this->addChild(rmycheckmate,0);

 bmycheckmate=CCSprite::create("chess\\blackcheckmate.png");
 rmycheckmate->setPosition(ccp(-500,SCREENY/2));
 this->addChild(bmycheckmate,0);

 for (int i=0;i<18;i++)
 {
  myhint[i]=CCSprite::create("chess\\hint.png");
  this->addChild(myhint[i],0);
  myhint[i]->setScale(2);
 }

 player = CCSprite::create("chess\\red.png");
 player->setPosition(ccp(SCREENX/2,SCREENY/4));
 this->addChild(player,1);
 player->setScale(0.5);

 newchess();

 Gamenotover = 1;
 icheckmate = 0;
 chessplayer = 1;
 ichessNumber = 0;
 click = 0;
 return true;
}

void Mygame::menuBackCallback(CCObject* pSender)
{
 CCDirector::sharedDirector()->pushScene(HelloWorld::scene());
}

bool Mygame::ccTouchBegan( CCTouch *pTouch, CCEvent *pEvent )
{
 return true;
}

void Mygame::ccTouchEnded( CCTouch *pTouch, CCEvent *pEvent )
{
 int Bossx,Bossy;
 if (chessplayer==1)
 {
  Bossx = qizi[4]->posx;
  Bossy = qizi[4]->posy;
 }
 else
 {
  Bossx = qizi[27]->posx;
  Bossy = qizi[27]->posy;
 }
// int ax = pTouch->getLocation().x,
// ay = pTouch->getLocation().y;
 if (Gamenotover)
 {
  if (click)
 {
  aposx = pTouch->getLocation().x;
  aposy = pTouch->getLocation().y;
  if (aposy>200)
  {
   raposx = (aposx-14)*2/77;
   raposy = (SCREENY-aposy)*2/77;
  }
  CCLOG("rax=%d,ray=%d",raposx,raposy);
  Move(raposx,raposy,Bossx,Bossy);
 }
 else
 {
  bposx = pTouch->getLocation().x;
  bposy = pTouch->getLocation().y;
  if (bposy>200)
  {
   rbposx = (bposx-14)*2/77;
   rbposy = (SCREENY-bposy)*2/77;
   CCLOG("rbx=%d,rby=%d",rbposx,rbposy);
   if (qizi[Map[rbposy][rbposx]-1]->color==chessplayer)
   {
    mylock->setPosition(ccp((rbposx*77+52)/2,(1148-rbposy*77)/2));
    click = 1;
    hintesc();
    hint(rbposx,rbposy,Bossx,Bossy);
   }
   else
   {
    mylock->setPosition(ccp(-500,-500));
    click = 0;
   }
  }
 }
 }
}

void Mygame::Move(int x1,int y1,int Bossx,int Bossy)
{
 if (qizi[Map[rbposy][rbposx]-1]->putchess(x1,y1,Map,Bossx,Bossy))
 {
   if (Map[y1][x1])
   {
    if (qizi[Map[y1][x1]-1]->color==chessplayer)
    {
     mylock->setPosition(ccp((x1*77+52)/2,(1148-y1*77)/2));
     rbposx = raposx;
     rbposy = raposy;
     hintesc();
     hint(x1,y1,Bossx,Bossy);
    }
    else
    {
     hintesc();
     CCMoveTo *put = CCMoveTo::create(0.5,ccp((x1*77+52)/2,(1148-y1*77)/2));
     chess[Map[rbposy][rbposx]-1]->runAction(put);
     qizi[Map[y1][x1]-1]->life = 0;
     chess[Map[y1][x1]-1]->setZOrder(0);
     Undo[ichessNumber][0] = x1;
     Undo[ichessNumber][1] = y1;
     Undo[ichessNumber][2] = rbposx;
     Undo[ichessNumber][3] = rbposy;
     Undo[ichessNumber][4] = qizi[Map[y1][x1]-1]->ID;
     Map[y1][x1] = Map[rbposy][rbposx];
     Map[rbposy][rbposx] = 0;
     qizi[Map[y1][x1]-1]->posx = x1;
     qizi[Map[y1][x1]-1]->posy = y1;
     Result();
     chessplayer = -chessplayer;
     click = 0;
     mylock->setPosition(ccp(-500,-500));
     checkmate(qizi[Map[y1][x1]-1]->color);
     ichessNumber++;
     BossToking();
    }
   }
   else
   {
    hintesc();
    CCMoveTo *put = CCMoveTo::create(0.5,ccp((x1*77+52)/2,(1148-y1*77)/2));
    chess[Map[rbposy][rbposx]-1]->runAction(put);
    Undo[ichessNumber][0] = x1;
    Undo[ichessNumber][1] = y1;
    Undo[ichessNumber][2] = rbposx;
    Undo[ichessNumber][3] = rbposy;
    Undo[ichessNumber][4] = 0;
    Map[y1][x1] = Map[rbposy][rbposx];
    Map[rbposy][rbposx] = 0;
    qizi[Map[y1][x1]-1]->posx = x1;
    qizi[Map[y1][x1]-1]->posy = y1;
    chessplayer = -chessplayer;
    click = 0;
    mylock->setPosition(ccp(-500,-500));
    checkmate(qizi[Map[y1][x1]-1]->color);
    ichessNumber++;
    BossToking();
   }
   redblack();
 }
 else
 {
  if (Map[raposy][raposx]&&(qizi[Map[raposy][raposx]-1]->color==chessplayer))
  {
   mylock->setPosition(ccp((raposx*77+52)/2,(1148-raposy*77)/2));
   rbposx = raposx;
   rbposy = raposy;
   hintesc();
   hint(rbposx,rbposy,Bossx,Bossy);
  }
  else
  {
   mylock->setPosition(ccp(-500,-500));
  }
 }
 
}

void Mygame::hint(int x1,int y1,int Bossx,int Bossy)
{
 for (int i=0,k=0;i<10;i++)
 {
  for (int j=0;j<9;j++)
  {
   if (qizi[Map[y1][x1]-1]->putchess(j,i,Map,Bossx,Bossy))
   {
    if (Map[i][j])
    {
     if (qizi[Map[i][j]-1]->color!=chessplayer)
     {
      myhint[k]->setPosition(ccp((j*77+52)/2,(1148-i*77)/2));
      myhint[k]->setZOrder(1);
      myhint[k]->setScale(0.8);
      k++;
     }
    }
    else
    {
     myhint[k]->setPosition(ccp((j*77+52)/2,(1148-i*77)/2));
     myhint[k]->setZOrder(1);
     myhint[k]->setScale(0.8);
     k++;
    }
   }
  }
 }
}

void Mygame::hintesc()
{
 for (int i=0;i<18;i++)
 {
  myhint[i]->setZOrder(0);
 }
}

void Mygame::checkmate(int color)
{
 int a=0,b=0;
 if (color==1)
 {
  for (int i=0;i<16;i++)
  {
   if (qizi[i]->life)
    {
     b = (int)qizi[i]->putchess(qizi[27]->posx,qizi[27]->posy,Map,bposx,bposy);
     a = a+b;
    }
  }
  if (a)
  {
   icheckmate = 1;
   CCMoveTo *bmove = CCMoveTo::create(1,ccp(SCREENX/2,SCREENY/2+100));
   bmycheckmate->runAction(bmove);
   bmycheckmate->setZOrder(1);
   bmycheckmate->setScale(0.5);
  }
  else
  {
   for (int i=16;i<32;i++)
   {
    if (qizi[i]->life)
    {
     b = (int)qizi[i]->putchess(qizi[4]->posx,qizi[4]->posy,Map,bposx,bposy);
     a = a+b;
    }
   }
   if (a)
   {
    CCMoveTo *rmove = CCMoveTo::create(1,ccp(SCREENX/2,SCREENY/2+100));
    rmycheckmate->runAction(rmove);
    rmycheckmate->setZOrder(1);
   }
   else
   {
    bmycheckmate->setPosition(ccp(-500,SCREENY/2+100));
    bmycheckmate->setScale(0.5);
    rmycheckmate->setPosition(ccp(1500,SCREENY/2+100));
    rmycheckmate->setScale(0.5);
   }
  }
 }
 else
 {
  for (int i=16;i<32;i++)
  {
   if (qizi[i]->life)
   {
    b = (int)qizi[i]->putchess(qizi[4]->posx,qizi[4]->posy,Map,bposx,bposy);
    a = a+b;
   }
  }
  if (a)
  {
   CCMoveTo *rmove = CCMoveTo::create(1,ccp(SCREENX/2,SCREENY/2+100));
   rmycheckmate->runAction(rmove);
   rmycheckmate->setZOrder(1);
  }
  else
  {
   for (int i=0;i<16;i++)
   {
    if (qizi[i]->life)
    {
     b = (int)qizi[i]->putchess(qizi[27]->posx,qizi[27]->posy,Map,bposx,bposy);
     a = a+b;
    }
   }
   if (a)
   {
    icheckmate = 1;
    CCMoveTo *bmove = CCMoveTo::create(1,ccp(SCREENX/2,SCREENY/2+100));
    bmycheckmate->runAction(bmove);
    bmycheckmate->setZOrder(1);
   }
   else
   {
    bmycheckmate->setPosition(ccp(-500,SCREENY/2+100));
    bmycheckmate->setScale(0.5);
    rmycheckmate->setPosition(ccp(1500,SCREENY/2+100));
    rmycheckmate->setScale(0.5);
   }
   
  }
 }
}

void Mygame::menuNewCallback( CCObject* pSender )
{
 CCDirector::sharedDirector()->replaceScene(Mygame::scene());
}

void Mygame::Result()
{
 if (qizi[27]->life==0)
 {
  Gamenotover = 0;
  CCSprite* bwin=CCSprite::create("chess\\blackwin.png");
  bwin->setPosition(ccp(SCREENX/2,SCREENY/2+100));
  this->addChild(bwin,1);
  bwin->setScale(0.5);
  BK->setZOrder(0);
 }
 if (qizi[4]->life==0)
 {
  Gamenotover = 0;
  CCSprite* rwin=CCSprite::create("chess\\redwin.png");
  rwin->setPosition(ccp(SCREENX/2,SCREENY/2+100));
  this->addChild(rwin,1);
  rwin->setScale(0.5);
  BK->setZOrder(0);

 }
}

void Mygame::menuUndoCallback( CCObject* pSender )
{
 if (ichessNumber!=0)
 {
  if (Undo[ichessNumber-1][4])
 {
  CCMoveTo *theUndo = CCMoveTo::create(0.5,ccp((Undo[ichessNumber-1][2]*77+52)/2,(1148-Undo[ichessNumber-1][3]*77)/2));
  chess[Map[Undo[ichessNumber-1][1]][Undo[ichessNumber-1][0]]-1]->runAction(theUndo);
  qizi[Map[Undo[ichessNumber-1][1]][Undo[ichessNumber-1][0]]-1]->posx = Undo[ichessNumber-1][2];
  qizi[Map[Undo[ichessNumber-1][1]][Undo[ichessNumber-1][0]]-1]->posy = Undo[ichessNumber-1][3];
  Map[Undo[ichessNumber-1][3]][Undo[ichessNumber-1][2]] = Map[Undo[ichessNumber-1][1]][Undo[ichessNumber-1][0]];
  Map[Undo[ichessNumber-1][1]][Undo[ichessNumber-1][0]] = Undo[ichessNumber-1][4];
  chess[Undo[ichessNumber-1][4]-1]->setZOrder(1);
  ichessNumber--;
  chessplayer = -chessplayer;
  redblack();
  checkmate(chessplayer);
 }
 else
 {
  CCMoveTo *theUndonull = CCMoveTo::create(0.5,ccp((Undo[ichessNumber-1][2]*77+52)/2,(1148-Undo[ichessNumber-1][3]*77)/2));
  chess[Map[Undo[ichessNumber-1][1]][Undo[ichessNumber-1][0]]-1]->runAction(theUndonull);
  qizi[Map[Undo[ichessNumber-1][1]][Undo[ichessNumber-1][0]]-1]->posx = Undo[ichessNumber-1][2];
  qizi[Map[Undo[ichessNumber-1][1]][Undo[ichessNumber-1][0]]-1]->posy = Undo[ichessNumber-1][3];
  Map[Undo[ichessNumber-1][3]][Undo[ichessNumber-1][2]] = Map[Undo[ichessNumber-1][1]][Undo[ichessNumber-1][0]];
  Map[Undo[ichessNumber-1][1]][Undo[ichessNumber-1][0]] = 0;
  ichessNumber--;
  chessplayer = -chessplayer;
  redblack();
  checkmate(chessplayer);
 }
 }
}

void Mygame::redblack()
{
 if (chessplayer==1)
 {
  CCTexture2D* texture0 = CCTextureCache::sharedTextureCache()->addImage("chess\\red.png");
  player->setTexture(texture0);
 }
 else
 {
  CCTexture2D* texture1 = CCTextureCache::sharedTextureCache()->addImage("chess\\black.png");
  player->setTexture(texture1);
 }
}

void Mygame::BossToking()
{
 if ((qizi[4]->posx==qizi[27]->posx)&&(qizi[27]->icount(qizi[4]->posx,qizi[4]->posy,Map)==0))
 {
  BK->setPosition(ccp(SCREENX/2,SCREENY/2+100));
  BK->setScale(0.5);
 }
}

void Mygame::menuredCallback( CCObject* pSender )
{
 if (chessplayer==1)
 {
  Gamenotover = 0;
  qizi[27]->life = 0;
  Result();
 }

}

void Mygame::menublackCallback( CCObject* pSender )
{
 if (chessplayer==-1)
 {
  Gamenotover = 0;
  qizi[4]->life = 0;
  Result();
 }
}

Qizi.h //棋子类

#pragma once
#include "cocos2d.h"
class Qizi
{
public:
 int ID,
  color,
  type,
  posx,
  posy,
  life;
 bool(Qizi::*fun[7])(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy);
 bool putchess(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy);
 int icount(int x1,int y1,int (&Map)[10][9]);
 void init();
 bool Che(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy);
 bool Ma(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy);
 bool Xiang(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy);
 bool Shi(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy);
 bool Boss(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy);
 bool Pao(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy);
 bool Bing(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy);
};


Qizi.cpp


#include "Qizi.h"
#include <math.h>

void Qizi::init()
{
 fun[0] = &Qizi::Che;
 fun[1] = &Qizi::Ma;
 fun[2] = &Qizi::Xiang;
 fun[3] = &Qizi::Shi;
 fun[4] = &Qizi::Boss;
 fun[5] = &Qizi::Pao;
 fun[6] = &Qizi::Bing;
}

bool Qizi::Che(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy)
{
 if (abs(x1-this->posx)*abs(y1-this->posy)==0)
 {
  if (icount(x1,y1,Map))
  {
   return 0;
  }
  else
  {
   return 1;
  }
 }
 else
 {
  return 0;
 }
}

bool Qizi::Ma(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy)
{
 if (abs((x1-this->posx)*(y1-this->posy))==2)
 {
  if (Map[this->posy+(y1-this->posy)/2][this->posx+(x1-this->posx)/2])
  {
   return 0;
  }
  else
  {
   return 1;
  }
 }
 else
 {
  return 0;
 }
}

bool Qizi::Xiang(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy)
{
 if (this->color==1)
 {
  if (y1>4)
  {
   if ((x1-this->posx)*(x1-this->posx)+(y1-this->posy)*(y1-this->posy)==8)
  {
   if (Map[this->posy+(y1-this->posy)/2][this->posx+(x1-this->posx)/2])
   {
    return 0;
   }
   else
   {
    return 1;
   }
  }
   else
   {
    return 0;
   }
  }
  else
  {
   return 0;
  }
 }
 else
 {
  if (y1<5)
  {
   if ((x1-this->posx)*(x1-this->posx)+(y1-this->posy)*(y1-this->posy)==8)
   {
    if (Map[this->posy+(y1-this->posy)/2][this->posx+(x1-this->posx)/2])
    {
     return 0;
    }
    else
    {
     return 1;
    }
   }
   else
   {
    return 0;
   }
  }
  else
  {
   return 0;
  }
 }
}

bool Qizi::Shi(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy)
{
 if ((2<x1)&&(x1<6))
 {
  if (this->color==1)
  {
   if ((y1>6)&&(((x1-this->posx)*(x1-this->posx)+(y1-this->posy)*(y1-this->posy))==2))
   {
    return 1;
   }
   else
   {
    return 0;
   }
  }
  else
  {
   if ((y1<3)&&(((x1-this->posx)*(x1-this->posx)+(y1-this->posy)*(y1-this->posy))==2))
   {
    return 1;
   }
   else
   {
    return 0;
   }
  }
 }
 else
 {
  return 0;
 }
}

bool Qizi::putchess( int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy)
{
 init();
 return (this->*fun[this->type])(x1,y1,Map,Bossx,Bossy);
}

bool Qizi::Boss(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy)
{
 if ((Bossx==this->posx)&&(icount(Bossx,Bossy,Map)==0))
 {
  if ((x1==Bossx)&&(y1==Bossy))
  {
   return 1;
  }
  else
  {
   return 0;
  }
 }
 else
 {
  if ((2<x1)&&(x1<6))
  {
   if (this->color==1)
   {
    if ((y1>6)&&((abs(x1-this->posx)+abs(y1-this->posy))==1))
    {
     return 1;
    }
    else
    {
     return 0;
    }
   }
   else
   {
    if ((y1<3)&&((abs(x1-this->posx)+abs(y1-this->posy))==1))
    {
     return 1;
    }
    else
    {
     return 0;
    }
   }
  }
  else
  {
   return 0;
  }
 }
 }

bool Qizi::Pao(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy)
{
 if ((x1-this->posx)*(y1-this->posy))
 {
  return 0;
 }
 else
 {
  if (icount(x1,y1,Map)<2)
  {
   int a = icount(x1,y1,Map);
   if (icount(x1,y1,Map))
   {
    if (Map[y1][x1])
    {
     return 1;
    }
    else
    {
     return 0;
    }
   }
   else
   {
    if (Map[y1][x1])
    {
     return 0;
    }
    else
    {
     return 1;
    }
   }
  }
  else
  {
   return 0;
  }
 }
}

bool Qizi::Bing(int x1,int y1,int (&Map)[10][9],int Bossx,int Bossy)
{
  if (this->color==1)
  {
   if (y1>4)
   {
    if (((this->posy-y1)==1)&&(x1==this->posx))
    {
     return 1;
    }
    else
    {
     return 0;
    }
   }
   else
   {
    if ((y1<=this->posy)&&((abs(x1-this->posx)+abs(y1-this->posy))==1))
    {
     return 1;
    }
    else
    {
     return 0;
    }
   }
  }
  else
  {
   if (y1<5)
   {
    if (((y1-this->posy)==1)&&(x1==this->posx))
    {
     return 1;
    }
    else
    {
     return 0;
    }
   }
   else
   {
    if ((y1>=this->posy)&&((abs(x1-this->posx)+abs(y1-this->posy))==1))
    {
     return 1;
    }
    else
    {
     return 0;
    }
   }
  }
 }

int Qizi::icount( int x1,int y1,int (&Map)[10][9])
{
  int count = 0;
  if ((abs(x1-this->posx)==1)||(abs(y1-this->posy)==1))
  {
   return count;
  }
  else
  {
   for(int i=1;i<abs((x1-this->posx)+(y1-this->posy));i++)
   {
    if ((x1-this->posx)!=0)
    {
     if (Map[this->posy][this->posx+i*(x1-this->posx)/abs(x1-this->posx)])
     {
      count++;
      if (i==(abs(x1-this->posx)-1))
      {
       return count;
      }
     }
     else
     {
      if (i==(abs(x1-this->posx)-1))
      {
       return count;
      }
     }
    }
    else
    {
     if (Map[this->posy+i*(y1-this->posy)/abs(y1-this->posy)][this->posx])
     {
      count++;
      if (i==abs(y1-this->posy)-1)
      {
       return count;
      }
     }
     else
     {
      if (i==abs(y1-this->posy)-1)
      {
       return count;
      }
     }
    }
   }
  }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值