C/C++实现经典象棋游戏的示例代码

目录
  • 大体思路
  • 效果展示
  • 核心代码

大体思路

采用面相过程的设计方式实现,类似于我们平时做的课程设计,实现这样的小游戏无非就是多了图形处理库。这里使用的是acllib图形库。

设计这种小游戏,首先要从宏观上去认识:象棋,要有棋盘,要有棋子,棋子要移动。

对于棋盘,十行九列画出即可。

对于棋子,分黑红两方,按照指定位置画出。

如何移动棋子,我们有mouseEvent函数。

初始化棋盘棋子:initmap,initpaint

利用鼠标实现棋子移动分两步(mouseEvent):

第一次点击,记录点击棋子的信息

第二次点击,将第一次点击记录的棋子信息移动到第二次点击的位置。

最后在添加亿点细节。

  • 首先画棋盘——chessboard()
  • 初始化棋子信息——initmp()
  • 将棋子画到棋盘上——initpaint()
  • 注册鼠标,通过鼠标点击不同位置实现棋子的移动(按照象棋行棋规范)——mouseEvent(int x, int y, int button, int e)x、y为点击位置,button(鼠标左右键与滑轮)、e(点击或抬起)为事件。
  • 行棋规范——judge_piece(int x, int y)代表能否从上一步走到x,y处。

此外,实现象棋必然缺不了人机对战。对于人机算法我们可以采用最简单的遍历。每次遍历获取所有人机方棋子能走的地方,然后根据评估函数找出这些步中对人机方来说收益最大的一步。思路很简单,但是想写出走一步看很多步的算法也很难。首先评估函数要尽量具有代表性,这直接决定了人机的棋力;其次需要迭代遍历,这样次数多了时间复杂度就会很高。

这里只写了走一步看一步的人机代码供参考,更深入的可以自行去了解:

1、void Get_All_path()——获取人机所有可能走的方式(将step存入向量v中)

2、int calculate_value()——评估函数

3、void Get_max_way()——得到对人机最有利的那一步(传到prex,prey与nowx,nowy中,即从pre走到now)。

效果展示

核心代码

struct step
{
	int x1, y1, x2, y2;
};//从x1,y1走到x2,y2
void Get_All_path()
{
	v.clear();//全局变量
	step temp;
	for (int a = 0; a < 16; a++)
	{
		if (black_chess[a].is == 0)continue;
		prex = black_chess[a].row;
		prey = black_chess[a].col;
		temp.x1 = prex;
		temp.y1 = prey;
		for(int i=0;i<10;i++)
			for (int j = 0; j < 9; j++)
			{
				temp.x2 = i;
				temp.y2 = j;
				if (judge_piece(i, j) == 1&&mp[i][j].color!='B')
				{
					v.push_back(temp);
				}
			}
	}
}
int calculate_value()
{
	int sum1 = 0, sum2 = 0;
	for(int a=0;a<10;a++)
		for (int b = 0; b < 9; b++)
		{
			if (mp[a][b].color == 'B' && mp[a][b].is == 1)
			{
				switch (mp[a][b].index)
				{
				case 0:
					sum1 += 100;
					break;
				case 1:
					sum1 += 50;
					break;
				case 2:
					sum1 += 10;
				case 3:
					sum1 += 10;
					break;
				case 4:
					sum1 += 1500;
					break;
				case 5:
					sum1 += 50;
					break;
				case 6:
					sum1 += 20;
					break;
				default:
					break;
				}
			}
			else if (mp[a][b].color == 'R' && mp[a][b].is == 1)
			{
				switch (mp[a][b].index)
				{
				case 7:
					sum2 += 100;
					break;
				case 8:
					sum2 += 50;
					break;
				case 9:
					sum2 += 10;
				case 10:
					sum2 += 10;
					break;
				case 11:
					sum2 += 1500;
					break;
				case 12:
					sum2 += 50;
					break;
				case 13:
					sum2 += 20;
					break;
				default:
					break;
				}
			}
		}
	return sum1 - sum2;
}
void Get_max_way()
{
	//step ans;
	int minn = -150000;
	int calculate;
	Get_All_path();
	for (int a = 0; a<v.size(); a++)
	{
		int f = 1;
		int index= mp[v[a].x2][v[a].y2].index;
		int pos=0;
		int generalx=mp[v[a].x1][v[a].y1].row, generaly= mp[v[a].x1][v[a].y1].col;
		if (mp[v[a].x2][v[a].y2].color == 'R')//黑方吃子
		{
			if (mp[v[a].x2][v[a].y2].pos == 1)//被吃掉的为南兵
			{
				pos = 1;
				mp[v[a].x2][v[a].y2].pos = 0;
			}
			if (mp[v[a].x1][v[a].y1].index == 4)//黑将移动
			{
				black_general_x = v[a].x2;
				black_general_y = v[a].y2;
			}
			mp[v[a].x1][v[a].y1].is = 0;
			mp[v[a].x1][v[a].y1].color = NULL;
			mp[v[a].x2][v[a].y2].is = 1;
			mp[v[a].x2][v[a].y2].color = 'B';
			mp[v[a].x2][v[a].y2].index = mp[v[a].x1][v[a].y1].index;
		}
		else
		{
			f = 0;
			if (mp[v[a].x1][v[a].y1].index == 4)
			{
				black_general_x = v[a].x2;
				black_general_y = v[a].y2;
			}
			mp[v[a].x1][v[a].y1].is = 0;
			mp[v[a].x1][v[a].y1].color = NULL;
			mp[v[a].x2][v[a].y2].is = 1;
			mp[v[a].x2][v[a].y2].color = 'B';
			mp[v[a].x2][v[a].y2].index = mp[v[a].x1][v[a].y1].index;
		}
		calculate = calculate_value();
		if (minn < calculate)
		{
			prex = v[a].x1;
			prey = v[a].y1;
			minn = calculate;
			nowx = v[a].x2;
			nowy = v[a].y2;
		}
		//printf("%d\n", minn);
		if (f == 1)
		{

			mp[v[a].x2][v[a].y2].pos = pos;
			if (mp[v[a].x2][v[a].y2].index == 4)
			{
				black_general_x = generalx;
				black_general_y = generaly;
			}
			mp[v[a].x1][v[a].y1].is = 1;
			mp[v[a].x1][v[a].y1].color = 'B';
			mp[v[a].x1][v[a].y1].index = mp[v[a].x2][v[a].y2].index;
			mp[v[a].x2][v[a].y2].color = 'R';
			mp[v[a].x2][v[a].y2].index = index;
			mp[v[a].x2][v[a].y2].is = 1;
		}
		else
		{
			if (mp[v[a].x2][v[a].y2].index == 4)
			{
				black_general_x = generalx;
				black_general_y = generaly;
			}
				mp[v[a].x1][v[a].y1].is = 1;
				mp[v[a].x1][v[a].y1].color = 'B';
				mp[v[a].x1][v[a].y1].index = mp[v[a].x2][v[a].y2].index;
				mp[v[a].x2][v[a].y2].color = NULL;
				mp[v[a].x2][v[a].y2].index = index;
				mp[v[a].x2][v[a].y2].is = 0;
		}
	}
	//printf("%d\n", v.size());
	//printf("%d\n", minn);
	//return ans;
}

