五子棋

1 篇文章 0 订阅

我们使用了easyx,制作简单五子棋,两人对战,代码如下。

//main.cpp中的代码
#include "main.h"
int main()
{
	main_game();
	_getch();
	closegraph();
	return 0;
}
//main.h中的代码

#pragma once
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
void init();
int paint(double x, double y,int flag);
int gameOver(int x,int y);
int isPiece(int x,int y);
int main_game();
//game.cpp中的代码

#include "main.h"
int map[23][24];
void init()
{
	int i, j;
	
	initgraph(640, 480);
	setbkcolor(BLUE);
	cleardevice();
	setlinecolor(BLACK);
	for (i = 20; i <= 440; i += 20)
	{
		line(20, i, 460, i);
	}
	for (j = 20; j <= 460; j += 20)
	{
		line(j, 20, j, 440);
	}
	
}

int paint(double x, double y,int flag)
{
	int i, j;
	int flag2 = 0, flag3 = 0;//判定此次下棋是否成功
	RECT re;
	re.top = 300;
	re.bottom = 400;
	re.left = 450;
	re.right = 700;
	if (flag == 1)
		setfillcolor(BLACK);
	else
		setfillcolor(WHITE);
	for (i = 20; i <= 480; i += 20)
	{
		if (fabs(x - i) < 10)
		{
			flag2 = 1;
			break;
		}
			
	}
	for (j = 20; j <= 480; j += 20)
	{
		if (fabs(y - j) < 10)
		{
			flag3 = 1;
			break;
		}
	}
	if (flag2&&flag3&&isPiece(i,j))
	{
		fillcircle(i, j, 10);
		if (flag == 1)
			map[j/20][i/20] = 1;
		else
			map[j/20][i/20] = 2;
		if (gameOver(i, j))
		{
			drawtext(_T("五秒后游戏自动开启下一局"), &re, DT_BOTTOM);
			Sleep(5000);
			main_game();
		}
		return 0;
	}
	return 1;
}

int gameOver(int x,int y)
{
	RECT re;
	re.top = 150;
	re.bottom = 250;
	re.left = 500;
	re.right = 600;


	if (isPiece(x, y) == 2)
	{
		drawtext(_T("黑棋胜"), &re, DT_BOTTOM);
		return 1;
	}
	if (isPiece(x, y) == 3)
	{
		drawtext(_T("白棋胜"), &re, DT_BOTTOM);
		return 1;
	}
	return 0;
}

int isPiece(int x, int y)
{
	int i, j;
	int m = y / 20;
	int n = x / 20;
	int flag = map[m][n];
	int count_black = 0, count_white = 0;
	if (map[m][n] == 0)
		return 1;
	if (flag == 1)
	{
		for (i = m; i >= 1; i--)
		{
			if (map[i][n] == 1)
				count_black+=1;
			if (map[i][n] == 2||map[i][n]==0)
				break;
		}
		for (i = m + 1; i <= 24; i++)
		{
			if (map[i][n] == 1)
				count_black+=1;
			if (map[i][n] == 2 || map[i][n] == 0)
				break;
		}
		if (count_black == 5)
		{
			count_black = 0;
			return 2;
		}
		else
			count_black = 0;
		for (j = n; j >= 1; j--)
		{
			if (map[m][j] == 1)
				count_black+=1;
			if (map[m][j] == 2 || map[m][j] == 0)
				break;
		}
		for (j = n + 1; j < 24; j++)
		{
			if (map[m][j] == 1)
				count_black+=1;
			if (map[m][j] == 2 || map[m][j] == 0)
				break;
		}
		if (count_black == 5)
		{
			count_black = 0;
			return 2;
		}
		else
			count_black = 0;
		for (i = m, j = n; i >= 1 && j >= 1; i--, j--)
		{
			if (map[i][j] == 1)
				count_black++;
			if (map[i][j] == 2 || map[i][j] == 0)
				break;
		}
		for (i = m + 1, j = n + 1; i <= 24 && j <= 24; i++, j++)
		{
			if (map[i][j] == 1)
				count_black++;
			if (map[i][j] == 2 || map[i][j] == 0)
				break;
		}
		if (count_black == 5)
		{
			count_black = 0;
			return 2;
		}
		else
			count_black = 0;
		for (i = m, j = n; i >= 1 && j <= 24; i--, j++)
		{
			if (map[i][j] == 1)
				count_black++;
			if (map[i][j] == 2 || map[i][j] == 0)
				break;
		}
		for (i = m + 1, j = n + 1; i <= 24 && j >= 1; i++, j--)
		{
			if (map[i][j] == 1)
				count_black++;
			if (map[i][j] == 2 || map[i][j] == 0)
				break;
		}
		if (count_black == 5)
		{
			count_black = 0;
			return 2;
		}
		else
			count_black = 0;
	}
	if (flag == 2)
	{
		for (i = m; i >= 0; i--)
		{
			if (map[i][n] == 2)
				count_white++;
			if (map[i][n] == 1 || map[i][n] == 0)
				break;
		}
		for (i = m + 1; i <= 24; i++)
		{
			if (map[i][n] == 2)
				count_white += 1;
			if (map[i][n] == 1 || map[i][n] == 0)
				break;
		}
		if (count_white == 5)
		{
			count_white = 0;
			return 3;
		}
		else
			count_white = 0;
		for (j = n; j >= 1; j--)
		{
			if (map[m][j] == 2)
				count_white += 1;
			if (map[m][j] == 1 || map[m][j] == 0)
				break;
		}
		for (j = n + 1; j < 24; j++)
		{
			if (map[m][j] == 2)
				count_white += 1;
			if (map[m][j] == 1 || map[m][j] == 0)
				break;
		}
		if (count_white == 5)
		{
			count_white = 0;
			return 3;
		}
		else
			count_white = 0;
		for (i = m, j = n; i >= 1 && j >= 1; i--, j--)
		{
			if (map[i][j] == 2)
				count_white++;
			if (map[i][j] == 1 || map[i][j] == 0)
				break;
		}
		for (i = m + 1, j = n + 1; i <= 24 && j <= 24; i++, j++)
		{
			if (map[i][j] == 2)
				count_white++;
			if (map[i][j] == 1 || map[i][j] == 0)
				break;
		}
		if (count_white == 5)
		{
			count_white = 0;
			return 3;
		}
		else
			count_white = 0;
		for (i = m, j = n; i >= 1 && j <= 24; i--, j++)
		{
			if (map[i][j] == 2)
				count_white++;
			if (map[i][j] == 1 || map[i][j] == 0)
				break;
		}
		for (i = m + 1, j = n + 1; i <= 24 && j >= 1; i++, j--)
		{
			if (map[i][j] == 2)
				count_white++;
			if (map[i][j] == 1 || map[i][j] == 0)
				break;
		}
		if (count_white == 5)
		{
			count_white = 0;
			return 3;
		}
		else
			count_white = 0;
	}
	return 0;
}

int main_game()
{
	int flag = 1;	//记录下黑/白棋
	init();
	memset(map, 0, sizeof(map));
	while (1)
	{

		MOUSEMSG msg;
		msg = GetMouseMsg();
		switch (msg.uMsg)
		{
		case WM_LBUTTONDOWN:
			if (0 == paint(msg.x, msg.y, flag))
				flag = !flag;
			break;
		}

	}
	return 0;
}

结果如右:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值