【发布时间】:2017-03-27 09:06:03
【问题描述】:
我叫 Eric,在使用 c++ 编写基本的“地牢爬行者”时遇到了问题。
问题是当玩家向左或向右移动某些空间时,会生成另一个玩家角色,因此屏幕上会出现两个而不是一个。
这是我的代码:
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
using namespace std;
const int columns = 7, rows = 10;
string gridArray[rows][columns];
bool xIsGenerated = false;
bool gISGenerated = false;
int playerX = 0, playerY = 0;
void displayGrid(int rows, int columns){
for(int i = 0; i < columns; i++){
for(int x = 0; x < rows; x++){
cout << gridArray[i][x];
}
cout << endl;
}
cout << gISGenerated << endl;
return;
}
void generatePieces(int rows, int columns){
int tcount = 0;
for(int i = 0; i < rows; i++){
for(int x = 0; x < columns; x++){
srand(time(NULL) + x + i);
int r = rand() % 5;
if(r == 1 && tcount < 4){
gridArray[i][x] = "T ";
tcount++;
}else if(r == 2 && !xIsGenerated){
gridArray[i][x] = "X ";
xIsGenerated = true;
}
}
}
if(!xIsGenerated){
srand(time(NULL)*3);
int r = rand() % rows+1;
int c = rand() % columns+1;
gridArray[r][c] = "X ";
xIsGenerated = true;
}
return;
}
void generatePlayer(int rows, int columns){
if(!gISGenerated){
srand(time(NULL)*3);
int r = rand() % rows+1;
int c = rand() % columns+1;
gridArray[r][c] = "G ";
playerX = r;
playerY = c;
gISGenerated = true;
}
}
void initGrid(int rows, int columns){
for(int i = 0; i < columns; i++){
for(int x = 0; x < rows; x++){
gridArray[i][x] = ". ";
}
}
generatePieces(rows, columns);
generatePlayer(rows, columns);
return;
}
//i is the rows
//x is the columns
void movePlayer(){
char input = 'x';
cin >> input;
if(input == 'w' && playerX != 0){
gridArray[playerX][playerY] = ". ";
gridArray[playerX-1][playerY] = "G ";
playerX--;
}
if(input == 's' && playerX != 6){
gridArray[playerX][playerY] = ". ";
gridArray[playerX+1][playerY] = "G ";
playerX++;
}
if(input == 'a' && playerY != 0){
gridArray[playerX][playerY] = ". ";
gridArray[playerX][playerY-1] = "G ";
playerY--;
}
if(input == 'd' && playerY != 9){
gridArray[playerX][playerY] = ". ";
gridArray[playerX][playerY+1] = "G ";
playerY++;
}
system("CLS");
displayGrid(rows, columns);
cout << playerX << ": " << playerY << endl;
}
void firstTime(){
displayGrid(rows, columns);
cout << playerX << ": " << playerY << endl;
return;
}
int main()
{
initGrid(rows,columns);
firstTime();
while(true){
movePlayer();
}
return 0;
}
代码快速解释:
一个多维数组将用作显示正在发生的事情的图形。这个以函数“initGrid”开头的数组将在数组和屏幕上打印出“.”字符串
Generate Pieces 函数获取填充有“.”字符串的数组,并使用随机数生成器放置“T”字符串和 1 个“X”字符串。这个“X”将是目标,而 T 将是杀死玩家的陷阱。
Generate Player 做同样的事情,但只放置 1 个“G”字符串。这是播放器。
initGrid 被调用后,main 函数里面就是“firstTime”函数,没什么复杂的,只是将数据显示到屏幕上。
最后,我有一个调用函数“movePlayer”的while循环,使用相同的数组,根据用户输入的内容,它将相应地移动“G”字符串并将g字符串的最后一个位置替换为一个空的“.”字符串。
我试图返回第二个 G 字符串的位置,一旦我这样做了,我尝试用“。”字符串替换它,但它失败了,因为代码没有删除第二个,并且一旦第二个一个在数组之外(第二个 g 字符对应于第一个 g 字符移动)第一个 G 字符被删除。
我在这里画了一个空白,我接下来应该做什么,起初这似乎是一个简单的问题,但它让我为它的钱而奔跑。
感谢您的阅读,我希望尽快得到我的问题的答案。
【问题讨论】:
-
欢迎来到 Stack Overflow。查看help center,尤其是minimal complete examples 部分。尝试将您的代码缩减为显示错误的最简单示例——这不仅仅是为了我们的方便,它是一项至关重要的技能,您可能会在此过程中自己发现错误。特别是,尽量去除随机性,这样我们就可以有一个reproducible的例子。
-
我真的不认为你想要:
srand(time(NULL) + x + i);,因为你所拥有的并不是随机的。这将是x和i严重偏颇的东西。srand最好在程序的早期使用一次以设置rand,然后不要理会,除非您要生成大量随机数。在这种情况下,take a look atstd::uniform_int_distribution以获得更好的结果。 -
除了将行和列混淆之外,您还有像
if(input == 'd' && playerY != 9)...这样的硬编码限制。您可以通过单独开发新功能来避免此类障碍。在尝试将它们组合起来之前,分别正确设置运动和障碍物。在你把它们做对之前,不要尝试随机放置,等等。
标签: c++ arrays string multidimensional-array