栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

用c++编写一个迷宫游戏-(二)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用c++编写一个迷宫游戏-(二)

再看一下之前的流程图,是不是编写了man.h与man.cpp后,一目了然,心中已经知道该如何写了呢?

先引入头文件并声明main函数与变量name:

#include 
#include 
#include "man.h"
#include "man.cpp"
using namespace std;
string name;
int main()
{
    return 0;
}

接着可以开始着手编写获得用户名的模块了,大家想必也对于输入输出流了如指掌了吧,在这里废话不说,直接上代码:

cout << "What's your name?" << endl;
cin >> name;

然后输入迷宫地图:

cout << "Hello," << name << ".Let's start this game!" << endl << "Target: go to the position (9,9)." << endl << "Now,write your map.(10 * 10)" << endl;
// input map
for (int i = 0 ; i < MAP_SIZE ; i++){
    cout << "The " << i << " line of map: ";
    for (int j = 0 ; j < MAP_SIZE ; j++){
 cin >> map[i][j];
    }
    cout << endl;
}

接着,在编写完class man后,创建小人就只需将类实例化。

MAN::man user(0,0,name[0]);

然后最好设定好目标,以便待会判断是否到达目的地。并声明存对象坐标的变量与接收输入的char类型变量。

POS target;
target.x = 9;
target.y = 9;
POS org;
char input;

然后编写一个while循环,作为游戏主循环。在里面获得用户输入,移动小人。

while (true)
{
    org = user.getPos();
    cout << "step: " << user.getStep() << " Position: (" << org.x << "," << org.y <<") input: ";
    cin >> input;
}

接着可以判断用户输入的指令,进行不同动作。

switch (input){
 case 'u':
     user.move(org.x,org.y - 1);
     break;
 case 'd':
     user.move(org.x,org.y + 1);
     break;
 case 'l':
     user.move(org.x - 1,org.y);
     break;
 case 'r':
     user.move(org.x + 1,org.y);
     break;
 default:
     update();
     cout << "input err!" << endl;
     break;
}

最后,可以编写一个won函数,表示胜利。

void won()
{
    char won[] = "won";
    int length = strlen(won);
    int pos = 0;
    for (int i = 0 ; i < MAP_SIZE ; i++){
 for (int j = 0 ; j < (MAP_SIZE - 1) ; j++){
     map[i][j] = won[pos];
     pos++;
     pos %= length;
 }
    }
    update();
    cout << "YOU WON!!!" << endl << "," << name << endl;
}

并判断是否达成目标

if (user.getPos().x == target.x && user.getPos().y == target.y){
    won();
    break;
}

main.cpp全部内容:

#include 
#include 
#include "man.h"
#include "man.cpp"
using namespace std;

string name;

void won();
int main()
{
    cout << "What's your name?" << endl;
    cin >> name;
    cout << "Hello," << name << ".Let's start this game!" << endl << "Target: go to the position (9,9)." << endl << "Now,write your map.(10 * 10)" << endl;
    // input map
    for (int i = 0 ; i < MAP_SIZE ; i++){
 cout << "The " << i << " line of map: ";
 for (int j = 0 ; j < MAP_SIZE ; j++){
     cin >> map[i][j];
 }
 cout << endl;
    }
    // create man
    MAN::man user(0,0,name[0]);
    // create target
    POS target;
    target.x = 9;
    target.y = 9;
    // create pos
    POS org;
    // create input
    char input;
    // main while
    while (true)
    {
 org = user.getPos();
 cout << "step: " << user.getStep() << " Position: (" << org.x << "," << org.y <<") input: ";
 cin >> input;
 switch (input){
     case 'u':
  user.move(org.x,org.y - 1);
  break;
     case 'd':
  user.move(org.x,org.y + 1);
  break;
     case 'l':
  user.move(org.x - 1,org.y);
  break;
     case 'r':
  user.move(org.x + 1,org.y);
  break;
     default:
  update();
  cout << "input err!" << endl;
  break;
 }
 if (user.getPos().x == target.x && user.getPos().y == target.y){
     won();
     break;
 }
    }
    return 0;
}
void won()
{
    char won[] = "won";
    int length = strlen(won);
    int pos = 0;
    for (int i = 0 ; i < MAP_SIZE ; i++){
 for (int j = 0 ; j < (MAP_SIZE - 1) ; j++){
     map[i][j] = won[pos];
     pos++;
     pos %= length;
 }
    }
    update();
    cout << "YOU WON!!!" << endl << "," << name << endl;
}

[完]

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号