消息=消息; 此->eventID=eventID; } //函数将坐标作为包含2个元素的向量返回 向量getCoords() { 向量坐标; 坐标。推回(X); 协调。推回(Y); 返回坐标; } //函数获取消息; 字符串getMessage() { 返回消息; } //函数来获取eventID int getEventID() { 返回事件ID; } }; //类,即游戏对象本身,在主“runner”中实现 阿托里亚级 { 私人: int-temp坐标[2]; int坐标[2]; 玩家; 敌我; 布尔主动敌; //这是我最初尝试的,但在运行时它抛出了一个错误的alloc //当我第一次尝试在populateMap()中为它赋值时 //MapTile地图[10][10]; //正在为地图做些什么 boost::multi_数组map2{boost::extensts[10][10]}; 向量项; 矢量武器; 矢量装甲; 随机发生器; 布尔playerded; 公众: //默认构造函数 阿托里亚() { populateItems(); populateMap(); 主动敌=假; startGame(); } //函数,该函数通过填充MapTile对象的多维数组来创建整个地图 void populateMap() { map2[0][0]=MapTile(0,0,“你不能走这条路。”,-1); //省略了其他指定以节省字符空间 map2[9][9]=MapTile(9,9,“你在炼金术士的实验室里!”,3); } //用于评估用户输入的函数 字符串求值输入(字符串用户输入) { boost::algorithm::to_lower(用户输入); 如果(用户输入==“北”) { 如果(!主动敌人) { //向南移动,以便向Y坐标添加一个 坐标[1]=(坐标[1]+1); } 其他的 { cout,c++,visual-c++,boost,C++,Visual C++,Boost" /> 消息=消息; 此->eventID=eventID; } //函数将坐标作为包含2个元素的向量返回 向量getCoords() { 向量坐标; 坐标。推回(X); 协调。推回(Y); 返回坐标; } //函数获取消息; 字符串getMessage() { 返回消息; } //函数来获取eventID int getEventID() { 返回事件ID; } }; //类,即游戏对象本身,在主“runner”中实现 阿托里亚级 { 私人: int-temp坐标[2]; int坐标[2]; 玩家; 敌我; 布尔主动敌; //这是我最初尝试的,但在运行时它抛出了一个错误的alloc //当我第一次尝试在populateMap()中为它赋值时 //MapTile地图[10][10]; //正在为地图做些什么 boost::multi_数组map2{boost::extensts[10][10]}; 向量项; 矢量武器; 矢量装甲; 随机发生器; 布尔playerded; 公众: //默认构造函数 阿托里亚() { populateItems(); populateMap(); 主动敌=假; startGame(); } //函数,该函数通过填充MapTile对象的多维数组来创建整个地图 void populateMap() { map2[0][0]=MapTile(0,0,“你不能走这条路。”,-1); //省略了其他指定以节省字符空间 map2[9][9]=MapTile(9,9,“你在炼金术士的实验室里!”,3); } //用于评估用户输入的函数 字符串求值输入(字符串用户输入) { boost::algorithm::to_lower(用户输入); 如果(用户输入==“北”) { 如果(!主动敌人) { //向南移动,以便向Y坐标添加一个 坐标[1]=(坐标[1]+1); } 其他的 { cout,c++,visual-c++,boost,C++,Visual C++,Boost" />