不含人机的代码如下:

#include"acllib.h"
#include<math.h>
#include<stdio.h>
#include<queue>
#define INTERVAL 50
using namespace std;
ACL_Image chessimg;
const double pi = 3.14;
//左上角为:(102,100)
// width:55
//height:55
//棋盘大小:440*495
//棋子半径:20
const char* chessname[] = { "車","馬","象","士","将","炮","卒","车","马","相","仕","帥","砲","兵" };
struct piece
{
	int x, y;
	int index;
	int is;
	char color;
	int pos;//兵 卒是否过河
	int row, col;
};
deque<piece>q;//悔棋——三步
void paint(piece s);
struct piece mp[10][9];
void initmp();
void initpaint();
void mouseEvent(int x, int y, int button, int e);
void paint_rect(int tmpx, int tmpy);
int judge_piece(int x, int y);
void chessboard();
int mouseflag = 0;
int prex, prey;
int black_general_x = 0, black_general_y = 4, black_general_isexist = 1;
int red_general_x = 9, red_general_y = 4, red_general_isexist = 1;
piece tmp;
int judge_general(int x, int y);
int Setup()
{
	//initConsole();
	initWindow("chess", DEFAULT, DEFAULT, 650, 701);
	chessboard();
	//loadImage("qipan.bmp", &chessimg);
	/*beginPaint();
	putImageScale(&chessimg, 0, 0, 650, 701);
	endPaint();*/
	registerMouseEvent(mouseEvent);
	//paint(piece s);
	initmp();
	initpaint();
	return 0;
}
void mouseEvent(int x, int y, int button, int e)
{
	if (button == LEFT_BUTTON && e == BUTTON_DOWN)
	{
		if (x >= INTERVAL + 8 * 55 + 33 && x <= INTERVAL + 8 * 55 + 33 + 100 && y >= INTERVAL + 55 && y <= INTERVAL + 55 + 100)// INTERVAL + 8 * 55 + 33, INTERVAL + 55)
		{
			/*beginPaint();
			setPenColor(BLACK);
			setPenWidth(3);
			setBrushColor(EMPTY);
			rectangle(INTERVAL + 8 * 55 + 33, INTERVAL + 55, INTERVAL + 8 * 55 + 33 + 100, INTERVAL + 55 + 50);
			endPaint();*/
			if (!q.empty())
			{
				piece p1 = q.back();
				q.pop_back();
				piece p2 = q.back();
				q.pop_back();
				//piece p3 = p1;
				mp[p1.row][p1.col].color = p1.color;
				mp[p1.row][p1.col].index = p1.index;
				mp[p1.row][p1.col].is = p1.is;
				mp[p1.row][p1.col].pos = p1.pos;
				mp[p2.row][p2.col].color = p2.color;
				mp[p2.row][p2.col].index = p2.index;
				mp[p2.row][p2.col].is = p2.is;
				mp[p2.row][p2.col].pos = p2.pos;
				initpaint();
			}
		}
		int tmpx, tmpy, f = 0;
		for (int a = 0; a < 10; a++)
		{
			if (f == 1)break;
			for (int b = 0; b < 9; b++)
			{
				if (sqrt(((double)x - mp[a][b].x) * ((double)x - mp[a][b].x) + ((double)y - mp[a][b].y) * ((double)y - mp[a][b].y)) <= 20)
				{
					f = 1;
					tmpx = a;
					tmpy = b;
					break;
				}
			}
		}
		if (f == 1)//有格子
		{
			initpaint();
			//paint_rect(tmpx, tmpy);
			/*tmp.x = mp[tmpx][tmpy].x;
			tmp.y = mp[tmpx][tmpy].y;
			tmp.is = mp[tmpx][tmpy].is;
			tmp.index = mp[tmpx][tmpy].index;*/
			if (mp[tmpx][tmpy].is == 1)//格子内有棋子
			{
				if (tmp.color == mp[tmpx][tmpy].color || tmp.color == NULL)//同色copy
				{
					paint_rect(tmpx, tmpy);
					prex = tmpx;
					prey = tmpy;
					tmp.is = mp[tmpx][tmpy].is;
					tmp.index = mp[tmpx][tmpy].index;
					tmp.color = mp[tmpx][tmpy].color;
				}
				else if (judge_piece(tmpx, tmpy) == 1)    //异色吃掉
				{
					q.push_back(mp[tmpx][tmpy]);
					q.push_back(mp[prex][prey]);
					while (q.size() > 6) {
						q.pop_front();
						q.pop_front();
					}
					if (mp[tmpx][tmpy].pos == 1)//若被吃掉的是南兵
					{
						mp[tmpx][tmpy].pos = 0;
					}
					else if (mp[prex][prey].pos == 1)//南兵吃别人
					{
						mp[prex][prey].pos = 0;
						mp[tmpx][tmpy].pos = 1;
					}
					if (mp[prex][prey].index == 4)//若是黑将移动,黑将位置重新赋值
					{
						black_general_x = tmpx;
						black_general_y = tmpy;
					}
					else if (mp[prex][prey].index == 11)//若是红将移动,红将位置重新赋值
					{
						red_general_x = tmpx;
						red_general_y = tmpy;
					}
					if (mp[tmpx][tmpy].index == 11)
						red_general_isexist = 0;
					else if (mp[tmpx][tmpy].index == 4)
						black_general_isexist = 0;
					mp[tmpx][tmpy].color = tmp.color;
					mp[tmpx][tmpy].index = tmp.index;
					mp[prex][prey].is = 0;
					mp[prex][prey].color = NULL;
					tmp.is = 0;
					tmp.color = NULL;
					initpaint();
					if (judge_general(tmpx, tmpy) == 1)
					{
						beginPaint();
						setTextColor(RED);
						setTextSize(30);
						setTextBkColor(EMPTY);
						paintText(250, 600, "将   军!");
						endPaint();
					}
				}
				else
				{
					beginPaint();
					setTextColor(RED);
					setTextSize(30);
					setTextBkColor(EMPTY);
					paintText(250, 600, "请规范行棋");
					endPaint();
					tmp.is = 0;
					tmp.color = NULL;
				}
			}
			else//格子内没有棋子
			{
				//paint_rect(tmpx, tmpy);
				if (tmp.is == 1)
				{
					if (judge_piece(tmpx, tmpy) == 1)
					{
						q.push_back(mp[tmpx][tmpy]);
						q.push_back(mp[prex][prey]);
						if (q.size() > 6) {
							q.pop_front();
							q.pop_front();
						}
						if (mp[prex][prey].pos == 1)
						{
							mp[prex][prey].pos = 0;
							mp[tmpx][tmpy].pos = 1;
						}
						if (mp[prex][prey].index == 4)//若是黑将移动,黑将位置重新赋值
						{
							black_general_x = tmpx;
							black_general_y = tmpy;
						}
						else if (mp[prex][prey].index == 11)//若是红将移动,红将位置重新赋值
						{
							red_general_x = tmpx;
							red_general_y = tmpy;
						}
						mp[prex][prey].is = 0;
						mp[prex][prey].color = NULL;
						mp[tmpx][tmpy].index = tmp.index;
						mp[tmpx][tmpy].is = tmp.is;
						mp[tmpx][tmpy].color = tmp.color;
						tmp.is = 0;
						tmp.color = NULL;
						initpaint();
						if (judge_general(tmpx, tmpy) == 1)
						{
							beginPaint();
							setTextColor(RED);
							setTextSize(30);
							setTextBkColor(EMPTY);
							paintText(250, 600, "将   军!");
							endPaint();
						}
					}
					else
					{
						beginPaint();
						setTextColor(RED);
						setTextSize(30);
						setTextBkColor(EMPTY);
						paintText(250, 600, "请规范行棋");
						endPaint();
						tmp.is = 0;
						tmp.color = NULL;
					}
					//initpaint();
				}
				else paint_rect(tmpx, tmpy);
				//initpaint();
			}

		}
		if (red_general_isexist == 0)
		{
			beginPaint();
			setTextSize(70);
			setTextColor(BLACK);
			setTextBkColor(RGB(3, 168, 158));
			paintText(130, 270, "黑方获胜");
			endPaint();
		}
		else if (black_general_isexist == 0)
		{
			beginPaint();
			setTextSize(70);
			setTextColor(RED);
			setTextBkColor(RGB(3, 168, 158));
			paintText(130, 270, "红方获胜");
			endPaint();
		}
	}
	else if (button == RIGHT_BUTTON && e == BUTTON_DOWN)
	{
		initpaint();
		tmp.is = 0;
		tmp.color = NULL;

	}

	//initpaint();
}
void paint(piece s)
{
	beginPaint();
	//clearDevice();
	//putImageScale(&chessimg, 0, 0, 650, 701);
	setPenColor(RGB(245, 222, 179));
	setPenWidth(3);
	setBrushColor(RGB(245, 222, 179));
	ellipse(s.x - 20, s.y - 20, s.x + 20, s.y + 20);
	if (s.color == 'B')
		setTextColor(BLACK);
	else setTextColor(RED);
	setTextSize((int)(20 * sin(pi / 4)) * 2);
	setTextBkColor(EMPTY);
	paintText(s.x - (int)(20 * sin(pi / 4)), s.y - (int)(20 * sin(pi / 4)), chessname[s.index]);
	endPaint();
}
/*void paintb(piece s)
{
	beginPaint();
	//clearDevice();
	setPenColor(RGB(245, 222, 179));
	setPenWidth(3);
	setBrushColor(RGB(245, 222, 179));
	ellipse(s.x - 20, s.y - 20, s.x + 20, s.y + 20);
	setTextColor(BLACK);
	setTextSize((int)(20 * sin(pi / 4)) * 2);
	setTextBkColor(EMPTY);
	paintText(s.x - (int)(20 * sin(pi / 4)), s.y - (int)(20 * sin(pi / 4)), chessname[s.index]);
	endPaint();
}*/
void initmp()
{
	for (int a = 0; a < 10; a++)
	{
		for (int b = 0; b < 9; b++)
		{
			mp[a][b].x = INTERVAL + 55 * (b);
			mp[a][b].y = INTERVAL + 55 * (a);
			mp[a][b].color = NULL;
			mp[a][b].index = -1;
			mp[a][b].is = 0;
			mp[a][b].pos = 0;
			mp[a][b].row = a;
			mp[a][b].col = b;
		}
	}
	int j = 0;
	for (int a = 0; a < 5; a++)
	{
		mp[0][a].index = j++;
		mp[0][a].is = 1;
		mp[0][a].color = 'B';
	}
	j = 3;
	for (int a = 5; a < 9; a++)
	{
		mp[0][a].index = j--;
		mp[0][a].is = 1;
		mp[0][a].color = 'B';
	}
	int i = 7;
	for (int a = 0; a < 5; a++)
	{
		mp[9][a].index = i++;
		mp[9][a].is = 1;
		mp[9][a].color = 'R';
	}
	i = 10;
	for (int a = 5; a < 9; a++)
	{
		mp[9][a].index = i--;
		mp[9][a].is = 1;
		mp[9][a].color = 'R';
	}
	mp[2][1].index = 5;
	mp[2][7].index = 5;
	mp[2][1].color = 'B';
	mp[2][7].color = 'B';
	mp[7][1].index = 12;
	mp[7][7].index = 12;
	mp[7][1].color = 'R';
	mp[7][7].color = 'R';
	mp[2][1].is = 1;
	mp[2][7].is = 1;
	mp[7][1].is = 1;
	mp[7][7].is = 1;
	for (int a = 0; a < 9; a += 2)
	{
		mp[3][a].index = 6;
		mp[6][a].index = 13;
		mp[3][a].is = 1;
		mp[6][a].is = 1;
		mp[3][a].color = 'B';
		mp[6][a].color = 'R';
		mp[6][a].pos = 1;
	}
}
void initpaint()
{
	/*for (int a = 0; a < 9; a++)
	{
		//mp[0][a].is = 1;
		paintb(mp[0][a]);
	}
	paintb(mp[2][1]);
	paintb(mp[2][7]);
	//mp[2][1].is = 1;
	//mp[2][7].is = 1;
	for (int a = 0; a < 9; a += 2)
	{
		//mp[3][a].is = 1;
		paintb(mp[3][a]);
	}
	for (int a = 0; a < 9; a++)
	{
		//mp[9][a].is = 1;
		paintr(mp[9][a]);
	}
	paintr(mp[7][1]);
	paintr(mp[7][7]);
	mp[7][1].is = 1;
	mp[7][7].is = 1;
	for (int a = 0; a < 9; a += 2)
	{
		//mp[6][a].is = 1;
		paintr(mp[6][a]);
	}*/
	/*beginPaint();
	clearDevice();
	putImageScale(&chessimg, 0, 0, 650, 701);
	endPaint();*/
	chessboard();
	for (int a = 0; a < 10; a++)
		for (int b = 0; b < 9; b++)
		{
			if (mp[a][b].is == 1)
			{
				paint(mp[a][b]);
			}
		}
}
void paint_rect(int tmpx, int tmpy)
{
	//initpaint();
	beginPaint();
	setPenColor(RGB(0, 0, 225));
	setBrushColor(EMPTY);
	rectangle(mp[tmpx][tmpy].x - 25, mp[tmpx][tmpy].y - 25, mp[tmpx][tmpy].x + 25, mp[tmpx][tmpy].y + 25);
	endPaint();
}
int judge_piece(int x, int y)//18ma07che5 12pao29xiang3 10shi6 13zu else jiang
{
	if (mp[prex][prey].index == 1 || mp[prex][prey].index == 8)
	{
		if ((x == prex - 2 && y == prey - 1 && mp[prex - 1][prey].is == 0) || (x == prex - 2 && y == prey + 1 && mp[prex - 1][prey].is == 0)
			|| (x == prex - 1 && y == prey + 2 && mp[prex][prey + 1].is == 0) || (x == prex + 1 && y == prey + 2 && mp[prex][prey + 1].is == 0)
			|| (x == prex + 2 && y == prey + 1 && mp[prex + 1][prey].is == 0) || (x == prex + 2 && y == prey - 1 && mp[prex + 1][prey].is == 0)
			|| (x == prex + 1 && y == prey - 2 && mp[prex][prey - 1].is == 0) || (x == prex - 1 && y == prey - 2 && mp[prex][prey - 1].is == 0))
		{
			return 1;
		}
		else return 0;
	}
	else if (mp[prex][prey].index == 0 || mp[prex][prey].index == 7)
	{
		if (prey == y)
		{
			for (int i = min(prex, x) + 1; i < max(prex, x); i++)
			{
				if (mp[i][prey].is == 1)return 0;
			}
			return 1;
		}
		else if (prex == x)
		{
			for (int i = min(prey, y) + 1; i < max(prey, y); i++)
			{
				if (mp[prex][i].is == 1)return 0;
			}
			return 1;
		}
		else return 0;
	}
	else if (mp[prex][prey].index == 5 || mp[prex][prey].index == 12)
	{
		if (mp[x][y].is == 1)
		{
			if (prey == y)
			{
				int ant = 0;
				for (int i = min(prex, x) + 1; i < max(prex, x); i++)
				{
					if (mp[i][prey].is == 1)ant++;
				}
				if (ant == 1)return 1;
				else return 0;
			}
			else if (prex == x)
			{
				int ant = 0;
				for (int i = min(prey, y) + 1; i < max(prey, y); i++)
				{
					if (mp[prex][i].is == 1)ant++;
				}
				if (ant == 1)return 1;
				else return 0;
			}
			else return 0;
		}
		else
		{
			if (prey == y)
			{
				for (int i = min(prex, x) + 1; i < max(prex, x); i++)
				{
					if (mp[i][prey].is == 1)return 0;
				}
				return 1;
			}
			else if (prex == x)
			{
				for (int i = min(prey, y) + 1; i < max(prey, y); i++)
				{
					if (mp[prex][i].is == 1)return 0;
				}
				return 1;
			}
			else return 0;
		}
	}
	else if (mp[prex][prey].index == 2 || mp[prex][prey].index == 9)
	{
		if (prex <= 4)
		{
			if (x <= 4)
			{
				if ((x == prex - 2 && y == prey - 2 && mp[prex - 1][prey - 1].is == 0)
					|| (x == prex - 2 && y == prey + 2 && mp[prex - 1][prey + 1].is == 0)
					|| (x == prex + 2 && y == prey + 2 && mp[prex + 1][prey + 1].is == 0)
					|| (x == prex + 2 && y == prey - 2 && mp[prex + 1][prey - 1].is == 0))
				{
					return 1;
				}
				else return 0;
			}
			else return 0;
		}
		else if (prex >= 5)
		{
			if (x > 4)
			{
				if ((x == prex - 2 && y == prey - 2 && mp[prex - 1][prey - 1].is == 0)
					|| (x == prex - 2 && y == prey + 2 && mp[prex - 1][prey + 1].is == 0)
					|| (x == prex + 2 && y == prey + 2 && mp[prex + 1][prey + 1].is == 0)
					|| (x == prex + 2 && y == prey - 2 && mp[prex + 1][prey - 1].is == 0))
				{
					return 1;
				}
				else return 0;
			}
			else return 0;
		}
	}
	else if (mp[prex][prey].index == 3 || mp[prex][prey].index == 10)
	{
		if (prex <= 4)
		{
			if (x > 2 || y < 3 || y>5)return 0;
			else
			{
				if ((x == prex - 1 && y == prey - 1) || (x == prex - 1 && y == prey + 1)
					|| (x == prex + 1 && y == prey + 1) || (x == prex + 1 && y == prey - 1))
				{
					return 1;
				}
				else return 0;
			}
		}
		else
		{
			if (x < 7 || y < 3 || y>5)return 0;
			else
			{
				if ((x == prex - 1 && y == prey - 1) || (x == prex - 1 && y == prey + 1)
					|| (x == prex + 1 && y == prey + 1) || (x == prex + 1 && y == prey - 1))
				{
					return 1;
				}
				else return 0;
			}
		}
	}
	else if (mp[prex][prey].index == 6 || mp[prex][prey].index == 13)
	{
		if (mp[prex][prey].pos == 0)//北卒
		{
			if (prex <= 4)
			{
				if ((x == prex + 1) && (y == prey))return 1;
				else return 0;
			}
			else
			{
				if (((x == prex + 1) && (y == prey)) || ((x == prex) && (y = prey - 1)) || ((x == prex) && (y = prey + 1)))return 1;
				else return 0;
			}
		}
		else//南兵
		{
			if (prex >= 5)
			{
				if ((x == prex - 1) && (y == prey)) { return 1; }
				else return 0;
			}
			else
			{
				if (((x == prex - 1) && (y == prey)) || ((x == prex) && (y = prey - 1)) || ((x == prex) && (y = prey + 1))) {
					return 1;
				}
				else return 0;
			}
		}
	}
	else
	{
		//int ant = 0;
		if (prex <= 4)
		{
			int ant = 0;
			for (int i = prex + 1; i <= 9; i++)
			{
				if (mp[i][prey].index == 4 || mp[i][prey].index == 11)break;
				if (mp[i][prey].is == 1)ant++;
			}
			if (ant == 0)return 1;
		}
		else
		{
			int ant = 0;
			for (int i = prex - 1; i >= 0; i--)
			{
				if (mp[i][prey].index == 4 || mp[i][prey].index == 11)break;
				if (mp[i][prey].is == 1)ant++;
			}
			if (ant == 0)return 1;
		}
		if (mp[prex][prey].pos == 0)
		{
			if (((x == prex) && (y == prey + 1)) || ((x == prex) && (y == prey - 1)) || ((y == prey) && (x == prex - 1)) || ((y == prey) && (x == prex + 1)))
			{
				return 1;
			}
			else return 0;
		}
		else
		{
			if (((x == prex) && (y == prey + 1)) || ((x == prex) && (y == prey - 1)) || ((y == prey) && (x == prex - 1)) || ((y == prey) && (x == prex + 1)))
			{
				return 1;
			}
			else return 0;

		}
	}
	//return 1;
}
void chessboard()
{
	beginPaint();
	clearDevice();
	setPenColor(RED);
	setPenWidth(1);
	for (int a = 0; a <= 9; a++)
	{
		line(INTERVAL, INTERVAL + a * 55, INTERVAL + 440, INTERVAL + a * 55);
	}
	for (int a = 0; a <= 8; a++)
	{
		line(INTERVAL + a * 55, INTERVAL, INTERVAL + a * 55, INTERVAL + 495);
	}
	line(INTERVAL + 3 * 55, INTERVAL, INTERVAL + 5 * 55, INTERVAL + 2 * 55);
	line(INTERVAL + 5 * 55, INTERVAL, INTERVAL + 3 * 55, INTERVAL + 2 * 55);
	line(INTERVAL + 3 * 55, INTERVAL + 7 * 55, INTERVAL + 5 * 55, INTERVAL + 9 * 55);
	line(INTERVAL + 5 * 55, INTERVAL + 7 * 55, INTERVAL + 3 * 55, INTERVAL + 9 * 55);
	setBrushColor(EMPTY);
	rectangle(INTERVAL - 5, INTERVAL - 5, INTERVAL + 5 + 440, INTERVAL + 5 + 495);
	setBrushColor(WHITE);
	rectangle(INTERVAL, INTERVAL + 220, INTERVAL + 440 + 1, INTERVAL + 275 + 1);
	setTextColor(BLACK);
	setTextFont("楷体");
	setTextSize(30);
	setTextBkColor(WHITE);
	paintText(INTERVAL + 73, INTERVAL + 235, "楚河           汉界");
	setTextBkColor(RGB(218, 112, 214));
	setTextSize(50);
	paintText(INTERVAL + 8 * 55 + 33, INTERVAL + 55, "悔棋");
	endPaint();

}
int judge_general(int x, int y)//判断是否被将军
{
	if (mp[x][y].index == 1 || mp[x][y].index == 8)
	{
		if (mp[x][y].color == 'B')
		{
			if ((red_general_x == x - 2 && red_general_y == y - 1) || (red_general_x == x - 2 && red_general_y == y + 1)
				|| (red_general_x == x - 1 && red_general_y == y + 2) || (red_general_x == x + 1 && red_general_y == y + 2)
				|| (red_general_x == x + 2 && red_general_y == y + 1) || (red_general_x == x + 2 && red_general_y == y - 1)
				|| (red_general_x == x + 1 && red_general_y == y - 2) || (red_general_x == x - 1 && red_general_y == y - 2))
			{
				return 1;
			}
			else return 0;
		}
		else
		{
			if ((black_general_x == x - 2 && black_general_y == y - 1) || (black_general_x == x - 2 && black_general_y == y + 1)
				|| (black_general_x == x - 1 && black_general_y == y + 2) || (black_general_x == x + 1 && black_general_y == y + 2)
				|| (black_general_x == x + 2 && black_general_y == y + 1) || (black_general_x == x + 2 && black_general_y == y - 1)
				|| (black_general_x == x + 1 && black_general_y == y - 2) || (black_general_x == x - 1 && black_general_y == y - 2))
			{
				return 1;
			}
			else return 0;
		}
	}
	else if (mp[x][y].index == 0 || mp[x][y].index == 7)
	{
		if (mp[x][y].color == 'B')
		{
			if (red_general_y == y)
			{
				for (int i = min(red_general_x, x) + 1; i < max(red_general_x, x); i++)
				{
					if (mp[i][red_general_y].is == 1)return 0;
				}
				return 1;
			}
			else if (red_general_x == x)
			{
				for (int i = min(red_general_y, y) + 1; i < max(red_general_y, y); i++)
				{
					if (mp[red_general_x][i].is == 1)return 0;
				}
				return 1;
			}
			else return 0;
		}
		else
		{
			if (black_general_y == y)
			{
				for (int i = min(black_general_x, x) + 1; i < max(black_general_x, x); i++)
				{
					if (mp[i][black_general_y].is == 1)return 0;
				}
				return 1;
			}
			else if (black_general_x == x)
			{
				for (int i = min(black_general_y, y) + 1; i < max(black_general_y, y); i++)
				{
					if (mp[black_general_x][i].is == 1)return 0;
				}
				return 1;
			}
			else return 0;
		}
	}
	else if (mp[x][y].index == 5 || mp[x][y].index == 12)
	{
		if (mp[x][y].color == 'B')
		{
			if (red_general_y == y)
			{
				int ant = 0;
				for (int i = min(red_general_x, x) + 1; i < max(red_general_x, x); i++)
				{
					if (mp[i][red_general_y].is == 1)ant++;
				}
				if (ant == 1)return 1;
				else return 0;
			}
			else if (red_general_x == x)
			{
				int ant = 0;
				for (int i = min(red_general_y, y) + 1; i < max(red_general_y, y); i++)
				{
					if (mp[red_general_x][i].is == 1)ant++;
				}
				if (ant == 1)return 1;
				else return 0;
			}
			else return 0;
		}
		else
		{
			if (black_general_y == y)
			{
				int ant = 0;
				for (int i = min(black_general_x, x) + 1; i < max(black_general_x, x); i++)
				{
					if (mp[i][black_general_y].is == 1)ant++;
				}
				if (ant == 1)return 1;
				else return 0;
			}
			else if (black_general_x == x)
			{
				int ant = 0;
				for (int i = min(black_general_y, y) + 1; i < max(black_general_y, y); i++)
				{
					if (mp[black_general_x][i].is == 1)ant++;
				}
				if (ant == 1)return 1;
				else return 0;
			}
			else return 0;
		}
		return  0;
	}
	else if (mp[x][y].index == 6 || mp[x][y].index == 13)
	{
		if (mp[x][y].color == 'B')
		{
			if ((red_general_x == x + 1 && red_general_y == y) || (red_general_x == x && red_general_y == y - 1)
				|| (red_general_x == x && red_general_y == y + 1))
			{
				return 1;
			}
			else return 0;
		}
		else
		{
			if ((black_general_x == x - 1 && black_general_y == y) || (black_general_x == x && black_general_y == y - 1)
				|| (black_general_x == x && black_general_y == y + 1))
			{
				return 1;
			}
			else return 0;
		}
	}
}

