Giter Club home page Giter Club logo

gobangbycpp's Introduction

C++实现五子棋项目


开发时间:2019.07.26
开发者:Summer
开发工具:Visual studio 2019

一、演示效果

二、头文件

三、主函数

四、画出棋盘

五、下棋

六、判断输赢


一、演示效果

Alt


二、头文件

#include<stdio.h>
#include<graphics.h>	//图形库头文件
#include<mmsystem.h>	//播放音乐头文件
#include<math.h>

#pragma comment(lib, "winmm.lib")	//播放音乐库文件

int flag = 0;	//表示下棋次数
int board[20][20] = { 0 };	//0表示棋盘没有棋子状态

void initGame();
int judge(int a, int b);
void playChess();

三、主函数

int main() {

	initGame();
	playChess();

	getchar();
	return 0;
}

四、画出棋盘

//1.画出棋盘
void initGame() {		//初始化游戏

	//1.1 绘图环境 库函数
	//默认调用系统的窗口
	initgraph(600, 500);	//创建自定义窗口
	//setbkcolor(BLUE);	//设置窗口背景颜色
	//cleardevice();	//刷新

	//1.2 贴图
	loadimage(NULL, "./src/bg.jpg");

	//1.3 背景音乐	mci 多媒体控制结口
	mciSendString("open ./src/skyCity.mp3", 0, 0, 0);
	//mciSendString("play ./src/skyCity.mp3", 0, 0, 0);

	//setlinecolor(BLACK);
	//1.4 绘制棋盘
	//画线 20 20 25 25 500 500
	for (int i = 0; i <= 500; i+=25) {
		line(0, i, 500, i);
		line(i, 0, i, 500);
	}
	line(501, 0, 501, 500);

	//1.5 print out
	outtextxy(510, 100, "玩家1:黑棋");
	outtextxy(510, 200, "玩家2:白棋");
}

五、下棋

//2.下棋
void playChess() {
	//鼠标
	MOUSEMSG m;	//保存鼠标消息
	int x=0, y=0;	//坐标
	int a=0, b=0;	//行列

	//持续下棋
	while (1) {
		m = GetMouseMsg();	//获取一个鼠标消息

		//获取离鼠标最近的点的坐标信息
		for (int i = 1; i < 20; i++) {
			for (int j = 1; j < 20; j++) {
				if (abs(m.x - i * 25) < 12 && abs(m.y - j * 25) < 12) {
					x = i * 25;
					y = j * 25;
					a = i;
					b = j;
				}
			}
		}


		if (m.uMsg == WM_LBUTTONDOWN) {
			//棋子重复下在一个位置上
			if (board[a][b] != 0) {
				MessageBox(NULL, "这里已经有棋子了,请重新选择。", "提示", MB_OK);	//弹出一个提示框
				continue;	//进入下一循环
			}

			//判断黑白子
			if (flag % 2 == 0) {	//	偶数次 黑子
				setfillcolor(BLACK);
				solidcircle(x, y, 10);
				board[a][b] = 1;
			}
			else {	//奇数次
				setfillcolor(WHITE);
				solidcircle(x, y, 10);
				board[a][b] = 2;
			}
			flag++;
		}

		if (judge(a, b)) {
			if (flag % 2 == 0) {
				MessageBox(NULL, "玩家2胜利。", "游戏结束", MB_OK);	//弹出一个提示框
				exit(0);
			}
			else {
				MessageBox(NULL, "玩家1胜利。", "游戏结束", MB_OK);	//弹出一个提示框
				exit(0);
			}
		}
	}
}   

六、判断输赢

//3.判断输赢
int judge(int a, int b) {
	int i, j;
	int t = 2 - flag % 2;	//1 判断黑子是否赢	2 判断白子是否赢

	//横向
	for (i = a - 4, j = b; i <= a; i++) {
		if (i > 0 && i < 16 && t == board[i][j] && t == board[i + 1][j] && t == board[i + 2][j] && t == board[i + 3][j] && t == board[i + 4][j]) {
			return 1;
		}
	}

	//纵向
	for (i = a, j = b - 4; j <= b; j++) {
		if (j > 0 && j < 16 && t == board[i][j] && t == board[i][j + 1] && t == board[i][j + 2] && t == board[i][j + 3] && t == board[i][j + 4]) {
			return 1;
		}
	}

	//右下
	for (i = a - 4, j = b - 4; i <= a, j <= b; i++, j++) {
		if (i > 0 && i < 16 && j > 0 && j < 16 && t == board[i][j] && t == board[i + 1][j + 1] && t == board[i + 2][j + 2] && t == board[i + 3][j + 3] && t == board[i + 4][j + 4]) {
			return 1;
		}
	}

	//左下
	for (i = a - 4, j = b + 4; i <= a, j >= b; i++, j--) {
		if (i > 0 && i < 16 && j > 0 && j < 16 && t == board[i][j] && t == board[i + 1][j - 1] && t == board[i + 2][j - 2] && t == board[i + 3][j - 3] && t == board[i + 4][j - 4]) {
			return 1;
		}
	}

	return 0;
}

gobangbycpp's People

Contributors

zz2summer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.