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?