Boost multi_阵列Boost_断言已抛出断点 我正在为我的C++类编写一个基于文本的地牢爬虫式游戏,我遇到一个运行时错误,说明Boost断言触发了断点。我用C语言做了一个非常相似的项目,所以我认为这是和C++有关的,这是导致我的问题的原因。 这是我的密码: ArtoriaClasses.h #pragma once //Jake Farley //CSC275 //Artoria (Final) //11/25/17 #include <iostream> #include <sstream> #include <memory> #include <boost/random/mersenne_twister.hpp> #include <boost/random/uniform_int_distribution.hpp> #include <boost/algorithm/string.hpp> #include <string> #include <ctime> #include <vector> #include <fstream> #include "boost/multi_array.hpp" #include <cassert> using namespace std; //Class representing one tile of the map class MapTile { private: int X; int Y; string message; int eventID; public: //Default Constructor MapTile() { } //Constructor with minimum arguments MapTile(int x, int y) { X = x; Y = y; message = "You are unable to go that way."; eventID = -1; } //Constructor with all arguments MapTile(int x, int y, string message, int eventID) { X = x; Y = y; this->message = message; this->eventID = eventID; } //function to return coords as a vector with 2 elements vector<int> getCoords() { vector<int> coords; coords.push_back(X); coords.push_back(Y); return coords; } //function to get the message; string getMessage() { return message; } //function to get the eventID int getEventID() { return eventID; } }; //Class that is the game object itself, is implemented in the main "runner" class Artoria { private: int tempCoordinates[2]; int coordinates[2]; Player player; Enemy currentEnemy; bool activeEnemy; //This is what I tried originally but it throws a bad_alloc at runtime //when I first try to assign it a value in populateMap() //MapTile map[10][10]; //Trying something for map boost::multi_array<MapTile, 2> map2 { boost::extents[10][10] }; vector<Item> looseItems; vector<Weapon> weapons; vector<Armor> armors; Random randomGen; bool playerDied; public: //Default Constructor Artoria() { populateItems(); populateMap(); activeEnemy = false; startGame(); } //Function that creates the entire map by populating the multidimensional array of MapTile objects void populateMap() { map2[0][0] = MapTile(0, 0, "You cannot go this way.", -1); //Omitted additional assignments to save character space map2[9][9] = MapTile(9, 9, "You are in the Alchemists' Lab!", 3); } //Function to evaluate user's input string evaluateInput(string userInput) { boost::algorithm::to_lower(userInput); if (userInput == "north") { if (!activeEnemy) { //moving south so add one to Y coordinate coordinates[1] = (coordinates[1] + 1); } else { cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl; return "false"; } } else if (userInput == "south") { if (!activeEnemy) { //moving south so subract one from Y coordinate coordinates[1] = (coordinates[1] - 1); } else { cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl; return "false"; } } else if (userInput == "east") { if (!activeEnemy) { //moving east so add 1 to X coordinate coordinates[0] = (coordinates[0] + 1); } else { cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl; return "false"; } } else if (userInput == "west") { if (!activeEnemy) { //moving west so subract 1 from X coordinate coordinates[0] = (coordinates[0] - 1); } else { cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl; return "false"; } } else if (userInput == "attack") { if (activeEnemy) { battleEnemy(); } else { cout << "There is no enemy attacking you, move around to find enemies." << endl << "Type 'help_me' for detailed instructions." << endl; } } else if (userInput == "stopplayingartoria") { string ret = quitGame(); return ret; } else if (userInput == "help_me") { stringstream ret; ret << "You can move around the map typing the following commands: 'north', 'south', 'east', and 'west'" << endl << "If you encounter an enemy, attack them by typing 'attack' If you wish to quit the game, type 'StopPlayingArtoria'" << endl; } else { return "false"; } return "true"; } //Function to generate the message output to the console. string genMessage() { if (!activeEnemy) { //get the current map tile from the map container MapTile currentTile = map2[coordinates[0]][coordinates[1]]; string message = currentTile.getMessage(); int eventID = currentTile.getEventID(); //string variable to fill with return value; string retStr; //3 different areas each with event id(1-3), event ids are added for additional custom events switch (eventID) { case 1: //Dungeon Hallway retStr = genDungeonHallway(); cout << message << endl << retStr; break; case 2: //Crypt retStr = genCrypt(); cout << message << endl << retStr; break; case 3: //Alchemist's Lab retStr = genAlchemistsLab(); cout << message << endl << retStr; break; case 4: retStr = "This appears to be a safe place, nothing can surprise you here, but you will also find nothing here."; cout << message << retStr << endl; break; case -1: //player cannot go here so reset his coordinates coordinates[0] = tempCoordinates[0]; coordinates[1] = tempCoordinates[1]; return message; default: // Case 0 is just print message(currently unused) retStr = message; break; } //set tempCoords to keep track of last tile. tempCoordinates[0] = coordinates[0]; tempCoordinates[1] = coordinates[1]; return retStr; } } }; #pragma一次 //杰克·法利 //CSC275 //阿托里亚(决赛) //11/25/17 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括“boost/multi_array.hpp” #包括 使用名称空间std; //类,表示地图的一个平铺 类映射块 { 私人: int X; int-Y; 字符串消息; int-eventID; 公众: //默认构造函数 mappile() { } //具有最小参数的构造函数 贴图块(整数x,整数y) { X=X; Y=Y; message=“您不能走那条路。”; eventID=-1; } //具有所有参数的构造函数 MapTile(int x、int y、字符串消息、int eventID) { X=X; Y=Y; 此->消息=消息; 此->eventID=eventID; } //函数将坐标作为包含2个元素的向量返回 向量getCoords() { 向量坐标; 坐标。推回(X); 协调。推回(Y); 返回坐标; } //函数获取消息; 字符串getMessage() { 返回消息; } //函数来获取eventID int getEventID() { 返回事件ID; } }; //类,即游戏对象本身,在主“runner”中实现 阿托里亚级 { 私人: int-temp坐标[2]; int坐标[2]; 玩家; 敌我; 布尔主动敌; //这是我最初尝试的,但在运行时它抛出了一个错误的alloc //当我第一次尝试在populateMap()中为它赋值时 //MapTile地图[10][10]; //正在为地图做些什么 boost::multi_数组map2{boost::extensts[10][10]}; 向量项; 矢量武器; 矢量装甲; 随机发生器; 布尔playerded; 公众: //默认构造函数 阿托里亚() { populateItems(); populateMap(); 主动敌=假; startGame(); } //函数,该函数通过填充MapTile对象的多维数组来创建整个地图 void populateMap() { map2[0][0]=MapTile(0,0,“你不能走这条路。”,-1); //省略了其他指定以节省字符空间 map2[9][9]=MapTile(9,9,“你在炼金术士的实验室里!”,3); } //用于评估用户输入的函数 字符串求值输入(字符串用户输入) { boost::algorithm::to_lower(用户输入); 如果(用户输入==“北”) { 如果(!主动敌人) { //向南移动,以便向Y坐标添加一个 坐标[1]=(坐标[1]+1); } 其他的 { cout

