First Upload
This commit is contained in:
2378
Projects/LifeGame/General.c
Normal file
2378
Projects/LifeGame/General.c
Normal file
File diff suppressed because it is too large
Load Diff
219
Projects/LifeGame/General.h
Normal file
219
Projects/LifeGame/General.h
Normal file
@@ -0,0 +1,219 @@
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#define DAYS_IN_MONTH 30
|
||||
#define MONTHS_IN_YEAR 12
|
||||
#define ONE_HUNDRED 100
|
||||
#define ONE_THOSEND 10000
|
||||
#define BOOLEAN unsigned short
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define SEVEN 7
|
||||
#define EIGHT 8
|
||||
#define TEN 10
|
||||
#define TWELVE 12
|
||||
#define THIRTY 30
|
||||
#define ZERO 0
|
||||
#define ONE 1
|
||||
#define TWO 2
|
||||
#define THREE 3
|
||||
#define FOUR 4
|
||||
#define FIVE 5
|
||||
#define TAKE_SIGNED_MATH(x) (((2 * (x)) + 1) % 2)
|
||||
#define TAKE_SIGNED(x) ((x) < 0) ? -1 : 1
|
||||
#define ABS(x) (x) * (((2 * (x)) + 1) % 2)
|
||||
#define MAX(x, y) (x > y) ? x : y
|
||||
#define MIN(x, y) (x < y) ? x : y
|
||||
#define BACKSLASH_ZERO '\0'
|
||||
#define MAX_SIZE_STRING 256
|
||||
#define MASK_FIRST_BIT 0x1
|
||||
|
||||
typedef char string[MAX_SIZE_STRING];
|
||||
typedef unsigned char byte;
|
||||
typedef unsigned short word;
|
||||
typedef unsigned int dblword;
|
||||
|
||||
typedef union
|
||||
{
|
||||
int int_;
|
||||
char char_;
|
||||
double double_;
|
||||
void * pointer;
|
||||
} Data_Type;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Data_Type * values;
|
||||
unsigned int items;
|
||||
} stack;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Data_Type * values;
|
||||
unsigned int items;
|
||||
} queue;
|
||||
|
||||
struct LLL
|
||||
{
|
||||
Data_Type value;
|
||||
struct LLL * next;
|
||||
};
|
||||
|
||||
struct DLLL
|
||||
{
|
||||
Data_Type info;
|
||||
struct DLLL * next;
|
||||
struct DLLL * prev;
|
||||
};
|
||||
|
||||
struct CLLL
|
||||
{
|
||||
Data_Type info;
|
||||
struct CLLL * next;
|
||||
};
|
||||
|
||||
struct CDLLL
|
||||
{
|
||||
Data_Type info;
|
||||
struct CDLLL * next;
|
||||
struct CDLLL * prev;
|
||||
};
|
||||
|
||||
struct SM
|
||||
{
|
||||
Data_Type info;
|
||||
int row;
|
||||
int col;
|
||||
struct SM * col_next;
|
||||
struct SM * row_next;
|
||||
};
|
||||
|
||||
typedef struct SM SM;
|
||||
typedef struct LLL LLL;
|
||||
typedef struct DLLL DLLL;
|
||||
typedef struct CLLL CLLL;
|
||||
typedef struct CDLLL CDLLL;
|
||||
|
||||
void ChangeYearsToMonths(int *years, short *month);
|
||||
void SwapNumbers(int *ptr_number1, int *ptr_number2);
|
||||
void SwapChars(char *ptr_char1, char *ptr_char2);
|
||||
void ChangeMonthsToDays(short *months, int *days);
|
||||
void ChangeDateToDays(int *date, int *days);
|
||||
void ChangeDaysToMonths(int *days, int *months);
|
||||
void ChangeMonthsToYears(int *months, int *years);
|
||||
void ChangeDaysToDate(int *days, int *date);
|
||||
int DifferentDates(int *date1, int *date2, int *different);
|
||||
BOOLEAN EvenNumber(int num);
|
||||
int OppositeNumber(int num);
|
||||
int OddDigitsInNumber(int num);
|
||||
int EvenDigitsInNumber(int num);
|
||||
int SumOfNumberWithPositiveDigitsAndNumberWithNegativeDigits(int num);
|
||||
double Power(float num, short pow);
|
||||
int GetRest(double num, unsigned short count_digits);
|
||||
unsigned short CountDigits(int num);
|
||||
float ConvertBaseToDeciaml(float num, float base, unsigned short rest_size);
|
||||
double Sum(double number1, double number2);
|
||||
int SumOfDigitsNumber(double number);
|
||||
int SumEvenDigits(double number);
|
||||
int SumOddDigits(double number);
|
||||
BOOLEAN CommonDigitsInTwoNumbers(double number1, double number2);
|
||||
double Sqrt(double number, short root);
|
||||
BOOLEAN PrimeNumber(int number);
|
||||
double Module(double number, double div); // TODO : FIX IT
|
||||
BOOLEAN DigitInNumber(double number, unsigned short digit);
|
||||
int ConcatenationNumbers(short number1, short number2);
|
||||
int Multiplication(short number1, short multiply); // TODO : FIX IT
|
||||
int GetDenominator(double number);
|
||||
short GetCounter(double number);
|
||||
double Divide(double number, double div); // TODO : FIX IT
|
||||
BOOLEAN SumInArray(int array[], unsigned int size, int sum);
|
||||
BOOLEAN ChangeBool(BOOLEAN bool1);
|
||||
BOOLEAN Equality(int number1, int number2);
|
||||
BOOLEAN NumBBiggerThenNumA(int a, int b);
|
||||
unsigned int SumOfTimesCharsArray1InCharsArray2(char array1[],
|
||||
unsigned int array1_size,
|
||||
char array2[],
|
||||
unsigned int array2_size);
|
||||
unsigned int CreateMask(unsigned short size, unsigned short digit_of_mask);
|
||||
unsigned short CountADigitInNumber(int number, unsigned short digit);
|
||||
short GetDigit(int number, unsigned short location);
|
||||
unsigned short CountOfIdenticalDigitsInSomeIndex(int number1, int number2);
|
||||
unsigned short CountOfIdenticalDigits(int number1, int number2);
|
||||
BOOLEAN ForeignDigits(int number);
|
||||
void PrintMatrix(void * ptr_mat, unsigned short col, unsigned short row, void print(void *));
|
||||
void CopyChar(char * ch, char * copy);
|
||||
void ResetMatrix(void * ptr_mat, unsigned short col, unsigned short row, void * type, void insert(void *, void *));
|
||||
BOOLEAN CharInString(char text[], char c);
|
||||
char * LastCharOfString(char * start_string);
|
||||
unsigned short StringLenght(char *start_string);
|
||||
BOOLEAN StringCompare(char *start_stringA_index, char *start_stringB_index);
|
||||
char * IndexCharInPointers(char *start_pointer, char *end_pointer , char c);
|
||||
unsigned short CountCharInString(char *start_string_index, char *c);
|
||||
void CutString(char *start_textA_index, char *end_textA_index, char *start_textB_index);
|
||||
void CopyString(char *start_string_index, char *start_copy_index);
|
||||
void LinkingString(char *start_stringA_index, char *start_stringB_index);
|
||||
void ReverseString(char textA[]);
|
||||
BOOLEAN StringBInStringA(char *start_string_a, char *start_string_b);
|
||||
char * IndexStringBInStringA(char *start_string_a, char *start_string_b);
|
||||
unsigned int CountStringBInStringA(char *start_string_a, char *start_string_b);
|
||||
void RemoveStringBFromStringA(char *start_string_a, char *start_string_b);
|
||||
void RemoveAllStringBFromStringA(char *start_stringA_index, char *start_stringB_index);
|
||||
BOOLEAN ValidParenthesesTemplate(char text[]);
|
||||
unsigned short CharToNumber(char *c);
|
||||
unsigned int MaxCountCharInString(char *start_string_index);
|
||||
void CopyPointers(char *start_string_index, char *start_copy_index, char *end_copy_index);
|
||||
BOOLEAN EvenBits(byte b);
|
||||
unsigned short CountBits(byte b);
|
||||
void Multiply(float *ptr_number1, float *ptr_number2, double *ptr_result);
|
||||
void InitStack(stack * sk);
|
||||
BOOLEAN IsEmptyStack(stack * sk);
|
||||
void PushStack(stack * sk , Data_Type item);
|
||||
Data_Type PopStack(stack * sk);
|
||||
void EmptyStack(stack * sk);
|
||||
void CopyStack(stack * sk, stack * copy);
|
||||
void OppositeStack(stack * sk);
|
||||
BOOLEAN IsEqualsStack(stack * sk1, stack * sk2);
|
||||
unsigned int ItemsStack(stack * sk);
|
||||
int SumStack(stack * sk);
|
||||
BOOLEAN FindNumberInStack(stack * sk, Data_Type item);
|
||||
void UnionStack(stack * sk1, stack * sk2);
|
||||
void InitQueue(queue * q);
|
||||
void InsertQueue(queue * q, Data_Type item);
|
||||
Data_Type RemoveQueue(queue * q); // Fix it
|
||||
void InsertAfterList(LLL * manager);
|
||||
void PushList(LLL ** manager);
|
||||
void PopList(LLL ** manager);
|
||||
void DeleteList(LLL * manager);
|
||||
LLL * GetLoactionList(LLL ** manager, unsigned short location);
|
||||
void PushDLLL(DLLL ** manager);
|
||||
void InsertAfterDLLL(DLLL * ptr_before);
|
||||
void InsertBeforeDLLL(DLLL * ptr_after);
|
||||
void PopDLLL(DLLL ** manager);
|
||||
void DeleteDLLL(DLLL * ptr_cur);
|
||||
void InsertLastCLLL(CLLL ** manager);
|
||||
void InsertAfterCLLL(CLLL * ptr_before);
|
||||
void InsertEndCLLL(CLLL ** manager);
|
||||
void DeleteLastCLLL(CLLL ** manager);
|
||||
void DeleteAfterCLLL(CLLL * ptr_before);
|
||||
void DeleteEndCLLL(CLLL ** manager);
|
||||
void InsertLastCDLLL(CDLLL ** manager);
|
||||
void InsertAfterCDLLL(CDLLL * ptr_before);
|
||||
void InsertBeforeCDLLL(CDLLL * ptr_after);
|
||||
void InsertEndCDLLL(CDLLL ** manager);
|
||||
void DeleteCDLLL(CDLLL * ptr_cur);
|
||||
void DeleteLastCDLLL(CDLLL ** manager);
|
||||
// ---------------------------- Sparce Matrix ------------------------------
|
||||
void InitSM(SM ** manager);
|
||||
void AddRowSM(SM * manager);
|
||||
void AddColSM(SM * manager);
|
||||
SM * GetItemSM(SM * manager, int row, int col);
|
||||
SM * FindAbove(SM * ptr_sm);
|
||||
SM * FindBefore(SM * ptr_sm);
|
||||
unsigned int RowsCountSM(SM * manager);
|
||||
unsigned short ColsCountSM(SM * manager);
|
||||
void AddItemSM(SM * manager, int row, int col);
|
||||
void RemoveItemSM(SM * manager, int row, int col);
|
||||
void RemoveColSM(SM * manager);
|
||||
void RemoveRowSM(SM * manager);
|
||||
void PrintIntDataType(Data_Type dt);
|
||||
void PrintSM(SM * manager, void print(Data_Type));
|
||||
BIN
Projects/LifeGame/a.out
Normal file
BIN
Projects/LifeGame/a.out
Normal file
Binary file not shown.
37
Projects/LifeGame/checkthis.c
Normal file
37
Projects/LifeGame/checkthis.c
Normal file
@@ -0,0 +1,37 @@
|
||||
void AddOneToNeighbors(int * ptr_mat, int * ptr_loc, unsigned int row_size, unsigned int col_size)
|
||||
{
|
||||
int * ptr_last = ptr_mat + (row_size * col_size);
|
||||
int * ptr_temp = ptr_loc - col_size - ONE;
|
||||
for (unsigned short count = ZERO; count < THREE; count++)
|
||||
{
|
||||
if (ptr_temp >= ptr_mat &&
|
||||
((int)ptr_temp / sizeof(int)) / row_size == ((((int)ptr_loc / sizeof(int)) / row_size) - ONE))
|
||||
{
|
||||
(*(ptr_temp))++;
|
||||
}
|
||||
ptr_temp++;
|
||||
}
|
||||
|
||||
ptr_temp = ptr_loc - ONE;
|
||||
for (unsigned short count = ZERO; count < TWO; count++)
|
||||
{
|
||||
if (ptr_temp >= ptr_mat &&
|
||||
ptr_temp < ptr_last &&
|
||||
((int)ptr_temp / sizeof(int)) / row_size == ((((int)ptr_loc / sizeof(int)) / row_size)))
|
||||
{
|
||||
(*(ptr_temp))++;
|
||||
}
|
||||
ptr_temp += TWO;
|
||||
}
|
||||
|
||||
ptr_temp = ptr_loc + col_size - ONE;
|
||||
for (unsigned short count = ZERO; count < THREE; count++)
|
||||
{
|
||||
if (ptr_temp < ptr_last &&
|
||||
((int)ptr_temp / sizeof(int)) / row_size == ((((int)ptr_loc / sizeof(int)) / row_size) + ONE))
|
||||
{
|
||||
(*(ptr_temp))++;
|
||||
}
|
||||
ptr_temp++;
|
||||
}
|
||||
}
|
||||
429
Projects/LifeGame/life_game.c
Normal file
429
Projects/LifeGame/life_game.c
Normal file
@@ -0,0 +1,429 @@
|
||||
#include "General.h"
|
||||
|
||||
#define WORLD_SIZE 25
|
||||
#define FIVETEEN 15
|
||||
#define FORTY 40
|
||||
#define FIVTY 50
|
||||
#define THIRTY 30
|
||||
#define TWENTY 20
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Random
|
||||
// ------
|
||||
//
|
||||
// General : Create random number.
|
||||
//
|
||||
// Parameters :
|
||||
// seed - seed of random (int **).
|
||||
// min - minimum for result.
|
||||
// max - maximum for result.
|
||||
//
|
||||
// Return value : random number.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
int Random(int ** seed, int min, int max)
|
||||
{
|
||||
return ((*((*seed)++) % (++max)) + min);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// InitWorld
|
||||
// ---------
|
||||
//
|
||||
// General : Initialize the world.
|
||||
//
|
||||
// Parameters :
|
||||
// world - sparce matrix pointer (SM *).
|
||||
// rows - Amount of rows in the world.
|
||||
// cols - Amount of columns in the world.
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void InitWorld(SM * world, int rows, int cols)
|
||||
{
|
||||
unsigned short counter;
|
||||
for (counter = ZERO; counter < rows; counter++)
|
||||
{
|
||||
AddRowSM(world);
|
||||
}
|
||||
for (counter = ZERO; counter < cols; counter++)
|
||||
{
|
||||
AddColSM(world);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// RemoveWorld
|
||||
// -----------
|
||||
//
|
||||
// General : Deletes the world, freeing up space in memory.
|
||||
//
|
||||
// Parameters :
|
||||
// world - sparce matrix pointer (SM *).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void RemoveWorld(SM * world)
|
||||
{
|
||||
unsigned short counter;
|
||||
unsigned short rows = RowsCountSM(world);
|
||||
unsigned short cols = ColsCountSM(world);
|
||||
for (counter = ZERO; counter < rows; counter++)
|
||||
{
|
||||
RemoveRowSM(world);
|
||||
}
|
||||
for (counter = ZERO; counter < cols; counter++)
|
||||
{
|
||||
RemoveColSM(world);
|
||||
}
|
||||
free(world);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// RandomWorld
|
||||
// -----------
|
||||
//
|
||||
// General : Creates a random population.
|
||||
//
|
||||
// Parameters :
|
||||
// world - sparce matrix pointer (SM *).
|
||||
// seed - seed for random (int **).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void RandomWorld(SM * world, int ** seed)
|
||||
{
|
||||
unsigned short rows = RowsCountSM(world);
|
||||
unsigned short cols = ColsCountSM(world);
|
||||
unsigned short counter;
|
||||
unsigned short length = rows * cols;
|
||||
for (counter = ZERO; counter < length; counter++)
|
||||
{
|
||||
Random(seed, ZERO, ONE) ? AddItemSM(world, counter / rows, counter % cols) : ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Earthquake
|
||||
// ----------
|
||||
//
|
||||
// General : Decreases world size because of earthquake.
|
||||
//
|
||||
// Parameters :
|
||||
// world - sparce matrix pointer (SM *).
|
||||
// row - row of point to check (unsigned int).
|
||||
// col - column of point to check (unsigned int).
|
||||
//
|
||||
// Return value : Amount of neughbors (short (0-8)).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
short CountNeighbors(SM * world, unsigned int row, unsigned int col)
|
||||
{
|
||||
short count = -ONE;
|
||||
short loop_counter;
|
||||
for (loop_counter = -ONE; loop_counter < TWO; loop_counter++)
|
||||
{
|
||||
count += GetItemSM(world, row - ONE , col + loop_counter) != NULL;
|
||||
count += GetItemSM(world, row, col + loop_counter) != NULL;
|
||||
count += GetItemSM(world, row + ONE, col + loop_counter) != NULL;
|
||||
}
|
||||
|
||||
return (count);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Earthquake
|
||||
// ----------
|
||||
//
|
||||
// General : Decreases world size because of earthquake.
|
||||
//
|
||||
// Parameters :
|
||||
// world - sparce matrix pointer (SM *).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void Earthquake(SM * world)
|
||||
{
|
||||
int del_rows_count = RowsCountSM(world) / TEN;
|
||||
int del_cols_count = ColsCountSM(world) / TEN;
|
||||
unsigned short counter;
|
||||
for (counter = ZERO; counter < del_rows_count; counter++)
|
||||
{
|
||||
RemoveRowSM(world);
|
||||
}
|
||||
for (counter = ZERO; counter < del_cols_count; counter++)
|
||||
{
|
||||
RemoveColSM(world);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Epidemic
|
||||
// --------
|
||||
//
|
||||
// General : Expands the world in rows and columns because of ocean retreat.
|
||||
//
|
||||
// Parameters :
|
||||
// world - sparce matrix pointer (SM *).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void WithdrawalOceans(SM * world)
|
||||
{
|
||||
int add_rows_count = RowsCountSM(world) / FIVE;
|
||||
int add_cols_count = ColsCountSM(world) / FIVE;
|
||||
unsigned short counter;
|
||||
for (counter = ZERO; counter < add_rows_count; counter++)
|
||||
{
|
||||
AddRowSM(world);
|
||||
}
|
||||
for (counter = ZERO; counter < add_cols_count; counter++)
|
||||
{
|
||||
AddColSM(world);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Epidemic
|
||||
// --------
|
||||
//
|
||||
// General : Reduces the amount of ranks in the world because of an epidemic.
|
||||
//
|
||||
// Parameters :
|
||||
// world - sparce matrix pointer (SM *).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void Epidemic(SM * world)
|
||||
{
|
||||
int del_rows_count = RowsCountSM(world) / FIVETEEN;
|
||||
unsigned short counter;
|
||||
for (counter = ZERO; counter < del_rows_count; counter++)
|
||||
{
|
||||
RemoveRowSM(world);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Drying
|
||||
// ------
|
||||
//
|
||||
// General : Expands the world space due to drying and lakes.
|
||||
//
|
||||
// Parameters :
|
||||
// world - sparce matrix pointer (SM *).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void Drying(SM * world)
|
||||
{
|
||||
int add_cols_count = RowsCountSM(world) / FIVETEEN;
|
||||
unsigned short counter;
|
||||
for (counter = ZERO; counter < add_cols_count; counter++)
|
||||
{
|
||||
AddColSM(world);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// PercentagePopulation
|
||||
// --------------------
|
||||
//
|
||||
// General : Checks the amount of population relative to world size.
|
||||
//
|
||||
// Parameters :
|
||||
// world - sparce matrix pointer (SM *).
|
||||
//
|
||||
// Return value : Percentage (int).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
int PercentagePopulation(SM * world)
|
||||
{
|
||||
unsigned short alives = ZERO;
|
||||
unsigned short counter;
|
||||
unsigned short rows = RowsCountSM(world);
|
||||
unsigned short cols = ColsCountSM(world);
|
||||
unsigned short length = rows * cols;
|
||||
for (counter = ZERO; counter < length; counter++)
|
||||
{
|
||||
alives += (GetItemSM(world, counter / rows, counter % cols) != NULL);
|
||||
}
|
||||
|
||||
return ((alives / length) * ONE_HUNDRED);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// OneGenerationWorld
|
||||
// ------------------
|
||||
//
|
||||
// General : Performs a one-generation world-wide process.
|
||||
//
|
||||
// Parameters :
|
||||
// world - sparce matrix pointer (SM *).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void OneGenerationWorld(SM * world)
|
||||
{
|
||||
int percentage = PercentagePopulation(world);
|
||||
(percentage >= FIVTY) ? Earthquake(world) : ZERO;
|
||||
(percentage < TWENTY) ? WithdrawalOceans(world) : ZERO;
|
||||
(percentage == THIRTY) ? Epidemic(world) : ZERO;
|
||||
(percentage == FORTY) ? Drying(world) : ZERO;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// OneGenerationPopulation
|
||||
// -----------------------
|
||||
//
|
||||
// General : Performs one generation process in population.
|
||||
//
|
||||
// Parameters :
|
||||
// world - pointer of sparce matrix pointer (SM **).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void OneGenerationPopulation(SM ** world)
|
||||
{
|
||||
SM * new_world;
|
||||
SM * temp;
|
||||
unsigned short counter;
|
||||
unsigned short rows = RowsCountSM(*world);
|
||||
unsigned short cols = ColsCountSM(*world);
|
||||
unsigned short length = rows * cols;
|
||||
short count_neighbors;
|
||||
InitSM(&new_world);
|
||||
InitWorld(new_world, rows, cols);
|
||||
for (counter = ZERO; counter < length; counter++)
|
||||
{
|
||||
count_neighbors = CountNeighbors(*world, counter / rows, counter % cols);
|
||||
temp = GetItemSM(*world, counter / rows, counter % cols);
|
||||
if ((temp == NULL && count_neighbors == TWO) ||
|
||||
(temp != NULL && count_neighbors <= THREE && count_neighbors >= TWO))
|
||||
{
|
||||
AddItemSM(new_world, counter / rows, counter % cols);
|
||||
}
|
||||
}
|
||||
RemoveWorld(*world);
|
||||
*world = new_world;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// OneGeneralGeneration
|
||||
// --------------------
|
||||
//
|
||||
// General : Performs one generation procedure and prints it.
|
||||
//
|
||||
// Parameters :
|
||||
// world - pointer of sparce matrix pointer (SM **).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void OneGeneralGeneration(SM ** world)
|
||||
{
|
||||
OneGenerationPopulation(world);
|
||||
OneGenerationWorld(*world);
|
||||
PrintSM(*world, PrintIntDataType);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Minesweeper
|
||||
// -----------
|
||||
//
|
||||
// General : The game is a zero-player game, meaning that its evolution is determined by
|
||||
// its initial state, requiring no further input. One interacts with the Game
|
||||
// of Life by creating an initial configuration and observing how it evolves.
|
||||
//
|
||||
// Input : Accepts like the generations the user wants to do.
|
||||
//
|
||||
// Process : The program accepts the number of generations and loops each time it
|
||||
// processes the world and prints to the user what happens in each generation.
|
||||
//
|
||||
// Output : What the world looks like in each generation.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
unsigned int generations;
|
||||
unsigned int counter;
|
||||
int * seed;
|
||||
SM * world;
|
||||
InitSM(&world);
|
||||
InitWorld(world, WORLD_SIZE, WORLD_SIZE);
|
||||
RandomWorld(world, &seed);
|
||||
PrintSM(world, PrintIntDataType);
|
||||
printf("Enter count of generations: ");
|
||||
scanf("%u", &generations);
|
||||
for (counter = ZERO; counter < generations; counter++)
|
||||
{
|
||||
OneGeneralGeneration(&world);
|
||||
}
|
||||
RemoveWorld(world);
|
||||
}
|
||||
Reference in New Issue
Block a user