用cocos2d-x 实现的带ai的五子棋

用cocos2d-x 实现的带ai的五子棋,第1张

概述做了几个场景切换,稍微美化了一下,大体效果如下 问题定义及分析 五子棋问题归结为一个寻找某状态下的棋盘的最佳落子点的问题,所以我们不如使用一个整型二维数组来代表棋盘状态,每个元素这一点的状态,0表示该处无子,1为黑子,2为白子。将下棋时棋落在哪一点 的决策标准抽象化为该点的得分 我们进一步分析两个人在下棋时的想法。落子时,考虑两方面的目的: 1.进攻,即设法使自己的局势朝向胜利方向转变; 2.防守 做了几个场景切换,稍微美化了一下,大体效果如下




问题定义及分析

五子棋问题归结为一个寻找某状态下的棋盘的最佳落子点的问题,所以我们不如使用一个整型二维数组来代表棋盘状态,每个元素这一点的状态,0表示该处无子,1为黑子,2为白子。将下棋时棋落在哪一点的决策标准抽象化为该点的得分

我们进一步分析两个人在下棋时的想法。落子时,考虑两方面的目的:

1.进攻,即设法使自己的局势朝向胜利方向转变;

2.防守,即设法阻止对家胜利(即若对家在改点落子可使对家走向成功,则我方应在此落子以阻碍对方取得胜利)。

故落子时必须综合考虑到上述两种因素:我们希望找到一点,当任意一方在此落子时,可造成对该方更容易达到五子连环的局面。而我们需要对“更容易达到五子连环的局面”的这一表达进行的界定:一是落子后所能达到的连环子数(这也是最直观的),二是下一个回何时是否能在此基础上继续加棋子(如:当该子下在了棋盘的边缘,则下一次最多只能下在连环的另一端,倘若另一端已有棋子,那么就不能在此继续造成更多的连环了)以及最多能加多少(1,2或0)。