Boost multi_阵列Boost_断言已抛出断点 我正在为我的C++类编写一个基于文本的地牢爬虫式游戏,我遇到一个运行时错误,说明Boost断言触发了断点。我用C语言做了一个非常相似的项目,所以我认为这是和C++有关的,这是导致我的问题的原因。 这是我的密码: ArtoriaClasses.h #pragma once //Jake Farley //CSC275 //Artoria (Final) //11/25/17 #include <iostream> #include <sstream> #include <memory> #include <boost/random/mersenne_twister.hpp> #include <boost/random/uniform_int_distribution.hpp> #include <boost/algorithm/string.hpp> #include <string> #include <ctime> #include <vector> #include <fstream> #include "boost/multi_array.hpp" #include <cassert> using namespace std; //Class representing one tile of the map class MapTile { private: int X; int Y; string message; int eventID; public: //Default Constructor MapTile() { } //Constructor with minimum arguments MapTile(int x, int y) { X = x; Y = y; message = "You are unable to go that way."; eventID = -1; } //Constructor with all arguments MapTile(int x, int y, string message, int eventID) { X = x; Y = y; this->message = message; this->eventID = eventID; } //function to return coords as a vector with 2 elements vector<int> getCoords() { vector<int> coords; coords.push_back(X); coords.push_back(Y); return coords; } //function to get the message; string getMessage() { return message; } //function to get the eventID int getEventID() { return eventID; } }; //Class that is the game object itself, is implemented in the main "runner" class Artoria { private: int tempCoordinates[2]; int coordinates[2]; Player player; Enemy currentEnemy; bool activeEnemy; //This is what I tried originally but it throws a bad_alloc at runtime //when I first try to assign it a value in populateMap() //MapTile map[10][10]; //Trying something for map boost::multi_array<MapTile, 2> map2 { boost::extents[10][10] }; vector<Item> looseItems; vector<Weapon> weapons; vector<Armor> armors; Random randomGen; bool playerDied; public: //Default Constructor Artoria() { populateItems(); populateMap(); activeEnemy = false; startGame(); } //Function that creates the entire map by populating the multidimensional array of MapTile objects void populateMap() { map2[0][0] = MapTile(0, 0, "You cannot go this way.", -1); //Omitted additional assignments to save character space map2[9][9] = MapTile(9, 9, "You are in the Alchemists' Lab!", 3); } //Function to evaluate user's input string evaluateInput(string userInput) { boost::algorithm::to_lower(userInput); if (userInput == "north") { if (!activeEnemy) { //moving south so add one to Y coordinate coordinates[1] = (coordinates[1] + 1); } else { cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl; return "false"; } } else if (userInput == "south") { if (!activeEnemy) { //moving south so subract one from Y coordinate coordinates[1] = (coordinates[1] - 1); } else { cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl; return "false"; } } else if (userInput == "east") { if (!activeEnemy) { //moving east so add 1 to X coordinate coordinates[0] = (coordinates[0] + 1); } else { cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl; return "false"; } } else if (userInput == "west") { if (!activeEnemy) { //moving west so subract 1 from X coordinate coordinates[0] = (coordinates[0] - 1); } else { cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl; return "false"; } } else if (userInput == "attack") { if (activeEnemy) { battleEnemy(); } else { cout << "There is no enemy attacking you, move around to find enemies." << endl << "Type 'help_me' for detailed instructions." << endl; } } else if (userInput == "stopplayingartoria") { string ret = quitGame(); return ret; } else if (userInput == "help_me") { stringstream ret; ret << "You can move around the map typing the following commands: 'north', 'south', 'east', and 'west'" << endl << "If you encounter an enemy, attack them by typing 'attack' If you wish to quit the game, type 'StopPlayingArtoria'" << endl; } else { return "false"; } return "true"; } //Function to generate the message output to the console. string genMessage() { if (!activeEnemy) { //get the current map tile from the map container MapTile currentTile = map2[coordinates[0]][coordinates[1]]; string message = currentTile.getMessage(); int eventID = currentTile.getEventID(); //string variable to fill with return value; string retStr; //3 different areas each with event id(1-3), event ids are added for additional custom events switch (eventID) { case 1: //Dungeon Hallway retStr = genDungeonHallway(); cout << message << endl << retStr; break; case 2: //Crypt retStr = genCrypt(); cout << message << endl << retStr; break; case 3: //Alchemist's Lab retStr = genAlchemistsLab(); cout << message << endl << retStr; break; case 4: retStr = "This appears to be a safe place, nothing can surprise you here, but you will also find nothing here."; cout << message << retStr << endl; break; case -1: //player cannot go here so reset his coordinates coordinates[0] = tempCoordinates[0]; coordinates[1] = tempCoordinates[1]; return message; default: // Case 0 is just print message(currently unused) retStr = message; break; } //set tempCoords to keep track of last tile. tempCoordinates[0] = coordinates[0]; tempCoordinates[1] = coordinates[1]; return retStr; } } }; #pragma一次 //杰克·法利 //CSC275 //阿托里亚(决赛) //11/25/17 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括“boost/multi_array.hpp” #包括 使用名称空间std; //类,表示地图的一个平铺 类映射块 { 私人: int X; int-Y; 字符串消息; int-eventID; 公众: //默认构造函数 mappile() { } //具有最小参数的构造函数 贴图块(整数x,整数y) { X=X; Y=Y; message=“您不能走那条路。”; eventID=-1; } //具有所有参数的构造函数 MapTile(int x、int y、字符串消息、int eventID) { X=X; Y=Y; 此->消息=消息; 此->eventID=eventID; } //函数将坐标作为包含2个元素的向量返回 向量getCoords() { 向量坐标; 坐标。推回(X); 协调。推回(Y); 返回坐标; } //函数获取消息; 字符串getMessage() { 返回消息; } //函数来获取eventID int getEventID() { 返回事件ID; } }; //类,即游戏对象本身,在主“runner”中实现 阿托里亚级 { 私人: int-temp坐标[2]; int坐标[2]; 玩家; 敌我; 布尔主动敌; //这是我最初尝试的,但在运行时它抛出了一个错误的alloc //当我第一次尝试在populateMap()中为它赋值时 //MapTile地图[10][10]; //正在为地图做些什么 boost::multi_数组map2{boost::extensts[10][10]}; 向量项; 矢量武器; 矢量装甲; 随机发生器; 布尔playerded; 公众: //默认构造函数 阿托里亚() { populateItems(); populateMap(); 主动敌=假; startGame(); } //函数,该函数通过填充MapTile对象的多维数组来创建整个地图 void populateMap() { map2[0][0]=MapTile(0,0,“你不能走这条路。”,-1); //省略了其他指定以节省字符空间 map2[9][9]=MapTile(9,9,“你在炼金术士的实验室里!”,3); } //用于评估用户输入的函数 字符串求值输入(字符串用户输入) { boost::algorithm::to_lower(用户输入); 如果(用户输入==“北”) { 如果(!主动敌人) { //向南移动,以便向Y坐标添加一个 坐标[1]=(坐标[1]+1); } 其他的 { cout,c++,visual-c++,boost,C++,Visual C++,Boost,什么是“省略的赋值”?如果它们处理非法索引,就可以了 我也看不到你在任何地方初始化坐标。你可能认为你是免费得到的,就像在C# 看起来你没有对移动(西、东等)进行任何范围验证,还可以查看复制粘贴注释…:) 暗示 最好抽象一点: enum { Rows = 10, Columns = 10 }; struct Coords { int x = 0, y = 0; bool west() { if (x>0) { --x; return true; } re

