2010-04-21 75 views
2

編輯#2 - 我完成了以前的狀態部分。現在我需要我的狀態算法,使網格像環形陣列一樣工作。也就是說,頂部/底部和左/右邊緣環繞。C++ - Conway的生命遊戲和退步

不知道如何做到這一點,但。如何更改thisTimer()中的嵌套if如果執行此操作?


編輯 - 是否連載真的有必要嗎?我看到它是如何工作的,但我只需要一個簡單的,如果粗略的解決方案就可以倒退。


我能夠創建一個版本的康威的生命遊戲,無論是前進每一次點擊,或只是用計時器向前跑。 (我正在使用Qt來做這件事。)

現在,我需要能夠保存所有以前的遊戲網格,以便我可以通過單擊按鈕向後退一步。我試圖使用一個堆棧,而且好像我正在將舊的gridcell推入堆棧。但是當我在QT中運行它時,單擊BACK時,網格不會更改。

我已經嘗試了過去三個小時不同的事情,無濟於事。有任何想法嗎?

gridwindow.cpp - 我的問題應該在這裏的某個地方。可能是handleBack()函數。

#include <iostream> 
#include "gridwindow.h" 

using namespace std; 

// Constructor for window. It constructs the three portions of the GUI and lays them out vertically. 
GridWindow::GridWindow(QWidget *parent,int rows,int cols) 
: QWidget(parent) 
{ 
    QHBoxLayout *header = setupHeader(); // Setup the title at the top. 
    QGridLayout *grid = setupGrid(rows,cols); // Setup the grid of colored cells in the middle. 
    QHBoxLayout *buttonRow = setupButtonRow(); // Setup the row of buttons across the bottom. 
    QVBoxLayout *layout = new QVBoxLayout(); // Puts everything together. 
    layout->addLayout(header); 
    layout->addLayout(grid); 
    layout->addLayout(buttonRow); 
setLayout(layout); 
} 

// Destructor. 
GridWindow::~GridWindow() 
{ 
delete title; 
} 

// Builds header section of the GUI. 
QHBoxLayout* GridWindow::setupHeader() 
{ 
QHBoxLayout *header = new QHBoxLayout(); // Creates horizontal box. 
header->setAlignment(Qt::AlignHCenter); 

this->title = new QLabel("CONWAY'S GAME OF LIFE",this); // Creates big, bold, centered label (title): "Conway's Game of Life." 
    this->title->setAlignment(Qt::AlignHCenter); 
    this->title->setFont(QFont("Arial", 32, QFont::Bold)); 

    header->addWidget(this->title); // Adds widget to layout. 

    return header;  // Returns header to grid window. 
} 

// Builds the grid of cells. This method populates the grid's 2D array of GridCells with MxN cells. 
QGridLayout* GridWindow::setupGrid(int rows,int cols) 
{ 
isRunning = false; 
QGridLayout *grid = new QGridLayout(); // Creates grid layout. 

    grid->setHorizontalSpacing(0); // No empty spaces. Cells should be contiguous. 
    grid->setVerticalSpacing(0); 
    grid->setSpacing(0); 
    grid->setAlignment(Qt::AlignHCenter); 

    for(int i=0; i < rows; i++)  //Each row is a vector of grid cells. 
    { 
    std::vector<GridCell*> row;  // Creates new vector for current row. 
    cells.push_back(row); 
    for(int j=0; j < cols; j++) 
    { 
    GridCell *cell = new GridCell(); // Creates and adds new cell to row. 
    cells.at(i).push_back(cell); 

    grid->addWidget(cell,i,j); // Adds to cell to grid layout. Column expands vertically. 
    grid->setColumnStretch(j,1); 
    } 
    grid->setRowStretch(i,1);  // Sets row expansion horizontally. 
    } 
    return grid;   // Returns grid. 
} 

