【C++ 程序】 移动迷宫游戏

这个移动迷宫(Running Maze)程序为两玩家竞技项目。目标为到达出口。

程序

请使用(C++/11)标准编译该程序。

// This is a Running Maze game program.

#include <iostream>
#include <vector>
#include <conio.h>
#include <Windows.h>
using namespace std;

struct location {
    
    
	unsigned x, y;
}xl, ol; // the location of X and O

int win = 0;
char c1, c2, c;

vector<vector<char>> p = {
    
     // define the maze
{
    
    ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', '<', '-', 'E', 'X', 'I', 'T', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{
    
    ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', '3', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{
    
    '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '4', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
{
    
    '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', '*', ' ', ' ', '*', '4', '*', ' ', '*', ' ', ' ', ' ', ' ', '*'},
{
    
    '*', ' ', '*', '*', '*', ' ', '*', ' ', '*', '*', '*', ' ', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '1', '*', '*', '*', ' ', '*', '*', '3', '*', ' ', '*', ' ', '*', '*', ' ', '*', ' ', '*'},
{
    
    '*', '4', '*', ' ', '*', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', '2', ' ', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ', ' ', '*', ' ', ' ', ' ', '2', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*'},
{
    
    '*', ' ', '*', ' ', '*', '*', '*', '*', '*', ' ', '*', '*', ' ', '*', ' ', '*', ' ', '*', '3', '*', ' ', '*', '3', ' ', ' ', ' ', ' ', '*', '*', ' ', '*', '*', '3', '1', '2', '*', ' ', '*', '*', '*'},
{
    
    '*', ' ', '*', ' ', '*', ' ', '3', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', '*', '3', '*', '*', '*', '*', '3', '*', '*', '*', ' ', '*', ' ', ' ', ' ', '*', '*', ' ', '2', ' ', '*'},
{
    
    '*', ' ', '1', ' ', '*', ' ', '*', '*', ' ', '*', ' ', ' ', ' ', '3', ' ', '*', '*', '*', ' ', '*', ' ', ' ', '*', ' ', '*', ' ', ' ', '*', '*', ' ', '*', '*', '*', ' ', '*', ' ', ' ', '*', ' ', '*'},
{
    
    '*', ' ', '*', ' ', '*', ' ', '*', '*', ' ', '*', '*', '3', '*', '*', '*', '*', ' ', '*', ' ', '*', '*', '*', '*', '4', '*', ' ', '*', '*', '*', ' ', ' ', '2', ' ', ' ', '*', ' ', '*', '*', '4', '*'},
{
    
    '*', ' ', '*', ' ', ' ', ' ', '3', ' ', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', '*', ' ', '*', '*', ' ', ' ', '*', ' ', '*'},
{
    
    '*', ' ', '*', ' ', '*', '*', '*', '*', ' ', '3', ' ', '3', ' ', '*', ' ', ' ', ' ', '*', '*', '*', '*', '2', '*', '*', '*', ' ', '*', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', '*', ' ', '*'},
{
    
    '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', '*', ' ', '2', ' ', '*', '*', '*', ' ', ' ', '2', ' ', '*', ' ', ' ', ' ', '*', ' ', '*', '*', '1', '*', '*', '*', ' ', '*', ' ', '*', ' ', '*', ' ', '*'},
{
    
    '*', ' ', '*', ' ', ' ', ' ', '*', ' ', '*', '*', ' ', '1', ' ', '*', ' ', '*', '*', ' ', '1', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', '3', ' ', '*', '*', '*', ' ', '*', ' ', '*'},
{
    
    '*', ' ', '*', '*', '*', '*', '*', ' ', '*', ' ', ' ', '*', '1', '3', ' ', '*', '*', '*', '*', '*', ' ', ' ', '*', ' ', '*', '*', '*', ' ', '*', '*', '*', '*', ' ', '3', '*', ' ', ' ', '*', '1', '*'},
{
    
    '*', ' ', ' ', ' ', '*', ' ', '2', ' ', '*', ' ', ' ', '*', '*', '*', ' ', '2', ' ', ' ', '4', '*', '*', '*', '*', ' ', '*', ' ', ' ', ' ', '*', ' ', ' ', '*', ' ', ' ', ' ', ' ', '2', '*', ' ', '*'},
{
    
    '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', '*', ' ', ' ', ' ', '*', '*', ' ', ' ', ' ', ' ', ' ', '1', ' ', '*', ' ', '*', '*', '*', '4', '*', '*', ' ', '*', ' ', '*', '*', '*', ' ', '*'},
{
    
    '*', '*', '*', ' ', '4', ' ', '*', '3', '*', '*', '4', '*', ' ', '*', '*', '*', ' ', ' ', '*', '4', '*', ' ', '*', '*', '*', ' ', '4', ' ', '*', ' ', ' ', ' ', ' ', '*', ' ', ' ', '3', '*', ' ', '*'},
{
    
    '*', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', '*', '3', '*', ' ', '*', ' ', '*', ' ', '1', ' ', '*', ' ', '*', '*', '*', '*', '*', ' ', '*', '*', ' ', '*'},
{
    
    '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', '*', '*', '*', ' ', '*', ' ', '*', ' ', '*', ' ', '3', ' ', '*', ' ', ' ', '*', ' ', ' ', ' ', '3', ' ', '*'},
{
    
    '*', ' ', '*', '*', '*', '3', '*', ' ', '*', ' ', '*', '*', ' ', '*', ' ', '*', '3', '*', '*', ' ', '*', ' ', '*', ' ', '1', ' ', '*', ' ', ' ', ' ', '*', ' ', '*', '*', '3', '*', ' ', '*', ' ', '*'},
{
    
    '*', ' ', '2', ' ', ' ', ' ', '1', ' ', '*', ' ', ' ', ' ', ' ', '*', ' ', ' ', ' ', '*', '4', ' ', '*', ' ', '1', ' ', '*', ' ', '*', ' ', '*', ' ', '4', ' ', '*', '2', ' ', '1', ' ', ' ', ' ', '*'},
{
    
    '*', ' ', '*', ' ', '*', '*', '*', '*', '*', '*', '4', '*', '3', '*', '*', '*', ' ', ' ', ' ', ' ', ' ', '*', '*', ' ', '2', ' ', '*', '*', '*', '3', '*', ' ', ' ', ' ', ' ', '*', '*', '*', '1', '*'},
{
    
    '*', ' ', '*', ' ', ' ', ' ', '*', ' ', '*', '*', ' ', '*', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', '1', '*', ' ', ' ', '*', '*', '*', ' ', '*', ' ', ' ', ' ', '*', '1', ' ', ' ', ' ', '*', ' ', '*'},
{
    
    '*', '1', '*', '*', '*', ' ', ' ', ' ', ' ', '2', ' ', '*', ' ', '*', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', '*', '*', '*', ' ', ' ', ' ', ' ', ' ', '1', '*', '*', '2', '*', '*', ' ', '*', ' ', '*'},
{
    
    '*', ' ', '3', ' ', '*', '*', '*', '*', ' ', '*', ' ', '*', ' ', '*', '*', '*', ' ', '*', ' ', '2', ' ', ' ', '*', ' ', '*', ' ', '*', '*', '1', '2', '3', '*', ' ', ' ', '*', ' ', ' ', '*', ' ', '*'},
{
    
    '*', ' ', ' ', ' ', '2', ' ', '*', '*', '2', '*', ' ', '*', '*', '*', ' ', '*', ' ', '*', '3', '*', '*', ' ', '1', ' ', '3', ' ', '1', '*', ' ', '*', ' ', '*', ' ', '*', '*', '2', '*', '*', ' ', '*'},
{
    
    '*', 'X', 'O', ' ', '*', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', '4', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', ' ', ' ', '1', ' ', '2', '*', ' ', ' ', ' ', '3', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*'},
{
    
    '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'} };

vector<vector<char>> p0 = p;

vector<location> l1;
vector<location> l2;
vector<location> l3;
vector<location> l4;

void l()
{
    
    
	for (int i = 0; i != 29; i++)
	{
    
    
		for (int j = 0; j != 40; j++)
		{
    
    
			if (p0[i][j] == '1')
			{
    
    
				location temp;
				temp.x = i;
				temp.y = j;
				l1.push_back(temp);
			}
			if (p0[i][j] == '2')
			{
    
    
				location temp;
				temp.x = i;
				temp.y = j;
				l2.push_back(temp);
			}
			if (p0[i][j] == '3')
			{
    
    
				location temp;
				temp.x = i;
				temp.y = j;
				l3.push_back(temp);
			}
			if (p0[i][j] == '4')
			{
    
    
				location temp;
				temp.x = i;
				temp.y = j;
				l4.push_back(temp);
			}
		}
	}
}

vector<vector<char>> change(vector<vector<char>> p, int n)
{
    
    
	l(); // call l1, l2, l3, l4
	if (n % 2 == 0)
	{
    
    
		for (auto c : l1)
		{
    
    
			p[c.x][c.y] = ' ';
		}
		for (auto c : l2)
		{
    
    
			p[c.x][c.y] = '*';
		}
    }
	else
	{
    
    
		for (auto c : l2)
		{
    
    
			p[c.x][c.y] = ' ';
		}
		for (auto c : l1)
		{
    
    
			p[c.x][c.y] = '*';
		}
	}
	if (n % 3 != 0)
	{
    
    
		for (auto c : l3)
		{
    
    
			p[c.x][c.y] = ' ';
		}
	}
	else
	{
    
    
		for (auto c : l3)
		{
    
    
			p[c.x][c.y] = '*';
		}
	}
	if (n % 7 != 2 && n % 7 != 4)
	{
    
    
		for (auto c : l4)
		{
    
    
			p[c.x][c.y] = ' ';
		}
	}
	else
	{
    
    
		for (auto c : l4)
		{
    
    
			p[c.x][c.y] = '*';
		}
	}
	return p;
}

vector<vector<char>> my_up(vector<vector<char>> p, int n)
{
    
    
	if (n == 1)
	{
    
    
		--xl.x;
		if (p[xl.x][xl.y] != ' ') win = 2;
		else // change the place 
		{
    
    
			p[xl.x + 1][xl.y] = ' ';
			p[xl.x][xl.y] = 'X';
		}
	}
	if (n == 2)
	{
    
    
		--ol.x;
		if (p[ol.x][ol.y] != ' ') win = 1;
		else // change the place 
		{
    
    
			p[ol.x + 1][ol.y] = ' ';
			p[ol.x][ol.y] = 'O';
		}
	}
	return p;
}

vector<vector<char>> my_down(vector<vector<char>> p, int n)
{
    
    
	if (n == 1)
	{
    
    
		++xl.x;
		if (p[xl.x][xl.y] != ' ') win = 2;
		else // change the place 
		{
    
    
			p[xl.x - 1][xl.y] = ' ';
			p[xl.x][xl.y] = 'X';
		}
	}
	if (n == 2)
	{
    
    
		++ol.x;
		if (p[ol.x][ol.y] != ' ') win = 1;
		else // change the place 
		{
    
    
			p[ol.x- 1][ol.y ] = ' ';
			p[ol.x][ol.y] = 'O';
		}
	}
	return p;
}

vector<vector<char>> my_left(vector<vector<char>> p, int n)
{
    
    
	if (n == 1)
	{
    
    
		--xl.y;
		if (p[xl.x][xl.y] != ' ') win = 2;
		else // change the place 
		{
    
    
			p[xl.x][xl.y + 1] = ' ';
			p[xl.x][xl.y] = 'X';
		}
	}
	if (n == 2)
	{
    
    
		--ol.y;
		if (p[ol.x][ol.y] != ' ') win = 1;
		else // change the place 
		{
    
    
			p[ol.x][ol.y + 1] = ' ';
			p[ol.x][ol.y] = 'O';
		}
	}
	return p;
}

vector<vector<char>> my_right(vector<vector<char>> p, int n)
{
    
    
	if (n == 1)
	{
    
    
		++xl.y;
		if (p[xl.x][xl.y] != ' ') win = 2;
		else // change the place 
		{
    
    
			p[xl.x][xl.y - 1] = ' ';
			p[xl.x][xl.y] = 'X';
		}
	}
	if (n == 2)
	{
    
    
		++ol.y;
		if (p[ol.x][ol.y] != ' ') win = 1;
		else // change the place 
		{
    
    
			p[ol.x][ol.y - 1] = ' ';
			p[ol.x][ol.y] = 'O';
		}
	}
	return p;
}

vector<vector<char>> X_input(char c1,char c2)
{
    
    
	c1 = _getch();
	c2 = _getch();
	switch (c2)
	{
    
    
	case 72: // up
		return my_up(p, 1);
		break;
	case 80: // down
		return my_down(p, 1);
		break;
	case 75: // left
		return my_left(p, 1);
		break;
	case 77: // right
		return my_right(p, 1);
		break;
	default:
		return p;
		break;
	}
}

vector<vector<char>> O_input(char c)
{
    
    
	c = _getch();
	c = toupper(c);
	switch (c)
	{
    
    
	case 'W': // up
		return my_up(p, 2);
		break;
	case 'S': // down
		return my_down(p, 2);
		break;
	case 'A': // left
		return my_left(p, 2);
		break;
	case 'D': // right
		return my_right(p, 2);
		break;
	default:
		return p;
		break;
	}
}

void print(vector<vector<char>> q)
{
    
    
	for (int i = 0; i != 29; i++)
	{
    
    
		for (int j = 0; j != 40; j++)
			cout << q[i][j] << " ";
		cout << endl;
	}
}

int win_lose(vector<vector<char>> q)
{
    
    
	if (q[0][13] == 'X') return 1; // X wins
	else if (q[0][13] == 'O') return 2; // O xins
	else return 0; // unfinished
}

int main()
{
    
    
	cout << "This is a Running Maze game program.\nProgrammer:Teddy van Jerry\n" << endl;
	Sleep(300);
	cout << "Now Loading";
	for (int i = 0; i != 6; i++)
	{
    
    
		Sleep(200);
		cout << ".";
	}
	Sleep(500); // stop for a while
	xl.x = 27; xl.y = 1;
	ol.x = 27; ol.y = 2;
	cout << "\n\nPlayer X moves first using ↑,↓,←,→." << endl;
	cout << "Player O moves second using W,S,A,D." << endl;
	cout << "Other operations will be regarded as 'skip'." << endl;
	cout << "Your aim is to reach the exit without bumping into anything else including your opponents." << endl;
	cout << "You should know that the maze is changing all the time." << endl;
	cout << "When you are eaten by the moving wall, don't worry, you are OK!" << endl;
	Sleep(12500); // time for reading the instruction
	cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << endl;
//	print(p0); // this is used to see your maze after designing it
	for (unsigned alr = 0; win == 0; alr++)
	{
    
    
		p = change(p, alr);
		print(p);
		char player = (alr % 2) ? 'O' : 'X';
		cout << "Player " << player << ", please make your move.";
		p = (alr % 2) ? O_input(c) : X_input(c1, c2);
		cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << endl;
		if (win_lose(p) != 0)
			break;
	}
	print(p);
	if (win == 1 || win_lose(p) == 1)
		cout << "\nConratulations! The winner is X!" << endl;
	else cout << "\nConratulations! The winner is O!" << endl;
	cout << "\nALL RIGHTS RESERVED (c) 2020 Teddy van Jerry" << endl;
	char anything;
	cin >> anything;
	return 0;
}

// ALL RIGHTS RESERVED (c) 2020 Teddy van Jerry

输出示例

(可以看出迷宫一直在变)
1
2
3
4
5

分析

  • 从头到尾思路还是比较清晰的,直接看 main 即可。下面讨论一些难点。
  • 1.Sleep()函数再库Windows.h里,可以让程序暂停一会。
  • 2.change()函数即定义移动的部分的处理。之前已经将那些会时隐时现的墙的位置(location)存好在 vector 里了,此处只要调用,并对其所属同余类进行讨论(即讨论除以某数的余数)(迷宫每42步才会重复一次)。
  • 3.控制移动用了在库conio.h中的_getch()(原来搜到是的是getch(),但是在C++/11中会报错,根据提示改成这个)函数,不输入直接判断键盘按键。尤其关注对方向键的读取方式。(网上有好多是错的,把我给害惨了)
  • 4.然后获胜情况包括到达出口或者对手撞墙(这个经常发生啊,一不小心就完蛋了)。
  • 5.351-352行本无意义,目的在于单独使用 exe 文件时有时结束游戏时会直接退出不显示最后结果。加上这句无用的话即可解决。详见我的博客 关于 C++中 程序在其他设备上运行 的思考
  • 其他应该比较好理解吧。
  • 对于迷宫大家也可以自己设计,对此我没有好好研究该怎么布置比较好。(但我知道画这个迷宫弄了我半天)。

ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。


See also

Teddy van Jerry 的导航页
【C++ 程序】 井字棋游戏(人 VS 人)
【C++ 程序】 井字棋游戏(人 VS Lv1电脑)
【C++ 程序】 井字棋游戏(人 VS Lv2电脑)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)
【C++ 程序】 五子棋游戏(人 VS 人)
【C++ 程序】 随机数
【C++ 程序】 贪吃蛇游戏
【C++ 程序】 数字推盘游戏(15-puzzle)
【C++ 程序】 2048游戏
【C++ 程序】 井字棋游戏(人 VS 人)(EasyX 图形界面)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)(EasyX 图形界面)

猜你喜欢

转载自blog.csdn.net/weixin_50012998/article/details/108410413
今日推荐