欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于EazyX实现五子棋小游戏(人人对战)

程序员文章站 2022-04-07 15:49:12
...

基于EazyX实现五子棋小游戏(人人对战)
写完了才发现棋子应该落在交点处,但是懒得改了,有兴趣的话修改一下相关坐标即可,还有就是代码可能有些臃肿,对于代码重用做的不是很好,可以优化一下

#include <cstdio>
#include <conio.h>
#include <iostream>
#include <graphics.h>
#define Graph_Size 750  //定义窗口大小
using namespace std;
const int Size = 15;  //棋盘为15*15
const int Average = Graph_Size / Size;  //每一个格子的大小
int vis[Size + 5][Size + 5];  //记录棋盘每个格子里面是否有棋子
inline void DrawMap() {  //背景初始化
	initgraph(Graph_Size, Graph_Size);  //初始化750*750窗口
	setbkcolor(YELLOW);  //背景颜色为黄色
	cleardevice(); //清空绘图设备
	setcolor(BLACK);  //设置当前绘图前景色
	setfillcolor(BLACK);  //设置当前设备填充颜色为黑色
	settextstyle(Average, Average, _T("宋体"));  //设置字体大小和字体样式
	for (int i = Average; i < Graph_Size; i += Average) {  //画横线
		line(i, 0, i, Graph_Size);
	}
	for (int i = Average; i < Graph_Size; i += Average) {  //画纵线
		line(0, i, Graph_Size, i);
	}
}
inline int judge() {  //判断游戏是否结束
	for (int i = 0; i < Size; ++i) {  //是否有横着的5个相同的棋子
		int j = 0;
		while (j < Size) {
			int Victory_Black = 5;
			int Victory_White = 5;
			while (j < Size && !vis[i][j]) ++j;
			while (j < Size && vis[i][j] == 1) {
				--Victory_Black;
				++j;
			}
			if (Victory_Black <= 0) return 1;
			while (j < Size && vis[i][j] == 2) {
				--Victory_White;
				++j;
			}
			if (Victory_White <= 0) return 2;
		}
	}
	for (int i = 0; i < Size; ++i) { //是否有纵着的5个相同的棋子
		int j = 0;
		while (j < Size) {
			int Victory_Black = 5;
			int Victory_White = 5;
			while (j < Size && !vis[j][i]) ++j;
			while (j < Size && vis[j][i] == 1) {
				--Victory_Black;
				++j;
			}
			if (Victory_Black <= 0) return 1;
			while (j < Size && vis[j][i] == 2) {
				--Victory_White;
				++j;
			}
			if (Victory_White <= 0) return 2;
		}
	}
	for (int i = 0; i < Size; ++i) { //是否有左右对角线的5个相同的棋子
		for (int j = Size - 1; j >= 0; --j) {
			int k = 0;
			while (i + k < Size && j + k < Size) {
				int Victory_Black = 5;
				int Victory_White = 5;
				while (i + k < Size && j + k < Size && !vis[i + k][j + k]) ++k;
				while (i + k < Size && j + k < Size && vis[i + k][j + k] == 1) {
					--Victory_Black;
					++k;
				}
				if (Victory_Black <= 0) return 1;
				while (i + k < Size && j + k < Size && vis[i + k][j + k] == 2) {
					--Victory_White;
					++k;
				}
				if (Victory_White <= 0) return 2;
			}
		}
	}
	for (int i = 0; i < Size; ++i) { //是否有右左对角线的5个相同的棋子
		for (int j = 0; j < Size; ++j) {
			int k = 0;
			while (i + k < Size && j - k >= 0) {
				int Victory_Black = 5;
				int Victory_White = 5;
				while (i + k < Size && j - k >= 0 && !vis[i + k][j - k]) ++k;
				while (i + k < Size && j - k >= 0 && vis[i + k][j - k] == 1) {
					--Victory_Black;
					++k;
				}
				if (Victory_Black <= 0) return 1;
				while (i + k < Size && j - k >= 0 && vis[i + k][j - k] == 2) {
					--Victory_White;
					++k;
				}
				if (Victory_White <= 0) return 2;
			}
		}
	}
	return 0;
}
int main() {
	DrawMap();  //画背景
	bool Black_Or_White = false; //false表示黑棋下,true表示白棋下
	while (true) {
		MOUSEMSG msg;
		msg = GetMouseMsg();
		if (msg.uMsg == WM_LBUTTONDOWN) {  //根据鼠标落下的坐标画黑白棋子
			if (vis[msg.x / Average][msg.y / Average]) continue;
			if (!Black_Or_White) {
				setfillcolor(BLACK);
				fillcircle((msg.x / Average) * Average + Average / 2, (msg.y / Average) * Average + Average / 2, Average / 2);
				IMAGE img((msg.x / Average) * Average + Average / 2, (msg.y / Average) * Average + Average / 2);
				SetWorkingImage(&img);
				setfillstyle(BS_PATTERN, NULL, &img);
				SetWorkingImage(NULL);
				Black_Or_White = true;
				vis[msg.x / Average][msg.y / Average] = 1;
			}
			else {
				setfillcolor(WHITE);
				fillcircle((msg.x / Average) * Average + Average / 2, (msg.y / Average) * Average + Average / 2, Average / 2);
				IMAGE img((msg.x / Average) * Average + Average / 2, (msg.y / Average) * Average + Average / 2);
				SetWorkingImage(&img);
				setfillstyle(BS_PATTERN, NULL, &img);
				SetWorkingImage(NULL);
				Black_Or_White = false;
				vis[msg.x / Average][msg.y / Average] = 2;
			}
			int If_Over = judge();  //0表示游戏继续,1表示黑棋胜,2表示白棋胜利
			if (!If_Over) continue;
			outtextxy(Average * 2, Average * 7, If_Over == 1 ? _T("Black  Win!") : _T("White  Win!"));
			break;
		}
	}
	_getch();  //按任意键退出
	closegraph();  //关闭窗口
	return 0;
}
相关标签: 小游戏