Easyx-----c语言实现斗兽棋

#include<stdio.h>
#include<easyx.h>
#include<stdlib.h>
#include<time.h>

#define IMAGE_NUM_ANIMAL 8	//动物图片数量
/* 动物类型枚举 从弱到强*/
enum AnimalType
{
	AT_None,		//没有
	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)
{
	//如果格子已经加密了
	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;		//动物类型
	//int bothType;		//双方类型 红 蓝
};
#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);
}
//消费者是否能吃食物
bool card_eat(Card* food, Card* consumer)
{
	//先把大于8的,也转成小于八的,方便比较
	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;	//当前行棋方
enum State
{
	Begin,
	End
};
int press = Begin;//第一次点击还是第二次点击
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);
		}
	}
	//打乱数组
	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;

		}

	}
	//加密数组
	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++)
		{
			//判断是否在卡牌上点击
			if (isIn(msg->x, msg->y, CardX(k), CardY(i), CardW(), CardH()))
			{
				//如果没有翻开,则翻开
				if (cards[i][k].animalType > 20)
				{
					cards[i][k].animalType -= 20;
				}
				//如果翻开了,可以移动或者吃
				else
				{
					//如果点击的是同一方的牌,直接改变begin
					//if (card_type(&cards[i][k]) == curBothType)
					//{
					//	press = Begin;
					//}
					//点击不同的牌在move函数里进行判断

					//移动或者吃,需要知道两次点击的牌
					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 并且不是同一个位置
	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]);
			//移动
			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;
}
  • 9
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qiuqiuyaq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值