什么是“省略的赋值”?如果它们处理非法索引,就可以了

我也看不到你在任何地方初始化坐标。你可能认为你是免费得到的,就像在C#

看起来你没有对移动(西、东等)进行任何范围验证,还可以查看复制粘贴注释…:)

暗示 最好抽象一点:

enum { Rows = 10, Columns = 10 };

struct Coords {
    int x = 0, y = 0;

    bool west()  { if (x>0)         { --x; return true; } return false; }
    bool south() { if (y>0)         { --y; return true; } return false; }
    bool east()  { if (x+1<Columns) { ++x; return true; } return false; }
    bool north() { if (y+1<Rows)    { ++y; return true; } return false; }
};
Coords lastCoordinates;
Coords coordinates;
不再需要评论,更不用说错误的评论:)

使用它:

        MapTile currentTile = map2[coordinates.x][coordinates.y];
或:

以及:

现场演示 设法使导航“可玩”。它到达炼金术士实验室,很好:

//#pragma once
//Jake Farley
//CSC275
//Artoria (Final)
//11/25/17
struct Player {
};

struct Enemy {
};

struct Item {
};

struct Weapon {
};

struct Armor {
};


#include <iostream>
#include <sstream>
#include <memory>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/algorithm/string.hpp>
#include <string>
#include <ctime>
#include <vector>
#include <fstream>
#include "boost/multi_array.hpp"
#include <cassert>

using Random = boost::random::mt19937;

using namespace std;

//Class representing one tile of the map
class MapTile
{
private:
    int X;
    int Y;
    string message;
    int eventID;
public:
    //Default Constructor
    MapTile()
    {

    }

    //Constructor with minimum arguments
    MapTile(int x, int y)
    {
        X = x;
        Y = y;
        message = "You are unable to go that way.";
        eventID = -1;
    }

    //Constructor with all arguments
    MapTile(int x, int y, string message, int eventID)
    {
        X = x;
        Y = y;
        this->message = message;
        this->eventID = eventID;
    }

    //function to return coords as a vector with 2 elements
    vector<int> getCoords()
    {
        vector<int> coords;
        coords.push_back(X);
        coords.push_back(Y);

        return coords;
    }

    //function to get the message;
    string getMessage()
    {
        return message;
    }

    //function to get the eventID
    int getEventID()
    {
        return eventID;
    }
};
//Class that is the game object itself, is implemented in the main "runner" 
class Artoria
{
private:
    enum { Rows = 10, Columns = 10 };

    struct Coords {
        int x = 0, y = 0;

        bool west()  { if (x>0)         { --x; return true; } return false; }
        bool south() { if (y>0)         { --y; return true; } return false; }
        bool east()  { if (x+1<Columns) { ++x; return true; } return false; }
        bool north() { if (y+1<Rows)    { ++y; return true; } return false; }
    };
    Coords lastCoordinates;
    Coords coordinates;
    Player player;
    Enemy currentEnemy;
    bool activeEnemy;

    boost::multi_array<MapTile, 2> map2 { boost::extents[Columns][Rows] };

