Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2014 Jirka Vavřík
- *
- * Licensed under the NWL - No Whining License.
- *
- * You may use this, modify this, redistribute this provided you agree:
- * - not to whine about this product from Jirka Vavřík
- * - that there is no warranty of any kind.
- * - retain this copyright in full.
- please compile with -g
- */
- #ifdef __cplusplus
- #include <iostream>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- //this is for sleep function - its different ín Windows/UNIX
- #ifdef WIN32
- #include <windows.h>
- #else
- #include <unistd.h>
- #endif
- short count_x;
- short count_y;
- short actual_state[256][256];
- void cls() {//the easiest solution
- #ifdef WIN32
- system("cls");
- #else
- system("clear");
- #endif
- }
- short check_life(short, short);
- int main()
- {
- int field;
- char _field[3];
- srand(time(NULL));
- entering_dimensions:
- printf("\tField size: ");
- #ifdef __cplusplus
- std::cin >> field;
- #else
- fgets(_field, 4, stdin);
- field = atoi(_field);
- #endif
- count_y = field;
- count_x = field;
- if( count_x > 256 || count_y > 256) {
- printf("Number must not be more than 256!\n");
- goto entering_dimensions;
- }
- short tmp_y;
- short tmp_x;
- for( tmp_y = 0; tmp_y < count_y; tmp_y++) {
- for( tmp_x = 0; tmp_x < count_x; tmp_x++) {
- short random = rand() % 2;
- if(random == 1) {
- actual_state[tmp_x][tmp_y] = 1;
- } else {
- actual_state[tmp_x][tmp_y] = 0;
- }
- }
- }
- begin:
- cls();
- short y_ax;
- short x_ax;
- for( y_ax = 0; y_ax < count_y; y_ax++) {
- for(x_ax = 0; x_ax < count_x; x_ax++) {
- if(actual_state[x_ax][y_ax] == 1) {
- printf("#");
- } else if(actual_state[x_ax][y_ax] == 0){
- printf(" ");
- }
- }
- printf("\n");
- }
- short next_state[256][256];
- for( y_ax = 0; y_ax < count_y; y_ax++) {
- for( x_ax = 0; x_ax < count_x; x_ax++) {
- next_state[x_ax][y_ax] = check_life(x_ax, y_ax);
- }
- }
- for(y_ax = 0; y_ax < count_y; y_ax++) {
- for(x_ax = 0; x_ax < count_x; x_ax++) {
- actual_state[x_ax][y_ax] = next_state[x_ax][y_ax];
- }
- }
- //vait 1 second
- #ifdef WIN32
- Sleep(1000);
- #else
- sleep(1);
- #endif
- goto begin;
- }
- short check_life(short x, short y) {
- short surr;
- if(x == 0 && y == 0) {
- surr = actual_state[x+1][y] + actual_state[x+1][y+1] + actual_state[x][y+1];
- }
- else if (x == 0 && y != 0 && x != count_x) {
- surr = actual_state[x-1][y] + actual_state[x-1][y+1] + actual_state[x][y+1] + actual_state[x+1][y+1] + actual_state[x][y+1];
- }
- else if(x == count_x && y == 0) {
- surr = actual_state[x-1][y] + actual_state[x-1][y-1] + actual_state[x][y-1];
- }
- else if(y > 0 && x == 0 && y < count_y) {
- surr = actual_state[x][y-1] + actual_state[x+1][y-1] + actual_state[x+1][y] + actual_state[x+1][y+1] + actual_state[x][y+1];
- }
- else if(x > 0 && y > 0 && x < count_x && y < count_y) {
- surr = actual_state[x-1][y-1] + actual_state[x][y-1] + actual_state[x+1][y-1] + actual_state[y][x+1] + actual_state[y+1][x+1] + actual_state[x][y+1] + actual_state[x-1][y+1] + actual_state[x-1][y];
- }
- else if(x == count_x && y > 0 && y != count_y) {
- surr = actual_state[x][y+1] + actual_state[x-1][y-1] + actual_state[x-1][y] + actual_state[x-1][y-1] + actual_state[x][y-1];
- }
- else if(x == 0 && y == count_y) {
- surr = actual_state[x][y-1] + actual_state[x+1][y-1] + actual_state[x+1][y];
- }
- else if(x > 0 && x != count_x && y == count_y) {
- surr = actual_state[x-1][y] + actual_state[x-1][y-1] + actual_state[x][y-1] + actual_state[x+1][y-1] + actual_state[x+1][y];
- }
- else if(x == count_x && y == count_y) {
- surr = actual_state[x-1][y] + actual_state[x-1][y-1] + actual_state[x][y-1];
- }
- if (actual_state[x][y] == 1 && surr < 2)
- return 0;
- else if (actual_state[x][y] == 1 && (surr == 2 || surr == 3) )
- return 1;
- else if(actual_state[x][y] == 1 && surr > 3)
- return 0;
- else if(actual_state[x][y] == 0 && surr == 3)
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement