基于控制台的游戏无缘无故崩溃

时间:2014-08-11 21:59:58

标签: c++

我正在尝试开始一个非常基本的游戏我试图让它只是让游戏的边界将是x' s并且他们的玩家是H但是当我构建并运行它时程序会立即崩溃请帮忙

 #include <iostream>

 using namespace std;
 int cords[2];
 string map[6][6] =
{
 {"X","X","X","X","X","X"},
 {"X"," "," "," "," ","X"},
 {"X"," "," "," "," ","X"},
 {"X"," ","H"," "," ","X"},
 {"X"," "," "," "," ","X"},
 {"X","X","X","X","X","X"}
};
string input;
bool running = true;

void tick(){
if(input == "w"){
cords[1]++;
}
if(input == "s"){
cords[1] = cords[1] - 1;
}
if(input == "a"){
cords[0] = cords[0] - 1;
}
if(input == "d"){
cords[0]++;
   }
    }

void render(){
cout << map[1][1] << map[1][2] << map[1][3] << map[1][4] << map[1][5] << map[1][6]      <<endl
    << map[2][1] << map[2][2] << map[2][3] << map[2][4] << map[2][5] << map[2][6]   <<endl
    << map[3][1] << map[3][2] << map[3][3] << map[3][4] << map[3][5] << map[3][6] <<endl
 << map[4][1] << map[4][2] << map[4][3] << map[4][4] << map[4][5] << map[4][6] <<endl
 << map[5][1] << map[5][2] << map[5][3] << map[5][4] << map[5][5] << map[5][6] <<endl
 << map[6][1] << map[6][2] << map[6][3] << map[6][4] << map[6][5] << map[6][6] <<endl;

 }

 void run(){
 int ticks;
 int frames;

 tick();
 render();



  } 


 int main(){
 while(running == true){
 run();
 cin>> input;
 }
 }

3 个答案:

答案 0 :(得分:2)

两个地图索引必须介于0和5之间:map [6] [3]不正确,例如

答案 1 :(得分:2)

如果您编写了一个循环来打印出您的2D阵列,则可能避免了错误:

const int matSize = 6;

for (int i = 0; i < matSize; ++i) 
{ 
    for (int j = 0; j < matSize; ++j ) 
      cout << m[i][j]; 
    cout << "\n"; 
}

答案 2 :(得分:1)

您应该将电路板更改为使用stringchar,而不是使用单字符串数组:

std::string map[6] = 
{
 "XXXXXX",
 "X    X",
 "X    X",
 "X H  X",
 "X    X",
 "XXXXXX",
};

char map_as_char_array[6][6] =
{
 {'X','X','X','X','X','X'},
 {'X',' ',' ',' ',' ','X'},
 {'X',' ',' ',' ',' ','X'},
 {'X',' ','H',' ',' ','X'},
 {'X',' ',' ',' ',' ','X'},
 {'X','X','X','X','X','X'}
};

string数据类型对于单个字母来说是过度的。

请记住,string类型可以作为单维字符数组进行访问。

相关问题