利用C語(yǔ)言實(shí)現(xiàn)經(jīng)典游戲斗獸棋
效果圖

核心代碼
#include<stdio.h>
#include<easyx.h>
#include<stdlib.h>
#include<time.h>
#define IMAGE_NUM_ANIMAL 8 //動(dòng)物圖片數(shù)量
/* 動(dòng)物類型枚舉 從弱到強(qiáng)*/
enum AnimalType
{
AT_None, //沒(méi)有
AT_Mouse, //老鼠
AT_Cat, //貓
AT_Dog, //狗
AT_Wolf, //狼
AT_Leopard, //豹子
AT_Tiger, //老虎
AT_Lion, //獅子
AT_Elephant, //大象
AT_Max
};
enum BothType
{
BT_Red,
BT_Blue
};
IMAGE img_animals[17];
IMAGE imgs[2]; //背景 前面間距28 水平間距42 上面間距65 垂直間距43
void loadResource()
{
loadimage(img_animals + 0, "./res/Road.png");
loadimage(img_animals + 1, "./res/BMouse.png");
loadimage(img_animals + 2, "./res/BCat.png");
loadimage(img_animals + 3, "./res/BDog.png");
loadimage(img_animals + 4, "./res/BWolf.png");
loadimage(img_animals + 5, "./res/BLeopard.png");
loadimage(img_animals + 6, "./res/BTiger.png");
loadimage(img_animals + 7, "./res/BLion.png");
loadimage(img_animals + 8, "./res/BElephant.png");
loadimage(img_animals + 9, "./res/RMouse.png");
loadimage(img_animals + 10, "./res/RCat.png");
loadimage(img_animals + 11, "./res/RDog.png");
loadimage(img_animals + 12, "./res/RWolf.png");
loadimage(img_animals + 13, "./res/RLeopard.png");
loadimage(img_animals + 14, "./res/RTiger.png");
loadimage(img_animals + 15, "./res/RLion.png");
loadimage(img_animals + 16, "./res/RElephant.png");
loadimage(imgs + 0, "./res/BackGround.png");
loadimage(imgs + 1, "./res/Card.png");
}
IMAGE* getAnimalImage(int type)
{
//如果格子已經(jīng)加密了
if (type > 20)
{
return imgs + 1;
}
if (type >= 0 && type < AT_Max)
{
return img_animals + type;
}
else if (type > 8 && type < 2 * AT_Max - 1)
{
return img_animals + type;
}
return NULL;
}
struct Card
{
int row;
int col;
int animalType; //動(dòng)物類型
//int bothType; //雙方類型 紅 藍(lán)
};
#define CardW() img_animals[0].getwidth()
#define CardH() img_animals[0].getheight()
#define CardX(col) (28 + col * (CardW() + 42)) //28 左邊的間隔 42 橫向中間的間隔
#define CardY(row) (65 + row * (CardH() + 43)) //65 上邊的間隔 43 橫向中間的間隔
void card_init(Card* card, int row, int col, int aniType)
{
card->row = row;
card->col = col;
card->animalType = aniType;
}
void card_draw(Card* card)
{
putimage(CardX(card->col), CardY(card->row), getAnimalImage(card->animalType));
}
//獲取
int card_type(Card* card)
{
if (card->animalType <= 8)
{
return BT_Blue;
}
else if (card->animalType > 8 && card->animalType < AT_Max * 2 - 1)
{
return BT_Red;
}
}
//兩張牌類型是否相同
bool card_sameType(Card* card1, Card* card2)
{
return card_type(card1) == card_type(card2);
}
//消費(fèi)者是否能吃食物
bool card_eat(Card* food, Card* consumer)
{
//先把大于8的,也轉(zhuǎn)成小于八的,方便比較
int ftype = food->animalType;
int ctype = consumer->animalType;
ftype = ftype > 8 ? food->animalType - 8 : ftype;
ctype = ctype > 8 ? consumer->animalType - 8 : ctype;
if (!card_sameType(food, consumer) && //不是同一方
ftype <= ctype || /*大吃小*/ (ftype == AT_Elephant && ctype == AT_Mouse)//老鼠吃大象
)
{
return true;
}
return false;
}
Card cards[4][4];
int curBothType = BT_Blue; //當(dāng)前行棋方
enum State
{
Begin,
End
};
int press = Begin;//第一次點(diǎn)擊還是第二次點(diǎn)擊
struct Index
{
int row;
int col;
}beg = { -1,-1 }, end = { -1,-1 };
void init()
{
srand(time(NULL));
for (int i = 0; i < 4; i++)
{
for (int k = 0; k < 4; k++)
{
card_init(cards[i] + k, i, k, i * 4 + k + 1);
}
}
//打亂數(shù)組
for (int i = 0; i < 4; i++)
{
for (int k = 0; k < 4; k++)
{
int r = rand() % 4;
int c = rand() % 4;
Card t = cards[r][c];
cards[r][c] = cards[i][k];
cards[i][k] = t;
}
}
//加密數(shù)組
for (int i = 0; i < 4; i++)
{
for (int k = 0; k < 4; k++)
{
cards[i][k].animalType += 20;
printf("%d ", cards[i][k].animalType);
}
putchar('\n');
}
}
void draw()
{
//繪制背景
putimage(0, 0, imgs + 0);
for (int i = 0; i < 4; i++)
{
for (int k = 0; k < 4; k++)
{
IMAGE* t = getAnimalImage(cards[i][k].animalType);
putimage(CardX(k), CardY(i), t);
}
}
if (beg.row != -1)
{
setlinestyle(PS_SOLID, 3);
setlinecolor(RED);
int x = CardX(beg.col);
int y = CardY(beg.row);
int w = CardW();
int h = CardH();
rectangle(x, y, x + w, y + h);
}
}
//65 264 28 65 90 108
bool isIn(int x, int y, int left, int top, int w, int h)
{
if (x > left && x < left + w && y > top && y < top + h)
{
return true;
}
return false;
}
void onMouseLbuttonDown(ExMessage* msg)
{
for (int i = 0; i < 4; i++)
{
for (int k = 0; k < 4; k++)
{
//判斷是否在卡牌上點(diǎn)擊
if (isIn(msg->x, msg->y, CardX(k), CardY(i), CardW(), CardH()))
{
//如果沒(méi)有翻開(kāi),則翻開(kāi)
if (cards[i][k].animalType > 20)
{
cards[i][k].animalType -= 20;
}
//如果翻開(kāi)了,可以移動(dòng)或者吃
else
{
//如果點(diǎn)擊的是同一方的牌,直接改變begin
//if (card_type(&cards[i][k]) == curBothType)
//{
// press = Begin;
//}
//點(diǎn)擊不同的牌在move函數(shù)里進(jìn)行判斷
//移動(dòng)或者吃,需要知道兩次點(diǎn)擊的牌
if (press == Begin)
{
beg = { i,k };
press = End;
printf("begin(%d %d) %d\n", i, k, curBothType);
}
else if (press == End)
{
end = { i ,k };
press = Begin;
printf("end(%d %d) %d\n", i, k, curBothType);
}
}
}
}
}
}
void move()
{
//beg end不為-1 并且不是同一個(gè)位置
if ((beg.row != -1 && end.row != -1) && !(beg.row == end.row && beg.col == end.col))
{
//在同一列或者同一行
if ((abs(beg.row - end.row) == 1 && beg.col == end.col) || (abs(beg.col - end.col) == 1 && beg.row == end.row))
{
//吃
bool ok = card_eat(&cards[end.row][end.col], &cards[beg.row][beg.col]);
//移動(dòng)
if (ok || cards[end.row][end.col].animalType == AT_None)
{
cards[end.row][end.col] = cards[beg.row][beg.col];
cards[beg.row][beg.col].animalType = AT_None;
beg = end = { -1,-1 };
//切換棋手
curBothType = (curBothType + 1) % 2;
}
}
}
}
int main()
{
initgraph(540, 677/*, EW_SHOWCONSOLE*/);
loadResource();
init();
while (true)
{
BeginBatchDraw();
draw();
EndBatchDraw();
ExMessage msg;
while (peekmessage(&msg, EM_MOUSE))
{
if (msg.message == WM_LBUTTONDOWN)
{
onMouseLbuttonDown(&msg);
}
}
move();
}
getchar();
return 0;
}以上就是利用C語(yǔ)言實(shí)現(xiàn)經(jīng)典游戲斗獸棋的詳細(xì)內(nèi)容,更多關(guān)于C語(yǔ)言斗獸棋的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++面向?qū)ο笾鄳B(tài)的實(shí)現(xiàn)和應(yīng)用詳解
相信大家都知道面向?qū)ο蟮娜筇匦允欠庋b,繼承和多態(tài),下面這篇文章主要給大家介紹了關(guān)于C++面向?qū)ο笾鄳B(tài)的實(shí)現(xiàn)和應(yīng)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09
C語(yǔ)言中交換int型變量的值及轉(zhuǎn)換為字符數(shù)組的方法
這篇文章主要介紹了C語(yǔ)言中交換int型變量的值及轉(zhuǎn)換為字符數(shù)組的方法,講解了以不同進(jìn)制將整型數(shù)字轉(zhuǎn)換成字符數(shù)組,需要的朋友可以參考下2016-04-04
C語(yǔ)言二叉樹(shù)常見(jiàn)操作詳解【前序,中序,后序,層次遍歷及非遞歸查找,統(tǒng)計(jì)個(gè)數(shù),比較,求深度】
這篇文章主要介紹了C語(yǔ)言二叉樹(shù)常見(jiàn)操作,結(jié)合實(shí)例形式詳細(xì)分析了基于C語(yǔ)言的二叉樹(shù)前序,中序,后序,層次遍歷及非遞歸查找,統(tǒng)計(jì)個(gè)數(shù),比較,求深度等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-04-04
C++封裝遠(yuǎn)程注入類CreateRemoteThreadEx實(shí)例
這篇文章主要介紹了C++封裝遠(yuǎn)程注入類CreateRemoteThreadEx實(shí)例,詳細(xì)講述了注入DLL到指定的地址空間以及從指定的地址空間卸載DLL的方法,需要的朋友可以參考下2014-10-10
全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
本篇文章是對(duì)全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語(yǔ)言實(shí)現(xiàn)鏈隊(duì)列代碼
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)鏈隊(duì)列代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
C++實(shí)現(xiàn)LeetCode(93.復(fù)原IP地址)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(93.復(fù)原IP地址),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

