基于EasyX 的五子棋小游戏。

Posted bosaidongmomo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于EasyX 的五子棋小游戏。相关的知识,希望对你有一定的参考价值。

EasyX下载地址

EasyX下载地址

// 五子棋.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include<windows.h>
#include <time.h>
#include <string>
#pragma warning(disable:4996)
enum ChessPieceColor 
	white = 1,
	blue = 2,
	/* any your want color here */
;
int x = 25, y = 25;  //初始坐标
int board[21][21] =  0 ;		//棋盘占位数组
void InitGame()  //棋盘初始化

	initgraph(600, 500);
	setlinecolor(RED);
	for (int i = 0; i < 20; i++)
	
		line(0, i * 25, 500, i * 25);
		line(i * 25, 0, i * 25, 500);
	
	setlinestyle(PS_SOLID, 2);  //画实线 宽度2
	line(500, 0, 500, 500);
	outtextxy(512, 60, _T("玩家1:白棋"));
	outtextxy(512, 180, _T("W,A,S,D移动"));
	outtextxy(512, 210, _T("G键下棋子"));
	outtextxy(512, 120, _T("玩家2:蓝棋"));
	setfillcolor(WHITE);  //棋盘白字初始位置
	board[1][1] = 1;
	solidcircle(x, y, 12);







int flag = 1;   //标记下棋状态
char key;		//检测键盘按键


void InitMove()     //棋子移动初步判断部分

	switch (key) 
	case 'w': //上 
		y -= 25;
		break;
	case 's': //下 
		y += 25;
		break;
	case 'a'://左 
		x += 25;
		break;

	case 'd': //右 
		x -= 25;
		break;
	default:
		break;
	
	if (x < 0)x = 500;
	if (y > 500)y = 0;
	if (x > 500)x = 0;
	if (y < 0)y = 500;




//判断获胜条件
int judge(int a, int b)

	int i, j;
	int t = 2 - flag % 2;
	for ((i = a - 4) && (j = b); i <= a; i++)  //横向
	
		if (i >= 1 && 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 <= a; j++)   //竖向
	
		if (j >= 1 && 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 >= 1 && i < 16 && j >= 1 && 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 >= 1 && i < 16 && j >= 1 && 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;
	

// 棋子的英文怎么拼 baby
void DrawChessPieces(short x, short y, ChessPieceColor color) 
	switch (color) 
	case ChessPieceColor::blue: 
		setfillcolor(BLUE);
		break;
	
	case ChessPieceColor::white: 
		setfillcolor(WHITE);
		break;
	

	
	solidcircle(x, y, 12);



//void Move()   //棋子移动,以及该位置是否有棋子检测,并按G下棋
//
//	setfillcolor(BLACK);		//消除上一步光标
//	solidcircle(x, y, 12);
//	key = getchar();
//	InitMove();
//	while (board[x / 25][y / 25] = 1)InitMove();//如果已经有棋子则跳转到下一个位置
//	if (flag % 2 == 1)  setfillcolor(WHITE); ;//根据flag数判断棋子颜色
//	if (flag % 2 == 0)  setfillcolor(BLUE); ;
//	solidcircle(x, y, 12);
//	if (getchar() == 'g' || 'G')
//		
//		board[x / 25][y / 25] = 1;
//		if (judge(x / 25, y / 25))         //每下完一次之后进行胜负判断
//		
//			if (1 == flag % 2)
//			
//				outtextxy(512, 400, _T("玩家2胜利"));
//		
//				return; 
//			
//			else
//			
//				outtextxy(512, 60, _T("玩家胜利"));
//				return;
//			
//		
//		//如果没有结束,跳转到下一个棋子的位置
//		InitMove();
//		while (board[x / 25][y / 25] = 1)InitMove();
//	
//
//	
//	
//
//	flag++;
//	if (flag % 2 == 1)  setfillcolor(WHITE); ;//根据flag数判断棋子颜色
//	if (flag % 2 == 0)  setfillcolor(BLUE); ;
//	solidcircle(x, y, 12);
//
//
MOUSEMSG m; int k;
bool judgeEdge(int x, int y) 
	if (x >= 1 && x < 19 && y >= 1 && y < 19) 
		return true;
	
	else 
		return false;
	


bool judgeWin(int x,int y, ChessPieceColor color) 
	int judgeX = x;
	int judgeY = y;
	int ChessPieces = 1;
	bool flag = false;
	 
		 
			 int judgeX = x;
			 int judgeY = y;
			 // 横向 向左
			 judgeX--;
			 while (board[judgeX][judgeY] == color) 
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) 
					 judgeX--;
				 
				 // 不满足直接退出
				 else 
					 break;
				 
			 
			 // 横向 向右
			 judgeX = x + 1;
			 while (board[judgeX][judgeY] == color) 
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) 
					 judgeX++;
				 
				 // 不满足直接退出
				 else 
					 break;
				 
			 
			 if (ChessPieces >= 5) 
				 flag = true;
			 
			 else 
				 ChessPieces = 1;
			 
			
		 
		 
			 // 横向 向左
			 judgeY--;
			 while (board[judgeX][judgeY] == color) 
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) 
					 judgeY--;
				 
				 // 不满足直接退出
				 else 
					 break;
				 
			 
			 // 横向 向右
			 judgeY = y + 1;
			 while (board[judgeX][judgeY] == color) 
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) 
					 judgeY++;
				 
				 // 不满足直接退出
				 else 
					 ChessPieces = 1;
				 
			 
			 if (ChessPieces >= 5) 
				 flag = true;
			 
			 else 
				 ChessPieces = 1;
			 
			
		 
		 
			 int judgeX = x;
			 int judgeY = y;
			 // 横向 向左
			 judgeY--;
			 judgeX--;
			 while (board[judgeX][judgeY] == color) 
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) 
					 judgeY--;
					 judgeX--;
				 
				 // 不满足直接退出
				 else 
					 break;
				 
			 
			 // 横向 向右
			 judgeY = y + 1;
			 judgeX = x + 1;
			 while (board[judgeX][judgeY] == color) 
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) 
					 judgeY++;
					 judgeX++;
				 
				 // 不满足直接退出
				 else 
					 ChessPieces = 1;
				 
			 
			 if (ChessPieces >= 5) 
				 flag = true;
			 
			 else 
				 ChessPieces = 1;
			 
		 
		 
			 int judgeX = x;
			 int judgeY = y;
			 // 横向 向左
			 judgeY++;
			 judgeX--;
			 while (board[judgeX][judgeY] == color) 
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) 
					 judgeY++;
					 judgeX--;
				 
				 // 不满足直接退出
				 else 
					 break;
				 
			 
			 // 横向 向右
			 judgeY = y - 1;
			 judgeX = x + 1;
			 while (board[judgeX][judgeY] == color) 
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) 
					 judgeY--;
					 judgeX++;
				 
				 // 不满足直接退出
				 else 
					 ChessPieces = 1;
				 
			 
			 if (ChessPieces >= 5) 
				 flag = true;
			 
			 else 
				 ChessPieces = 1;
			 
		 
		
		
	
	return flag;
	
		
	