// Builds footer section of the GUI. 
QHBoxLayout* GridWindow::setupButtonRow() 
{ 
QHBoxLayout *buttonRow = new QHBoxLayout(); // Creates horizontal box for buttons. 
buttonRow->setAlignment(Qt::AlignHCenter); 

// Clear Button - Clears cell; sets them all to DEAD/white. 
    QPushButton *clearButton = new QPushButton("CLEAR"); 
    clearButton->setFixedSize(100,25); 
    connect(clearButton, SIGNAL(clicked()), this, SLOT(handlePause())); // Pauses timer before clearing. 
connect(clearButton, SIGNAL(clicked()), this, SLOT(handleClear())); // Connects to clear function to make all cells DEAD/white. 
    buttonRow->addWidget(clearButton); 

// Forward Button - Steps one step forward. 
    QPushButton *forwardButton = new QPushButton("FORWARD"); 
    forwardButton->setFixedSize(100,25); 
    connect(forwardButton, SIGNAL(clicked()), this, SLOT(handleForward())); // Signals to handleForward function.. 
    buttonRow->addWidget(forwardButton); 

// Back Button - Steps one step backward. 
    QPushButton *backButton = new QPushButton("BACK"); 
    backButton->setFixedSize(100,25); 
    connect(backButton, SIGNAL(clicked()), this, SLOT(handleBack())); // Signals to handleBack funciton. 
    buttonRow->addWidget(backButton); 

// Start Button - Starts game when user clicks. Or, resumes game after being paused. 
QPushButton *startButton = new QPushButton("START/RESUME"); 
    startButton->setFixedSize(100,25); 
connect(startButton, SIGNAL(clicked()), this, SLOT(handlePause())); // Deletes current timer if there is one. Then restarts everything. 
    connect(startButton, SIGNAL(clicked()), this, SLOT(handleStart())); // Signals to handleStart function. 
    buttonRow->addWidget(startButton); 

// Pause Button - Pauses simulation of game. 
    QPushButton *pauseButton = new QPushButton("PAUSE"); 
    pauseButton->setFixedSize(100,25);  
    connect(pauseButton, SIGNAL(clicked()), this, SLOT(handlePause())); // Signals to pause function which pauses timer. 
    buttonRow->addWidget(pauseButton); 

    // Quit Button - Exits program. 
    QPushButton *quitButton = new QPushButton("EXIT"); 
quitButton->setFixedSize(100,25); 
connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));  // Signals the quit slot which ends the program. 
    buttonRow->addWidget(quitButton); 

    return buttonRow; // Returns bottom of layout. 
} 

/* 
SLOT method for handling clicks on the "clear" button. 
Receives "clicked" signals on the "Clear" button and sets all cells to DEAD. 
*/ 
void GridWindow::handleClear() 
{ 

    for(unsigned int row=0; row < cells.size(); row++) // Loops through current rows' cells. 
{ 
for(unsigned int col=0; col < cells[row].size(); col++) // Loops through the rows'columns' cells. 
    { 
    GridCell *cell = cells[row][col];  // Grab the current cell & set its value to dead. 
    cell->setType(DEAD);   
    } 
} 
} 

/* 
SLOT method for handling clicks on the "start" button. 
Receives "clicked" signals on the "start" button and begins game simulation. 
*/ 
void GridWindow::handleStart() 
{ 
isRunning = true;    // It is running. Sets isRunning to true. 
this->timer = new QTimer(this);   // Creates new timer. 
connect(this->timer, SIGNAL(timeout()), this, SLOT(timerFired())); // Connect "timerFired" method class to the "timeout" signal fired by the timer. 
this->timer->start(500);   // Timer to fire every 500 milliseconds. 
} 

/* 
SLOT method for handling clicks on the "pause" button. 
Receives "clicked" signals on the "pause" button and stops the game simulation. 
*/ 
void GridWindow::handlePause() 
{ 
if(isRunning)  // If it is running... 
    this->timer->stop(); // Stops the timer. 
isRunning = false; // Set to false. 

} 

void GridWindow::handleForward() 
{ 
if(isRunning);  // If it's running, do nothing. 
else 
    timerFired(); // It not running, step forward one step. 
} 

