康威的生命游戏 - C ++和Qt

时间:2010-04-08 01:21:55

标签: c++ qt

我已经完成了所有布局并且编写了大部分代码。但是,我被困在两个地方。

1)我不太确定如何设置计时器。我在gridwindow类中正确使用它吗?而且,我是否正确使用了定时器功能/信号/插槽与其他网格窗口功能。

2)在GridWindow的timerFired()函数中,我无法检查/创建矢量向量。我在该函数的评论中写到了我正在尝试的内容。

非常感谢任何帮助。

的main.cpp

// Main file for running the grid window application.
#include <QApplication>
#include "gridwindow.h"
//#include "timerwindow.h"
#include <stdexcept>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>

void Welcome();             // Welcome Function - Prints upon running program; outputs program name, student name/id, class section.
void Rules();               // Rules Function: Prints the rules for Conway's Game of Life.

using namespace std;

// A simple main method to create the window class  and then pop it up on the screen.
int main(int argc, char *argv[])
{
    Welcome();                                      // Calls Welcome function to print student/assignment info.
    Rules();                                        // Prints Conway's Game Rules.
    QApplication app(argc, argv);                   // Creates the overall windowed application.
    int rows = 25, cols = 35;                       //The number of rows & columns in the game grid.
    GridWindow widget(NULL,rows,cols);              // Creates the actual window (for the grid).
    widget.show();                                  // Shows the window on the screen.
    return app.exec();                              // Goes into visual loop; starts executing GUI.
}    

// Welcome Function: Prints my name/id, my class number, the assignment, and the program name.
void Welcome()                                                              
{
    cout << endl;
    cout << "-------------------------------------------------------------------------------------------------" << endl;
    cout << "Name/ID - Gabe Audick #7681539807" << endl;
    cout << "Class/Assignment - CSCI-102 Disccusion 29915: Homework Assignment #4" << endl;
    cout << "-------------------------------------------------------------------------------------------------" << endl << endl;
}

// Rules Function: Prints the rules for Conway's Game of Life.
void Rules()
{
    cout << "Welcome to Conway's Game of Life." << endl;
    cout << "Game Rules:" << endl;
    cout << "\t 1) Any living cell with fewer than two living neighbours dies, as if caused by underpopulation." << endl;
    cout << "\t 2) Any live cell with more than three live neighbours dies, as if by overcrowding." << endl;
    cout << "\t 3) Any live cell with two or three live neighbours lives on to the next generation." << endl;
    cout << "\t 4) Any dead cell with exactly three live neighbours becomes a live cell." << endl << endl;
    cout << "Enjoy." << endl << endl;
}

gridcell.h

// A header file for a class representing a single cell in a grid of cells.
#ifndef GRIDCELL_H_
#define GRIDCELL_H_

#include <QPalette>
#include <QColor>
#include <QPushButton>
#include <Qt>
#include <QWidget>
#include <QFrame>
#include <QHBoxLayout>
#include <iostream>

// An enum representing the two different states a cell can have. 
enum CellType
{
    DEAD,                   // DEAD = Dead Cell. --> Color = White.
    LIVE                    // LIVE = Living Cell. ---> Color = White.
};

/*
Class: GridCell.
    A class representing a single cell in a grid.  Each cell is implemented
    as a QT QFrame that contains a single QPushButton.  The button is sized
    so that it takes up the entire frame.  Each cell also keeps track of what
    type of cell it is based on the CellType enum.
*/
class GridCell : public QFrame
{
    Q_OBJECT                            // Macro allowing us to have signals & slots on this object.

    private:    
        QPushButton* button;            // The button inside the cell that gives its clickability.
        CellType type;                  // The type of cell (DEAD or LIVE.)

    public slots:
        void handleClick();             // Callback for handling a click on the current cell.
        void setType(CellType type);    // Cell type mutator. Calls the "redrawCell" function.

    signals:
        void typeChanged(CellType type);        // Signal to notify listeners when the cell type has changed.

    public:
        GridCell(QWidget *parent = NULL);       // Constructor for creating a cell. Takes parent widget or default parent to NULL.
        virtual ~GridCell();                    // Destructor.
        void redrawCell();                      // Redraws cell: Sets new type/color.
        CellType getType() const;               //Simple getter for the cell type.

    private:
        Qt::GlobalColor getColorForCellType();  // Helper method. Returns color that cell should be based from its value.       
};

#endif

gridcell.cpp

#include <iostream>

#include "gridcell.h"
#include "utility.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();
}

// 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
}

gridwindow.h

// A header file for a QT window that holds a grid of cells.
#ifndef GRIDWINDOW_H_
#define GRIDWINDOW_H_

#include <vector>
#include <QWidget>
#include <QTimer>
#include <QGridLayout>
#include <QLabel>
#include <QApplication>
#include "gridcell.h"

/*
class GridWindow:
    This is the class representing the whole window that comes up when this program runs.  
    It contains a header section with a title, a middle section of MxN cells and a bottom section with buttons.
*/
class GridWindow : public QWidget
{
    Q_OBJECT                            // Macro to allow this object to have signals & slots.

    private:
        std::vector<std::vector<GridCell*> > cells;     // A 2D vector containing pointers to all the cells in the grid.
        QLabel *title;                                  // A pointer to the Title text on the window.
        QTimer *timer;                                  // Creates timer object.