以上就是C/C++实现经典象棋游戏的示例代码的详细内容,更多关于C++象棋游戏的资料请关注我们其它相关文章!

(0)

相关推荐

  • C++实现鼠标控制的黑框象棋

    本文实例为大家分享了C++实现鼠标控制的黑框象棋的具体代码,供大家参考,具体内容如下 该象棋小游戏的特色 有颜色标注出 红方和绿方 可以用鼠标控制 颜色原理 直接调用用Windows自带的颜色API 用到了 颜色头文件.h 代码. //consolecolor.hpp这是着色的实现头文件 #pragma once #include<Windows.h>//调用win32API函数 #include<iostream>//调用flush成员函数,首先刷新缓冲区 namespace c

  • C语言实现中国象棋

    本文实例为大家分享了C语言实现中国象棋的具体代码,供大家参考,具体内容如下 运行截图 实现思路 老套路,二维数组存储棋盘,宏定义各种棋子,每次棋子的移动实质为二维数组的赋值.重点是判断棋子是否可移动到目标位置,移动有两种可能,一是单纯移动,二是吃子移动.主要飞将的特殊规则.废话不多说,贴源码(主要下面是两个源文件哦,嘿嘿.) 源码 main.c #include<stdio.h> #include<string.h> #include<windows.h> //自定义源

  • C语言用Easyx绘制围棋和象棋的棋盘

    本文实例为大家分享了C语言绘制围棋和象棋棋盘的具体代码,供大家参考,具体内容如下 一.绘制围棋棋盘 代码如下: #include<graphics.h> #include<conio.h> int main() {     int step=30;     //初始化绘图窗口     initgraph(600,600);     //设置背景色为黄色     setbkcolor(YELLOW);     //用背景色清空屏幕     cleardevice();     set

  • 基于c++的中国象棋游戏设计与实现

    目录 1.文档 2.游戏操作逻辑 3.UI框架 4.网络通信 1.文档 文档分为两部分,一部分在代码中,然后通过doxygen生成HTML.解压本目录下的html.zip后打开index.html即可查看:第二部分在此说明文档内,在这里会介绍一些架构方面的信息. 2.游戏操作逻辑 相关的命名空间有: Chess:这是包含中国象棋的操作逻辑的命名空间 主要操作是possibleMove(int x, int y),通过整个棋盘每个位置上的信息.中国象棋的规则来获得位置(x, y)这个棋子可以移动到

  • C++ 中国象棋的实现流程详解

    中国象棋的中国棋文化,也是中华民族的文化瑰宝,它源远流长,趣味浓厚,基本规则简明易懂.中国象棋在中国的群众中基础远远超过围棋,是普及最广的棋类项目,中国象棋已流传到十几个国家和地区. 中国象棋使用方形格状棋盘,圆形棋子共有32个,红黑二色各有16个棋子,摆放和活动在交叉点上.双方交替行棋,先把对方的将(帅)"将死"的一方获胜. 我们今天就来看看我们自己能不能写出这样一个游戏呢? 今天就不话不多说了,先说一下,今天我们做的是一个简易版的单机中国象棋,希望大家理解,联网对弈的话需要用到的知

  • 基于C++和MFC开发象棋程序

    这是我要和大家分享的基于C++和MFC开发的一个象棋程序,目的是练习编程实践和大家分享同时希望大家能给出指教. 进入主题 一.棋盘分析 这是我绘制的棋盘,棋盘的组成由9条竖线和10条横线构成.这儿我们设置每条线间的间隔是50. 二.绘制过程 1.在vs中新建MFC程序,去除环境自动生成的按钮和文字. 2.打开***Dlg.cpp文件,在void CChessDlg::OnPaint()中定义一个棋盘间隔值和绘图设备CDC *cd = CWnd::GetDC(); int nWid = 50; C

  • C/C++实现经典象棋游戏的示例代码

    目录 大体思路 效果展示 核心代码 大体思路 采用面相过程的设计方式实现,类似于我们平时做的课程设计,实现这样的小游戏无非就是多了图形处理库.这里使用的是acllib图形库. 设计这种小游戏,首先要从宏观上去认识:象棋,要有棋盘,要有棋子,棋子要移动. 对于棋盘,十行九列画出即可. 对于棋子,分黑红两方,按照指定位置画出. 如何移动棋子,我们有mouseEvent函数. 初始化棋盘棋子:initmap,initpaint 利用鼠标实现棋子移动分两步(mouseEvent): 第一次点击,记录点击

  • JAVA实现经典扫雷游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 总结 前言 windows自带的游戏<扫雷>是陪伴了无数人的经典游戏,本程序参考<扫雷>的规则进行了简化,用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 1.要有难度等级,初级,中级,高级 2.由玩家逐个翻开方块,以找出所有地雷为最终游戏目标.如果玩家翻开的方块有地雷,则游戏结束 3.游戏主区域由很多个方格组成.使用鼠标左键随机点击一个方格,方格即被打开并显示出方格中的数字:方格中数字则表示其

  • C#实现经典飞行棋游戏的示例代码

    目录 效果展示 主函数 场景类型枚举 控制台基础设置 开始及结束场景逻辑 游戏场景逻辑 固定打印的信息 格子类型枚举和格子结构体 地图结构体 玩家和电脑结构体 绘制玩家 扔骰子逻辑 效果展示 主函数      static void Main(string[] args)         {             int w = 50;             int h = 30;             ConsoleInit(w, h);             E_SceneType

  • Java实现经典大富翁游戏的示例详解

    目录 前言 主要设计 功能截图 代码实现 总结 前言 大富翁,又名地产大亨.是一种多人策略图版游戏.参与者分得游戏金钱,凭运气(掷骰子)及交易策略,买地.建楼以赚取租金.英文原名monopoly意为“垄断”,因为最后只得一个胜利者,其余均破产收场. <大富翁>游戏是用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 可多人参与的大富翁游戏,玩家有初始资金,通过掷骰子,玩家移动指定骰子点数步骤,根据对应格子上的交易策略,来决定是赚钱还是亏钱,其他玩家破

  • Java实现经典角色扮演侦探游戏游戏的示例代码

    目录 前言 游戏背景 主要需求 主要设计 功能截图 代码实现 游戏主界面 主卧 初始化 大厅 总结 前言 游戏背景 百变山庄坐落于太平洋的一座小岛上,山庄主人亦是小岛的主人.这个神秘主人细致周到,邀请函里不仅附着往返港口的机票,港口的邮船也是通往小岛的专线. 初登小岛,恢宏大气的山庄直入眼帘,通过门廊,金碧辉煌的大厅震人心魄. 受邀的侦探们陆续到齐,[侍者]彬彬有礼地站在一旁,他安排你们围坐在一个奇特十边形的桌子旁稍加等待.[侦探指尖]回忆着自己临行前调查的各位名侦探的资料,除了那个神秘的[电话

  • Java实现经典拳皇误闯冒险岛游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 游戏主界面 英雄 总结 前言 <拳皇误闯冒险岛>是拳皇和冒险岛素材的基于JavaSwing的动作类游戏,独创改编. 主要需求 拳皇迷迷糊糊醒来,发现自己在一间废弃的工厂里,地上爬满怪兽..这么可爱的怪兽,一拳下去,应该会哭很久吧~拳皇心里吐槽了下,向怪兽的怀抱冲了上去~~ 主要设计 1.游戏面板生成显示 2.背景选用冒险岛素材图 3.设计英雄,包含生命值,法术值,英雄的动作变化处理,英雄的技能特效 4.设计怪兽,包含怪物血量,攻击力,位置,步长等 5.

  • Java实现经典捕鱼达人游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 游戏窗体 鱼 鱼池类继承自Jpanel 总结 前言 <捕鱼达人>是一款以深海狩猎为题材的休闲竞技游戏.这是一场海底世界的远征,享受捕获大鱼的乐趣,但不是所有的鱼都是友善的,它们会用自己的方式保护自己,保卫属于自己的海底世界.当然,这里也是冒险与机遇共存的地方,诸多埋藏于海底的宝藏等待着被探寻. 游戏是用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 在鱼池中有很多鱼,鱼各自游动. 有一张渔网,随鼠标移动,点

  • C语言实现经典扫雷小游戏的示例代码

    目录 一.游戏简介 二.游戏实现 1.初始化棋盘 2.打印棋盘 3.布置雷 4.排查雷 三.源文件 1.game.h 2.game.c 3.Test.c 一.游戏简介 游戏初始界面有两个选择,选项“1”为开始游戏,选项“0”为退出游戏:选择开始游戏之后将会打印出9*9的棋盘,此时系统已经为游戏设置了10个雷,输入坐标之后将会打印出此坐标周围八个位置有几个雷,如果踩到了雷,那么游戏结束,打印出所有雷的位置. 二.游戏实现 1.初始化棋盘 void InitBoard(char board[ROWS

  • Java实现贪吃蛇大作战小游戏的示例代码

    目录 效果展示 项目介绍 项目背景 总体需求 实现过程 代码展示 项目结构 总结 大家好,今天尝试用swing技术写一个贪吃蛇大作战小游戏,供大家参考. 效果展示 效果展示 一.游戏界面 二.得分情况 项目介绍 项目背景 “贪吃蛇大作战”游戏是一个经典的游戏,它因操作简单.娱乐性强,自从计算机实现以来,深受广大电脑玩家的喜爱,本项目基于Java技术,开发了一个 操作简单.界面美观.功能较齐全 的“贪吃蛇”游戏.通过本游戏的开发,达到学习Java技术和熟悉软件开发流程的目的. 总体需求  本系统主

  • C语言实现猜数字小游戏的示例代码

    目录 一.猜数字小游戏的要求 二.猜数字小游戏实现的过程 2.1项目创建 2.2头文件内容 2.3源文件内容 三.猜数字小游戏调试结果如下 四.基于猜数字小游戏的总结 五.完整代码 一.猜数字小游戏的要求 猜数字小游戏是我们小时候喜欢我们一个经典小游戏,在本文中,猜数字小游戏主要的功能如下所示 1.登入猜数字小游戏系统,显示小时欢迎界面. 2.用户猜的数字有系统随机在1-20之间生成. 3.用户可以有5次机会猜这个随机生成的数字. 4.若用户猜大了,则系统会显示猜大了,并提示还有多少猜数字的机会

随机推荐