    vector<Item> looseItems;
    vector<Weapon> weapons;
    vector<Armor> armors;
    Random randomGen;
    bool playerDied;

public:
    //Default Constructor
    Artoria(std::string const& /*s*/ = {})
    {
        //populateItems();
        populateMap();
        activeEnemy = false;
        //startGame();
    }
    //Function that creates the entire map by populating the multidimensional array of MapTile objects
    void populateMap()
    {
        map2[0][0] = MapTile(0, 0, "You cannot go this way.", -1);
        //Omitted additional assignments to save character space
        map2[9][9] = MapTile(9, 9, "You are in the Alchemists' Lab!", 3);

    }
    //Function to evaluate user's input
    string evaluateInput(string userInput)
    {
        boost::algorithm::to_lower(userInput);
        if (userInput == "north")
        {
            if (!activeEnemy)
            {
                coordinates.north();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "south")
        {
            if (!activeEnemy)
            {
                coordinates.south();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "east")
        {
            if (!activeEnemy)
            {
                coordinates.east();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "west")
        {
            if (!activeEnemy)
            {
                coordinates.west();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "attack")
        {
            if (activeEnemy)
            {
                //battleEnemy();
            }
            else
            {
                cout << "There is no enemy attacking you, move around to find enemies." << endl << "Type 'help_me' for detailed instructions." << endl;
            }
        }
        else if (userInput == "stopplayingartoria")
        {
            //string ret = quitGame();
            return "quit";
        }
        else if (userInput == "help_me")
        {
            stringstream ret;
            ret << "You can move around the map typing the following commands: 'north', 'south', 'east', and 'west'" << endl << "If you encounter an enemy, attack them by typing 'attack' If you wish to quit the game, type 'StopPlayingArtoria'" << endl;
        }
        else
        {
            return "false";
        }
        return "true";
    }

    //Function to generate the message output to the console.
    string genMessage()
    {
        if (!activeEnemy)
        {
            //get the current map tile from the map container
            MapTile currentTile = map2[coordinates.x][coordinates.y];
            string message = currentTile.getMessage();
            int eventID = currentTile.getEventID();

            //string variable to fill with return value;
            string retStr;
            //3 different areas each with event id(1-3), event ids are added for additional custom events
            switch (eventID)
            {
            case 1:
                //Dungeon Hallway
                retStr = "genDungeonHallway()";
                cout << message << endl << retStr;
                break;
            case 2:
                //Crypt
                retStr = "genCrypt()";
                cout << message << endl << retStr;
                break;
            case 3:
                //Alchemist's Lab
                retStr = "genAlchemistsLab()";
                cout << message << endl << retStr;
                break;
            case 4:
                retStr = "This appears to be a safe place, nothing can surprise you here, but you will also find nothing here.";
                cout << message << retStr << endl;
                break;
            case -1:
                //player cannot go here so reset his coordinates
                coordinates = lastCoordinates;
                return message;
            default:
                // Case 0 is just print message(currently unused)
                retStr = message;
                break;
            }
            //set lastCoordinates to keep track of last tile.
            lastCoordinates = coordinates;
            return retStr;
        }
    }
};

//Jake Farley
//CSC275
//Artoria (Final)
//11/25/17

#include <iostream>
#include <string>
#include <memory>
#include <boost/algorithm/string.hpp>
//#include "ArtoriaClasses.h";

using namespace std;

int main()
{
    string userInput;

    cout << "Welcome to the game 'Artoria'. You can start a new game by typing 'new' " << endl << " load an existing save by typing 'load' or view instructions on how to play the game by typing 'help_me'" << endl;
    getline(cin, userInput);
    boost::algorithm::to_lower(userInput);
    if (userInput == "new")
    {
        Artoria artoriaGame = Artoria();
        while (userInput != "QUIT")
        {
            getline(cin, userInput);
            string ret = artoriaGame.evaluateInput(userInput);
            if (ret == "true")
            {
                cout << artoriaGame.genMessage();
            }
            else if (ret == "false")
            {
                cout << "You have entered an invalid command. You can type 'help_me' for instructions.";
            }
            else if (ret == "QUIT")
            {

            }
        }
    }
    else if (userInput == "load")
    {
        cout << "What is the name you used?" << endl;
        getline(cin, userInput);
        Artoria artoriaGame = Artoria(userInput);
        while (userInput != "QUIT")
        {
            getline(cin, userInput);
            string ret = artoriaGame.evaluateInput(userInput);
            if (ret == "true")
            {
                cout << artoriaGame.genMessage();
            }
            else if (ret == "false")
            {
                cout << "You have entered an invalid command. You can type 'help_me' for instructions.";
            }
            else if (ret == "QUIT")
            {

            }
        }
    }
    else if (userInput == "help_me")
    {
        cout << "Artoria is a text-based dungeon crawler created by Jake Farley. The goal of the game is to get the highest score." << endl;
        cout << "You score points by defeating monsters and picking up items, as you walk around the map you will encounter various enemies and find various items." << endl;
        cout << "As you fight enemies, you will take damage; be warned, there is no way to regain your health. Once your health reaches zero, the game will end." << endl;
        cout << "You can move around the map by typing the four compass directions 'north', 'south', 'east', and 'west'" << endl;
        cout << "If you encounter an enemy, attack them by typing 'attack' you cannot run away from an enemy encounter." << endl;
        cout << "If you wish to quit the game, type 'StopPlayingArtoria'" << endl;
    }
    else
    {
        cout << "Error: Invalid response, closing program." << endl;
    }
}
正如您所见,您需要处理输入循环并检测文件结尾:)

什么是“省略的赋值”?如果它们处理非法索引,就可以了

我也看不到你在任何地方初始化坐标。你可能认为你是免费得到的,就像在C#

看起来你没有对移动(西、东等)进行任何范围验证,还可以查看复制粘贴注释…:)

暗示 最好抽象一点:

enum { Rows = 10, Columns = 10 };

struct Coords {
    int x = 0, y = 0;

    bool west()  { if (x>0)         { --x; return true; } return false; }
    bool south() { if (y>0)         { --y; return true; } return false; }
    bool east()  { if (x+1<Columns) { ++x; return true; } return false; }
    bool north() { if (y+1<Rows)    { ++y; return true; } return false; }
};
Coords lastCoordinates;
Coords coordinates;
不再需要评论,更不用说错误的评论:)

使用它:

        MapTile currentTile = map2[coordinates.x][coordinates.y];
或:

以及:

现场演示 设法使导航“可玩”。它到达炼金术士实验室,很好:

//#pragma once
//Jake Farley
//CSC275
//Artoria (Final)
//11/25/17
struct Player {
};

struct Enemy {
};

struct Item {
};

struct Weapon {
};

struct Armor {
};


#include <iostream>
#include <sstream>
#include <memory>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/algorithm/string.hpp>
#include <string>
#include <ctime>
#include <vector>
#include <fstream>
#include "boost/multi_array.hpp"
#include <cassert>

using Random = boost::random::mt19937;

using namespace std;

//Class representing one tile of the map
class MapTile
{
private:
    int X;
    int Y;
    string message;
    int eventID;
public:
    //Default Constructor
    MapTile()
    {

    }

