cocos2d-x3.14中國象棋AI(三)擺棋2

擺棋看起來可能會有點(diǎn)煩蒿秦,但是棋子亂擺的話后面會很難管理,所以還是要沉下心把棋子按一定規(guī)則擺好蛋济。

為了擺棋更方便棍鳖,我們需要再給Stone類添加多兩個棋子類型成員變量

  • TYPE _type
  • Texture2D *texture

在上一篇我們已經(jīng)能夠在棋盤對應(yīng)的位置添加棋子,那么這一篇我們則需要在對應(yīng)的位置添加對應(yīng)的棋子瘫俊。
在這里我們繼續(xù)在initStone寫擺棋的規(guī)則。
initStone函數(shù):

void Stone::initStone(int id)
{
    struct
    {
        TYPE type;
        int row;
        int col;
    }proper[9] = {
        {CHE,0,0},
        {MA,0,1},
        {XIANG,0,2},
        {SHI,0,3},
        {BING,3,2},
        {BING,3,0},
        {PAO,2,1},

        {JIANG,0,4},
        {BING,3,4}
    };
    _red = id < 16;
    if (id <= 8)
    {
        this->_id = id;
        this->_type = proper[id].type;
        this->_col = proper[id].col;
        this->_row = proper[id].row;
    }
    else if (id > 8 && id < 16)
    {
        this->_id = id;
        this->_type = proper[id - 9].type;
        this->_col = 8 - proper[id - 9].col;
        this->_row = proper[id - 9].row;
    }
    else if (id >= 16 && id <= 24)
    {
        this->_id = id;
        this->_type = proper[id - 16].type;
        this->_col = 8 - proper[id - 16].col;
        this->_row = 9 - proper[id - 16].row;
    }
    else if (id > 24)
    {
        this->_id = id;
        this->_type = proper[id - 25].type;
        this->_col = proper[id - 25].col;
        this->_row = 9 - proper[id - 25].row;
    }
    //按棋子類型選擇棋子紋理
    switch (this->_type)
    {
        case CHE:
            if (_red)
                this->setStoneTexture("Stone/rche.png");
            else
                this->setStoneTexture("Stone/bche.png");
            break;
        case MA:
            if (_red)
                this->setStoneTexture("Stone/rma.png");
            else
                this->setStoneTexture("Stone/bma.png");
            break;
        case XIANG:
            if (_red)
                this->setStoneTexture("Stone/rxiang.png");
            else
                this->setStoneTexture("Stone/bxiang.png");
            break;
        case SHI:
            if (_red)
                this->setStoneTexture("Stone/rshi.png");
            else
                this->setStoneTexture("Stone/bshi.png");
            break;
        case PAO:
            if (_red)
                this->setStoneTexture("Stone/rpao.png");
            else
                this->setStoneTexture("Stone/bpao.png");
            break;
        case BING:
            if (_red)
                this->setStoneTexture("Stone/rbing.png");
            else
                this->setStoneTexture("Stone/bzu.png");
            break;
        case JIANG:
            if (_red)
                this->setStoneTexture("Stone/rshuai.png");
            else
                this->setStoneTexture("Stone/bjiang.png");
            break;
        default:
            break;
    }
}

而設(shè)置紋理函數(shù)setStoneTexture是直接從原來init函數(shù)中抽離處理悴灵,以文件路徑字符串作為傳參扛芽,封裝成函數(shù)。
setStoneTexture函數(shù):

void Stone::setStoneTexture(const char* filename)
{
    texture = Director::getInstance()->getTextureCache()->addImage(filename);
    this->setTexture(texture);
    this->setTextureRect(Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height));
}

至此积瞒,Stone類算是寫好了川尖,我們只需要在LayerGameMain類的addStones通過id創(chuàng)建32個棋子并掛在渲染樹上即可。
LayerGameMain的addStones函數(shù):

void LayerGameMain::addStones()
{
    int i = 0;
    Stone *stone;
    for (i = 0; i < 32; i++)
    {
        stone = Stone::create(i);
        this->addChild(stone);
    }
}

最后貼出Stone類的代碼:
Stone.h

#ifndef __STONE_H__
#define __STONE_H__

#include "cocos2d.h"
USING_NS_CC;

/*
布局棋子時因為棋盤左側(cè)及下側(cè)都空出一段茫孔,
所以在布局時叮喳,每個棋子都需要加上左側(cè)空白段_offx及下側(cè)空白段_offy。
*/

class Stone : public Sprite
{
public:
    static int _d;//棋子直徑
    static int _offx;//棋子左側(cè)空白段
    static int _offy;//棋子下側(cè)空白段

    int _id;//棋子id
    int _col;//棋子的列號
    int _row;//棋子的行號
    bool _red;//標(biāo)記當(dāng)前棋子顏色
    Texture2D *texture;

    //枚舉出所有棋子類型
    enum TYPE{
        CHE,MA,XIANG,SHI,JIANG,PAO,BING
    };
    TYPE _type;

    static Stone* create(int id);
    virtual bool init(int id);
    void initStone(int id);//該函數(shù)用于初始化棋子成員變量
    void setStoneTexture(const char *filename);
    
    Point getPositionFromPlate();//該函數(shù)用于獲取棋子相對于棋盤的位置
};

