Symulacja zycia

0

Witam, chcę zrobić losową generacje pol w tym kodzie, jak mam sięza to zabrać ?

 #include "Zycie.h"
#include <iostream>
#include "time.h"
using namespace std;
 

Zycie::Zycie(void)
{int pole[17][17] = {};

}


Zycie::~Zycie(void)
{
    
}
int Zycie::menu(int pole[][17])
{
    system("cls");
    //// WYSWIETLANIE PLANSZY
    int x=0,y=0;
    cout << "          GRA W ZYCIE - SYMULACJA"<<endl<<endl;
    for(int x = 1; x <= 15; ++x)
    {
          for(int y = 1; y <= 15; ++y)
          {
                  /// SPRAWDZANIE CZY ZYWA JEST KOMORKA
                  if(pole[x][y] == 1) cout << " X ";
                  else cout << " O ";
          }
 
    cout << endl;
    }
    int wybor;
 
    cout << "\n\nWybierz opcje: ";
    
    cout << "\n1.Wybierz zywe pola";
    cout << "\n2.Przewin 1 ture" ;
    cout << "\n3.Generuj"<< endl;
    cin >> wybor;
    if(wybor == 1) { ustaw(pole); }
    else if(wybor == 2) { przewin(pole); }
    else if(wybor == 3){    }
    else {cout << "Nie ma takiej opcji"; system("pause>nul"); system("cls"); menu(pole);}
    return 0;

}

int Zycie::ustaw(int pole[][17])
{
    int ax, ay;
    system("cls");
    cout << "Jesli komorka jest zywa to zostanie zniszczona,\njesli martwa to zostanie stworzona. (1-4)";
    cout << "\nPodaj X: ";
    cin >> ax;
    if(ax < 1 || ax > 15) ustaw(pole);
    cout << "\nPodaj Y: ";
    cin >> ay;
    if(ay < 1 || ay > 15) ustaw(pole);
 
    /////// USTAWIANIE ISTNIENIA
 
    if(pole[ay][ax] == 0) { pole[ay][ax] = 0; cout << "\nPole x: " << ax << " y: " << ay << " zostalo zniszczone."; system("pause>nul");}
    else { pole[ay][ax] = 1; cout << "\nPole x: " << ax << " y: " << ay << " zostalo ozywione."; system("pause>nul");}
    menu(pole);
    return 0;
}

int Zycie::przewin(int pole[][17]) 
{
    int kopia[17][17] = {};
    for(int y = 1; y <= 15; ++y)
    {
            for(int x = 1; x <= 15; ++x)
            {
                    int l = 0;
                        if(pole[y-1][x] == 1) l++;
                        if(pole[y+1][x] == 1) l++;
                        if(pole[y-1][x-1] == 1) l++;
                        if(pole[y+1][x+1] == 1) l++;
                        if(pole[y+1][x-1] == 1) l++;
                        if(pole[y-1][x+1] == 1) l++;
                        if(pole[y][x-1] == 1) l++;
                        if(pole[y][x+1] == 1) l++;
 
                    if(pole[y][x] == 0)
                    {
                        if(l == 3) kopia[y][x] = 1;
                        else kopia[y][x] = 0;     
                    }
                    else
                    {
                        if(l == 2 || l == 3) kopia[y][x] = 1;
                        else kopia[y][x] = 0;
                    }
 
 
 
 
            }
 
    }
   
    for(int y = 1; y <= 15; ++y)
    {
            for(int x = 1; x <= 15; ++x)
            {
                    pole[y][x] = kopia[y][x];
            }
    }
    menu(pole);
    return 0;
}

0

Jak losowe to może rand()?
http://www.cplusplus.com/reference/cstdlib/rand/

0
int Zycie::Generuj(int pole[][17])
{
    srand( time( NULL ) );  
 int n=0,m=0;
    for(int n=0; n < 17; n++){
         for(int m=0 ; m < 17; m++){
             pole[n][m] = rand()%2;
        
             
    if(pole[n][m] == 1) cout << " X ";
                  else cout << " O ";
     
      }}
    menu(pole);
    return 0;
} 

Zrobiłem takie coś.

0

Teraz inny problem.
Jeżeli wygenerowalem sobie tablice , to nie ustawic recznie innej komorki na "zywa".
Jak temu zaradzic ?

 
#include "Zycie.h"
#include <iostream>
#include "time.h"
using namespace std;
 

Zycie::Zycie(void)
{int pole[17][17] = {};

}