void GridWindow::handleBack() 
{ 
std::vector<std::vector<GridCell*> > cells2; 
if(isRunning);  // If it's running, do nothing. 
else if(backStack.empty()) 
    cout << "EMPTYYY" << endl; 
else 
{ 
cells2 = backStack.peek(); 
    for (unsigned int f = 0; f < cells.size(); f++)  // Loop through cells' rows. 
    { 
    for (unsigned int g = 0; g < cells.at(f).size(); g++) // Loop through cells columns. 
    { 
    cells[f][g]->setType(cells2[f][g]->getType()); // Set cells[f][g]'s type to cells2[f][g]'s type. 
    } 
    } 
cout << "PRE=POP" << endl; 
backStack.pop(); 
cout << "OYYYY" << endl; 
} 
} 

// Accessor method - Gets the 2D vector of grid cells. 
std::vector<std::vector<GridCell*> >& GridWindow::getCells() 
{ 
return this->cells; 
} 

/* 
TimerFired function: 
    1) 2D-Vector cells2 is declared. 
    2) cells2 is initliazed with loops/push_backs so that all its cells are DEAD. 
    3) We loop through cells, and count the number of LIVE neighbors next to a given cell. 
    --> Depending on how many cells are living, we choose if the cell should be LIVE or DEAD in the next simulation, according to the rules. 
    -----> We save the cell type in cell2 at the same indice (the same row and column cell in cells2). 
    4) After check all the cells (and save the next round values in cells 2), we set cells's gridcells equal to cells2 gridcells. 
    --> This causes the cells to be redrawn with cells2 types (white or black). 
*/ 
void GridWindow::timerFired() 
{ 
backStack.push(cells); 
std::vector<std::vector<GridCell*> > cells2; // Holds new values for 2D vector. These are the next simulation round of cell types. 
for(unsigned int i = 0; i < cells.size(); i++) // Loop through the rows of cells2. (Same size as cells' rows.) 
{ 
    vector<GridCell*> row;   // Creates Gridcell* vector to push_back into cells2. 
    cells2.push_back(row);   // Pushes back row vectors into cells2. 
    for(unsigned int j = 0; j < cells[i].size(); j++) // Loop through the columns (the cells in each row). 
    { 
    GridCell *cell = new GridCell();  // Creates new GridCell. 
    cell->setType(DEAD);  // Sets cell type to DEAD/white. 
    cells2.at(i).push_back(cell);  // Pushes back the DEAD cell into cells2. 
    }    // This makes a gridwindow the same size as cells with all DEAD cells. 
    } 

    for (unsigned int m = 0; m < cells.size(); m++) // Loop through cells' rows. 
    { 
     for (unsigned int n = 0; n < cells.at(m).size(); n++) // Loop through cells' columns. 
     { 
      unsigned int neighbors = 0;  // Counter for number of LIVE neighbors for a given cell. 

    // We know check all different variations of cells[i][j] to count the number of living neighbors for each cell. 
    // We check m > 0 and/or n > 0 to make sure we don't access negative indexes (ex: cells[-1][0].) 
    // We check m < size to make sure we don't try to access rows out of the vector (ex: row 5, if only 4 rows). 
    // We check n < row size to make sure we don't access column item out of the vector (ex: 10th item in a column of only 9 items). 
    // If we find that the Type = 1 (it is LIVE), then we add 1 to the neighbor. 
    // Else - we add nothing to the neighbor counter. 
    // Neighbor is the number of LIVE cells next to the current cell. 
    if(m > 0 && n > 0) 
    { 
    if (cells[m-1][n-1]->getType() == 1) 
    neighbors += 1; 
    } 
    if(m > 0) 
    { 
    if (cells[m-1][n]->getType() == 1) 
    neighbors += 1; 
    if(n < (cells.at(m).size() - 1)) 
    { 
    if (cells[m-1][n+1]->getType() == 1) 
     neighbors += 1; 
    } 
    } 
    if(n > 0) 
    { 
    if (cells[m][n-1]->getType() == 1) 
    neighbors += 1; 
    if(m < (cells.size() - 1)) 
    { 
     if (cells[m+1][n-1]->getType() == 1) 
     neighbors += 1; 
    } 
    } 
    if(n < (cells.at(m).size() - 1)) 
    { 
    if (cells[m][n+1]->getType() == 1) 
    neighbors += 1; 
      } 
    if(m < (cells.size() - 1)) 
    { 
    if (cells[m+1][n]->getType() == 1) 
    neighbors += 1; 
      } 
    if(m < (cells.size() - 1) && n < (cells.at(m).size() - 1)) 
    { 
    if (cells[m+1][n+1]->getType() == 1) 
    neighbors += 1; 
    } 

    // Done checking number of neighbors for cells[m][n] 
    // Now we change cells2 if it should switch in the next simulation step. 
    // cells2 holds the values of what cells should be on the next iteration of the game. 
    // We can't change cells right now, or it would through off our other cell values. 
    // Apply game rules to cells: Create new, updated grid with the roundtwo vector. 
    // Note - LIVE is 1; DEAD is 0. 
    if (cells[m][n]->getType() == 1 && neighbors < 2) // If cell is LIVE and has less than 2 LIVE neighbors -> Set to DEAD. 
       cells2[m][n]->setType(DEAD); 
      else if (cells[m][n]->getType() == 1 && neighbors > 3) // If cell is LIVE and has more than 3 LIVE neighbors -> Set to DEAD. 
       cells2[m][n]->setType(DEAD); 
      else if (cells[m][n]->getType() == 1 && (neighbors == 2 || neighbors == 3)) // If cell is LIVE and has 2 or 3 LIVE neighbors -> Set to LIVE. 
       cells2[m][n]->setType(LIVE); 
      else if (cells[m][n]->getType() == 0 && neighbors == 3) // If cell is DEAD and has 3 LIVE neighbors -> Set to LIVE. 
       cells2[m][n]->setType(LIVE); 
    } 
    } 

    // Now we've gone through all of cells, and saved the new values in cells2. 
    // Now we loop through cells and set all the cells' types to those of cells2. 
    for (unsigned int f = 0; f < cells.size(); f++)  // Loop through cells' rows. 
    { 
    for (unsigned int g = 0; g < cells.at(f).size(); g++) // Loop through cells columns. 
    { 
     cells[f][g]->setType(cells2[f][g]->getType()); // Set cells[f][g]'s type to cells2[f][g]'s type. 
    } 
    } 
} 

