Like Tree1Likes
  • 1 Post By iMalc

C++ excercise with linked lists not working properly, code getting complicated

This is a discussion on C++ excercise with linked lists not working properly, code getting complicated within the C++ Programming forums, part of the General Programming Boards category; Hi, it was kinda hard to title this thread, but here is the thing, I'm currently doing the excercise "Graduation" ...

  1. #1
    Registered User Vana Papi's Avatar
    Join Date
    Jul 2012
    Posts
    11

    C++ excercise with linked lists not working properly, code getting complicated

    Hi, it was kinda hard to title this thread, but here is the thing, I'm currently doing the excercise "Graduation". I'm currently at 4th *. Didn't bother to mark juvenile bunnies and marked vampires with V not X.
    I'm trying to make new babies in an adjacent square next to the mother, but I'm stuck.
    What my program does: Creates 1 new bunny in adjacent square every turn next to only 1 female.
    What I want it to do: Create 2 bunnies in adjacent square next to every female.

    Code is 412 lines long, but focus should be put on the
    Code:
    void new_bunnies()
    at line 168. If it isn't then it's
    Code:
    void  create_bunny(pos_to_create)
    line 213 . I know something is wrong in new_bunnies(), but I just can't figure it out.

    Here is the code(Deleted some couts and functions to make it shorter and more readable):
    Code:
    #include <iostream>           
    #include <time.h>            
    #include <string>            
    #include <stdio.h>   
    #include <stdlib.h>
    #include <fstream>
    #include <Windows.h>          
    #include <conio.h>            
    using namespace std;
    
    
    
    //Using strings instead of enums, here are some for bunny struct.
    
    std::string sex1[]={"male", "female", "vampire"};
    
    std::string  name1[]={"Acanthus","Ambrosia","Aria","Atiyana","Avalon","Azalea","Baylyn","Braelyn","Cadence","Calliope","Cashmere","Cassia",
    "Cayenne","Chavonne","Fauna","Flint","Forest","Forsythia","Glimmer","Harmony","Jescha","Keveen","Lark","Lilac","Meadow","Osment",
    "Quintessence","Rontae","Serendipity","Serenity","Slate","Sonnet","Taffeta","Temperance"};
    
    std::string color[]={"white", "brown", "black", "spotted"};
    
    std::string position[6400];             //Strings for board 80x80 grid
    
    struct bunny{
    public:
        std::string sex;   //"male", "female", "vampire"
        std::string color;    //"white", "brown", "black", "spotted"
        std::string name;
        int age;          
        int radioactive_mutant_vampire_bunny; //false:0 true:1
        int alive;             //false:0 true:1
        bunny* next;           //pointer to next link
        int pos;
        };
    
    
    
    bunny* root;
    bunny* conductor; 
    
    int n,r; ///////using n with for() and r with rand() functions, don't want to declare them every time
    std::string female_color[2000];  //to pass female color from new bunnies to create bunny
    int k;                           //variable for female_color
    int bun_check;                   //for main while(). Function can cout the reason for while loop to end and make bun_check to 1
    int number_of_bunnies=0;         //Just to know the number of bunnies at any point of the program in any function
    
    
    
    void create_5_bunnies();     //Creates 5 bunnies to beegin the whole mess with.               
    
    void up_age();               //Increases the age.
    
    void new_bunnies();          //Checks for males and females, appropriate  position for a new bunny, and calls create_bunny function.
    
    void create_bunny(int pos_to_create);      //Creates a bunny.
    
    void check_population();      //Checks if population hasn't exceeded 1000 and kills half if it has.
    
    void kill_old();              //Kills old bunnies.
    
    void make_mutant_bunnies();     //Makes 1 mutant bunny for every mutant bunny alive.
    
    void cull_rabbits();            //If user hits k, half of the bunnies will be killed.
    
    void clear_screen();           //Clears the screen.
    
    void cout_board();           //Couts the board.
    
    
    int main()
    {
        for(n=0;n<6400;n++){                   //Makes every grid of the board '.'                    
            position[n]='.';
        }
    
        bun_check=0;
        create_5_bunnies();
    
        while(bun_check!=1){
        new_bunnies();
    
        check_population();
            
        up_age();
            
        kill_old();
    
        make_mutant_bunnies();
    
        if(number_of_bunnies<=0){
            
            std::cout<<"\nAll bunnies died\n";
        
            
            bun_check=1;
            
            }
        if(bun_check!=1){clear_screen();} //It cleans the cause of the end otherwise.
        cout_board();
        std::cin.get();             //So you can see the problem
        
        
        }
    
    
        std::cout<<"\nOVER";
        std::cin.get();
        return(0);
    }
    
    void create_5_bunnies(){
        srand(time(NULL)+number_of_bunnies);
        root=new bunny;
        conductor=root;
        
        for(n=1;n<(5+1);n++){
            
            r=rand()%6400+1;
            conductor->pos=r;
    
            r=rand()%2+1;
            if(r==1)conductor->sex="male";
            else if(r==2)conductor->sex="female";
    
            r=rand()%4+1;
            if(r==1)conductor->color="white";
            else if(r==2)conductor->color="brown";
            else if(r==3)conductor->color="black";
            else if(r==4)conductor->color="spotted";
    
            r=rand()%34+0;
            conductor->name=name1[r];
    
            conductor->age=0;
    
            r=rand()%100+1;
            if(r==1||r==2){
                conductor->radioactive_mutant_vampire_bunny=1;
                conductor->sex="vampire";
            }
            else{conductor->radioactive_mutant_vampire_bunny=0;}
    
            if(conductor->sex=="female")position[conductor->pos]="F";
            if(conductor->sex=="male")position[conductor->pos]="M";
            if(conductor->sex=="vampire")position[conductor->pos]="V";
    
            conductor->alive=1;
            number_of_bunnies++;
    
    
            conductor->next=new bunny;
            conductor=conductor->next;
            conductor->next=0;
            cull_rabbits();
        }
    }
    
    void up_age(){
        conductor=root;
        while(conductor!=NULL){
            conductor->age++;
            conductor=conductor->next;
            cull_rabbits();
        }
    }
    
    void new_bunnies(){
        srand(time(NULL)+number_of_bunnies);
        bool temp_conductor_created=false;
        bool male_count=false;
        bool position_found=false;
        bool position_not_found=false;
        bunny* temp_conductor;
        int pos_to_create;
        k=1;
        conductor=root;
        while(conductor!=NULL){
            if(conductor->sex=="male"&&conductor->alive==1&&conductor->age>1){male_count=true;}
            conductor=conductor->next;
        }
        if(male_count==true){
        male_count=false;
        conductor=root;
        while(conductor!=NULL){
            if(conductor->sex=="female"&&conductor->alive==1&&conductor->age>1){
                temp_conductor=conductor;
                temp_conductor_created=true;
                for(n=1;n<2+1;n++){while(position_found!=true&&position_not_found!=true){
                if(position[conductor->pos+1]=="."){pos_to_create=conductor->pos+1;position_found=true;}
                else if(position[conductor->pos-1]=="."){pos_to_create=conductor->pos-1;position_found=true;}
                else  if(conductor->pos>80&&position[conductor->pos+80]=="."){pos_to_create=conductor->pos+80;position_found=true;}
                else  if(conductor->pos<6220&&position[conductor->pos-80]=="."){pos_to_create=conductor->pos-80;position_found=true;}
                else{position_not_found=true;}
                }
                if(position_found==true){
                    female_color[k]=conductor->color;
                    create_bunny(pos_to_create);
                    k++;
                    position_found==false;
                    conductor=temp_conductor;
                }
                    
                }
            }
            conductor=conductor->next;
            }
        cull_rabbits();
    
        }
    }
            
    void create_bunny(int pos_to_create){
        bool conductor_found=false;
        conductor=root;
        while(conductor_found!=true){
            if(conductor->alive==0){conductor_found=true;}
            else if(conductor->next==0){
            conductor->next=new bunny;
            conductor=conductor->next;
            conductor->next=0;
            conductor_found=true;
            }
            else{conductor=conductor->next;}
        }
        
        srand(time(NULL)+number_of_bunnies);
        r=rand()%2+1;
            if(r==1)conductor->sex="male";
            else if(r==2)conductor->sex="female";
    
            conductor->color=female_color[k];
            r=rand()%34+0;
            conductor->name=name1[r];
            conductor->age=0;
            r=rand()%100+1;
            if(r==1||r==2){
                conductor->radioactive_mutant_vampire_bunny=1;
                conductor->sex="vampire";
            }
            else{conductor->radioactive_mutant_vampire_bunny=0;}
        conductor->pos=pos_to_create;
        if(conductor->sex=="female")position[conductor->pos]='F';
        else if(conductor->sex=="male")position[conductor->pos]='M';
        else if(conductor->sex=="vampire")position[conductor->pos]='V';
    
        number_of_bunnies++;
        conductor->alive=1;
    
        check_population();
    }
    
    void check_population(){
        if(number_of_bunnies==1000){
                bunny* temp;
                temp=conductor;
                int random;
                int bunnies_killed=0;                  
                while(bunnies_killed!=500){
                    conductor=root;
                    random=rand()%number_of_bunnies+1;
                    for(n=1;n<random;n++){conductor=conductor->next;} 
                    if(conductor->alive==1){
                        conductor->alive=0;
                        position[conductor->pos]=".";
                        number_of_bunnies--;
                        bunnies_killed++;
                    }
                }
                conductor=temp;
        }
        cull_rabbits();
    }
    
    void kill_old(){
        conductor=root;
        while(conductor!=NULL){
            if(conductor->age>10&&conductor->sex!="vampire"){
            position[conductor->pos]=".";
            conductor->alive=0;
            number_of_bunnies--;
            }
            if(conductor->sex=="vampire"){
                if(conductor->age>50){
                    position[conductor->pos]=".";
                    conductor->alive=0;
                    number_of_bunnies--;
                    }
            }
            conductor=conductor->next;
        }
    cull_rabbits();
    }
    
    void make_mutant_bunnies(){
        int mutant_created=0;
        int mutant_count=0;
        conductor=root;
        while(conductor!=NULL){              
            if(conductor->radioactive_mutant_vampire_bunny==1&&conductor->alive==1){
                mutant_count=mutant_count+1;
            }
        conductor=conductor->next;
        }
    
        conductor=root;
    
        while(mutant_created!=mutant_count&&bun_check!=1){ 
            if(conductor==0){
                bool vampire_check=true;
                conductor=root;
                while(conductor!=0){
                    if(conductor->sex=!"vampire"&&conductor->alive==1){
                    vampire_check=false;
                    conductor=0;
                    }
                    else{conductor=conductor->next;}
                    }    
                if(vampire_check==true){
                std::cout<<"ALL BUNNIES TURNED INTO VAMPIRES!";
                bun_check=1;
                }
                else if(vampire_check==false){conductor=root;}
            }
    
            else if(conductor->radioactive_mutant_vampire_bunny==0&&conductor->alive==1){
                conductor->radioactive_mutant_vampire_bunny=1;
                conductor->sex="vampire";
                position[conductor->pos]="V";
                mutant_created++;
                }
            if(conductor!=0){conductor=conductor->next;}
            cull_rabbits();
        }
    }
    
    void cull_rabbits(){
        if (_kbhit())
        {
            int bunnies_killed=0;  
            switch (_getch())
            {
            case 'k':
                int random;
                int bunnies_to_kill=number_of_bunnies/2;/////////////////////////
                Sleep(3000);
                while(bunnies_killed!=bunnies_to_kill){
                    conductor=root;
                    random=rand()%number_of_bunnies+1;
                    for(n=1;n<random;n++){conductor=conductor->next;} //////////////////////////////////
                    if(conductor->alive==1){
                        conductor->alive=0;
                        position[conductor->pos]=".";
                        bunnies_killed++;
                    }
                }
         break;
            }
            number_of_bunnies=number_of_bunnies-bunnies_killed;
        }
    }
    
    void cout_board(){
        for(n=0;n<6400;n++){
            std::cout<<position[n];
            if(n==79||n==159||n==239||n==319||n==399||n==479||n==559||n==639||n==719||n==799
                ||n==879||n==959||n==1039||n==1119||n==1199||n==1279||n==1359||n==1439||n==1519
                ||n==1599||n==1679||n==1759||n==1839||n==1919||n==1999||n==2079||n==2159||n==2239||n==2319
                ||n==2399||n==2479||n==2559||n==2639||n==2719||n==2799||n==2879||n==2959||n==3039||n==3119
                ||n==3199||n==3279||n==3359||n==3439||n==3519||n==3599||n==3679||n==3759||n==3839||n==3919
                ||n==3999||n==4079||n==4159||n==4239||n==4319||n==4399||n==4479||n==4559||n==4639||n==4719
                ||n==4799||n==4879||n==4959||n==5039||n==5119||n==5199||n==5279||n==5359||n==5439||n==5519
                ||n==5599||n==5679||n==5759||n==5839||n==5919||n==5999||n==6079||n==6159||n==6239||n==6319
                ||n==6399){std::cout<<"\n";}
        }
        std::cout<<"\n";
    }
            
    void clear_screen()
    {
    DWORD n;                         /* Number of characters written */
    DWORD size;                      /* number of visible characters */
    COORD coord = {0};               /* Top left screen position */
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    
    /* Get a handle to the console */
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    
    GetConsoleScreenBufferInfo ( h, &csbi );
    
    /* Find the number of characters to overwrite */
     size = csbi.dwSize.X * csbi.dwSize.Y;
    
    /* Overwrite the screen buffer with whitespace */
    FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
    GetConsoleScreenBufferInfo ( h, &csbi );
    FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
    
    /* Reset the cursor to the top left position */
    SetConsoleCursorPosition ( h, coord );
    }
    Another question: If use alot of if, while, for statements and logical operators(the simple stuff) will it make the code more complicated than it actually is?(I can understand it, because I wrote it, but others may not). Should I use a little more advanced techniques?
    Last edited by Vana Papi; 07-16-2012 at 04:32 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Lines 366 - 374, all you need is this:
    Code:
            if ((n % 80) == 79) { std::cout << "\n"; }
    You're using srand wrong.
    Remove every place where you call it and just put this near the top of main:
    Code:
    srand(time(NULL));
    You should have stuck with the enums you were using almost a week ago, instead of switching to using strings for the gender.

    Hint: Don't tell me where to focus my efforts. If you ever want effort focussed on a particular part then you should do some work to produce a cut-down but still compileable example that still demonstrates the problem. Then there will be nothing else for us to focus on.
    Of course you then lose any benefit of other useful suggestions for improvements.
    Salem likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 12:07 PM
  2. Reducing rational numbers - code not working properly
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 07:42 AM
  3. Sorting Linked Lists (code not concept)
    By Newbie Magic in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2004, 08:57 AM
  4. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21