当前位置:   article > 正文

用easyX图形库实现马踏棋盘动画_马踏棋盘 动态

马踏棋盘 动态

马踏棋盘

#undef UNICODE
#undef _UNICODE					//取消unicode的定义
#include<stdio.h>
#include<graphics.h>
#include<windows.h>
#include<stdlib.h>
#define Row 8
#define Col 8
#define maxStep 64
#define White 1
#define Yellow 0
typedef struct QI {
	int x;
	int y;
	int i;
	int j;
}QI;
typedef struct {
	int abscissa;  //横坐标
	int ordinate;  //纵坐标
	int direction;  //方向
}SqStack;

int ChessBoard[Row + 1][Col + 1] = {}; //储存路径的棋盘
//分别有(1 ~ 8)个方向
int HTry1[8] = { 1, -1, -2, 2, 2, 1, -1, -2 };
int HTry2[8] = { 2, -2, 1, 1, -1, -2, 2, -1 };

SqStack PointStack[maxStep];
int top = -1; //栈顶
int flagOperate = 0; //操作标记
int num = 0;  //记录结果数




int Q[8][8] = { {1,0,1,0,1,0,1,0},
				{0,1,0,1,0,1,0,1},
				{1,0,1,0,1,0,1,0},
				{0,1,0,1,0,1,0,1},
				{1,0,1,0,1,0,1,0},
				{0,1,0,1,0,1,0,1},
				{1,0,1,0,1,0,1,0},
				{0,1,0,1,0,1,0,1}, };
int start_x = 0;
int start_y = 0;
int Re_i = 0;
int Re_j = 0;
int Window_Wide = 900;
int Window_High = 700;
int qipan_start = 30;
int qipan_end = 670;
int qige_wide = 80;
int menu_start = 700;
int menu_end = 870;
int step = 0;
void basic();
void marke(int x, int y);
void Show(int x, int y, int flag);
void Reback(int x, int y, int flag);
int transmit_x_to_j(int x);
int transmit_j_to_x(int x);

void printChessBoard();
void push(int abscissa, int ordinate);
void pop();
void printStack();
void markChessBoard(int abscissa, int ordinate);
void runChessBoard();
void InitStartPoint();
void marke(int x, int y);
int main() {
	int x;
	int y;
	int i;
	int j;
	int flag;
	int MA = 0;
	initgraph(Window_Wide, Window_High, SHOWCONSOLE);
	basic();
	MOUSEMSG m;		// 定义消息变量
	while (!MA)
	{
		m = GetMouseMsg();
		switch (m.uMsg)
		{
		case WM_LBUTTONDOWN:
			if (m.x >= qipan_start && m.x <= qipan_end && m.y >= qipan_start && m.y <= qipan_end) {
				j = transmit_x_to_j(m.x);
				i = transmit_x_to_j(m.y);
				flag = Q[i][j];
				x = transmit_j_to_x(j);
				y = transmit_j_to_x(i);
				printf("%d,%d", i + 1, j + 1);
				Show(x, y, flag);

				push(j + 1, i + 1);
				markChessBoard(j + 1, i + 1);
				runChessBoard();
				MA = 1;//标志马的初始位置已经确定
				break;
			}
			else
				;
		}
	}
	getchar();
}
void basic() {
	IMAGE img;
	int left = qipan_start;
	int right = qipan_end;
	int top = qipan_start;
	int bottom = qipan_end;
	setlinecolor(RED);
	int x = 0, y = 0;
	x = left;
	y = top;
	loadimage(&img, _T("棋盘.png"), 640, 640);
	putimage(30, 30, &img);
	rectangle(left, top, right, bottom);
	for (int i = 0; i < 7; i++) {
		x += qige_wide;
		y += qige_wide;
		line(x, top, x, bottom);
		line(left, y, right, y);
	}


	setcolor(WHITE);
	setfillcolor(RGB(250, 228, 178));
	fillrectangle(0, 0, 900, 30);
	fillrectangle(0, 30, 30, 670);
	fillrectangle(870, 30, 900, 700);//四周边框设计
	fillrectangle(670, 30, 700, 700);
	fillrectangle(0, 670, 900, 700);
	setfillcolor(RGB(255, 205, 134));
	fillrectangle(700, 30, 870, 670);
	setcolor(RGB(209, 139, 25));
	rectangle(700, 30, 870, 670);
}
void Show(int x, int y, int flag) {
	IMAGE img1;
	IMAGE img2;
	IMAGE img3;
	step++;
	loadimage(&img1, _T("马1.png"), qige_wide - 20, qige_wide - 20);
	loadimage(&img2, _T("马2.png"), qige_wide - 20, qige_wide - 20);
	if (flag == Yellow) {
		putimage(x + 10, y + 10, &img1);
		Re_i = transmit_x_to_j(y);
		Re_j = transmit_x_to_j(x);
	}
	else if (flag == White)
	{
		putimage(x + 10, y + 10, &img2);
		Re_i = transmit_x_to_j(y);
		Re_j = transmit_x_to_j(x);
	}
	else
		printf("Show函数出错了!!!\n");
	Sleep(100);
	Reback(x, y, flag);
	return;
}
void Reback(int x, int y, int flag) {
	IMAGE img1, img2;
	loadimage(&img1, _T("Reback1.png"), qige_wide - 20, qige_wide - 20);
	loadimage(&img2, _T("Reback2.png"), qige_wide - 20, qige_wide - 20);
	if (flag == White)
		putimage(x + 10, y + 10, &img2);
	else if (flag == Yellow)
		putimage(x + 10, y + 10, &img1);
	else
		printf("Reback出错了!!!\n");
	marke(x, y);
	return;
}
int transmit_x_to_j(int x) {
	int j = 0;
	j = (x - qipan_start) / qige_wide;
	return j;
}
int transmit_j_to_x(int j) {
	int x = 0;
	x = qipan_start + j * qige_wide;
	return x;
}
//int caculate(int x, int y) {

