C ++文本冒险游戏:2D数组作为地图

时间:2017-03-21 00:57:23

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

好的,所以我正在开发一个非常简单的Text Adventure游戏。现在我有一个功能正常的菜单,以及一个非常简单的“战斗模式”,它将尽快转换为随机遭遇。我现在遇到麻烦的是地图。

我们的想法是使用2D阵列系统作为地图,将位置存储为坐标(如果有意义的话。)您可以输入4个方向,“北,东,南和西”来移动,你可以去3种类型的地方。田野,森林和城堡。而地图本身的尺寸是5乘6。

我想要做的是让你从坐标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()
{
    ;
}

1 个答案:

答案 0 :(得分: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();
}

现在,我可以详细介绍一下,但我认为这应该可以让您了解这样的结构是如何形成的。基本思想是划分和划分以获得清晰的概述,以创造可读性。

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

在你的主体中,它会被称为:

#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-bracket之后不应该有分号if(..){..}else{..},而不是if(..){..};else{..}。此外,在每种情况下,都缺少一个括号。
  6. mapx和locationx有什么区别? mapx已设置但是使用了locationx?我删除了mapx和mapy,并将locationx和locationy设置为2。
  7. 未使用位置。这是为了什么?
  8. 您定义池塘,田地等,但不要使用它。
  9. main不需要一次编译指示(也不需要警卫)
  10. oper未使用。您可能希望更改if条件,如if(oper ==“North”)等等。为此,您需要更改操作类型。 char只能容纳一个字符。你可以使用char *,但是指针很糟糕,所以最好用字符串(你需要#include <string>
  11. 阅读和评估只进行一次。做一步,程序结束。不是你想要的,我想 - 你可能想绕过它,比如bool running = true; while(running){...}
  12. 对于任何方向,位置都以相同方式更改。
  13. 更改了它们以使其运行(但您确实应该真正改变设计):

    #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