【问题标题】:C++ Text Adventure Game: 2D Array's as a mapC++ 文本冒险游戏:2D 数组作为地图
【发布时间】:2017-03-21 00:57:23
【问题描述】:

好的,我正在开发一款非常简单的文字冒险游戏。现在我有一个功能菜单和一个非常简单的“战斗模式”,它将尽快转换为随机遭遇。我现在遇到的问题是地图。

这个想法是使用 2D 阵列系统作为地图,并将位置存储为坐标(如果有意义的话)。您可以输入 4 个方向,“北、东、南和西”来四处移动,您可以去 3 种类型的地方。一片田野,一片森林,一座城堡。地图本身的大小是 5 x 6。

我想要做的是让你从坐标 2、2 的中心开始,然后你可以四处移动,例如一个字段(由顶部的整数指定的 1)它说“你在田野里”并为向城堡、森林等移动做类似的事情。我还希望它告诉玩家他们是否试图移动到标有 0 的位置,他们不能去那里停下来他们从移动。

有人有什么建议吗?

编辑:好的,所以我再次运行了我的程序,我已经设法摆脱了一些错误,但我还有很多错误(准确地说是 26 个,还有 1 个警告。) 有人愿意给我一些建议吗?

#pragma once
#include "Map.h"
#include <iostream>
using namespace std;

Map::Map()
{
}