#endif

Stone.cpp

#include "Stone.h"

int Stone::_d = 32;
int Stone::_offx = 32;
int Stone::_offy = 16;

bool Stone::init(int id)
{
    if (!Sprite::init())
    {
        return false;
    }
    //設(shè)置紋理圖片缰贝,此處與2.x版本有點(diǎn)區(qū)別,但是區(qū)別不大

    initStone(id);
    setPosition(getPositionFromPlate());

    return true;
}

Stone* Stone::create(int id)
{
    Stone *ret = new Stone();
    if (ret && ret->init(id))
    {
        ret->autorelease();
    }
    else
    {
        delete ret;
        ret = nullptr;
    }
    return ret;
}

void Stone::initStone(int id)
{
    struct
    {
        TYPE type;
        int row;
        int col;
    }proper[9] = {
        {CHE,0,0},
        {MA,0,1},
        {XIANG,0,2},
        {SHI,0,3},
        {BING,3,2},
        {BING,3,0},
        {PAO,2,1},

        {JIANG,0,4},
        {BING,3,4}
    };
    _red = id < 16;
    if (id <= 8)
    {
        this->_id = id;
        this->_type = proper[id].type;
        this->_col = proper[id].col;
        this->_row = proper[id].row;
    }
    else if (id > 8 && id < 16)
    {
        this->_id = id;
        this->_type = proper[id - 9].type;
        this->_col = 8 - proper[id - 9].col;
        this->_row = proper[id - 9].row;
    }
    else if (id >= 16 && id <= 24)
    {
        this->_id = id;
        this->_type = proper[id - 16].type;
        this->_col = 8 - proper[id - 16].col;
        this->_row = 9 - proper[id - 16].row;
    }
    else if (id > 24)
    {
        this->_id = id;
        this->_type = proper[id - 25].type;
        this->_col = proper[id - 25].col;
        this->_row = 9 - proper[id - 25].row;
    }
    //按棋子類型選擇棋子紋理
    switch (this->_type)
    {
        case CHE:
            if (_red)
                this->setStoneTexture("Stone/rche.png");
            else
                this->setStoneTexture("Stone/bche.png");
            break;
        case MA:
            if (_red)
                this->setStoneTexture("Stone/rma.png");
            else
                this->setStoneTexture("Stone/bma.png");
            break;
        case XIANG:
            if (_red)
                this->setStoneTexture("Stone/rxiang.png");
            else
                this->setStoneTexture("Stone/bxiang.png");
            break;
        case SHI:
            if (_red)
                this->setStoneTexture("Stone/rshi.png");
            else
                this->setStoneTexture("Stone/bshi.png");
            break;
        case PAO:
            if (_red)
                this->setStoneTexture("Stone/rpao.png");
            else
                this->setStoneTexture("Stone/bpao.png");
            break;
        case BING:
            if (_red)
                this->setStoneTexture("Stone/rbing.png");
            else
                this->setStoneTexture("Stone/bzu.png");
            break;
        case JIANG:
            if (_red)
                this->setStoneTexture("Stone/rshuai.png");
            else
                this->setStoneTexture("Stone/bjiang.png");
            break;
        default:
            break;
    }
}

Point Stone::getPositionFromPlate()
{
    Point ret = Point(Stone::_offx + this->_col*_d, Stone::_offy + this->_row*_d);

    return ret;
}

void Stone::setStoneTexture(const char* filename)
{
    texture = Director::getInstance()->getTextureCache()->addImage(filename);
    this->setTexture(texture);
    this->setTextureRect(Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height));
}

運(yùn)行效果:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末馍悟,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子剩晴,更是在濱河造成了極大的恐慌锣咒,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件赞弥,死亡現(xiàn)場離奇詭異毅整,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)绽左,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進(jìn)店門悼嫉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拼窥,你說我怎么就攤上這事戏蔑√D” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵辛臊,是天一觀的道長仙粱。 經(jīng)常有香客問我,道長彻舰,這世上最難降的妖魔是什么伐割? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮刃唤,結(jié)果婚禮上隔心,老公的妹妹穿的比我還像新娘。我一直安慰自己尚胞,他們只是感情好硬霍,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著笼裳,像睡著了一般唯卖。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上躬柬,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天拜轨,我揣著相機(jī)與錄音,去河邊找鬼允青。 笑死橄碾,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的颠锉。 我是一名探鬼主播法牲,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼琼掠!你這毒婦竟也來了拒垃?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤瓷蛙,失蹤者是張志新(化名)和其女友劉穎恶复,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體速挑,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡谤牡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了姥宝。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片翅萤。...
    茶點(diǎn)故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出套么,到底是詐尸還是另有隱情培己,我是刑警寧澤,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布胚泌,位于F島的核電站省咨,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏玷室。R本人自食惡果不足惜零蓉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望穷缤。 院中可真熱鬧敌蜂,春花似錦、人聲如沸津肛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽身坐。三九已至秸脱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間部蛇,已是汗流浹背摊唇。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留搪花,地道東北人遏片。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓嘹害,卻偏偏與公主長得像撮竿,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子笔呀,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評論 2 354

推薦閱讀更多精彩內(nèi)容