    public slots:
        void handleClear();             // Handler function for clicking the Clear button.
        void handleStart();             // Handler function for clicking the Start button.
        void handlePause();             // Handler function for clicking the Pause button.
        void timerFired();              // Method called whenever timer fires.

    public:
        GridWindow(QWidget *parent = NULL,int rows=3,int cols=3);       // Constructor.
        virtual ~GridWindow();                                          // Destructor.
        std::vector<std::vector<GridCell*> >& getCells();               // Accessor for the array of grid cells.

    private:
        QHBoxLayout* setupHeader();                     // Helper function to construct the GUI header.
        QGridLayout* setupGrid(int rows,int cols);      // Helper function to constructor the GUI's grid.
        QHBoxLayout* setupButtonRow();                  // Helper function to setup the row of buttons at the bottom.
};

#endif

gridwindow.cpp

#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)
{
    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(handleClear()));     
    buttonRow->addWidget(clearButton);

    // 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(handleStart()));     
    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()));     
    buttonRow->addWidget(pauseButton);  

    // Quit Button - Exits program.
    QPushButton *quitButton = new QPushButton("EXIT");
    quitButton->setFixedSize(100,25); 
    connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));     
    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++)
        {
            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()
{
    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()
{
        this->timer->stop();        // Stops the timer.
        delete this->timer;         // Deletes timer.
}

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


void GridWindow::timerFired()
{
    // I'm not sure how to write this code.
    // I want to take the original vector-vector, and also make a new, empty vector-vector of the same size.
    // I would then go through the code below with the original vector, and apply the rules to the new vector-vector.
    // Finally, I would make the new vector-vecotr the original vector-vector. (That would be one step in the simulation.)

    cout << cells[1][2];    
    /*
     for (unsigned int m = 0; m < original.size(); m++)
     {
         for (unsigned int n = 0; n < original.at(m).size(); n++)
         {
             unsigned int neighbors = 0;                                //Begin counting number of neighbors.

             if (original[m-1][n-1].getType() == LIVE)      // If a cell next to [i][j] is LIVE, add one to the neighbor count.
                neighbors += 1;
             if (original[m-1][n].getType() == LIVE) 
                neighbors += 1;
             if (original[m-1][n+1].getType() == LIVE) 
                neighbors += 1;
             if (original[m][n-1].getType() == LIVE) 
                neighbors += 1;
             if (original[m][n+1].getType() == LIVE) 
                neighbors += 1;
             if (original[m+1][n-1].getType() == LIVE) 
                neighbors += 1;
             if (original[m+1][n].getType() == LIVE) 
                neighbors += 1;
             if (original[m+1][n+1].getType() == LIVE) 
                neighbors += 1;

             if (original[m][n].getType() == LIVE && neighbors < 2)             // Apply game rules to cells: Create new, updated grid with the roundtwo vector.
                roundtwo[m][n].setType(LIVE);
             else if (original[m][n].getType() == LIVE && neighbors > 3)
                roundtwo[m][n].setType(DEAD);
             else if (original[m][n].getType() == LIVE && (neighbors == 2 || neighbors == 3))
                roundtwo[m][n].setType(LIVE);
             else if (original[m][n].getType() == DEAD && neighbors == 3)
                roundtwo[m][n].setType(LIVE);  
         }
    }*/
}

1 个答案:

答案 0 :(得分:3)

看起来计时器已正确设置给我。

对于timerFired()函数,我不会创建GridCell个对象的临时矩阵,而只是一个临时的标志矩阵,用于指示单元格是否有效。这些标志实际上是函数中的所有变化,因此只需临时存储新标志,然后将它们设置在原始网格单元格上,以节省创建临时单元格矩阵所需的所有额外内存和分配时间。这是一个例子:

//Store flags that represent whether the new grid cells will be live or not
vector< vector<bool> > is_live(cells.size());
for(int m=0; m<cells.size(); m++)
{
    is_live.at(m).resize(cells.at(m).size());
    for(int n=0; n<cells.at(m).size(); n++)
    {
        //count neighbors
        unsigned int neighbors = 0;
        for(int i=-1; i<=1; i++)
        {
            for(int j=-1; j<=1; j++)
            {
                neighbors += static_cast<int>(
                             cells[m+i][n+j]->getType() == LIVE);
            }
        }
        //we counted the current cell when counting the neighbors so
        //subtract it back off if needed.
        neighbors -= static_cast<int>(cells[m][n]->getType() == LIVE);

        //Set the type to the original value
        is_live[m][n] = cells[m][n]->getType() == LIVE;

        //change it based on the neighbor count.
        //Some of your logic around here seemed repetitive so I 
        //did it differently.  You may want to change it back 
        //if you had a specific purpose for the way you did it
        is_live[m][n] = (is_live[m][n] && neighbors <= 3) || 
                        (!is_live[m][n] && neighbors == 3);
    }
}

//Set the cell types based on the is_live flags.
for(int m=0; m<cells.size(); m++)
{
    for(int n=0; n<cells.at(m).size(); n++)
        cells[m][n]->setType(is_live[m][n] ? LIVE : DEAD);
}

注意:我没有编译或测试这个,所以没有保证。