stack.h - 這是我的堆棧。

#ifndef STACK_H_ 
#define STACK_H_ 
#include <iostream> 
#include "node.h" 

template <typename T> 
class Stack 
{ 
private: 
    Node<T>* top; 
    int listSize; 

public: 
    Stack(); 
    int size() const; 
    bool empty() const; 
    void push(const T& value); 
    void pop(); 
    T& peek() const; 
}; 

template <typename T> 
Stack<T>::Stack() : top(NULL) 
{ 
    listSize = 0; 
} 

template <typename T> 
int Stack<T>::size() const 
{ 
    return listSize; 
} 

template <typename T> 
bool Stack<T>::empty() const 
{ 
    if(listSize == 0) 
    return true; 
    else 
    return false; 
} 

template <typename T> 
void Stack<T>::push(const T& value) 
{ 
Node<T>* newOne = new Node<T>(value); 
newOne->next = top; 
top = newOne; 
listSize++; 
} 

template <typename T> 
void Stack<T>::pop() 
{ 
    Node<T>* oldT = top; 
    top = top->next; 
    delete oldT; 
    listSize--; 
} 

template <typename T> 
T& Stack<T>::peek() const 
{ 
    return top->data; 
    // Returns data in top item. 
} 

#endif 

gridcell.cpp - 柵格單元實現

#include <iostream> 

#include "gridcell.h" 

using namespace std; 

// Constructor: Creates a grid cell. 
GridCell::GridCell(QWidget *parent) 
: QFrame(parent) 
{ 
this->type = DEAD; // Default: Cell is DEAD (white). 
setFrameStyle(QFrame::Box); // Set the frame style. This is what gives each box its black border. 

this->button = new QPushButton(this); //Creates button that fills entirety of each grid cell. 
this->button->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); // Expands button to fill space. 
this->button->setMinimumSize(19,19); //width,height   // Min height and width of button. 

QHBoxLayout *layout = new QHBoxLayout(); //Creates a simple layout to hold our button and add the button to it. 
layout->addWidget(this->button); 
setLayout(layout); 

