第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

用c++編寫一個(gè)迷宮游戲-(二)

標(biāo)簽:
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;
}

[完]

點(diǎn)擊查看更多內(nèi)容
3人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
全棧工程師
手記
粉絲
54
獲贊與收藏
183

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消