gpt4 book ai didi

c++ - 使用调试器与构建和运行 C++ 时输出不同

转载 作者:搜寻专家 更新时间:2023-10-31 01:53:53 24 4
gpt4 key购买 nike

我正在尝试制作一个基本的基于网格的地牢爬虫。当我构建并运行并且程序打印出 board[7][10] 字符数组时,板上只有一个空间有一个“T”字符。但是,如果我一步一步地使用调试器,它会完美地工作,并像我想要的那样在板上放置 3 个“T”字符。我不知道如何解决这个问题,甚至不知道是什么原因造成的。我没有从编译器中得到任何错误,它只是奇怪的输出:

主要.cpp

#include <iostream>
#include "DungeonBoard.h"
#include "Tokens.h"


int main()
{
DungeonBoard dungeonCrawl; // create DungeonBoard object
dungeonCrawl.start(); // call start function
}

DungeonBoard.h

#ifndef DUNGEONBOARD_H
#define DUNGEONBOARD_H

class DungeonBoard
{
public:
DungeonBoard(){} // default constructor
void start(); //gameplay
void printBoard(); // print out the gameboard
private:

};

#endif // DUNGEONBOARD_H

DungeonBoard.cpp

#include <iostream>
#include "DungeonBoard.h"
#include "Tokens.h"

const int ROW = 7;
const int COLUMN = 10;
char board[ROW][COLUMN]= {{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}};
bool occupied[ROW][COLUMN] = {{false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false}, {false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false}, {false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false}, {false, false, false, false, false, false, false, false, false, false}};

Tokens hero(0, 0); //creates user starting location at top left corner
Tokens treasure(6, 9); // creates treasure location at bottom left corner
Tokens trap1, trap2, trap3;

void DungeonBoard::start(){
char userDirection; // user input
bool gameOn = true; // sentinel value for while loop

std::cout << "DUNGEON CRAWL 1.0\n";
std::cout << "------------------\n\n";
std::cout << "H is the hero, T are traps, X is the treasure and .'s are open spaces.\n";
std::cout << "The goal is to move around the gameboard and reach the treasure while avoiding \ndtraps.\n\n";
std::cout << "CONTROLS: W - UP, S - DOWN, A - LEFT, D - RIGHT.\n\n";
std::cout << "Enjoy! :)\n\n";

board[hero.get_row()][hero.get_col()] = 'H'; // set user starting position
occupied[hero.get_row()][hero.get_col()] = true; // make space used
board[treasure.get_row()][treasure.get_col()] = 'X'; // set treasure position
occupied[treasure.get_row()][treasure.get_col()] = true; // make space used

trap1.trap_loc(); // randomize location of trap 1
while(occupied[trap1.get_row()][trap1.get_col()] == true){ //check to make sure space is not occupied
trap1.trap_loc(); //if occupied, re-randomize
}
trap2.trap_loc(); //randomize location of trap 2
while(occupied[trap2.get_row()][trap2.get_col()] == true){ //occupy check
trap2.trap_loc(); // re-randomize
}
trap3.trap_loc(); //randomize location of trap 3
while(occupied[trap3.get_row()][trap3.get_col()] == true){ //occupy check
trap3.trap_loc(); //re-randomize
}

//set board spaces for traps to T and mark that space as used

board[trap1.get_row()][trap1.get_col()] = 'T';
occupied[trap1.get_row()][trap1.get_col()] = true;
board[trap2.get_row()][trap2.get_col()] = 'T';
occupied[trap2.get_row()][trap2.get_col()] = true;
board[trap3.get_row()][trap3.get_col()] = 'T';
occupied[trap3.get_row()][trap3.get_col()] = true;

printBoard();
}

void DungeonBoard::printBoard(){
for(int i = 0; i<7; i++){
for(int r = 0; r<10; r++){
if(r == 0)
std::cout << " ";
std::cout << board[i][r] << " ";
}
std::cout << "\n\n";
}
}

token .h

#ifndef TOKENS_H
#define TOKENS_H


class Tokens
{
public:
Tokens(){} //default constructor
Tokens(int row, int col); //constructor with coordinates already known
void set_pos(int new_row, int new_col){ rowLoc = new_row; colLoc = new_col;} // set new row and col values
int get_row(){return rowLoc;} //row getter
int get_col(){return colLoc;} //col getter
void trap_loc(); // randomize trap location
int rand0toN1(int n); //psuedo random number generator
private:
int rowLoc, colLoc; //tokens location by row and column
};

#endif // TOKENS_H

token .cpp

#include <ctime>
#include <cstdlib>
#include "Tokens.h"
#include "DungeonBoard.h"

Tokens::Tokens(int row, int col){ //sets the position of the token
set_pos(row, col);
}


void Tokens::trap_loc(){ //gets random row and col for trap
int trapRow = rand0toN1(6);
int trapCol = rand0toN1(9);
set_pos(trapRow, trapCol); // sets position of trap token to the random numbers created
}

int Tokens::rand0toN1(int n){ //psuedo-random num generator
srand(time(NULL)); // set seed for randomization
return rand()%n;
}

最佳答案

问题是你一直在重新初始化srand在内部使用时使用相同的种子 Tokens::rand0toN1 .这是因为 time(NULL)具有秒分辨率。

通过逐步执行代码,您将导致 srand用不同的值初始化,因此是预期的结果。

srand不需要在每次使用rand 之前调用, 只需要初始化它,所以如果你移动 srand(time(NULL));到某个地方,比如 DungeonBoard::start() 的开头(基本上确保在第一次使用 rand 之前调用一次),您的程序应该按预期工作。

关于c++ - 使用调试器与构建和运行 C++ 时输出不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10474777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com