void Move()   //鼠标点击下棋


	int x, y;
	m = GetMouseMsg();
	if (m.mkLButton == 1)	//点击下棋
	
		char msg[200];
		sprintf(msg, "x=%d,y=%d", m.x, m.y);
		//	MessageBoxA(NULL, msg, "鼠标在哪儿", NULL);
			/*MessageBoxA(NULL, msg, "鼠标在哪儿", NULL);*/
		/*	被你气死得 for什么for*/
		
		// x=0,y=0             x=25,y=25

			/*int x = m.x % 25 >12;
			int y = m.y % 25;*/
			int x = m.x / 25;
			int y = m.y / 25;

			int offsetX = m.x % 25 <= 12 ? 0 : 25;
			int offsetY = m.y % 25 <= 12 ? 0 : 25;

			x *= 25;
			y *= 25;
			x += offsetX;
			y += offsetY;

			
			

			if (x <= 500 && y <= 500 && x >= 0 && y >= 0) 
				int boardX = x / 25;
				int boardY = y / 25;
				if (!board[boardX][boardY]) 
					if (flag) 
						DrawChessPieces(x, y, ChessPieceColor::blue);
						board[boardX][boardY] = ChessPieceColor::blue;
						bool winFlag = judgeWin(boardX, boardY, ChessPieceColor::blue);
						if (winFlag) 
							MessageBoxA(NULL, "WIN", "", NULL);
						
						flag = false;
					
					else 
						DrawChessPieces(x, y, ChessPieceColor::white);
						board[boardX][boardY] = ChessPieceColor::white;
						bool winFlag = judgeWin(boardX, boardY, ChessPieceColor::white);
						if (winFlag) 
							MessageBoxA(NULL, "WIN", "", NULL);
						
						flag = true;
					
				
			
		/*if (m.x >= x && m.x <= (x + 25) && m.y >= y && y <= (y + 25))*/
			
		//for (x = 12; x < 600; x += 25)
		//	for (y = 12; y < 500; y += 25)
		//	
		//		if (m.x >= x && m.x <= (x + 25) && m.y >= y && y <= (y + 25))
		//		
		//			if (board[(x + 13) / 25][(y + 13) / 25] = 0)     //判断该位置是否有棋子
		//			
		//				if (flag % 2 == 1)  setfillcolor(WHITE); ;//根据flag数判断棋子颜色
		//				if (flag % 2 == 0)  setfillcolor(BLUE); ;
		//				solidcircle(x + 13, y + 13, 12);
		//				board[(x + 13) / 25][(y + 13) / 25] = 1;
		//				flag++;
		//			
		//		
		//	
	
	FlushMouseMsgBuffer();

	//if (judge(x / 25, y / 25))         //每下完一次之后进行胜负判断
	//
	//	if (1 == flag % 2)
	//	
	//		k = 1;
	//		outtextxy(512, 400, _T("玩家2胜利"));

	//		return;
	//	
	//	else
	//	
	//		k = 1;
	//		outtextxy(512, 60, _T("玩家胜利"));
	//		return;
	//	
	//







void main()

	InitGame();
	while (1)
		Move();


以上是关于基于EasyX 的五子棋小游戏。的主要内容,如果未能解决你的问题,请参考以下文章

双人五子棋对战(需要EasyX图像库)

项目实战轻松实现C/C++大作业:五子棋游戏

利用easyx制作简易的五子棋项目

利用easyx制作简易的五子棋项目

基于EasyX的扫雷游戏

C语言大一课设——不会五子棋