//}
void marke(int x, int y) {
	char str[5] = { "0" };
	int num_x;
	int num_y;
	num_x = x + 1 * qige_wide / 5;
	num_y = y + qige_wide / 5;
	setbkmode(TRANSPARENT);
	settextcolor(BLACK);
	settextstyle(3 * qige_wide / 5, 0, _T("Consolas"));
	sprintf_s(str, "%d", step);
	outtextxy(num_x, num_y, str);
}

void printChessBoard() {
	char str[5];
	int num_x;
	int num_y;
	int flag = 0;
	setbkmode(TRANSPARENT);
	settextcolor(BLACK);
	settextstyle(3 * qige_wide / 4, 0, _T("Consolas"));
	printf("棋盘路径是:\n");
	while(step!=64){
		for (int i = 1; i <= Row; i++) {
			for (int j = 1; j <= Col; j++) {
				printf("%5d ", ChessBoard[i][j]);

				if (ChessBoard[i][j] == step+1) {
					num_x = transmit_j_to_x(j-1);
					num_y = transmit_j_to_x(i-1);
					flag = Q[i - 1][j - 1];
					Show(num_x, num_y, flag);
				}

			}
			printf("\n");
		}
		}
		printf("\n\n");
}
//入栈
void push(int abscissa, int ordinate) {
	++top;
	PointStack[top].abscissa = abscissa;
	PointStack[top].ordinate = ordinate;
	PointStack[top].direction = -1; //初始化方向
}

//出栈
void pop() {
	PointStack[top].abscissa = 0;
	PointStack[top].ordinate = 0;
	PointStack[top].direction = -1; //初始化方向
	--top;
}

//标记棋盘
void markChessBoard(int abscissa, int ordinate) {

	ChessBoard[ordinate][abscissa] = top + 1;
}


void runChessBoard() {
	int xNow, yNow; //当前马所在的坐标

	while (1) {

		if (top == maxStep - 1) {
			printChessBoard();
			break;
		}
		xNow = PointStack[top].abscissa;
		yNow = PointStack[top].ordinate;

		//对下面八个方向重新排序,出口最少的优先
		int count[8] = {};
		for (int i = 0; i < 8; i++) {
			int xNext = xNow, yNext = yNow;
			xNext += HTry1[i];
			yNext += HTry2[i];
			if (xNext > 0 && xNext <= Col && yNext > 0 && yNext <= Row && ChessBoard[yNext][xNext] == 0) {
				for (int j = 0; j < 8; j++) {
					int xNextNext = xNext, yNextNext = yNext;
					xNextNext += HTry1[j];
					yNextNext += HTry2[j];
					if (xNextNext > 0 && xNextNext <= Col && yNextNext > 0 && yNextNext <= Row && ChessBoard[yNextNext][xNextNext] == 0) {
						count[i]++;
					}
				}
			}
		}


		//对方向进行排序,实际要走的方向记录在directionNext中
		int directionNext[8] = {};
		int temp = 9;
		int  k = 0;
		for (int i = 0; i < 8; i++) {
			temp = 9;

			for (int j = 0; j < 8; j++) {

				if (count[j] < temp) {
					directionNext[i] = j;
					temp = count[j];
					k = j;
				}

			}
			count[k] = 9;
		}
		//走下一步
		int direnow = 0;
		int i = 1;
		for (direnow = PointStack[top].direction + 1; direnow < 8; direnow++) {
			int xRealNext = xNow, yRealNext = yNow;
			xRealNext += HTry1[directionNext[direnow]];
			yRealNext += HTry2[directionNext[direnow]];

			PointStack[top].direction += 1;

			if (xRealNext <= Col && xRealNext > 0 && yRealNext <= Row && yRealNext > 0 && ChessBoard[yRealNext][xRealNext] == 0) {
				push(xRealNext, yRealNext);
				//                标记在棋盘路径中 
				markChessBoard(xRealNext, yRealNext);
				break;
			}
		}
		//判断无路可走出栈
		if (PointStack[top].direction >= 7) {
			int x, y;
			x = PointStack[top].abscissa;
			y = PointStack[top].ordinate;
			ChessBoard[y][x] = 0; //棋盘标记取消
			pop();
		}
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/94148
推荐阅读
相关标签
  

闽ICP备14008679号