将上述思想转化程序思想。对于上述“更容易达到五子连环的局面”的这一表达进行的界定,采用计分的形式(如:当放置在此点可造成五子连环,便得到一个最大的分值,若只有四子连环,则得到一个小的分值,等等,再将各分值累加。

遍历棋盘所有空位(在某一位置,假设放上己方的棋子,计算此时己方得分,(此为进攻得分);假设放上对方棋子,计算此时对方得分(此为防守得分),二者相加即得最终得分。选取分值最大的点,即为要下的位置


下面是我从网上搜集到的计分表



参照上表,实现的计分函数score如下:

int ChessBox::score(int color) //1 black   2 white{char d[16][7] = { "OOOOO","+OOOO+","+OOO++","++OOO+","+OO+O+","+O+OO+","OOOO+","+OOOO","OO+OO","O+OOO","OOO+O","++OO++","++O+O+","+O+O++","+++O++","++O+++" };int s[16] = { 50000,4320,720,120,20,20 };int score = 0;int i,j,k,l;/*检查行*/for (i = 0; i < 15; i++)for (j = 0; j < 15; j++)for (k = 0; k < 16; k++)if (strlen(d[k]) + j - 1 <= 15){int flag = 1;//flag==1 初始默认可行for (l = 0; l < strlen(d[k]); L++)if (((d[k][l] == 'O') && (a[i][j + l] != color)) || ((d[k][l] == '+') && (a[i][j + l] != 0))){flag = 0;//发现不可行break;}if (flag) score += s[k];}/*检查列*/for (j = 0; j < 15; j++)for (i = 0; i < 15; i++)for (k = 0; k < 16; k++)if (strlen(d[k]) + i - 1 <= 15){int flag = 1;for (l = 0; l < strlen(d[k]); L++)if (((d[k][l] == 'O') && (a[i + l][j] != color)) || ((d[k][l] == '+') && (a[i + l][j] != 0))){flag = 0;break;}if (flag) score += s[k];}/*检查斜*/for (i = 0; i < 15; i++)for (j = 0; j < 15; j++)for (k = 0; k<16; k++){int ii = i,jj = j;int flag = 1;for (l = 0; l<strlen(d[k]); L++){if ((ii>15) || (jj>15)){flag = 0;break;}if (((d[k][l] == 'O') && (a[ii][jj] != color)) || ((d[k][l] == '+') && (a[ii][jj] != 0))){flag = 0;break;}ii++; jj++;}if (flag) score += s[k];}/*检查斜*/for (i = 0; i < 15; i++)for (j = 0; j < 15; j++)for (k = 0; k < 16; k++){int ii = i,jj = j;int flag = 1;for (l = 0; l<strlen(d[k]); L++){if ((ii>15) || (jj < 1)){flag = 0;break;}if (((d[k][l] == 'O') && (a[ii][jj] != color)) || ((d[k][l] == '+') && (a[ii][jj] != 0))){flag = 0;break;}ii++; jj--;}if (flag) score += s[k];}return score;}

进一步提高AI智能——应用博弈树

原先的算法只从电脑落子的角度考虑了一步。我们可以从玩家落子的角度再考虑一步,搜索两步。第一步搜电脑落子在该点的score[i][j],记为s1;第二步搜电脑在(i,j)落完子后,玩家落子所能获得的最大score[x][y],记为s2.取使得(s1-s2)的值最大的(i,j)作为电脑该步的落子点。这样我们综合考虑了两点:1.电脑在该步的下法优秀(s1尽量大);2.玩家在该步的最优下法差(s2尽量小),即(s1-s2)的值尽量大。同理可再向下搜第三步(s3尽量大),第四步(s4尽量小)……使(s1-s2+s3-s4……)尽量大,但是综合考虑时间效率,如果向下搜索太多步,程序运行时间较长,搜索两步时从运行时间和下棋智能两个方面综合考虑效果较好。

提高AI效率——Alpha-Beta剪枝

在博弈树的搜索过程中有些搜索是可以舍弃的,但是博弈树的计分方法必须修改,舍弃(s1-s2+s3-s4……)的计分方式,改为最大/最小的计分方法,只计算底层搜索的分数并且某个父节点的分数等于由它的某个子节点,在这个基础上,我们成功应用了Alpha-Beta剪枝,我们通过如下图的启发策略减少了不必要的搜索



矩形框节点是电脑走,圆形框节点是玩家走,我们搜索固定的层数n(搜索层数即为要搜的步数,图中的例子n=5),第n层为叶子节点,计算每个叶子节点对应的棋局的score,对于节点E,E是玩家走的节点,因此会在I、J节点中选择score最小的节点(即节点I,score=2),并将I的score作为自己的score。对于节点C,C是电脑走的节点,因此会在E、F节点中选score最大的节点。搜索节点F时,F是玩家走的节点,会在K、L、M中选score最小的节点,发现K的score为1,则F的score小于等于1,而E的score是2,E、F中score较大的点必定是E,F的子节点L、M不用再搜索。对于节点A,A是玩家走的点,会在C、D中取score最小的点。对于节点D,D是电脑走的点,会在G、H取score最大的点,因为G的score为7,所以D的score大于等于7,而C节点score为2,因此A节点取子节点C,D的子节点H不用搜索。


用递归实现的Alpha-beta剪枝如下:

</pre><pre name="code" >int ChessBox::nextscore(int currentDepth,int color,int depth){	int highest = 0;	int lowest = 0;	int rescore = 0;	for (int i = 0; i < 15; i++){		for (int j = 0; j < 15; j++){			if (a[i][j] == 0){				if (currentDepth == depth){//搜索到底层,计算分数					a[i][j] = 3 - color;					rescore = score(3 - color);					a[i][j] = color;					rescore += score(color);					a[i][j] = 0;				}				if (currentDepth % 2 == 0){					a[i][j] = color;					if (currentDepth<depth)rescore = nextscore(currentDepth + 1,3 - color,depth);//深度优先,进入下一层搜索					a[i][j] = 0;					if (rescore>= beta) {//发现大于等于当前层的beta值,余下的不需再探索,发生beta剪枝						return rescore;					}					if (rescore >= highest){						highest = rescore;						if (currentDepth == 1){							recordx = i;							recordy = j;						}					}				}				if (currentDepth % 2 != 0){					a[i][j] = color;					if (currentDepth < depth)rescore = nextscore(currentDepth + 1,depth);///深度优先,进入下一层搜索					a[i][j] = 0;					if (currentDepth == 1)rescore = -rescore;					if (rescore <= Alpha) {//发现小于等于当前层的Alpha值,余下的不需再探索,发生Alpha剪枝						return rescore;					}					if (rescore <= lowest){						lowest = rescore;						if (currentDepth == 1){							recordx = i;							recordy = j;						}					}				}			}		}	}	if (currentDepth % 2 == 0){//max层返回最大值给父节点		beta = highest;		return beta;	}	else {//min层返回最小值给父节点		Alpha = lowest;		return Alpha;	}}



附上主要代码:

_____________________________________HelloWorldScene.h_____________________________________________________#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include <stack>#include <iostream>#include "cocos2d.h"#include"editor-support\cocostudio\CCSGUIReader.h"#include"ui\CocosGUI.h"using namespace cocos2d;using namespace cocos2d::ui;using namespace cocostudio;using namespace std;//场景Chooseclass Choose : public cocos2d::Layer{public:static cocos2d::Scene* createScene();virtual bool init();voID whiteenter(Ref* pSender);voID blackenter(Ref* pSender);CREATE_FUNC(Choose);};//场景PLAYclass Play : public cocos2d::Layer{public:    // there's no 'ID' in cpp,so we recommend returning the class instance pointer    static cocos2d::Scene* createScene();    // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'ID' in cocos2d-iphone    virtual bool init();      // a selector callbackLayout* layout;button* restart;button* back;    // implement the "static create()" method manually    CREATE_FUNC(Play);};/************************************************************************//* ChessBox类绑定了重要的其他控件,方便我们在回调函数中对这些控件进行 *** 作   (这次为了方便,我没有做好很好的封装,数据和函数都放在public部分,这是我   做的不好的地方,下次有待改进)*//************************************************************************/class ChessBox :public cocos2d::Node{public:ChessBox();~ChessBox();voID BinDWhite(Sprite* sprite);voID Bindblack(Sprite* sprite);voID Bindback(Sprite* sprite);voID Bindrestart(Sprite* sprite);voID Check();//检查游戏是否结束的函数,包含了结束的动画int score(int color);//计算棋盘分数的函数int nextscore(int currentDepth,int depth);Sprite* ai();//ai函数,修改抽象化棋盘,返回一个棋子精灵的指针stack<Sprite*> chessstack;//棋子栈stack<int*> posstack;//坐标栈Sprite* back;//将要绑定的悔棋控件Sprite* blackBox;//将要绑定的黑棋盒控件Sprite* whiteBox;//将要绑定的白棋盒控件Sprite* restart;//将要绑定的新局控件int isai;//人机对战的状态bool end;//是否结束的状态int recordx,recordy;int a[15][15];//棋盘的抽象化int maxmin[10];int Alpha=60000,beta=-60000;int pitch;//下棋的状态,1代表这一步走白子,2走黑子int aicolor;//ai执棋颜色CREATE_FUNC(ChessBox);virtual bool init();};class Introduction : public Layer{public:static Scene* createScene();virtual bool init();voID oneenter(Ref* pSender);voID twoenter(Ref* pSender);CREATE_FUNC(Introduction);};#endif 
</pre><pre name="code" >__________________________________Introduction.cpp____________________________________________________________________#include "HelloWorldScene.h"USING_NS_CC;using namespace std;//两个全局变量的初始化int Aicolor = 0;int playway = 0;Scene* Introduction::createScene(){auto scene = Scene::create();auto layer = Introduction::create();scene->addChild(layer);return scene;}voID Introduction::oneenter(Ref* pSender){playway = 1;//人机对战Director::getInstance()->replaceScene(TransitionCrossFade::create(0.5f,Choose::createScene()));//这里使用了场景切换动画,效果是从中心向四周扩散}voID Introduction::twoenter(Ref* pSender){playway = 2;//双人对弈Director::getInstance()->replaceScene(TransitionCrossFade::create(0.5f,Play::createScene()));//同样使用了场景切换动画}bool Introduction::init(){Size visibleSize = Director::getInstance()->getVisibleSize();Point origin = Director::getInstance()->getVisibleOrigin();Layout* InBg = Layout::create();this->setposition(Point(visibleSize.wIDth/2,visibleSize.height/2));this->addChild(InBg);InBg->setBackGroundImage("BG.png");auto item1 = MenuItemImage::create("one1.png","one.png",CC_CALLBACK_1(Introduction::oneenter,this));auto item2 = MenuItemImage::create("two1.png","two.png",CC_CALLBACK_1(Introduction::twoenter,this));item2->setposition(Point(0,-200));Menu* pMenu = Menu::create(item1,item2,NulL);pMenu->setposition(Point(0,140));this->addChild(pMenu,1);return true;} 


_____________________________________HelloWorldScene.cpp___________________________________________________#include "HelloWorldScene.h"USING_NS_CC;using namespace std;extern int Aicolor;//定义的外部全局变量,这里做声明extern int playway;Scene* Play::createScene()//默认的场景生成函数{    // 'scene' is an autorelease object    auto scene = Scene::create();        // 'layer' is an autorelease object    auto layer = Play::create();    // add layer as a child to scenescene->addChild(layer);    // return the scene    return scene;}//场景Play的初始化bool Play::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }//获取窗体大小以及origin坐标Size visibleSize = Director::getInstance()->getVisibleSize();Point origin = Director::getInstance()->getVisibleOrigin();//Layout的初始化;添加背景图片layout = Layout::create();layout->setSize(Size(visibleSize.wIDth,visibleSize.height));layout->setBackGroundImage("BG.jpg");layout->setposition(Point(0,0));this->addChild(layout);//创建一个精灵代表棋盘Sprite* broad = Sprite::create("broad.png");broad->setposition(Point(65+217,70+222));broad->setScale(1.19f);layout->addChild(broad);//ChessBox以及棋盘上各按钮,组件的初始化ChessBox* chessBox = ChessBox::create();Sprite* blackBox = Sprite::create("BBox.png");Sprite* whiteBox = Sprite::create("WBox.png");Sprite* back = Sprite::create("back.png");Sprite* restart = Sprite::create("restart.png");//玩家个数,是否人机对战等信息chessBox->aicolor =Aicolor;chessBox->pitch = 3-Aicolor;chessBox->isai = playway;/************************************************************************//*将其他控件绑定到chessBox                                                                      *//************************************************************************/chessBox->Bindblack(blackBox);chessBox->BinDWhite(whiteBox);chessBox->Bindback(back);chessBox->Bindrestart(restart);chessBox->setposition(Point(600,120));//调整好位置layout->addChild(chessBox,300);//如果是人机对战,玩家持白字,则ai先落子if (Aicolor == 1 && playway == 1){auto aichess = chessBox->ai();layout->addChild(aichess);}auto chessListener=EventListenertouchOneByOne::create();/************************************************************************//* 接下来将开始监听事件使chessListener的ontouchBegan指向一个lambda函数,而lambda表达式内部是异步线程所以,由点击事件触发的一切变化都最好通过事件绑定的精灵对象来 *** 作Ps:我尝试过直接在[&]形式的lambda表达式里对栈以及其它对象进行 *** 作,发现对cocos的Node类以及其子类直接利用我截取的引用来调用addChild和removeChild居然不会发生错误,其他的都报错。这两个方法是否有什么特殊之处?*//************************************************************************/chessListener->ontouchBegan = [&](touch* touch,Event* event){auto target = static_cast<ChessBox*>(event->getCurrentTarget());Point pos = Director::getInstance()->convertToGL(touch->getLocationInVIEw());//获取棋子的棋盘坐标int x = (pos.x - (72 + 15+30*3)) / 36 +3;int y = (pos.y - (82 + 15+30*7)) / 32+7;if (x >= 0 && y >= 0 && x <= 14 && y <= 14 && target->end == false){if (target->pitch== 1 &&target->a[x][y]==0)//pitch 表示下棋的状态,1为黑子下,2为白子下{target->whiteBox->setScale((1.2f));target->blackBox->setopacity(0);target->blackBox->setScale(1.0f);target->whiteBox->setopacity(255);target->whiteBox->setposition(Point(70,0));target->blackBox->setposition(Point(70,0));Sprite* newchess = Sprite::create("black.png");newchess->setposition(Point(72 + x * 30,82 + y * 30));//放置棋子layout->addChild(newchess,10);newchess->setScale(0.193f);target->chessstack.push(newchess);//将棋子压入棋子栈target->a[x][y] = 1;//该位置不能再落子target->posstack.push(&(target->a[x][y]));//将此坐标压入坐标栈target->Check();//检查游戏是否结束if (target->end == false){/*若游戏未结束,则继续*/if (target->isai == 1){               /*若目前为人机对战模式,则ai落子*/target->blackBox->setScale((1.2f));target->whiteBox->setopacity(0);target->whiteBox->setScale(1.0f);target->blackBox->setopacity(255);target->whiteBox->setposition(Point(70,0));layout->addChild(target->ai(),10);target->Check();//检查游戏是否结束}else target->pitch = 2;              /*若目前为双人对战模式,则改变棋子颜色,等待下一玩家落子*/}return true;} if (target->pitch == 2 && target->a[x][y] == 0)//下白子的情况,和黑子基本一样{target->blackBox->setScale((1.2f));target->whiteBox->setopacity(0);target->whiteBox->setScale(1.0f);target->blackBox->setopacity(255);target->whiteBox->setposition(Point(0,0));target->whiteBox->setposition(Point(70,0));Sprite* newchess = Sprite::create("white.png");newchess->setposition(Point(72 + x * 30,82 + y * 30));layout->addChild(newchess,10);newchess->setScale(0.21f);target->chessstack.push(newchess);target->a[x][y] = 2;target->posstack.push(&(target->a[x][y]));target->Check();if (target->end == false){if (target->isai == 1){target->whiteBox->setScale((1.2f));target->blackBox->setopacity(0);target->blackBox->setScale(1.0f);target->whiteBox->setopacity(255);target->whiteBox->setposition(Point(70,10);target->Check();}else target->pitch = 1;}return true;}}return false;};chessListener->ontouchended = [&](touch* touch,Event* event){};//监听时间的注册_eventdispatcher->addEventListenerWithSceneGraPHPriority(chessListener,chessBox);auto restartListener = EventListenertouchOneByOne::create();/*设置重新开始监听*/restartListener->ontouchBegan = [](touch* touch,Event* event){auto target = static_cast<ChessBox*>(event->getCurrentTarget());Point pos = Director::getInstance()->convertToGL(touch->getLocationInVIEw());if (pos.x<839 && pos.x>651 && pos.y<521 && pos.y>399){target->restart->setScale(1.25f);//点击开始只在视图层层响应,逻辑层响应在松开鼠标之后return true;}return false;};restartListener->ontouchended = [&](touch * touch,Event* event){auto target = static_cast<ChessBox*>(event->getCurrentTarget());target->restart->setScale(1.0f);target->end = false;while (!(target->chessstack.empty())){/*将棋子栈和坐标栈置空,同时重新初始化棋盘*/layout->removeChild(target->chessstack.top());target->chessstack.pop();*(target->posstack.top()) = 0;target->posstack.pop();}};//注册第二个监听事件,同样和chessBox绑定_eventdispatcher->addEventListenerWithSceneGraPHPriority(restartListener,chessBox);/************************************************************************//*       悔棋功能的实现                                              */                                                /************************************************************************/auto backListener = EventListenertouchOneByOne::create();backListener->ontouchBegan = [&](touch* touch,Event* event){auto target = static_cast<ChessBox*>(event->getCurrentTarget());Point pos = Director::getInstance()->convertToGL(touch->getLocationInVIEw());if (pos.x<859 && pos.x>661 && pos.y<361 && pos.y>259 && target->end == false){target->back->setScale(1.25f);if (!target->chessstack.empty()){layout->removeChild(target->chessstack.top());target->chessstack.pop();*(target->posstack.top())=0;target->posstack.pop();}if (!target->chessstack.empty()){layout->removeChild(target->chessstack.top());target->chessstack.pop();*(target->posstack.top()) = 0;target->posstack.pop();}return true;}return false;};backListener->ontouchended = [](touch * touch,Event* event){auto target = static_cast<ChessBox*>(event->getCurrentTarget());target->back->setScale(1.0f);};_eventdispatcher->addEventListenerWithSceneGraPHPriority(backListener,chessBox);    return true;}


______________________________________Choose.cpp__________________________________________________________#include "HelloWorldScene.h"extern int Aicolor;extern int playway;Scene* Choose::createScene(){auto scene = Scene::create();auto layer = Choose::create();scene->addChild(layer);return scene;}voID Choose::whiteenter(Ref* pSender){Aicolor = 1;//修改全局变量,表示ai执黑子Director::getInstance()->replaceScene(TransitionProgressInOut::create(0.5f,Play::createScene()));}voID Choose::blackenter(Ref* pSender){Aicolor = 2;//修改全局变量,表示ai执白子Director::getInstance()->replaceScene(TransitionProgressInOut::create(0.5f,Play::createScene()));}bool Choose::init(){Size visibleSize = Director::getInstance()->getVisibleSize();Point origin = Director::getInstance()->getVisibleOrigin();Layout* InBg = Layout::create();this->setposition(Point(visibleSize.wIDth / 2,visibleSize.height / 2));this->addChild(InBg);InBg->setBackGroundImage("BG.png");auto white = MenuItemImage::create("WC.png","BWC.png",CC_CALLBACK_1(Choose::whiteenter,this));auto black = MenuItemImage::create("BC.png","BBC.png",CC_CALLBACK_1(Choose::blackenter,this));black->setposition(Point(-200,0));white->setposition(Point(200,0));Menu* pMenu = Menu::create(white,black,0));this->addChild(pMenu,20);return true;}

_____________________________________________________ChessBox.cpp___________________________________________________________________#include "HelloWorldScene.h"extern int playway;extern int Aicolor;ChessBox::ChessBox(){blackBox = NulL;whiteBox = NulL;end = false;for (int i = 0; i < 15; i++){for (int j = 0; j < 15; j++){a[i][j] = 0;}}}ChessBox::~ChessBox(){}voID ChessBox::Bindblack(Sprite* sprite){blackBox = sprite;blackBox->setposition(Point(0,0));this->addChild(sprite);}voID ChessBox::BinDWhite(Sprite* sprite){whiteBox = sprite;whiteBox->setposition(Point(130,0));this->addChild(sprite);}voID ChessBox::Bindback(Sprite* sprite){back = sprite;back->setposition(Point(50,150));this->addChild(sprite);}voID ChessBox::Bindrestart(Sprite* sprite){restart = sprite;restart->setposition(Point(50,300));this->addChild(sprite);}int ChessBox::score(int color) //1 black   2 white{char d[16][7] = { "OOOOO",jj = j;int flag = 1;for (l = 0; l<strlen(d[k]); L++){if ((ii>15) || (jj < 1)){flag = 0;break;}if (((d[k][l] == 'O') && (a[ii][jj] != color)) || ((d[k][l] == '+') && (a[ii][jj] != 0))){flag = 0;break;}ii++; jj--;}if (flag) score += s[k];}return score;}int ChessBox::nextscore(int currentDepth,int depth){int highest = 0;int lowest = 0;int rescore = 0;for (int i = 0; i < 15; i++){for (int j = 0; j < 15; j++){if (a[i][j] == 0){if (currentDepth == depth){//搜索到底层,计算分数a[i][j] = 3 - color;rescore = score(3 - color);a[i][j] = color;rescore += score(color);a[i][j] = 0;}if (currentDepth % 2 == 0){a[i][j] = color;if (currentDepth<depth)rescore = nextscore(currentDepth + 1,depth);//深度优先,进入下一层搜索a[i][j] = 0;if (rescore>= beta) {//发现大于等于当前层的beta值,余下的不需再探索,发生beta剪枝return rescore;}if (rescore >= highest){highest = rescore;if (currentDepth == 1){recordx = i;recordy = j;}}}if (currentDepth % 2 != 0){a[i][j] = color;if (currentDepth < depth)rescore = nextscore(currentDepth + 1,depth);///深度优先,进入下一层搜索a[i][j] = 0;if (currentDepth == 1)rescore = -rescore;if (rescore <= Alpha) {//发现小于等于当前层的Alpha值,余下的不需再探索,发生Alpha剪枝return rescore;}if (rescore <= lowest){lowest = rescore;if (currentDepth == 1){recordx = i;recordy = j;}}}}}}if (currentDepth % 2 == 0){//max层返回最大值给父节点beta = highest;return beta;}else {//min层返回最小值给父节点Alpha = lowest;return Alpha;}}Sprite* ChessBox::ai(){for (int i = 0; i < 10; i++){maxmin[i] = 0;}Alpha = 60000;beta = -60000;recordx = 0;recordy = 0;nextscore(1,pitch,2);if (pitch == 2 && a[recordx][recordy] == 0){Sprite* aichess = Sprite::create("black.png");aichess->setposition(Point(72 + recordx * 30,82 + recordy * 30));aichess->setScale(0.193f);chessstack.push(aichess);a[recordx][recordy] = 1;posstack.push(&(a[recordx][recordy]));return aichess;}if (pitch == 1 && a[recordx][recordy] == 0){Sprite* aichess = Sprite::create("white.png");aichess->setposition(Point(72 + recordx * 30,82 + recordy * 30));aichess->setScale(0.21f);chessstack.push(aichess);a[recordx][recordy] = 2;posstack.push(&(a[recordx][recordy]));return aichess;}}voID ChessBox::Check(){MoveBy* ending = MoveBy::create(0.9f,Point(0,-300));MoveBy* stop = MoveBy::create(0.9f,0));MoveBy* start = MoveBy::create(0.9f,500));if (playway == 1){if (score(aicolor) > 50000){end = true;Sprite* lose = Sprite::create("lose.png");lose->setposition(Point(-300,500));this->addChild(lose,300);auto re = [&](){this->removeChild(lose);};CallFunc* ca = CallFunc::create(re);Action* moves = Sequence::create(ending,stop,start,ca,NulL);lose->runAction(moves);}if (score(3 - aicolor) > 50000){end = true;Sprite* win = Sprite::create("win.png");win->setposition(Point(-300,500));this->addChild(win,300);auto re = [&](){this->removeChild(win);};CallFunc* ca = CallFunc::create(re);Action* moves = Sequence::create(ending,NulL);win->runAction(moves);}}else{if (score(1) > 50000){end = true;Sprite* blackwin = Sprite::create("blackwin.png");blackwin->setposition(Point(-300,500));this->addChild(blackwin,300);auto re = [&](){this->removeChild(blackwin);};CallFunc* ca = CallFunc::create(re);Action* moves = Sequence::create(ending,NulL);blackwin->runAction(moves);}if (score(2) > 50000){end = true;Sprite* whitewin = Sprite::create("whitewin.png");whitewin->setposition(Point(-300,500));this->addChild(whitewin,300);auto re = [&](){this->removeChild(whitewin);};CallFunc* ca = CallFunc::create(re);Action* moves = Sequence::create(ending,NulL);whitewin->runAction(moves);}}} bool ChessBox::init(){return true;}
总结

以上是内存溢出为你收集整理的用cocos2d-x 实现的带ai的五子棋全部内容,希望文章能够帮你解决用cocos2d-x 实现的带ai的五子棋所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1034944.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存