所以,我正在创建一个程序来创建一个(某种)模拟近战死亡竞赛视频游戏(目前还没有制作视频游戏,只是制作简单的AI,目标是互相杀戮)。为了做到这一点,我使用的是基于磁贴的回合制系统。
现在介绍已经不在了,这里是具体问题:在我使用的其中一个数组中,无论数组中有多少变量,最后一个值都会错误地存储在RAM中。以下是相关代码:
(我将发布我在底部的所有代码,但问题出在这里)
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
int npcTileAttacker[] = { 0, 0, 0, 0, 0};
int s = 0;
while (s < 6)
{
cout << "The value that is being selected from the array is " << s << endl;
cout << npcTileAttacker[s] << endl;
s++;
cout << "The value of s has now been set to " << s << endl;
}
输出:
The value that is being selected from the array is 0
0
The value of s has now been set to 1
The value that is being selected from the array is 1
0
The value of s has now been set to 2
The value that is being selected from the array is 2
0
The value of s has now been set to 3
The value that is being selected from the array is 3
0
The value of s has now been set to 4
The value that is being selected from the array is 4
0
The value of s has now been set to 5
The value that is being selected from the array is 5
-858993640
The value of s has now been set to 6
显然,数组的最后一个值是不正确的。我想知道的是为什么会发生这种情况。 除此之外,当程序结束时,我收到一条错误消息: “运行时检查失败#2 - 变量'npcTileAttacker'周围的堆栈已损坏。”
我尝试将s的输出值和数组代码放在程序中的其他数组周围,导致出现同样的问题。
如果需要,这是我的完整代码:
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
int numberOfNPCs = 5;
//Remember whose alive (so we can skip the dead's turns)
int npcAlive[5] = { 1, 1, 1, 1, 1 };
/*This determines what action is going to be carried out on this square. For the moment:
if npcTileActivity[n] = 1;
the goals is death
WARNING! THIS WILL RESULT IN BUGS!!! I need to figure out a way that allows multiple activities on a tile
(maybe apply actions onto NPCs directly, rather than onto their tiles)
*/
int npcTileActivity[] = { 0, 0, 0, 0, 0};
//This tells you who is doing the action on this tile
int npcTileAttacker[5] = { 1, 2, 3, 4, 0 };
int s = 0;
while (s < 6)
{
cout << "The value that is being selected from the array is " << s << endl;
cout << npcTileAttacker[s] << endl;
s++;
cout << "The value of s has now been set to " << s << endl;
}
//This determines whether or not the NPC will fight back. Currently pointless, as this will just kill them.
int npcPacifism[5] = { 0 };
//This is their HP
int npcDefense[5] = {5, 5, 5, 5, 5};
//This is the default damage (presumably this is done with knives)
int npcOffense[5] = {1, 1, 1, 1, 1};
/*This determines what each NPC wants to do.
1 - Kill Target
*/
int npcGoal[5] = {1, 1, 1, 1, 1};
//This is the NPC they are aiming at
int npcTarget[5] = {1, 2, 3, 4, 0};
/*The x coord for their target. In the future:
-I want this to be able to be changed during the sim
-This should be disabled until the NPC can find out where their target is
*/
int npcGoalLocationX[5] = {4, 1, 4, 3, 1};
/* The Y coord for their target
*/
int npcGoalLocationY[5] = {2, 3, 4, 2, 1};
/*Their x coord.
This will change, then the all npcGoalLocations need to be updated
*/
int npcLocationX[5] = {1, 4, 1, 4, 3};
/* Their y coord.
*/
int npcLocationY[5] = {1, 2, 3, 4, 2};
int m = 1;
while (m != 0)
{
int i = 0;
//Loop until every npc has had a turn
while (i < (numberOfNPCs + 1))
{
/*npcGoalLocationY[i] = npcLocationY[npcTarget[i]];
npcGoalLocationY[i] = npcLocationY[npcTarget[i]];*/
if (npcAlive[i] = 1)
{
/* Tile activities:
1 - Kill occupant
*/
if (npcTileActivity[i] = 1)
{
cout << "This shouldn't be the first thing." << endl;
//This gets the attack and defense values for the appropriate acting NPC
int j = 0;
while (j < (numberOfNPCs + 1))
{
if (npcTileAttacker[i] = j)
{
//Defender's HP - Attacker's damage
int rollAttack1 = npcDefense[i] - npcOffense[j];
if (rollAttack1 > 0)
{
npcDefense[i] = rollAttack1;
cout << "NPC " << j << " attacks NPC " << i << endl;
if (npcPacifism[i] = 0)
{
//Defender retaliates
int rollAttack2 = npcDefense[j] - npcOffense[i];
if (rollAttack2 > 0)
{
npcDefense[j] = rollAttack2;
cout << "NPC " << i << " retaliates" << endl;
}else
{
npcAlive[j] = 0;
cout << "NPC " << j << " dies" << endl;
}
}
}else
{
npcAlive[i] = 0;
cout << "NPC " << i << " dies" << endl;
}
}
j++;
}
}else
{
cout << "This should be the first." << endl;
if (npcGoal[i] != 0)
{
if (npcGoalLocationX[i] = npcLocationX[i])
{
if (npcGoalLocationY[i] = npcLocationY[i])
{
//The Tile Activity of the current NPC's target is set to whatever the current NPC's goal is
npcTileActivity[npcTarget[i]] = npcGoal[i];
}
}
if (npcGoalLocationX[i] > npcLocationX[i])
{
npcLocationX[i]++;
}
if (npcGoalLocationX[i] < npcLocationX[i])
{
npcLocationX[i]--;
}
if (npcGoalLocationY[i] > npcLocationY[i])
{
npcLocationY[i]++;
}
if (npcGoalLocationY[i] < npcLocationY[i])
{
npcLocationY[i]--;
}
}
}
}
i++;
}
cin >> m;
}
return 0;
}
另外,我遇到了一个问题(围绕着哪个cout“这应该是第一个”和“这不应该是第一件事”):不应该是第一个的是第一个,应该是首先从不执行。但是,这可能与数组错误有关。
感谢您的协助。
答案 0 :(得分:3)
你的状况是一个人:
while (s < 6)
应该是
while (s < 5)
数组{ 0, 0, 0, 0, 0}
有五个元素,因此有效索引为0,1,2,3,4
。
当s < 6
为false
时,您的情况会停止,因此s == 5
仍然如此。
答案 1 :(得分:1)
你的数组只有5个单元格:
int npcTileAttacker[] = { 0, 0, 0, 0, 0};
也就是说,s
应该从0到4,而不是0到5。
您看到的“随机”值实际上是npcTileAttacker
数组之后堆栈上的值,因为您正在溢出该数组。
答案 2 :(得分:1)
数组的大小为5.因此有效索引为0-4。因此,npcTileAttacker [5]将始终发布垃圾。
答案 3 :(得分:1)
你的while循环表达式中你已经超过了1。
你最好还是使用for循环而不是硬编码数组的长度。尝试这样的事情:
int npcTileAttacker[] = { 0, 0, 0, 0, 0};
int npcTileAttackerLength = sizeof(npcTileAttacker)/sizeof(npcTileAttacker[0]);
for(int s=0; s<npcTileAttackerLength; s++)
{
cout << "The value that is being selected from the array is " << s << endl;
cout << npcTileAttacker[s] << endl;
}
这样长度变量将始终保存数组中的项目数。