Saturday, May 18, 2013

Game Of Life Issue - C And C++ | Dream.In.Code


Example
#{example}
"); ipb.editor_values.get('templates')['togglesource'] = new Template("
??? Cancel Source Edit
"); ipb.editor_values.get('templates')['toolbar'] = new Template(""); ipb.editor_values.get('templates')['button'] = new Template("
  • Icon
  • "); ipb.editor_values.get('templates')['menu_item'] = new Template("
  • #{title}
  • "); ipb.editor_values.get('templates')['togglesource'] = new Template("
    ??? Cancel Source Edit
    "); ipb.editor_values.get('templates')['emoticons_showall'] = new Template(""); ipb.editor_values.get('templates')['emoticon_wrapper'] = new Template("

    Emoticons

    "); // Add smilies into the mix ipb.editor_values.set( 'show_emoticon_link', false ); ipb.editor_values.set( 'bbcodes', $H({"snapback":{"id":"1","title":"Post Snap Back","desc":"This tag displays a little linked image which links back to a post - used when quoting posts from the board. Opens in same window by default.","tag":"snapback","useoption":"0","example":"[snapback]100[/snapback]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"topic":{"id":"5","title":"Topic Link","desc":"This tag provides an easy way to link to a topic","tag":"topic","useoption":"1","example":"[topic=1]Click me![/topic]","switch_option":"0","menu_option_text":"Enter the topic ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"post":{"id":"6","title":"Post Link","desc":"This tag provides an easy way to link to a post.","tag":"post","useoption":"1","example":"[post=1]Click me![/post]","switch_option":"0","menu_option_text":"Enter the Post ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"spoiler":{"id":"7","title":"Spoiler","desc":"Spoiler tag","tag":"spoiler","useoption":"0","example":"[spoiler]Some hidden text[/spoiler]","switch_option":"0","menu_option_text":"","menu_content_text":"Enter the text to be masked","single_tag":"0","optional_option":"0","image":""},"acronym":{"id":"8","title":"Acronym","desc":"Allows you to make an acronym that will display a description when moused over","tag":"acronym","useoption":"1","example":"[acronym='Laugh Out Loud']lol[/acronym]","switch_option":"0","menu_option_text":"Enter the description for this acronym (EG: Laugh Out Loud)","menu_content_text":"Enter the acronym (EG: lol)","single_tag":"0","optional_option":"0","image":""},"hr":{"id":"12","title":"Horizontal Rule","desc":"Adds a horizontal rule to separate text","tag":"hr","useoption":"0","example":"[hr]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"1","optional_option":"0","image":""},"php":{"id":"14","title":"PHP Code","desc":"Allows you to enter PHP code into a formatted/highlighted syntax box","tag":"php","useoption":"0","example":"[php]$variable = true;\n\nprint_r($variable);[/php]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"html":{"id":"15","title":"HTML Code","desc":"Allows you to enter formatted/syntax-highlighted HTML code","tag":"html","useoption":"0","example":"[html]\n \n[/html]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"sql":{"id":"16","title":"SQL Code","desc":"Allows you to enter formatted/syntax-highlighted SQL code","tag":"sql","useoption":"0","example":"[sql]SELECT p.*, t.* FROM posts p LEFT JOIN topics t ON t.tid=p.topic_id WHERE t.tid=7[/sql]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"xml":{"id":"17","title":"XML Code","desc":"Allows you to enter formatted/syntax-highlighted XML code","tag":"xml","useoption":"0","example":"[xml]\n \n Test\n \n[/xml]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"member":{"id":"31","title":"Member","desc":"Given a member name, a link is automatically generated to the member's profile","tag":"member","useoption":"1","example":"[member=skyhawk133] runs this site.","switch_option":"0","menu_option_text":"Input Username of Member","menu_content_text":"","single_tag":"1","optional_option":"0","image":"memberbbcode.png"},"extract":{"id":"33","title":"Extract Blog Entry","desc":"This will allow users to define an extract for an entry. Only this piece of the entry will be displayed on the main blog page and will show up in the RSS feed.","tag":"extract","useoption":"0","example":"[extract]This is an example![/extract]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"blog":{"id":"34","title":"Blog Link","desc":"This tag provides an easy way to link to a blog.","tag":"blog","useoption":"1","example":"[blog=100]Click me![/blog]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"entry":{"id":"35","title":"Blog Entry Link","desc":"This tag provides an easy way to link to a blog entry.","tag":"entry","useoption":"1","example":"[entry=100]Click me![/entry]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"twitter":{"id":"36","title":"Twitter","desc":"A tag to link to a user's twitter account","tag":"twitter","useoption":"0","example":"[twitter]userName[/twitter]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":"twitter.png"},"inline":{"id":"37","title":"Inline Code","desc":"Formats code inline instead of in a seperate code box. ","tag":"inline","useoption":"0","example":"[inline]style=\"font-size: 12px;\"[/inline]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"il":{"id":"38","title":"Abbreviated Inline (IL)","desc":"Abbreviated version of the [inline] tag. ","tag":"il","useoption":"0","example":"[il]Code Here[/il]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":"il2.png"},"code":{"id":"41","title":"Code","desc":"Allows you to enter general code","tag":"code","useoption":"1","example":"[code]$text = 'Some long code here';[/code]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"1","image":""}}) ); ipb.vars['emoticon_url'] = "http://cdn.dreamincode.net/forums/public/style_emoticons/default"; //Search Setup ipb.vars['search_type'] = 'forum'; ipb.vars['search_type_id'] = 15; ipb.vars['search_type_2'] = 'topic'; ipb.vars['search_type_id_2'] = 321307; //]]>

    3 Replies - 44 Views - Last Post: Today, 02:08 AM Rate Topic: -----

    #1 adam32885 ?Icon User is offline

    Reputation: 0

    • Posts: 3
    • Joined: Today, 12:41 AM

    Posted Today, 12:45 AM

    I am writing a program for my programming class and I seem to be having trouble with making sure the cells come to life correctly I think the issue is in my neighbor If statements but cant seem to find it. Here are the instructions my code and the text file

    The game of life is a computer simulation of the life and death events of a population of organisms. This program will determine the life, death, and survival of bacteria from one generation to the next, assuming the starting grid of bacteria is generation zero (0). Each cell has a total of up to 8 neighbors, including the 4 immediately adjacent cells and the 4 diagonal cells. The rules for the creation of each cell in the next generation are as follows:

    If the cell is currently empty:
    If the cell has exactly three living neighbors, it will come to life in the next generation.
    If the cell has any other number of living neighbors, it will remain empty.
    If the cell is currently living:
    If the cell has one or zero living neighbors, it will die of loneliness in the next generation.
    If the cell has four or more living neighbors, it will die of overcrowding in the next generation.
    If the cell has two or three neighbors, it will remain living.
    All births and deaths occur simultaneously (make sure you don't get this one wrong!).

    
 #include <iostream> #include <fstream> #include <iomanip> using namespace std;  const int SIZE = 20;   void initGrid(bool life[][SIZE],bool life2[][SIZE]); void readGrid(bool life[][SIZE], bool life2[][SIZE]); void printGrid(bool life[][SIZE]); int livecellNeighbors (bool life[][SIZE],int,int); int deadcellNeighbors (bool life [][SIZE],int,int); void determineNextGen(bool life[][SIZE], bool life2 [][SIZE]);   int main() {     bool life[SIZE][SIZE];     bool life2[SIZE][SIZE];      readGrid(life,life2);     int neighbors;     int neighbors2;     for (int count = 0; count < 1; count++)         {             determineNextGen(life, life2);          }      printGrid(life);     printGrid(life2);     return 0; }   /*-------------------------------------------------------             readGrid (and related functions)  ---------------------------------------------------------*/  void readGrid(bool life[][SIZE], bool life2[][SIZE]) {     ifstream infile("bacteria.txt");      int numBacteria, row, col;      initGrid(life,life2);      infile >> row >> col;     while (infile){         life[row][col] = true;         infile >> row >> col;     }     infile.close(); }   void initGrid(bool life[][SIZE],bool life2[][SIZE]) {     for (int row = 0; row < SIZE; row++){         for (int col = 0; col < SIZE; col++){             life[row][col] = false;         }     }     for (int row = 0; row < SIZE; row++){         for (int col = 0; col < SIZE; col++){             life2[row][col] = false;         }     } }  void printGrid(bool life[][SIZE]) {     cout << "  01234567890123456789" << endl;     for (int row = 0; row < SIZE; row++){         cout << setw(2) << row;         for (int col = 0; col < SIZE; col++){             if (life[row][col]){                 cout << "*";             } else {                 cout << "!";             }         }         cout << endl;     } } int livecellNeighbors (bool life[SIZE][SIZE],int row,int col) {     int neighbors=0;           if(life[row][col]== true && life[row][col-1]==true && col-1>=0)//left             {                 neighbors++;             }           if(life[row][col]== true && life[row+1][col-1]==true && row+1<=20 && col-1>=0)//left bottom             {                 neighbors++;             }           if(life[row][col]== true && life[row+1][col]==true & row+1<20)//bottom             {                 neighbors++;             }           if(life[row][col]== true && life[row][col+1]==true && col+1<20)//right             {                 neighbors++;             }           if(life[row][col]== true && life[row-1][col-1]==true && row-1>=0 && col-1>=0 )//top left             {                 neighbors++;             }           if(life[row][col]== true && life[row+1][col+1]==true && row+1<3 && col+1<=20)//bottom right             {                 neighbors++;             }           if(life[row][col]== true && life[row-1][col]==true && row-1>=0)//top             {                 neighbors++;             }           if(life[row][col]== true && life[row-1][col+1]==true && row-1>=0 && col+1<20)//top right             {                 neighbors++;             }     return neighbors; } int deadcellNeighbors (bool life [SIZE][SIZE],int row,int col) {        int neighbors2=0;        if(life[row][col]== false && life[row][col-1]==true && col-1>=0)             {                 neighbors2++;             }           if(life[row][col]== false && life[row+1][col-1]==true && row+1<=20 && col-1>=0)             {                 neighbors2++;             }           if(life[row][col]== false && life[row+1][col]==true & row+1<20)             {                 neighbors2++;             }           if(life[row][col]== false && life[row][col+1]==true && col+1<20)             {                 neighbors2++;             }           if(life[row][col]== false && life[row-1][col-1]==true && row-1>=0 && col-1>=0 )             {                 neighbors2++;             }           if(life[row][col]== false && life[row+1][col+1]==true && row+1<3 && col+1<=20)             {                 neighbors2++;             }           if(life[row][col]== false && life[row-1][col]==true && row-1>=0)             {                 neighbors2++;             }           if(life[row][col]== false && life[row-1][col+1]==true && row-1>=0 && col+1<20)             {                 neighbors2++;             }    return neighbors2;  } void determineNextGen(bool life[][SIZE],bool life2 [][SIZE]) {     int neighbors=0,neighbors2=0;     for (int row=0;row<20;row++)     {         for (int col=0;col<20;col++)         {             neighbors=livecellNeighbors(life,row,col);              if (neighbors<=1)                 life2[row][col]=false;             if (neighbors>=4)                 life2[row][col]=false;             if (neighbors==2 || neighbors==3)                 life2[row][col]=true;          }     }          for (int row=0;row<20;row++)             {             for (int col=0;col<20;col++)             {                 neighbors2=deadcellNeighbors(life,row,col);                 if (neighbors2==3)                 life2[row][col]=true;             }          }     /*for (int row=0;row<SIZE;row++)     {         for (int col=0;col<SIZE;col++)             life[row][col]=life2[row][col];     }*/  }    


    Is This A Good Question/Topic? 0

    Replies To: Game of life Issue

    #2 snoopy11 ?Icon User is online

    Reputation: 473

    • Posts: 1,522
    • Joined: 20-March 10

    Re: Game of life Issue

    Posted Today, 01:27 AM

    Hi,

    Well the first errors are on lines 100 and 139

    line 100 if(life[row][col]== true && life[row+1][col]==true & row+1<20)//bottom

    This should be && shouldn't it

    line 139 is the same...

    you have variables that you dont use as well.

    Regards

    Snoopy.


    #3 adam32885 ?Icon User is offline

    Reputation: 0

    • Posts: 3
    • Joined: Today, 12:41 AM

    Re: Game of life Issue

    Posted Today, 01:37 AM

    wow i totally missed that but i am still having some issues with the right cells coming to life but i am trying to get a for loop to take the place of all the if statements


    #4 adam32885 ?Icon User is offline

    Reputation: 0

    • Posts: 3
    • Joined: Today, 12:41 AM

    Re: Game of life Issue

    Posted Today, 02:08 AM

    so i switched to for loops but I am not sure how to check the edge cells here is my new neighbor count code
    
 int livecellNeighbors (bool life[SIZE][SIZE],int row,int col) {     int neighbors=0;          if (life[row][col]==true)         {            for (int i=-1; row+i<=row+1; i++)          {              for (int c=-1;col+c<=col+1;c++)              {                  if (i==0 && c==0)                  {                   }                   else if (life[row+i][col+c]==true)                     neighbors++;                  }              }         }      return neighbors; } 


    Page 1 of 1


    Source: http://www.dreamincode.net/forums/topic/321307-game-of-life-issue/

    LucasArts Finding Dory Chaz Ebert Mike Rice yu darvish Skylar Diggins kim jong un

    No comments:

    Post a Comment

    Note: Only a member of this blog may post a comment.