手记

用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人推荐
随时随地看视频
慕课网APP

热门评论

您好,为什么我使用您的代码,运行会报错

错误 7 error LNK2005: "void __cdecl update(void)" (?update@@YAXXZ) 已经在 main.obj 中定义 E:\lianxi\ConsoleApplication1\ConsoleApplication1\man.obj ConsoleApplication1

感谢分享,步骤讲解的很详细,受教了?

查看全部评论