Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Asking for Tips: Writing readable, beautiful code
#10
like this ?????

Code:
#include<iostream>
#include<sstream>
#include<conio.h>
#include<windows.h>

using namespace std;

int GEN;

/************************************CLASS*OF*CELL*FOR*GETTING*AND*SETTING*ALIVE*CELL***********************************/

class Cell
{
      private:
              bool Cell_Check;
      public:
            
        bool Cell_Get()const
        {
            return(Cell_Check);
        }
        
        void Cell_Set(bool Stats)
        {
            Cell_Check = Stats;
        }    
};
      
int main()
{
    system("color 0A");
    
    int cell_x, cell_y;
    
    char choice;
    
    /***************************************BASIC*GRID*INPUT*****************************************************/
    
    cout << "Enter dimensions of your cell \20\20\20" << endl;
    cin >> cell_x >> cell_y;
    
    Cell Grid[cell_y][cell_x], Dead[cell_y + 2][cell_x + 2], Next_Grid[cell_y][cell_x];
    
    for(int i = 0; i < cell_y + 2; i++)
    {
            for(int j = 0; j < cell_x + 2; j++)
            {
                    Dead[i][j].Cell_Set(false);
            }
    }
    for(int i = 0; i < cell_y; i++)
    {
            for(int j = 0; j < cell_x; j++)
            {
                    Grid[i][j].Cell_Set(false);
                    Next_Grid[i][j].Cell_Set(false);
            }
    }
    
    cout << "Do you want to give input through coordinates(c) or you want to try navigation input system(n) \20\20\20" << endl;
    cin >> choice;
    
    fflush(stdin);
    
    /***********************************************************************************************************************8/
    
    /************************************************INPUT*THROUGH*COORDINATES********************************************************************/
        if(choice == 'c')
        {
                  int co_x, co_y;
                  
                  char ch_a;
                  
                    for(int i = 0; ; i++)
                    {
                            cout << "Enter the coordinates \20\20\20 " << endl;
                            cin >> co_x >> co_y;
                            
                            Grid[co_y][co_x].Cell_Set(true);
                            
                            cout << "Do you want to add more \20\20\20" << endl;
                            cin >> ch_a;
                            
                            if(ch_a == 'n')
                            break;
                    }
    /**********************************************************************************************************************/
                    
    /****************************************OUTPUT*OF*USER'S*INPUT************************************************/                
      cout << "Your input is \20\20\20" << endl;
    
     for(int y = 0; y < cell_y; y++)
            {
                    for(int x = 0; x < cell_x; x++)
                    {
                    
                            if(Grid[y][x].Cell_Get() == false)
                                cout << " . ";
                        
                            else if(Grid[y][x].Cell_Get() == true)
                                cout << " * ";
                        
                    }
            
                    cout << endl << endl;
            }
        }                
    /*****************************************************************************************************************/
    
    
    /*****************************************NAVIGATOR SYSTEM*******************************************************/
    
    else if(choice == 'n')
    {
    
    cout << "Use W,A,S,D keys to navigate and press K to make the cell alive" << endl;
    
    char key_Arrow;
    
    int move_A = 0, move_B = 0;
    
    for(int y = 0; y < cell_y; y++)
    {
            for(int x = 0; x < cell_x; x++)
            {
                    if((x == move_A) && (y == move_B))
                    cout << " <> ";
                    
                    else
                    {
                        if(Grid[y][x].Cell_Get() == false)
                        cout << " . ";
                        
                        if(Grid[y][x].Cell_Get() == true)
                        cout << " * ";
                    }
            }
            
            cout << endl << endl;
    }
    /*************************************BASIC*KEY*NAVIGATION*********************************************************/
    for(int inf = 0; ; inf++)
    {
            key_Arrow = getch();

                                       if(key_Arrow == 'w') //UP
                                              move_B--;
                                      
                                       if(key_Arrow == 's')    //DOWN  
                                              move_B++;
                                      
                                              
                                        if(key_Arrow == 'a') //LEFT
                                              move_A--;
                                              
                                              
                                        if(key_Arrow == 'd')   //RIGHT      
                                              move_A++;
                                      
                                        
                                         if(key_Arrow == 'k')   //ALIVE
                                              Grid[move_B][move_A].Cell_Set(true);
                                        
                                              
                                        if(key_Arrow == 13)
                                             break;      

                        
            
            
            system("cls");
            
            for(int y = 0; y < cell_y; y++)
            {
                    for(int x = 0; x < cell_x; x++)
                    {
                            if((x == move_A) && (y == move_B))
                            cout << " <> ";
                    
                            else
                            {
                                if(Grid[y][x].Cell_Get() == false)
                                cout << " . ";
                        
                                if(Grid[y][x].Cell_Get() == true)
                                cout << " * ";
                            }
                    }
            
                    cout << endl << endl;
            }
    }
    /******************************************************************************************************/
    
    /*******************************OUTPUT*OF*USER'S*INPUT***********************************************/  
    
    cout << "Your input is \20\20\20" << endl;
    
     for(int y = 0; y < cell_y; y++)
            {
                    for(int x = 0; x < cell_x; x++)
                    {
                    
                            if(Grid[y][x].Cell_Get() == false)
                                cout << " . ";
                        
                            else if(Grid[y][x].Cell_Get() == true)
                                cout << " * ";
                        
                    }
            
                    cout << endl << endl;
            }
     }                      
    /**************************************************************************************************************/
    
    /*********************************MAIN GAME OF LIFE************************************************************/
    
    int flag = 0;
    char Gen, G = 'y';
    
    cout << "Do you want to view all generations(a) one by one or a specific generation(s) \20\20\20 " << endl;
    cin >> Gen;
    
    fflush(stdin);
    
    if(Gen == 's')
    {
           cout << "Enter generation e.g. 25 for 25 generation \20\20\20 ";
           cin >> GEN;
    }
    for(int k = 0; ; k++)
    {
            for(int j = 1; j <= cell_y; j++)
            {
                    for(int i = 1; i <= cell_x; i++)
                    {
                            Dead[j][i].Cell_Set(Grid[j - 1][i - 1].Cell_Get());
                    }
            }
            
            int c = 0;
            
            for(int i = 1; i <= cell_y; i++)
            {
                    for(int j = 1; j <= cell_x; j++)
                    {
                            if(Dead[i][j + 1].Cell_Get() == true)
                            c++;
                            if(Dead[i][j - 1].Cell_Get() == true)
                            c++;
                            if(Dead[i + 1][j].Cell_Get() == true)
                            c++;
                            if(Dead[i + 1][j + 1].Cell_Get() == true)
                            c++;
                            if(Dead[i + 1][j - 1].Cell_Get() == true)
                            c++;
                            if(Dead[i - 1][j].Cell_Get() == true)
                            c++;
                            if(Dead[i - 1][j + 1].Cell_Get() == true)
                            c++;
                            if(Dead[i - 1][j - 1].Cell_Get() == true)
                            c++;
                            
                            if(c < 2)
                            Next_Grid[i - 1][j - 1].Cell_Set(false);
                            
                            if(c == 3 && Dead[i][j].Cell_Get() == false)
                            Next_Grid[i - 1][j - 1].Cell_Set(true);
                            
                            if((c == 2 || c == 3) && Dead[i][j].Cell_Get() == true)
                            Next_Grid[i - 1][j - 1].Cell_Set(true);
                            
                            c = 0;
                    }
            }
            
            if(Gen == 's')
            {
                   if(k == GEN)
                   break;
            }
            
            /*************************************CHECKS*FURTHER*GENERATIONS*ARE*SAME*******************************************/
            flag = 0;
            
             for(int y = 0; y < cell_y; y++)
                       {
                               for(int x = 0; x < cell_x; x++)
                               {
                      
                                       if(Grid[y][x].Cell_Get() != Next_Grid[y][x].Cell_Get())
                                       flag = 1;
                               }
                       }
           if(flag == 0)
           {            
            cout << "further generations are all same";
            break;
           }      
          
           /*****************************************************************************************************************/
          
           /***********************************ALL*GENERATIONS*******************************************************/
            if(Gen == 'a')
            {
                   if(G == 'y')
                   {
                       cout << "Generation #" << k + 1 << " \20\20\20" << endl;                      
                        
                       for(int y = 0; y < cell_y; y++)
                       {
                               for(int x = 0; x < cell_x; x++)
                               {
                                       if(Grid[y][x].Cell_Get() == false)
                                       cout << " . ";
                        
                                       else if(Grid[y][x].Cell_Get() == true)
                                       cout << " * ";
                        
                               }
            
                               cout << endl << endl;
                        }
                      
                   }
                  
                   else
                   break;                      
                  
                   G == getch();
            }
            
            for(int j = 0; j < cell_y; j++)
            {
                    for(int i = 0; i < cell_x; i++)
                    {
                            Grid[j][i].Cell_Set(Next_Grid[j][i].Cell_Get());
                            Next_Grid[j][i].Cell_Set(false);
                    }
            }
            for(int j = 0; j < cell_y + 2; j++)
            {
                    for(int i = 0; i < cell_x + 2; i++)
                    {
                            Dead[j][i].Cell_Set(false);
                    }
            }
    }
    /**********************************************************************************************************/
    /*******************************************N*GENERATION*************************************************************/
    if(Gen == 's')
    {
         cout << "Generation # " << GEN << " \20\20\20" << endl;
                        
                       for(int y = 0; y < cell_y; y++)
                       {
                               for(int x = 0; x < cell_x; x++)
                               {
                    
                                       if(Grid[y][x].Cell_Get() == false)
                                       cout << " . ";
                        
                                       else if(Grid[y][x].Cell_Get() == true)
                                       cout << " * ";
                        
                               }
            
                               cout << endl << endl;
                        }  
    }
/**********************************************************************************************************/

  system("pause");

}
Reply
Thanks given by:


Messages In This Thread
RE: Asking for Tips: Writing readable, beautiful code - by NewToTheEra - 03-13-2014, 12:37 PM



Users browsing this thread: 1 Guest(s)