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

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

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

  • TYPE _type
  • Texture2D *texture

在上一篇我們已經(jīng)能夠在棋盤對(duì)應(yīng)的位置添加棋子,那么這一篇我們則需要在對(duì)應(yīng)的位置添加對(duì)應(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是直接從原來(lái)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通過(guò)id創(chuàng)建32個(gè)棋子并掛在渲染樹上即可。
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;

/*
布局棋子時(shí)因?yàn)槠灞P左側(cè)及下側(cè)都空出一段,
所以在布局時(shí),每個(gè)棋子都需要加上左側(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;//棋子的列號(hào)
    int _row;//棋子的行號(hào)
    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ù)用于獲取棋子相對(duì)于棋盤的位置
};

#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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容