layout->setStretchFactor(this->button,1); // Lets the buttons expand all the way to the edges of the current frame with no space leftover 
layout->setContentsMargins(0,0,0,0); 
layout->setSpacing(0); 

connect(this->button,SIGNAL(clicked()),this,SLOT(handleClick())); // Connects clicked signal with handleClick slot. 
redrawCell(); // Calls function to redraw (set new type for) the cell. 
} 

// Basic destructor. 
GridCell::~GridCell() 
{ 
delete this->button; 
} 


// Accessor for the cell type. 
CellType GridCell::getType() const 
{ 
return(this->type); 
} 

// Mutator for the cell type. Also has the side effect of causing the cell to be redrawn on the GUI. 
void GridCell::setType(CellType type) 
{ 
this->type = type; 
redrawCell(); // Sets type and redraws cell. 
} 

// Handler slot for button clicks. This method is called whenever the user clicks on this cell in the grid. 
void GridCell::handleClick() 
{   // When clicked on... 
    if(this->type == DEAD) // If type is DEAD (white), change to LIVE (black). 
    type = LIVE; 
    else 
type = DEAD;  // If type is LIVE (black), change to DEAD (white). 

    setType(type);   // Sets new type (color). setType Calls redrawCell() to recolor. 
} 

// Method to check cell type and return the color of that type. 
Qt::GlobalColor GridCell::getColorForCellType() 
{ 
switch(this->type) 
{ 
    default: 
    case DEAD: 
    return Qt::white; 
    case LIVE: 
    return Qt::black; 
} 
} 


// Helper method. Forces current cell to be redrawn on the GUI. Called whenever the setType method is invoked. 
void GridCell::redrawCell() 
{ 
Qt::GlobalColor gc = getColorForCellType(); //Find out what color this cell should be. 
this->button->setPalette(QPalette(gc,gc)); //Force the button in the cell to be the proper color. 
this->button->setAutoFillBackground(true); 
this->button->setFlat(true);  //Force QT to NOT draw the borders on the button 
} 

非常感謝。需要幫助請叫我。

回答

2

您可能想要serialise(相當全面的文章)每個步驟的遊戲狀態(對於每個玩家或全局或兩者 - 全局撤消?),然後推送該序列化狀態。然後當你需要退後時,你只需恢復正確的狀態並反序列化它。

正確序列化的額外好處是爲了保存遊戲,您只需將堆棧的序列化狀態寫入文件,然後加載遊戲即可還原堆棧的序列化狀態。

+0

可以工作......只要序列化代碼足夠聰明以排除屬於其基本狀態類「GridCell」的一部分的按鈕。 – 2010-04-21 03:08:12

+0

沒錯。或者,如果代碼被重新修改爲不再使用GridCell中的按鈕。 – 2010-04-21 03:15:16

+0

或者也許完全重新設計,將遊戲狀態包含在適當的狀態類中。 GUI和遊戲邏輯/狀態的分離很重要。 – 2010-04-21 03:17:04

2

您正在存儲指針的向量。當你恢復它們時,你自己恢復指針而不是指向的數據。

您正在TimerFired中分配很多新的GridCell對象,將它們添加到cells2數據結構中,然後泄漏它們。他們全部。覆蓋保存的指針所指向的內容之後。

您的潛在問題是模型和視圖混淆。您的基本數據結構中包含Button,這就是爲什麼您不能將它換成全新的矩陣。

編輯:等待一秒,那些GridCell實例被泄露...是GUI框架小部件?我很驚訝模擬正常運行,更不用說撤消功能。也許Qt不會分配GUI資源,直到實例成爲父對象。你以兩種不同的方式使用GridCell,這是由兩個構造函數演示的,這是一個非常糟糕的代碼味道。

+0

+1 - 在數據結構中的按鈕很棒 - 絕對會讓保存和恢復狀態更難。 – 2010-04-21 03:05:10

0

要解決超環面映射問題,您可以在每個相關的if語句中附加一個else,以處理邊緣條件。

我認爲我會做一些與衆不同的地方 - 存儲相鄰列和行的地址,處理邊緣條件,然後使用該方法查看相鄰單元格。