    //Constructor with minimum arguments
    MapTile(int x, int y)
    {
        X = x;
        Y = y;
        message = "You are unable to go that way.";
        eventID = -1;
    }

    //Constructor with all arguments
    MapTile(int x, int y, string message, int eventID)
    {
        X = x;
        Y = y;
        this->message = message;
        this->eventID = eventID;
    }

    //function to return coords as a vector with 2 elements
    vector<int> getCoords()
    {
        vector<int> coords;
        coords.push_back(X);
        coords.push_back(Y);

        return coords;
    }

    //function to get the message;
    string getMessage()
    {
        return message;
    }

    //function to get the eventID
    int getEventID()
    {
        return eventID;
    }
};
//Class that is the game object itself, is implemented in the main "runner" 
class Artoria
{
private:
    enum { Rows = 10, Columns = 10 };

    struct Coords {
        int x = 0, y = 0;

        bool west()  { if (x>0)         { --x; return true; } return false; }
        bool south() { if (y>0)         { --y; return true; } return false; }
        bool east()  { if (x+1<Columns) { ++x; return true; } return false; }
        bool north() { if (y+1<Rows)    { ++y; return true; } return false; }
    };
    Coords lastCoordinates;
    Coords coordinates;
    Player player;
    Enemy currentEnemy;
    bool activeEnemy;

    boost::multi_array<MapTile, 2> map2 { boost::extents[Columns][Rows] };

