Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help on how to make an array of rectangles for game of life program  (Read 3108 times)

0 Members and 1 Guest are viewing this topic.

Idiot22

  • Newbie
  • *
  • Posts: 2
    • View Profile
So I have to code Conway's game of life using QT and SFML. I have a text version of the game and have to add graphics to it with SFML. The way my professor said we should do it is by drawing multiple rectangles (with an array) on the screen, like a grid, with each one being connected to the neighborhood array (the first rectangle is connected to (array)[0][0] and determines whether to print or not depending on if the cell is alive or dead). HOw can I do this?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Help on how to make an array of rectangles for game of life program
« Reply #1 on: June 08, 2017, 07:50:55 am »
We're not going to do your homework ;) So what part of the project do you need help with? Have you already tried something?
Laurent Gomila - SFML developer

Idiot22

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Help on how to make an array of rectangles for game of life program
« Reply #2 on: June 08, 2017, 05:03:09 pm »
This what I have. When I try to run it all I get is a blank window.

#include "game.h"
#include <gol_functions.h>
#include <iostream>
using namespace std;

Game::Game(){
    window.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Game of Life");

    window.setFramerateLimit(60);

    int x=10;
    int y=10;

    initialize(world);

    for (int i=0; i<MAX; i++)
    {
        for (int j=0; j<COL; j++)
        {
             int n;
             n=count_neighbors(world, rowpos, colpos);
             if (n<2 || n>3){
                 box[i][j].setFillColor(sf::Color::Black);
             }
             if (n==3)
             {
             box[i][j].setFillColor(sf::Color::Red);
             }
             box[i][j].setSize(sf::Vector2f(20, 20));
             box[i][j].setPosition(sf::Vector2f(x+=25, y+=25));
        }
    }

}

void Game::Draw(){
    //Look at the data and based on the data, draw shapes on window object.
   // window.draw(box);
    for (unsigned int i = 0; i <MAX ; i++)
        {
        for (int j=0; j<COL; j++)
        {
            window.draw(box[i][j]);
        }
        }
}


void Game::update()
{
    play=true;
        do
        {
            next_gen(world, rowpos, colpos);
        }while(play);
}
void Game::render(){
       window.clear();
       Draw();
       window.display();
}



void Game::processEvents()
{
   sf::Event event;
   while (window.pollEvent(event))//or waitEvent
       {
       // check the type of the event...
           switch (event.type)
           {
               // window closed
               // "close requested" event: we close the window
               case sf::Event::Closed:
                   window.close();
                   break;

               // key pressed
               case sf::Event::KeyPressed:
                   switch(sf::Event::KeyPressed)
                   {
                        case 'p':
                        case 'P':
                           if (play)
                           {
                               play=false;
                           }
                           if (!play)
                           {
                               play=true;
                           }
                           break;
                        case 's':
                        case 'S':
                           save_neighborhood(world);
                           break;
                        case 'R':
                        case 'r':
                           initialize(world);
                           break;
                        default:
                           break;
                   }

                   break;

                   default:
                       break;
           }
       }
}
void Game::run()
{
   while (window.isOpen())
   {
       processEvents();
       update();
       render(); //clear/draw/display
   }
}
 
« Last Edit: June 08, 2017, 05:22:21 pm by eXpl0it3r »

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: Help on how to make an array of rectangles for game of life program
« Reply #3 on: June 09, 2017, 01:43:23 am »
My guess:

do
{
    next_gen(world, rowpos, colpos);
}while(play);

Are you sure this loop stops at some point? Because it seems to me like it doesn't. And you get stuck in update()
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Ruskointhehizzy

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Help on how to make an array of rectangles for game of life program
« Reply #4 on: June 24, 2017, 08:23:43 am »
if(n<2||n>3)
n is an int and then you intialize it - it like looks like you go through both for loops and set the table black, because it will always go into that if statement. Is this what you intend to do?
« Last Edit: June 24, 2017, 08:27:23 am by Ruskointhehizzy »