当前位置:主页 > 查看内容

利用vs 2019和easyx图形库完成五子棋小游戏

发布时间:2021-07-19 00:00| 位朋友查看

简介:利用vs 2019和easyx图形库完成五子棋小游戏 需要的工具 win 10 vs 2019 easyx C 源码 : # include iostream # include graphics.h using namespace std ; int map [ 16 ] [ 16 ] ; int num 1 ; int flag 256 ; void Initgame ( ) { IMAGE image ; loadimage……

利用vs 2019和easyx图形库完成五子棋小游戏

需要的工具:

  1. win 10
  2. vs 2019
  3. easyx
C++源码:
#include<iostream>
#include<graphics.h>
using namespace std;

int map[16][16];
int num = 1; int flag = 256;
void Initgame()
{
	IMAGE image;
	loadimage(&image, "back.jpg", 750, 750);
	putimage(0, 0, &image);
}
void Initmap()
{
	for (size_t i = 0; i < 16; i++)
	{
		for (size_t j = 0; j < 16; j++)
		{
			map[i][j] = 0;
		}
	}
}
bool JudgeCOL()
{
	for (size_t j = 0; j < 15; j++)
	{
		for (size_t i = 0; i < 11; i++)
		{
			if (map[i][j] == 1 &&
				map[i + 1][j] == 1 &&
				map[i + 2][j] == 1 &&
				map[i + 3][j] == 1 &&
				map[i + 4][j] == 1)
			{
				return true;
			}
			if (map[i][j] == 2 &&
				map[i + 1][j] == 2 &&
				map[i + 2][j] == 2 &&
				map[i + 3][j] == 2 &&
				map[i + 4][j] == 2)
			{
				return true;
			}
		}
	}
	return false;
}
bool JudgeROW()
{
	for (size_t i = 0; i < 15; i++)
	{
		for (size_t j = 0; j < 10; j++)
		{
			if (map[i][j] == 1 &&
				map[i][j + 1] == 1 &&
				map[i][j + 2] == 1 &&
				map[i][j + 3] == 1 &&
				map[i][j + 4] == 1)
			{
				return true;
			}
			else if (map[i][j] == 2 &&
				map[i][j + 1] == 2 &&
				map[i][j + 2] == 2 &&
				map[i][j + 3] == 2 &&
				map[i][j + 4] == 2)
			{
				return true;
			}
		}
	}
	return false;
}
bool MouseEvent()
{

	MOUSEMSG msg = GetMouseMsg();

	if (msg.uMsg == WM_LBUTTONDOWN) {

		if (num % 2 == 0)
		{
			setfillcolor(WHITE);

			if (msg.x % 50 <= 10 && msg.y % 50 <= 10)
			{
				if (map[msg.y / 50][msg.x / 50] == 1 || map[msg.y / 50][msg.x / 50] == 2)
				{
					return false;
				}
				map[msg.y / 50][msg.x / 50] = 1;
				num++;
				fillcircle(msg.x - msg.x % 50, msg.y - msg.y % 50, 20);
				return true;
			}
			if (msg.x % 50 <= 10 && msg.y % 50 >= 40)
			{
				if (map[msg.y / 50 + 1][msg.x / 50] == 1 || map[msg.y / 50 + 1][msg.x / 50] == 2)
				{
					return false;
				}
				map[msg.y / 50 + 1][msg.x / 50] = 1;
				num++;
				fillcircle(msg.x - msg.x % 50, (msg.y / 50 + 1) * 50, 20);
				return true;
			}
			if (msg.x % 50 >= 40 && msg.y % 50 <= 10)
			{
				if (map[msg.y / 50][msg.x / 50 + 1] == 1 || map[msg.y / 50][msg.x / 50 + 1] == 2)
				{
					return false;
				}
				map[msg.y / 50][msg.x / 50 + 1] = 1;
				num++;
				fillcircle(50 * (msg.x / 50 + 1), msg.y - msg.y % 50, 20);
				return true;
			}
			if (msg.x % 50 >= 40 && msg.y % 50 >= 40)
			{
				if (map[msg.y / 50 + 1][msg.x / 50 + 1] == 1 || map[msg.y / 50 + 1][msg.x / 50 + 1] == 2)
				{
					return false;
				}
				map[msg.y / 50 + 1][msg.x / 50 + 1] = 1;
				num++;
				fillcircle(50 * (msg.x / 50 + 1), 50 * (msg.y / 50 + 1), 20);
				return true;
			}
		}
		else
		{
			setfillcolor(BLACK);

			if (msg.x % 50 <= 10 && msg.y % 50 <= 10)
			{
				if (map[msg.y / 50][msg.x / 50] == 1 || map[msg.y / 50][msg.x / 50] == 2)
				{
					return false;
				}
				map[msg.y / 50][msg.x / 50] = 2;
				num++;
				fillcircle(msg.x - msg.x % 50, msg.y - msg.y % 50, 20);
				return true;
			}
			if (msg.x % 50 <= 10 && msg.y % 50 >= 40)
			{
				if (map[msg.y / 50 + 1][msg.x / 50] == 1 || map[msg.y / 50 + 1][msg.x / 50] == 2)
				{
					return false;
				}
				map[msg.y / 50 + 1][msg.x / 50] = 2;
				num++;
				fillcircle(msg.x - msg.x % 50, (msg.y / 50 + 1) * 50, 20);
				return true;
			}
			if (msg.x % 50 >= 40 && msg.y % 50 <= 10)
			{
				if (map[msg.y / 50][msg.x / 50 + 1] == 1 || map[msg.y / 50][msg.x / 50 + 1] == 2)
				{
					return false;
				}
				map[msg.y / 50][msg.x / 50 + 1] = 2;
				num++;
				fillcircle(50 * (msg.x / 50 + 1), msg.y - msg.y % 50, 20);
				return true;
			}
			if (msg.x % 50 >= 40 && msg.y % 50 >= 40)
			{
				if (map[msg.y / 50 + 1][msg.x / 50 + 1] == 1 || map[msg.y / 50 + 1][msg.x / 50 + 1] == 2)
				{
					return false;
				}
				map[msg.y / 50 + 1][msg.x / 50 + 1] = 2;
				num++;
				fillcircle(50 * (msg.x / 50 + 1), 50 * (msg.y / 50 + 1), 20);
				return true;
			}
		}
	}
	return false;
}
bool JudgeSkew()
{
	for (size_t i = 0; i < 15; i++)
	{
		for (size_t j = 0; j < 15; j++)
		{
			if (i <= 11 &&
				j <= 11 &&
				map[i][j] == 1 &&
				map[i + 1][j + 1] == 1 &&
				map[i + 2][j + 2] == 1 &&
				map[i + 3][j + 3] == 1 &&
				map[i + 4][j + 4] == 1)
			{
				return true;
			}
			else if (
				i <= 11 &&
				j <= 11 &&
				map[i][j] == 2 &&
				map[i + 1][j + 1] == 2 &&
				map[i + 2][j + 2] == 2 &&
				map[i + 3][j + 3] == 2 &&
				map[i + 4][j + 4] == 2)
			{
				return true;
			}
			else if (i <= 11 &&
				j >= 4 &&
				map[i][j] == 2 &&
				map[i + 1][j - 1] == 2 &&
				map[i + 2][j - 2] == 2 &&
				map[i + 3][j - 3] == 2 &&
				map[i + 4][j - 4] == 2)
			{
				return true;
			}
			else if (i <= 11 &&
				j >= 4 &&
				map[i][j] == 1 &&
				map[i + 1][j - 1] == 1 &&
				map[i + 2][j - 2] == 1 &&
				map[i + 3][j - 3] == 1 &&
				map[i + 4][j - 4] == 1)
			{
				return true;
			}
		}
	}
	return false;
}
bool Judgemap()
{
	if (0 == flag)
	{
		HWND hwnd = GetHWnd();
		MessageBox(hwnd, "No winner Appear", "Over", MB_OK);
		cout << "流局" << endl;
		return true;
	}
	return false;
}
int Game()
{
	while (1)
	{
		if (true == MouseEvent())
		{
			flag -= 1;
		}
		if (true == Judgemap())
		{
			return 0;
		}
		if (JudgeCOL() || JudgeROW() || JudgeSkew())
		{
			cout << "游戏结束" << endl;
			if (num % 2 == 0)
			{
				HWND Hwnd = GetHWnd();
				MessageBoxA(Hwnd, "Black Has Won", "Over", MB_OK);
				cout << "黑棋胜利" << endl;
			}
			else
			{
				HWND Hwnd = GetHWnd();
				MessageBoxA(Hwnd, "White Has Won", "Over", MB_OK);
				cout << "白棋胜利" << endl;
			}
			return 0;
		}
	}
}

int main()
{
	initgraph(750, 750, 0);
	Initmap();
	Initgame();	Game();

	system("pause");
	return 0;
}
;原文链接:https://blog.csdn.net/qq_47750142/article/details/115773990
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