Zycie::~Zycie(void)
{
    
}
int Zycie::menu(int pole[][17])
{
    system("cls");
    //// WYSWIETLANIE PLANSZY
    int x=0,y=0;
    cout << "          GRA W ZYCIE - SYMULACJA"<<endl<<endl;
    for(int x = 1; x <= 15; ++x)
    {
          for(int y = 1; y <= 15; ++y)
          {
                  /// SPRAWDZANIE CZY ZYWA JEST KOMORKA
                  if(pole[x][y] == 1) cout << " X ";
                  else cout << " O ";
          }
 
    cout << endl;
    }
    int wybor;
 
    cout << "\n\nWybierz opcje: ";
    
    cout << "\n1.Wybierz zywe pola";
    cout << "\n2.Przewin 1 ture" ;
    cout << "\n3.Generuj" ;
    cout << "\n4.Wyczysc"<< endl;
    cin >> wybor;
    if(wybor == 1) { ustaw(pole); }
    else if(wybor == 2) { przewin(pole); }
    else if(wybor == 3){  Generuj(pole);} 
    else if(wybor == 4){  Wyczysc(pole);}
    else {cout << "Nie ma takiej opcji"; system("pause>nul"); system("cls"); menu(pole);}
    return 0;

}

int Zycie::ustaw(int pole[][17])
{
    int ax, ay;
    system("cls");
    cout << "Jesli komorka jest zywa to zostanie zniszczona,\njesli martwa to zostanie stworzona. (1-4)";
    cout << "\nPodaj X: ";
    cin >> ax;
    if(ax < 1 || ax > 15) ustaw(pole);
    cout << "\nPodaj Y: ";
    cin >> ay;
    if(ay < 1 || ay > 15) ustaw(pole);
 
    /////// USTAWIANIE ISTNIENIA
 
    if(pole[ay][ax] == 0) { pole[ay][ax] = 0; cout << "\nPole x: " << ax << " y: " << ay << " zostalo zniszczone."; system("pause>nul");}
    else { pole[ay][ax] = 1; cout << "\nPole x: " << ax << " y: " << ay << " zostalo ozywione."; system("pause>nul");}
    menu(pole);
    return 0;
}

int Zycie::przewin(int pole[][17]) 
{
    int kopia[17][17] = {};
    for(int y = 1; y <= 15; ++y)
    {
            for(int x = 1; x <= 15; ++x)
            {
                    int l = 0;
                        if(pole[y-1][x] == 1) l++;
                        if(pole[y+1][x] == 1) l++;
                        if(pole[y-1][x-1] == 1) l++;
                        if(pole[y+1][x+1] == 1) l++;
                        if(pole[y+1][x-1] == 1) l++;
                        if(pole[y-1][x+1] == 1) l++;
                        if(pole[y][x-1] == 1) l++;
                        if(pole[y][x+1] == 1) l++;
 
                    if(pole[y][x] == 0)
                    {
                        if(l == 3) kopia[y][x] = 1;
                        else kopia[y][x] = 0;     
                    }
                    else
                    {
                        if(l == 2 || l == 3) kopia[y][x] = 1;
                        else kopia[y][x] = 0;
                    }
 
 
 
 
            }
 
    }
   
    for(int y = 1; y <= 15; ++y)
    {
            for(int x = 1; x <= 15; ++x)
            {
                    pole[y][x] = kopia[y][x];
            }
    }
    menu(pole);
    return 0;
}


int Zycie::Generuj(int pole[][17])
{
    srand( time( NULL ) );  
 int n=0,m=0;
    for(int n=0; n < 17; n++){
         for(int m=0 ; m < 17; m++){
             pole[n][m] = rand()%2;
        
             
    if(pole[n][m] == 1) cout << " X ";
                  else cout << " O ";
     
      }}
    menu(pole);
    return 0;
}

int Zycie::Wyczysc(int pole[][17])
{
     system("cls");
    //// WYSWIETLANIE PLANSZY
    int x=0,y=0;
    cout << "          GRA W ZYCIE - SYMULACJA"<<endl<<endl;
    for(int x = 1; x <= 15; ++x)
    {
          for(int y = 1; y <= 15; ++y)
          {
                  /// SPRAWDZANIE CZY ZYWA JEST KOMORKA
                  if(pole[x][y] == 1) pole[x][y] = 0;
                  {cout << " O ";}
                 // else cout << " O ";
          }
 
    cout << endl;
    }
    menu(pole);
    return 0;

}
1

Napisz to:
Jeżeli wygenerowalem sobie tablice , to nie ustawic recznie innej komorki na "zywa".
W języku polskim może?

PS ten kod jest beznadziejny, zdajesz sobie z tego sprawę, oczywiście?

1 użytkowników online, w tym zalogowanych: 0, gości: 1