void main()
{
    // Declare variables & functions
    int locationy;
    int locationx;

    char oper;
    char location;

    int pond = 0;
    int field = 1;
    int forest = 2;
    int castle = 3;

    int mapy;
    int mapx;
    int map;

    //These two values declare the start location on the array map for the player
    int mapy = 3;
    int mapx = 3;

    //These two variables track your current position on the map
    mapy = 2;
    mapx = 2;

    map[locationy][locationx];

    //Declares the amount of space within an array
    int map[6][6] = 
    {
        { 0, 0, 0, 0, 0, 0 },
        { 0, 2, 1, 2, 1, 0 },
        { 0, 1, 2, 1, 2, 0 },
        { 0, 1, 2, 2, 1, 0 },
        { 0, 1, 3, 1, 2, 0 },
        { 0, 0, 0, 0, 0, 0 }
    };

    //Asks the player where they want to go around the map
    cout << "Where to?" << endl;

    //Request for user to enter an direction (I.e., North, East, South, West.)
    cout << "Please choose  North, East, South or West" << endl;

    //Displays the inputted values
    cin >> oper;

    //Pauses system so we can see what the program does
    system("pause");

    //Checks input from the player
    if (cin == "North")
    {
        //Moves location upwards on the y axis
        if (map[locationx][locationy + 1] != 0) { locationy += 1; };
        else { cout << "That is water, dude. Swimming in platemail is NOT recommended.\n";
    }

    //Checks input from the player
        if (cin == "East")
    {
        //Moves location to the right on the x axis
        if (map[locationx + 1][locationy] != 0) { locationy += 1; };
        else { cout << "That is water, dude. Swimming in platemail is NOT recommended.\n";
    }

    //Checks input from the player
    if (cin == "South")
    {
        //Moves location downwards on the y axis
        if (map[locationx][locationy - 1] != 0) { locationy += 1; }
        else { cout << "That is water, dude. Swimming in platemail is NOT recommended.\n";
    }

    //Checks input from the player
    if (cin == "West")
    {
        //Moves location to the left on the x axis
        if (map[locationx - 1][locationy] != 0) { locationy += 1; }
        else { cout << "That is water, dude. Swimming in platemail is NOT recommended.\n"

    };
}

Map::~Map()
{
    ;
}

【问题讨论】:

  • if (map[locationx][locationy + 1] != 0) { locationy += 1; } else { cout &lt;&lt; "That is water, dude. Swimming in platemail is NOT recommended.\n" }
  • 嗨,欢迎来到 Stack Overflow!这种问题可能更适合这个网站:codereview.stackexchange.com。在 codereview 上,您可能会发现更多的人能够以从长远来看对您更有帮助的方式回答您的问题。
  • 感谢您的建议,我会在 40 分钟后立即尝试在那里发帖。与此同时,您有什么建议可以给我吗?
  • 在代码审查中,我们可以帮助您改进您的工作代码,但是这不是询问如何为您的代码编写新功能的好地方(这与“代码即还没有写”因此无法审查)。
  • 你会建议我在哪里询问如何编写新功能?

标签: c++ arrays visual-c++


【解决方案1】:

欢迎使用 C++。现在,您犯了初学者的基本错误,那就是编写命令式代码。 C++是面向对象的,这是使用它的关键。使您的代码更加有效。

在这种情况下,您应该首先确定您的项目有哪些部分,哪些东西可以被识别为类。您使用 Map 完成了此操作,但您错过的是创建一个包含所有内容的类,例如 Game。

这是我的建议,目前还有很多部分没有实现。

首先,代表某些地形的数字很难看。让我们用枚举替换它们:

enum class Terrain: unsigned char {
    pond = 0,
    field = 1,
    forest = 2,
    castle = 3
}

从现在开始,只要是指池塘,您就可以编写 Terrain::pond。使代码更具可读性。

现在是一个类布局(在 Game.h 中):

#include <vector>
using std::vector;

#include "Terrain.h"
//alternatively, define Terrain here instead of making it a separate header

class Game {

public:

    Game();

    Game(const string& file_name);
    //Game(file_name) is something you might want to do later -
    //create an instance of Game based on a file that contains a map,
    //maybe more data, instead of hard-coding such things

    void run();

private:

    vector<vector<Terrain> > map;
    //arrays are something you want to avoid. Use std::vector instead.

    unsigned int location_x, location_y;

    bool game_running;
    //aka not has ended

    void evaluate_step();
    //this method is repeated and repeated until the game ends
    //and contains the order in which every step is evaluated

    void handle_input();
    //Takes cin and does something with it

    void evaluate_position();
    //takes the position, gets the terrain from the position,
    //does what happens on that terrain

    void print();
    //prints the whole map to the console

    void set_default_map();
    //sets map to the data you have in your code,
    //to be used until reading from file is implemented
}

现在我们创建空构造函数来创建默认值(在 Game.cpp 中):

Game::Game(){

    set_default_map();
    location_x = ...;
    location_y = ...;
    game_running = true;
}

run 将简单地重复评估步骤:

void Game::run(){

    while(game_running){
        evaluate_step();
    }
}

void Game::evaluate_step(){

    handle_input();
    print();
    evaluate_terrain();
}

现在,我可以详细介绍一下,但我认为这应该让您对这种结构的外观有所了解。基本思想是为了获得清晰的概述,为了创造可读性而进行划分和划分。

如果您对这个提议的课程有任何疑问,我会编辑这个答案。

在你的 main 中,它会这样调用:

#include "Game.h"

int main(){

    Game game;
    game.run();
    return 0;
}

编辑:现在为您的代码错误:

  1. 您需要编写 int main 而不是 void main。 void main 看起来像 Java 语法。
  2. mapy 和 mapx 被声明了两次:int mapy;int mapy=3;。这是不允许的。要么删除第一个声明,要么让赋值不声明(int mapy;mapy=3;,尽管我更愿意删除第一个声明)。
  3. 不知道你想要什么map[locationy][locationx];。无论如何,它都不是有效的代码。
  4. map 也被声明了两次,有两种不同的类型(int 和 int[][])。删除第一个声明。
  5. 在您的所有 cin 案例中,您的条件格式错误。 if 括号后面不应该有分号,if(..){..}else{..},而不是if(..){..};else{..}。此外,在每种情况下,都缺少一个括号。
  6. mapx 和 locationx 有什么区别?已设置 mapx 但使用了 locationx?我删除了 mapx 和 mapy 并将 locationx 和 locationy 设置为 2。
  7. 未使用位置。有什么用?
  8. 您定义了池塘、田野等,但您不使用它。
  9. main 不需要 pragma 一次(也不需要守卫)
  10. 操作未使用。你可能想改变你的 if 条件,比如 if (oper == "North") 等等。为此,您需要更改操作的类型。一个 char 只能容纳一个字符。你可以使用 char*,但指针不好,所以最好使用字符串(你需要#include &lt;string&gt;
  11. 读入和评估只进行一次。执行一步,程序结束。不是你想要的,我猜 - 你可能想在它周围放一个循环,比如bool running = true; while(running){...}
  12. 对于任何方向,位置都以相同的方式更改。

改变了那些让它运行(但你真的应该改变设计):

#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;

void print_water_warning() {
    cout << "That is water, dude. Swimming in platemail is NOT recommended.\n";
}

int main() {
    // Declare variables & functions
    int locationy = 2;
    int locationx = 2;

    string oper;

    //Declares the amount of space within an array
    int map[6][6] = { {0, 0, 0, 0, 0, 0},
                      {0, 2, 1, 2, 1, 0},
                      {0, 1, 2, 1, 2, 0},
                      {0, 1, 2, 2, 1, 0}, 
                      {0, 1, 3, 1, 2, 0},
                      {0, 0, 0, 0, 0, 0} };

    while (true) {

        //Asks the player where they want to go around the map
        cout << "Where to?" << endl;

        //Request for user to enter an direction (I.e., North, East, South, West.)
        cout << "Please choose  North, East, South or West" << endl;

        //Displays the inputted values
        cin >> oper;

        //Checks input from the player
        if (oper.compare("North") == 0) {
            //Moves location upwards on the y axis
            if (map[locationx][locationy + 1] != 0) {
                locationy += 1;
            } else {
                print_water_warning();
            }
        }

        //Checks input from the player
        if (oper == "East") {
            //Moves location to the right on the x axis
            if (map[locationx + 1][locationy] != 0) {
                locationx += 1;
            } else {
                print_water_warning();
            }
        }

        //Checks input from the player
        if (oper == "South") {
            //Moves location downwards on the y axis
            if (map[locationx][locationy - 1] != 0) {
                locationy -= 1;
            } else {
                print_water_warning();
            }
        }

        //Checks input from the player
        if (oper == "West") {
            //Moves location to the left on the x axis
            if (map[locationx - 1][locationy] != 0) {
                locationx -= 1;
            } else {
                print_water_warning();
            }
        }
    } //end while
} //end main

【讨论】:

  • 对不起,我真的应该在我原来的帖子中添加这个,但我对 C++ 很陌生,而且你提到的很多东西我什至还没有学过。此外,尽管它很烦人,但它要求我的地图存储为一个数组,我怀疑我是否可以使用另一个系统。感谢您的帮助,但无论如何,我真的很感激,我一定会尝试一下,不管它是如何工作的。
  • @Thane_Mantis 啊,是的,要求。我建议你至少问问老师。也就是说,如果您希望我详细说明一些您迄今为止尚未听说过的概念,请特别询问。我的意思是,看起来你至少学到了一些关于课程的东西?还是 Map.h 没有解释?至于枚举,它们并不难,通过en.cppreference.com/w/cpp/language/enum 你应该了解它们是如何工作的。
  • @Thane_Mantis 编辑了我的答案,最后添加了代码错误列表。
猜你喜欢
  • 2016-08-25
  • 2013-07-08
  • 2021-07-14
  • 2016-03-25
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
相关资源
最近更新 更多