    vector<Item> looseItems;
    vector<Weapon> weapons;
    vector<Armor> armors;
    Random randomGen;
    bool playerDied;

public:
    //Default Constructor
    Artoria(std::string const& /*s*/ = {})
    {
        //populateItems();
        populateMap();
        activeEnemy = false;
        //startGame();
    }
    //Function that creates the entire map by populating the multidimensional array of MapTile objects
    void populateMap()
    {
        map2[0][0] = MapTile(0, 0, "You cannot go this way.", -1);
        //Omitted additional assignments to save character space
        map2[9][9] = MapTile(9, 9, "You are in the Alchemists' Lab!", 3);

    }
    //Function to evaluate user's input
    string evaluateInput(string userInput)
    {
        boost::algorithm::to_lower(userInput);
        if (userInput == "north")
        {
            if (!activeEnemy)
            {
                coordinates.north();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "south")
        {
            if (!activeEnemy)
            {
                coordinates.south();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "east")
        {
            if (!activeEnemy)
            {
                coordinates.east();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "west")
        {
            if (!activeEnemy)
            {
                coordinates.west();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "attack")
        {
            if (activeEnemy)
            {
                //battleEnemy();
            }
            else
            {
                cout << "There is no enemy attacking you, move around to find enemies." << endl << "Type 'help_me' for detailed instructions." << endl;
            }
        }
        else if (userInput == "stopplayingartoria")
        {
            //string ret = quitGame();
            return "quit";
        }
        else if (userInput == "help_me")
        {
            stringstream ret;
            ret << "You can move around the map typing the following commands: 'north', 'south', 'east', and 'west'" << endl << "If you encounter an enemy, attack them by typing 'attack' If you wish to quit the game, type 'StopPlayingArtoria'" << endl;
        }
        else
        {
            return "false";
        }
        return "true";
    }

    //Function to generate the message output to the console.
    string genMessage()
    {
        if (!activeEnemy)
        {
            //get the current map tile from the map container
            MapTile currentTile = map2[coordinates.x][coordinates.y];
            string message = currentTile.getMessage();
            int eventID = currentTile.getEventID();

            //string variable to fill with return value;
            string retStr;
            //3 different areas each with event id(1-3), event ids are added for additional custom events
            switch (eventID)
            {
            case 1:
                //Dungeon Hallway
                retStr = "genDungeonHallway()";
                cout << message << endl << retStr;
                break;
            case 2:
                //Crypt
                retStr = "genCrypt()";
                cout << message << endl << retStr;
                break;
            case 3:
                //Alchemist's Lab
                retStr = "genAlchemistsLab()";
                cout << message << endl << retStr;
                break;
            case 4:
                retStr = "This appears to be a safe place, nothing can surprise you here, but you will also find nothing here.";
                cout << message << retStr << endl;
                break;
            case -1:
                //player cannot go here so reset his coordinates
                coordinates = lastCoordinates;
                return message;
            default:
                // Case 0 is just print message(currently unused)
                retStr = message;
                break;
            }
            //set lastCoordinates to keep track of last tile.
            lastCoordinates = coordinates;
            return retStr;
        }
    }
};

//Jake Farley
//CSC275
//Artoria (Final)
//11/25/17

#include <iostream>
#include <string>
#include <memory>
#include <boost/algorithm/string.hpp>
//#include "ArtoriaClasses.h";

using namespace std;

int main()
{
    string userInput;

    cout << "Welcome to the game 'Artoria'. You can start a new game by typing 'new' " << endl << " load an existing save by typing 'load' or view instructions on how to play the game by typing 'help_me'" << endl;
    getline(cin, userInput);
    boost::algorithm::to_lower(userInput);
    if (userInput == "new")
    {
        Artoria artoriaGame = Artoria();
        while (userInput != "QUIT")
        {
            getline(cin, userInput);
            string ret = artoriaGame.evaluateInput(userInput);
            if (ret == "true")
            {
                cout << artoriaGame.genMessage();
            }
            else if (ret == "false")
            {
                cout << "You have entered an invalid command. You can type 'help_me' for instructions.";
            }
            else if (ret == "QUIT")
            {

            }
        }
    }
    else if (userInput == "load")
    {
        cout << "What is the name you used?" << endl;
        getline(cin, userInput);
        Artoria artoriaGame = Artoria(userInput);
        while (userInput != "QUIT")
        {
            getline(cin, userInput);
            string ret = artoriaGame.evaluateInput(userInput);
            if (ret == "true")
            {
                cout << artoriaGame.genMessage();
            }
            else if (ret == "false")
            {
                cout << "You have entered an invalid command. You can type 'help_me' for instructions.";
            }
            else if (ret == "QUIT")
            {

            }
        }
    }
    else if (userInput == "help_me")
    {
        cout << "Artoria is a text-based dungeon crawler created by Jake Farley. The goal of the game is to get the highest score." << endl;
        cout << "You score points by defeating monsters and picking up items, as you walk around the map you will encounter various enemies and find various items." << endl;
        cout << "As you fight enemies, you will take damage; be warned, there is no way to regain your health. Once your health reaches zero, the game will end." << endl;
        cout << "You can move around the map by typing the four compass directions 'north', 'south', 'east', and 'west'" << endl;
        cout << "If you encounter an enemy, attack them by typing 'attack' you cannot run away from an enemy encounter." << endl;
        cout << "If you wish to quit the game, type 'StopPlayingArtoria'" << endl;
    }
    else
    {
        cout << "Error: Invalid response, closing program." << endl;
    }
}

正如您所见,您需要处理输入循环并检测文件结尾:)

BOOST\u ASSERT已向调试器抛出一个断点中断,并沿调用堆栈走到导致问题的代码行。如果
ActiveForeign
为true,
genMessage()
从不返回值->未定义的行为。对于其余部分:@drescherjm所说的。当您甚至没有显示断言发生的位置时,这远远不是自足的,而且代码太多了。boost\u assert已向调试器抛出一个断点,并沿着调用堆栈走到您的代码行,该行可以使用该问题。如果
ActiveForeign
为真,
genMessage()
永远不会返回值->未定义的行为。对于其余部分:正如@drescherjm所说的。当您甚至没有显示断言发生的位置时,这远远不是自足的,而且代码太多,无法开始.它到达了炼金术士实验室,很好:我按照你的建议使用了枚举数和结构,我在那里不再有问题了。谢谢!我现在正在处理其他问题,但这些是我将自己尝试解决的其他问题。我在上学时作为开发人员工作,但我几乎只使用C#,所以我得到了tripp我被一些差异所困扰。设法使导航“可玩”。它到达了炼金术士实验室,很好:我按照你的建议使用了枚举和结构,我在那里不再有问题了。谢谢!我现在正在处理其他问题,但这些是其他问题
        MapTile currentTile = map2[coordinates.x][coordinates.y];
        case -1:
            //player cannot go here so reset his coordinates
            coordinates = lastCoordinates;
            return message;
        //set lastCoordinates to keep track of last tile.
        lastCoordinates = coordinates;
//#pragma once
//Jake Farley
//CSC275
//Artoria (Final)
//11/25/17
struct Player {
};

struct Enemy {
};

struct Item {
};

struct Weapon {
};

struct Armor {
};


#include <iostream>
#include <sstream>
#include <memory>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/algorithm/string.hpp>
#include <string>
#include <ctime>
#include <vector>
#include <fstream>
#include "boost/multi_array.hpp"
#include <cassert>

using Random = boost::random::mt19937;

using namespace std;

//Class representing one tile of the map
class MapTile
{
private:
    int X;
    int Y;
    string message;
    int eventID;
public:
    //Default Constructor
    MapTile()
    {

    }

    //Constructor with minimum arguments
    MapTile(int x, int y)
    {
        X = x;
        Y = y;
        message = "You are unable to go that way.";
        eventID = -1;
    }

    //Constructor with all arguments
    MapTile(int x, int y, string message, int eventID)
    {
        X = x;
        Y = y;
        this->message = message;
        this->eventID = eventID;
    }

    //function to return coords as a vector with 2 elements
    vector<int> getCoords()
    {
        vector<int> coords;
        coords.push_back(X);
        coords.push_back(Y);

        return coords;
    }

    //function to get the message;
    string getMessage()
    {
        return message;
    }

    //function to get the eventID
    int getEventID()
    {
        return eventID;
    }
};
//Class that is the game object itself, is implemented in the main "runner" 
class Artoria
{
private:
    enum { Rows = 10, Columns = 10 };

    struct Coords {
        int x = 0, y = 0;

        bool west()  { if (x>0)         { --x; return true; } return false; }
        bool south() { if (y>0)         { --y; return true; } return false; }
        bool east()  { if (x+1<Columns) { ++x; return true; } return false; }
        bool north() { if (y+1<Rows)    { ++y; return true; } return false; }
    };
    Coords lastCoordinates;
    Coords coordinates;
    Player player;
    Enemy currentEnemy;
    bool activeEnemy;

    boost::multi_array<MapTile, 2> map2 { boost::extents[Columns][Rows] };

    vector<Item> looseItems;
    vector<Weapon> weapons;
    vector<Armor> armors;
    Random randomGen;
    bool playerDied;

public:
    //Default Constructor
    Artoria(std::string const& /*s*/ = {})
    {
        //populateItems();
        populateMap();
        activeEnemy = false;
        //startGame();
    }
    //Function that creates the entire map by populating the multidimensional array of MapTile objects
    void populateMap()
    {
        map2[0][0] = MapTile(0, 0, "You cannot go this way.", -1);
        //Omitted additional assignments to save character space
        map2[9][9] = MapTile(9, 9, "You are in the Alchemists' Lab!", 3);

    }
    //Function to evaluate user's input
    string evaluateInput(string userInput)
    {
        boost::algorithm::to_lower(userInput);
        if (userInput == "north")
        {
            if (!activeEnemy)
            {
                coordinates.north();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "south")
        {
            if (!activeEnemy)
            {
                coordinates.south();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "east")
        {
            if (!activeEnemy)
            {
                coordinates.east();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "west")
        {
            if (!activeEnemy)
            {
                coordinates.west();
            }
            else
            {
                cout << "You cannot move when there is an enemy attacking you!" << endl << "Type 'Attack' to attack the enemy. Type 'help_me' for detailed instructions." << endl;
                return "false";
            }
        }
        else if (userInput == "attack")
        {
            if (activeEnemy)
            {
                //battleEnemy();
            }
            else
            {
                cout << "There is no enemy attacking you, move around to find enemies." << endl << "Type 'help_me' for detailed instructions." << endl;
            }
        }
        else if (userInput == "stopplayingartoria")
        {
            //string ret = quitGame();
            return "quit";
        }
        else if (userInput == "help_me")
        {
            stringstream ret;
            ret << "You can move around the map typing the following commands: 'north', 'south', 'east', and 'west'" << endl << "If you encounter an enemy, attack them by typing 'attack' If you wish to quit the game, type 'StopPlayingArtoria'" << endl;
        }
        else
        {
            return "false";
        }
        return "true";
    }

    //Function to generate the message output to the console.
    string genMessage()
    {
        if (!activeEnemy)
        {
            //get the current map tile from the map container
            MapTile currentTile = map2[coordinates.x][coordinates.y];
            string message = currentTile.getMessage();
            int eventID = currentTile.getEventID();

            //string variable to fill with return value;
            string retStr;
            //3 different areas each with event id(1-3), event ids are added for additional custom events
            switch (eventID)
            {
            case 1:
                //Dungeon Hallway
                retStr = "genDungeonHallway()";
                cout << message << endl << retStr;
                break;
            case 2:
                //Crypt
                retStr = "genCrypt()";
                cout << message << endl << retStr;
                break;
            case 3:
                //Alchemist's Lab
                retStr = "genAlchemistsLab()";
                cout << message << endl << retStr;
                break;
            case 4:
                retStr = "This appears to be a safe place, nothing can surprise you here, but you will also find nothing here.";
                cout << message << retStr << endl;
                break;
            case -1:
                //player cannot go here so reset his coordinates
                coordinates = lastCoordinates;
                return message;
            default:
                // Case 0 is just print message(currently unused)
                retStr = message;
                break;
            }
            //set lastCoordinates to keep track of last tile.
            lastCoordinates = coordinates;
            return retStr;
        }
    }
};

//Jake Farley
//CSC275
//Artoria (Final)
//11/25/17

#include <iostream>
#include <string>
#include <memory>
#include <boost/algorithm/string.hpp>
//#include "ArtoriaClasses.h";

using namespace std;

int main()
{
    string userInput;

    cout << "Welcome to the game 'Artoria'. You can start a new game by typing 'new' " << endl << " load an existing save by typing 'load' or view instructions on how to play the game by typing 'help_me'" << endl;
    getline(cin, userInput);
    boost::algorithm::to_lower(userInput);
    if (userInput == "new")
    {
        Artoria artoriaGame = Artoria();
        while (userInput != "QUIT")
        {
            getline(cin, userInput);
            string ret = artoriaGame.evaluateInput(userInput);
            if (ret == "true")
            {
                cout << artoriaGame.genMessage();
            }
            else if (ret == "false")
            {
                cout << "You have entered an invalid command. You can type 'help_me' for instructions.";
            }
            else if (ret == "QUIT")
            {

            }
        }
    }
    else if (userInput == "load")
    {
        cout << "What is the name you used?" << endl;
        getline(cin, userInput);
        Artoria artoriaGame = Artoria(userInput);
        while (userInput != "QUIT")
        {
            getline(cin, userInput);
            string ret = artoriaGame.evaluateInput(userInput);
            if (ret == "true")
            {
                cout << artoriaGame.genMessage();
            }
            else if (ret == "false")
            {
                cout << "You have entered an invalid command. You can type 'help_me' for instructions.";
            }
            else if (ret == "QUIT")
            {

            }
        }
    }
    else if (userInput == "help_me")
    {
        cout << "Artoria is a text-based dungeon crawler created by Jake Farley. The goal of the game is to get the highest score." << endl;
        cout << "You score points by defeating monsters and picking up items, as you walk around the map you will encounter various enemies and find various items." << endl;
        cout << "As you fight enemies, you will take damage; be warned, there is no way to regain your health. Once your health reaches zero, the game will end." << endl;
        cout << "You can move around the map by typing the four compass directions 'north', 'south', 'east', and 'west'" << endl;
        cout << "If you encounter an enemy, attack them by typing 'attack' you cannot run away from an enemy encounter." << endl;
        cout << "If you wish to quit the game, type 'StopPlayingArtoria'" << endl;
    }
    else
    {
        cout << "Error: Invalid response, closing program." << endl;
    }
}
Welcome to the game 'Artoria'. You can start a new game by typing 'new' 
 load an existing save by typing 'load' or view instructions on how to play the game by typing 'help_me'
You cannot go this way.
You are in the Alchemists' Lab!
genAlchemistsLab()
genAlchemistsLab()
You have entered an invalid command. 
You can type 'help_me' for instructions.
You have entered an invalid command. You can type 'help_me' for instructions.
You have entered an invalid command. You can type 'help_me' for instructions.
You have entered an invalid command. You can type 'help_me' for instructions.
You have entered an invalid command. You can type 'help_me' for instructions.
You have entered an invalid command. You can type 'help_me' for instructions.
You have entered an invalid command. You can type 'help_me' for instructions.
...