美好的一天。我的代码发生了一件相当奇怪的事情。我想创建一个随机数输入到我的 quest_postition 数组中以在我的网格数组中使用它。我希望每次运行程序时位置都不同。
它目前正在做的是进入一个无限循环并一直显示网格。
这是我的代码:
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
using namespace std;
void draw_grid()
{
char grid[9][9] = { {'x','x','x','x','x','x','x','x','x'},
{'x','x','x','x','x','x','x','x','x'},
{'x','x','x','x','x','x','x','x','x'},
{'x','x','x','x','x','x','x','x','x'},
{'x','x','x','x','x','x','x','x','x'},
{'x','x','x','x','x','x','x','x','x'},
{'x','x','x','x','x','x','x','x','x'},
{'x','x','x','x','x','x','x','x','x'},
{'x','x','x','x','x','x','x','x','x'}};
char character = '*';
char quest = 'Q';
int position[2] = {4,4};
int quest_position[2];
srand(time(NULL));
quest_position[0] = rand() % 9 + 0;
quest_position[1] = rand() % 9 + 0;
char direction;
for(int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
if(i == position[0] && j == position[1])
cout << character;
if(i = quest_position[0] && j == quest_position[1])
cout << quest;
else
cout << grid[i][j];
cout << " ";
}
cout << endl;
}
}
int main()
{
draw_grid();
}
请你帮忙。
谢谢。