这段特殊代码导致我的程序崩溃。是什么原因?
square [b] = 'T';
其中' square'是具有64个元素的1维字符数组,b是整数。 当我给出常量而不是b时,它不会崩溃,例如,
square [5] = 'T';
编辑: @smilepleeaz导致问题的部分..
void traps()
{
int a,b,r;
cout<<"Deciding placement of traps";
for (a = 1; a <= 8; a++)
{
r = clock();
cout<<"\nPress any key\n";
getch();
cout<<"\nPress any key\n";
getch();
r = r - clock();
b = r % 64;
if (square [b] == '.')
{
square[b] = 'T';
}
}
cout<<"\nTraps generated..... Press any key to continue\n";
getch();
}
这是整个代码......
#include <iostream>
#include <conio.h>
#include <ctime> //mainly to generate random numbers
#include <cstdlib> // for clear screen
using namespace std;
int trap_hit = 0; //0 => not stepped on trap
//1 => stepped on trap
char m; //where the player wants to move
int player_position = 0;
//The game board "." denotes empty square
char square [64] {'O','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','X'};
//place traps
void traps()
{
int a,b,r;
cout<<"Deciding placement of traps";
for (a = 1; a <= 8; a++)
{
r = clock();
cout<<"\nPress any key\n";
getch();
cout<<"\nPress any key\n";
getch();
r = r - clock();
b = r % 64;
if (square [b] == '.')
{
square[b] = 'T';
cout<<"Debug1"; //for debugging
}
}
cout<<"\nTraps generated..... Press any key to continue\n";
getch();
}
//to draw the board
void board()
{
system ("cls");
cout<<" Dungeon crawl\n";
cout<<"INSTRUCTIONS:use the keys \"w\", \"s\", \"a\" and \"d\"\n to move up, down, left and right respectively.\n Do not touch the Traps (T) or enemies (E)\n";
cout<<"\n "<<square [0]<<" "<<square [1]<<" "<<square [2]<<" "<<square [3]<<" "<<square [4]<<" "<<square [5]<<" "<<square [6]<<" "<<square [7]<<"\n";
cout<<"\n "<<square [8]<<" "<<square [9]<<" "<<square [10]<<" "<<square [11]<<" "<<square [12]<<" "<<square [13]<<" "<<square [14]<<" "<<square [15]<<"\n";
cout<<"\n "<<square [16]<<" "<<square [17]<<" "<<square [18]<<" "<<square [19]<<" "<<square [20]<<" "<<square [21]<<" "<<square [22]<<" "<<square [23]<<"\n";
cout<<"\n "<<square [24]<<" "<<square [25]<<" "<<square [26]<<" "<<square [27]<<" "<<square [28]<<" "<<square [29]<<" "<<square [30]<<" "<<square [31]<<"\n";
cout<<"\n "<<square [32]<<" "<<square [33]<<" "<<square [34]<<" "<<square [35]<<" "<<square [36]<<" "<<square [37]<<" "<<square [38]<<" "<<square [39]<<"\n";
cout<<"\n "<<square [40]<<" "<<square [41]<<" "<<square [42]<<" "<<square [43]<<" "<<square [44]<<" "<<square [45]<<" "<<square [46]<<" "<<square [47]<<"\n";
cout<<"\n "<<square [48]<<" "<<square [49]<<" "<<square [50]<<" "<<square [51]<<" "<<square [52]<<" "<<square [53]<<" "<<square [54]<<" "<<square [55]<<"\n";
cout<<"\n "<<square [56]<<" "<<square [57]<<" "<<square [58]<<" "<<square [59]<<" "<<square [60]<<" "<<square [61]<<" "<<square [62]<<" "<<square [63]<<"\n";
}
//allow user to move
void pl_move()
{
char move_dir;
invalid_move:
cout<<"\nPress w, a, s or d ...... ";
cin>>move_dir;
cin.clear();
cin.ignore(100000, '\n');
//allow the player to move
if (move_dir == 'w')
{
if (player_position < 8) //trying to move out of the board
{
cout<<"Invalid move";
goto invalid_move;
}
square [player_position] = '.';
player_position = (player_position - 8);
if (square [player_position] == 'T')
{
trap_hit = 1;
}
square [player_position] = 'O';
}
else if (move_dir == 's')
{
if (player_position >55 && player_position <64) //trying to move out of the board
{
cout<<"Invalid move";
goto invalid_move;
}
square [player_position] = '.';
player_position = (player_position + 8);
if (square [player_position] == 'T')
{
trap_hit = 1;
}
square [player_position] = 'O';
}
else if (move_dir == 'a')
{
if ((player_position % 8) == 0) //trying to move out of the board
{
cout<<"Invalid move";
goto invalid_move;
}
square [player_position] = '.';
player_position = (player_position - 1);
if (square [player_position] == 'T')
{
trap_hit = 1;
}
square [player_position] = 'O';
}
else if (move_dir == 'd')
{
if ((player_position % 8) == 7) //trying to move out of the board
{
cout<<"Invalid move";
goto invalid_move;
}
square [player_position] = '.';
player_position = (player_position + 1);
if (square [player_position] == 'T')
{
trap_hit = 1;
}
square [player_position] = 'O';
}
else
{
cout<<"\nInvalid move";
goto invalid_move;
}
}
int main()
{
traps();
while(trap_hit == 0 && player_position != 63)
{
board();
pl_move();
}
board();
if (trap_hit == 1)
{
cout<<"\nYou have stepped on a trap. You lose!";
}
else if (player_position == 63)
{
cout<<"\nYou have retrieved the treasure and won!";
}
getch();
return 0;
}
答案 0 :(得分:0)
您需要检查数组边界的有效性和b
的值。
b
显然应该是:
(伪代码):
MinBound <= b <= MaxBound
在您的情况下,您可以执行以下操作:
void traps()
{
int a,b,r;
cout<<"Deciding placement of traps";
for (a = 1; a <= 8; a++)
{
r = clock();
cout<<"\nPress any key\n";
getch();
cout<<"\nPress any key\n";
getch();
cout<<"f1"; //debug
r = clock() - r; //Edited to fix negetive values
cout<<"f2";
b = r % 64;
cout<<"f3";
//if (square [b] == '.')
//{
if(b < 0 || b>=64) //Validation Here
continue;
square[b] = 'T';
cout<<"Debug1";
//}
}
cout<<"\nTraps generated..... Press any key to continue\n";
getch();
}