我正在尝试用c ++ 11中的Qt编程扫雷。
如果我按下一个带有0个炸弹的按钮,我想检查这个按钮周围的按钮,以及它们是否也有0个炸弹。如果他们有0个炸弹,我想检查按钮。 (图片:红色方块)
这是我的按钮类:
#ifndef ABUTTON_H
#define ABUTTON_H
#include "QPushButton"
#include "QMouseEvent"
class AButton : public QPushButton
{
Q_OBJECT
public:
AButton(QWidget* parent);
//AButton();
~AButton();
bool& set(bool state); //set bomb state
bool get(); //get bomb state
void increment_counter(); //increment counter for surrounding bombs
int get_counter(); //get bomb counter
bool get_locked(); //is it locked (->flagged)
void set_locked(bool); //set it to locked
AButton* get_Button(char c); //get Button above, beneath, left, right
void set_Button(AButton* Button, char c); //set Button above, ...; char is for setting the right one
private:
bool bomb; //is button a bomb
int Nachbar_Bomben; // how many bombs around this button
bool locked; // is the button locked
AButton* b_links; //pointer to the button to the left
AButton* b_rechts; //pointer to the button to the right
AButton* b_oben; //pointer to the button above
AButton* b_unten; //pointer to the button beneath
public slots:
void mousePressEvent(QMouseEvent *event);
signals:
void rightclicked();
void leftclicked();
};
#endif // ABUTTON_H
如果单击按钮会发生这种情况:
void Layout::ButtonClicked()
{
char Buffer [50];
AButton *clickedButton = qobject_cast <AButton*>(sender()); //which button
if (!clickedButton->get_locked())
{
clickedButton->setChecked(1); //set button to checked
{
if (clickedButton->get()) //Is button a bomb?
{
clickedButton->setText(QString ("B"));
Fehlermeldung *Fehler = new Fehlermeldung(); //make error window
Fehler->show();
}
else
{
if(clickedButton->get_counter() == 0) //has this button 0 bombs?
{
check_for_surrounding_bombs(clickedButton); //start the recursiv check, if there are buttons with 0 bombs around
}
else
{
sprintf(Buffer, "%i", clickedButton->get_counter());
clickedButton->setText(QString (Buffer)); //write how many bombs are in this button
}}}}}
我的问题是,我通过调用函数“check_for_surrounding_bombs”来获得SegFault。
void Layout::check_for_surrounding_bombs(AButton* clickedButton) //function doesnt work
{
if (clickedButton->get_Button('o')) //does the button above exist?
{
if (clickedButton->get_Button('o')->get_counter()== 0) //has this button 0 bombs
{
clickedButton->get_Button('o')->setText(QString ("")); // write nothing in it
if (!clickedButton->get_Button('o')->get_locked()) //if it isnt locked (= set flag)
{
clickedButton->get_Button('o')->setChecked(1); //set the button to checked
}
check_for_surrounding_bombs(clickedButton->get_Button('o')); //do the same thing for the button above
}
//... the function does the same with the buttons to the left, right, beneath
我不确定我的递归方法是否正确。
调用函数“check_for_surrounding_bombs(clickedButton);”时,调试器给出了这个错误:
实现get_Button成员fct。
AButton* AButton::get_Button(char c)
{
if (c == 'o')
return b_oben; //return button above
else if (c == 'u')
return b_unten; //return button beneath
else if (c == 'l')
return b_links; //return button to the left
else if (c == 'r')
return b_rechts; //return button to the right
else
return 0;
}
有什么想法吗?
提前谢谢。
答案 0 :(得分:0)
崩溃可能是由堆栈溢出引起的。发生这种情况是因为递归解决方案没有机制来简化问题。
如果您在递归之前更改了所访问过的每个按钮的状态,那么问题就会受到限制,您的问题就会消失。
递归解决方案需要为解决方案中的每个步骤提供一些堆栈。这是一个相对有限的资源,因为堆栈有一个限制,并且不能成长为用于其他目的的内存。所以这种情况下,如果你有一个100x100元素的网格布局,你可以有10,000步。
因此,我会寻找另一种非递归解决方案,您可以在其中创建一个向量或要访问的项目列表,并向其中添加项目。
仍然势在必行