为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
C C++

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

流程图

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

#include <iostream>
#include <cstring>
#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 <iostream>
#include <cstring>
#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;
}

[完]

点击查看更多内容
3人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
54
获赞与收藏
183

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消