Results 1 to 10 of 31

Thread: Beginners Programming Challenge 16

Hybrid View

  1. #1
    Join Date
    Aug 2008
    Location
    Rio de Janeiro, Brazil
    Beans
    234
    Distro
    Ubuntu 9.10 Karmic Koala

    Beginners Programming Challenge 16

    The challenge now is related to the Conway's Game of Life. Yes - you must create a program which emulates it.
    Rules: http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
    You may consider dead anything outside the bounds of the grid.
    Input: the program will be called with two arguments: a file containing the original state and (being it a square) the length of the side of the grid.
    The file: it will be a string of 0's and 1's - 0 means a dead cell, 1 means a live cell. Example file:
    Code:
    0000000000000000000000000000000000000011000001100000000011000110000000100101010100100001110110110111000001010101010100000001110001110000000000000000000000000111000111000000010101010101000001110110110111000010010101010010000000110001100000000011000001100000000000000000000000000000000000000
    Being this in a file named 'pulsar', this should merrily display the pulsar pattern being called by:
    Code:
    ./gol pulsar 17
    It should display: http://upload.wikimedia.org/wikipedi...ife_pulsar.gif
    Displaying: get creative. If you have no GUI experience, a terminal displaying is completely acceptable.
    Ubuntu Beginners Team - For beginners in need of help.

  2. #2
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Beans
    24
    Distro
    Kubuntu 9.10 Karmic Koala

    Re: Beginners Programming Challenge 16

    Can we presume that the length of the file will be (or should be) the exact right length? Because if so we needn't really take the length of the side, surely. Or might the input file be longer than we need?

  3. #3
    Join Date
    Sep 2010
    Beans
    0

    Re: Beginners Programming Challenge 16

    Is it okay to use a non standard library?
    This is my first challenge so I don't know the rules.
    Language is C++ and SFML for the graphics.

    PHP Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <sstream>
    #include <SFML/Graphics.hpp>

    typedef std::vectorstd::vector<char> > matrix_type;

    struct Point
    {
        
    Point(int x 0int y 0
            : 
    x(x),y(y){}
        
    int x;
        
    int y;
    };

    int ToInt(const std::stringstr
    {
        
    std::istringstream is(str);
        
    int x 0;
        
    is >> x;
        return 
    x;
    }

    void PrintMatrix(const matrix_typematrix)
    {
        for(
    int i 0matrix.size(); ++i)
        {
            for(
    int j 0matrix[i].size(); ++j)
                
    std::cout << matrix[i][j];    
            
    std::cout << "\n";
        }
        
    std::cout << "\n";
    }

    void LoadMatrix(matrix_typematrixstd::ifstreamisfile)
    {
        for(
    int i 0matrix.size(); ++i)
            for(
    int j 0matrix.size(); ++j)
                
    isfile >> matrix[i][j];
    }

    const 
    int PossibleMovements 8;

    const 
    Point Movements[PossibleMovements] = 
    {
        
    Point(10),  Point(-10), 
        
    Point(01),  Point(0, -1), 
        
    Point(11),  Point(1, -1), 
        
    Point(-11), Point(-1, -1)
    };

    enum State{Dead '0'Alive '1'};

    void ChangeState(matrix_typematrixint rowint colstd::vector<Point>& changes)
    {
        
    int count 0;
        for(
    int i 0PossibleMovements; ++i)
        {
            
    int x Movements[i].col;
            
    int y Movements[i].row;
            if(
    || || >= matrix.size() || >= matrix.size() )
                continue;
            if(
    matrix[y][x] == Alive) ++count;
        }
        if(
    matrix[row][col] == Alive )
        {
            if(
    count || count 3)
                
    changes.push_back(Point(colrow) );    
        }
        else if(
    count == 3)
            
    changes.push_backPoint(colrow) );
    }

    void Pulse(matrix_typematrix)
    {
        
    std::vector<Pointchanges;
        for(
    int i 0matrix.size(); ++i)
            for(
    int j 0matrix.size(); ++j)
                
    ChangeState(matrixijchanges);
        for(
    int i 0changes.size(); ++i)
        {    
            
    char= (matrix[changes[i].y][changes[i].x]);
            
    = (== '0') ? ('1') : ('0');
        }    
        
    }

    void CommandLine(matrix_typematrix)
    {
        
    PrintMatrix(matrix);
        
        while(
    std::cin.get())
        {        
            
    Pulse(matrix);
            
    PrintMatrix(matrix);
            
    std::cout << "Enter - Next pulse\n";
        }
    }

    void Graphics(matrix_typematrix)
    {
        
    sf::RenderWindow app(sf::VideoMode(800600), "Life");
        const 
    int TileSize 10;
        
        
    sf::Clock timer;    
        while(
    app.IsOpened())
        {
            
    sf::Event event;
            while(
    app.GetEvent(event))
                if(
    event.Type == sf::Event::Closed)
                    
    app.Close();    
            
            
    app.Clear();
            if(
    timer.GetElapsedTime() > 0.5f)
            {
                
    Pulse(matrix);        
                
    timer.Reset();    
            }
            for(
    int i 0matrix.size(); ++i)
            {
                for(
    int j 0matrix.size(); ++j)
                    if(
    matrix[i][j] == Alive)
                        
    app.Draw(sf::Shape::Rectangle(TileSize jTileSize iTileSizeTileSizesf::Color::Red));
            }        
            
    app.Display();
        }
    }

    void Run(matrix_typematrix)
    {
        
    std::cout << "Graphics? [Y/N]: ";
        
    char answer;
        
    std::cin >> answer;
        
        if(
    std::tolower(answer) == 'y')
            
    Graphics(matrix);
        else
            
    CommandLine(matrix);    
    }

    int main(int argcchar** argv)
    {    
        if(
    argc 3)
        {
            
    std::cout << "Usage: " << argv[0] << " <filename> <size>\n";
            return 
    1;        
        }
        
        
    std::ifstream isfile(argv[1]);    
        if(!
    isfile)
        {
            
    std::cout << "Error: bad filename\n";
            return 
    1;
        }
        
        
        const 
    int Size ToInt(argv[2]);
        
        
    matrix_type matrix(Sizestd::vector<char>(Size));
        
    LoadMatrix(matrixisfile);
            
        
    Run(matrix);
    }    



    /*
    CPP=g++
    FLAGS=-Wall -Wextra -pedantic -ansi
    LIBS=-lsfml2-graphics -lsfml2-window -lsfml2-system


    Main:    main.o
        $(CPP) $(FLAGS) -o Main main.o $(LIBS)
        
        
    Main.o: main.cpp
        $(CPP) -c main.cpp
    */ 

  4. #4
    Join Date
    Aug 2008
    Location
    Rio de Janeiro, Brazil
    Beans
    234
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Beginners Programming Challenge 16

    Yes the file will be the right length. I realize that you can do without the size, bonus points I guess.
    To the non-standard library, can I download it from the ubuntu repo?
    Last edited by pedro3005; October 9th, 2010 at 02:48 AM.
    Ubuntu Beginners Team - For beginners in need of help.

  5. #5
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginners Programming Challenge 16

    Here's a few more patterns for people to test with. I'll withhold my code until some more real entries are in.

    Beacon (4x4)
    Code:
    1100110000110011
    Glider (10x10)
    Code:
    0010000000101000000001100000000000000000000000000000000000000000000000000000000000000000000000000000
    Gosper's Glider Gun (38x25) (Very cool)
    Code:
    00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000010100000000000000000000000001100000011000000000000110000000000000100010000110000000000001100110000000010000010001100000000000000001100000000100010110000101000000000000000000000001000001000000010000000000000000000000001000100000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    38x38 if your program requires the board to be square:
    Code:
    0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000001010000000000000000000000000110000001100000000000011000000000000010001000011000000000000110011000000001000001000110000000000000000110000000010001011000010100000000000000000000000100000100000001000000000000000000000000100010000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    Last edited by schauerlich; October 9th, 2010 at 07:28 PM.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  6. #6
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    977
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginners Programming Challenge 16

    Is the program supposed to emulate the game of life as well, should the image change or is it just the initial snapshot of the image?
    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


  7. #7
    Join Date
    Sep 2010
    Beans
    0

    Re: Beginners Programming Challenge 16

    Quote Originally Posted by pedro3005 View Post
    Yes the file will be the right length. I realize that you can do without the size, bonus points I guess.
    To the non-standard library, can I download it from the ubuntu repo?
    The version of the library I'm using is not yet in the repositories.

  8. #8
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: Beginners Programming Challenge 16

    I found comprehensive library of patterns and there is a Java applet that renders them. It looks like they don't surround the field with dead cells but let things wrap round. It would be nice to know the file format so we can try them in our own gol apps too.

  9. #9
    Join Date
    Sep 2009
    Location
    UK
    Beans
    535
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Beginners Programming Challenge 16

    This is a very interesting and fun challenge .

    @mo.reina: I like your idea of using the unicode boxes; they make it easier to read than the +s and -s that I was using.

    A note of warning for those taking part: don't modify your grid in place. You need to make a full clone of your data structure, applying the rules to the original but writing to the clone. Otherwise the patterns get messed up! (I made this mistake at first and it took me ages to work out what the problem was ).

    I look forward to seeing everyones' solutions and will post my (ruby) version after the challenge has been judged.
    DMedia - Distributed Media Library
    LaVida - A simulation game for Linux
    AskUbuntu

  10. #10
    Join Date
    Nov 2009
    Beans
    1,081

    Re: Beginners Programming Challenge 16

    Some fun extensions to try:

    * Multiple species: anything but 0 is a species, amend the dead cell -> live cell rule to pick a majority if present and randomly otherwise.
    * Steady-state detection.
    * Cycle detection, a superproblem of the previous.


    Code:
    0010000010000001110000000000000022200002000000020
    has a sample 6x6 grid w/ 2 gliders; according to my sim (a very uninteresting and straightforwards Perl implementation -- one module which does the computation, and one driver script which handles reading the file and outputing results to STDOUT; never bothered to learn any graphic modules for Perl), glider #2 is gone 4 iterations after the start state, and 6 iterations after the start state a steady state is reached w/ 6 1's.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •