First Upload
This commit is contained in:
2587
Projects/AtomBomb/General.c
Normal file
2587
Projects/AtomBomb/General.c
Normal file
File diff suppressed because it is too large
Load Diff
274
Projects/AtomBomb/General.h
Normal file
274
Projects/AtomBomb/General.h
Normal file
@@ -0,0 +1,274 @@
|
||||
#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;
|
||||
};
|
||||
|
||||
struct BinaryTree
|
||||
{
|
||||
Data_Type info;
|
||||
struct BinaryTree * right;
|
||||
struct BinaryTree * left;
|
||||
};
|
||||
|
||||
struct ThirdTree
|
||||
{
|
||||
Data_Type info;
|
||||
struct ThirdTree * right;
|
||||
struct ThirdTree * left;
|
||||
struct ThirdTree * middle;
|
||||
};
|
||||
|
||||
struct GeneralTree
|
||||
{
|
||||
Data_Type info;
|
||||
struct LLL * manager_general_trees;
|
||||
};
|
||||
|
||||
typedef struct SM SM;
|
||||
typedef struct LLL LLL;
|
||||
typedef struct DLLL DLLL;
|
||||
typedef struct CLLL CLLL;
|
||||
typedef struct CDLLL CDLLL;
|
||||
typedef struct BinaryTree BinaryTree;
|
||||
typedef struct ThirdTree ThirdTree;
|
||||
typedef struct GeneralTree GeneralTree;
|
||||
|
||||
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);
|
||||
int Random(int ** seed, int min, int max);
|
||||
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 int StringLength(char * str);
|
||||
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);
|
||||
// -------------------------------------- Stack -----------------------------------------
|
||||
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);
|
||||
// -------------------------------------- Queue -----------------------------------------
|
||||
void InitQueue(queue * q);
|
||||
void InsertQueue(queue * q, Data_Type item);
|
||||
Data_Type RemoveQueue(queue * q);
|
||||
BOOLEAN IsEmptyQueue(queue * q);
|
||||
// -------------------------------------- LLL -------------------------------------------
|
||||
void InsertAfterLLL(LLL * manager);
|
||||
void PushLLL(LLL ** manager);
|
||||
void PopLLL(LLL ** manager);
|
||||
void DeleteLLL(LLL * manager);
|
||||
LLL * GetLoactionLLL(LLL ** manager, unsigned short location);
|
||||
// -------------------------------------- DLLL ------------------------------------------
|
||||
void InitDLLL(DLLL ** manager);
|
||||
void PushDLLL(DLLL ** manager);
|
||||
void InsertAfterDLLL(DLLL * ptr_before);
|
||||
void InsertBeforeDLLL(DLLL * ptr_after);
|
||||
void PopDLLL(DLLL ** manager);
|
||||
void DeleteDLLL(DLLL * ptr_cur);
|
||||
void PrintDLLL(DLLL * manager, void Print(Data_Type dt));
|
||||
// --------------------------------- CLLL -----------------------------------------------
|
||||
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);
|
||||
// --------------------------------- CDLLL ----------------------------------------------
|
||||
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);
|
||||
void PrintCDLLL(CDLLL * manager, void Print(Data_Type dt));
|
||||
// ---------------------------- 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));
|
||||
// -------------------------------------- Binary Tree -----------------------------------
|
||||
void MakeBinaryTree(BinaryTree ** tree);
|
||||
void AddLeftAfterBinaryTree(BinaryTree * tree);
|
||||
void AddRightAfterBinaryTree(BinaryTree * tree);
|
||||
BOOLEAN IsLeafBinaryTree(BinaryTree * tree);
|
||||
// -------------------------------------- Third Tree -----------------------------------
|
||||
void MakeThirdTree(ThirdTree ** tree);
|
||||
void AddLeftAfterThirdTree(ThirdTree * tree);
|
||||
void AddRightAfterThirdTree(ThirdTree * tree);
|
||||
void AddMiddleAfterThirdTree(ThirdTree * tree);
|
||||
BOOLEAN IsLeafThirdTree(ThirdTree * tree);
|
||||
// -------------------------------------- General Tree -----------------------------------
|
||||
void MakeGeneralTree(GeneralTree ** tree);
|
||||
void AddAfterGeneralTree(GeneralTree * tree);
|
||||
BOOLEAN IsLeafGeneralTree(GeneralTree * tree);
|
||||
// -------------------------------------- Funcations Tree -----------------------------------
|
||||
void PreodererPrintBinaryTree(BinaryTree * root, void print(Data_Type));
|
||||
void PosorderPrintBinaryTree(BinaryTree * root, void print(Data_Type));
|
||||
void InorderPrintBinaryTree(BinaryTree * root, void print(Data_Type));
|
||||
void LevelPrintBinaryTree(BinaryTree * root, void print(Data_Type));
|
||||
BIN
Projects/AtomBomb/a.out
Normal file
BIN
Projects/AtomBomb/a.out
Normal file
Binary file not shown.
480
Projects/AtomBomb/atom_bomb.c
Normal file
480
Projects/AtomBomb/atom_bomb.c
Normal file
@@ -0,0 +1,480 @@
|
||||
#include "General.h"
|
||||
|
||||
#define ATOM 4
|
||||
#define NEUTRON_UP 3
|
||||
#define NEUTRON_DOWN 5
|
||||
#define NEUTRON_LEFT 7
|
||||
#define NEUTRON_RIGHT 9
|
||||
#define FILE_NAME "test.txt"
|
||||
#define OPEN_FILE_TYPE "rt"
|
||||
#define ASCII_COMMA ','
|
||||
#define ASCII_ZERO '0'
|
||||
#define ASCII_HYPHEN '-'
|
||||
#define SM_ROWS 500
|
||||
#define SM_COLUMNS 600
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// ReadStringFromFile
|
||||
// ------------------
|
||||
//
|
||||
// General : Read from file as string and save the string to array.
|
||||
//
|
||||
// Parameters :
|
||||
// char ** ptr_save_data - save data from file in array
|
||||
// unsigned int length - length of array
|
||||
//
|
||||
// Return value : The number (int).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void ReadStringFromFile(char ** ptr_save_data, unsigned int * length)
|
||||
{
|
||||
*length = ZERO;
|
||||
char ch;
|
||||
FILE * fp = fopen(FILE_NAME, OPEN_FILE_TYPE);
|
||||
if (fp)
|
||||
{
|
||||
*ptr_save_data = malloc(sizeof(ch));
|
||||
while ((ch = fgetc(fp)) != EOF)
|
||||
{
|
||||
*((*ptr_save_data) + *length) = ch;
|
||||
(*length)++;
|
||||
*ptr_save_data = realloc(*ptr_save_data, sizeof(ch) * (*length + ONE));
|
||||
}
|
||||
*ptr_save_data = realloc(*ptr_save_data, sizeof(ch) * (*length));
|
||||
fclose(fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Can't read file : \"%s\"\n", FILE_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// StringToNumber
|
||||
// --------------
|
||||
//
|
||||
// General : Convert string to number.
|
||||
//
|
||||
// Parameters :
|
||||
// char * ptr_str - array of char
|
||||
// unsigned int length - length of array
|
||||
//
|
||||
// Return value : The number (int).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
int StringToNumber(char * ptr_str, unsigned int length)
|
||||
{
|
||||
int number = ZERO;
|
||||
unsigned int counter;
|
||||
if (*ptr_str == ASCII_HYPHEN)
|
||||
{
|
||||
number = -(*(++ptr_str));
|
||||
ptr_str++;
|
||||
}
|
||||
for (counter = ZERO; counter < length; counter++)
|
||||
{
|
||||
number = (number * TEN) + (*(ptr_str++) - ASCII_ZERO);
|
||||
}
|
||||
|
||||
return (number);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// FileDataToArray
|
||||
// ---------------
|
||||
//
|
||||
// General : Change the data we get from file as string to numbers array.
|
||||
//
|
||||
// Parameters :
|
||||
// char * ptr_file_data - pointer to char array
|
||||
// unsigned int length_file_data - length of char array
|
||||
// int ** ptr_array - pointer to save data from char array
|
||||
// unsigned int * length_array - pointer of ptr_array length
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void FileDataToArray(char * ptr_file_data, unsigned int length_file_data, int ** ptr_array, unsigned int * length_array)
|
||||
{
|
||||
char * temp_ptr_file_data = ptr_file_data;
|
||||
int temp_number;
|
||||
unsigned int loop_counter;
|
||||
*length_array = ONE;
|
||||
*ptr_array = malloc(sizeof(int));
|
||||
|
||||
for (loop_counter = ZERO; loop_counter < length_file_data; loop_counter++)
|
||||
{
|
||||
if (*temp_ptr_file_data == ASCII_COMMA)
|
||||
{
|
||||
(*ptr_array)[*length_array - ONE] = StringToNumber(ptr_file_data, temp_ptr_file_data - ptr_file_data);
|
||||
(*length_array)++;
|
||||
*ptr_array = realloc(*ptr_array, sizeof(int) * (*length_array));
|
||||
ptr_file_data = ++temp_ptr_file_data + ONE;
|
||||
loop_counter++;
|
||||
}
|
||||
temp_ptr_file_data++;
|
||||
}
|
||||
*ptr_array = realloc(*ptr_array, sizeof(int) * (--(*length_array)));
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// InitBoard
|
||||
// ---------
|
||||
//
|
||||
// General : Initializing simulation board.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// unsigned int rows - Amount of rows in board
|
||||
// unsigned int cols - Amount of cols in board
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void InitBoard(SM * board, unsigned int rows, unsigned int cols)
|
||||
{
|
||||
unsigned int counter;
|
||||
for (counter = ZERO; counter < rows; counter++)
|
||||
{
|
||||
AddRowSM(board);
|
||||
}
|
||||
for (counter = ZERO; counter < cols; counter++)
|
||||
{
|
||||
AddColSM(board);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// ArrayDataToSM
|
||||
// -------------
|
||||
//
|
||||
// General : Copies all points to a sparse matrix from the array.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// int * ptr_arr - pointer of points array (x1, y1, x2, y2, ..)
|
||||
// unsigned int length - length of points array
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void ArrayDataToSM(SM * board, int * ptr_arr, unsigned int length)
|
||||
{
|
||||
unsigned int counter;
|
||||
for (counter = ZERO; counter < length; counter += TWO)
|
||||
{
|
||||
AddItemSM(board, ptr_arr[counter], ptr_arr[counter+ONE]);
|
||||
GetItemSM(board, ptr_arr[counter], ptr_arr[counter+ONE])->info.int_ = ATOM;
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// MoveUp
|
||||
// ------
|
||||
//
|
||||
// General : Creates to the up of the node, a neutron facing the up side
|
||||
// of the simulation board.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// SM * node - address of node in simulation board
|
||||
// queue * qt - queue of active objects
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void MoveUp(SM * manager, SM * node, queue * qt)
|
||||
{
|
||||
SM * temp = NULL;
|
||||
if (node->row > ZERO)
|
||||
{
|
||||
temp = GetItemSM(manager, node->row - ONE, node->col);
|
||||
if (!temp)
|
||||
{
|
||||
AddItemSM(manager, node->row - ONE, node->col);
|
||||
temp->info.int_ = ONE;
|
||||
}
|
||||
temp->info.int_ *= node->info.int_;
|
||||
}
|
||||
if (temp != NULL)
|
||||
{
|
||||
InsertQueue(qt, (Data_Type)(void *)temp);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// MoveDown
|
||||
// --------
|
||||
//
|
||||
// General : Creates to the down of the node, a neutron facing the down side
|
||||
// of the simulation board.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// SM * node - address of node in simulation board
|
||||
// queue * qt - queue of active objects
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void MoveDown(SM * manager, SM * node, queue * qt)
|
||||
{
|
||||
SM * temp = NULL;
|
||||
if ((node->row + ONE) < RowsCountSM(manager))
|
||||
{
|
||||
temp = GetItemSM(manager, node->row + ONE, node->col);
|
||||
if (!temp)
|
||||
{
|
||||
AddItemSM(manager, node->row + ONE, node->col);
|
||||
temp->info.int_ = ONE;
|
||||
}
|
||||
temp->info.int_ *= NEUTRON_DOWN;
|
||||
}
|
||||
if (temp != NULL)
|
||||
{
|
||||
InsertQueue(qt, (Data_Type)(void *)temp);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// MoveLeft
|
||||
// --------
|
||||
//
|
||||
// General : Creates to the left of the node, a neutron facing the left side
|
||||
// of the simulation board.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// SM * node - address of node in simulation board
|
||||
// queue * qt - queue of active objects
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void MoveLeft(SM * manager, SM * node, queue * qt)
|
||||
{
|
||||
SM * temp = NULL;
|
||||
if (node->col > ZERO)
|
||||
{
|
||||
temp = GetItemSM(manager, node->row, node->col - ONE);
|
||||
if (!temp)
|
||||
{
|
||||
AddItemSM(manager, node->row, node->col - ONE);
|
||||
temp->info.int_ = ONE;
|
||||
}
|
||||
temp->info.int_ *= NEUTRON_LEFT;
|
||||
}
|
||||
if (temp != NULL)
|
||||
{
|
||||
InsertQueue(qt, (Data_Type)(void *)temp);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// MoveRight
|
||||
// ---------
|
||||
//
|
||||
// General : Creates to the right of the node, a neutron facing the right side
|
||||
// of the simulation board.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// SM * node - address of node in simulation board
|
||||
// queue * qt - queue of active objects
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void MoveRight(SM * board, SM * node, queue * qt)
|
||||
{
|
||||
SM * temp = NULL;
|
||||
if ((node->col + ONE) < ColsCountSM(board))
|
||||
{
|
||||
temp = GetItemSM(board, node->row, node->col + ONE);
|
||||
if (!temp)
|
||||
{
|
||||
AddItemSM(board, node->row, node->col + ONE);
|
||||
temp->info.int_ = ONE;
|
||||
}
|
||||
temp->info.int_ *= NEUTRON_RIGHT;
|
||||
}
|
||||
if (temp != NULL)
|
||||
{
|
||||
InsertQueue(qt, (Data_Type)(void *)temp);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Explode
|
||||
// -------
|
||||
//
|
||||
// General : Handles atomic fission mode.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// SM * node - address of node in simulation board
|
||||
// queue * qt - queue of active objects
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void Explode(SM * board, SM * node, queue * qt)
|
||||
{
|
||||
MoveUp(board, node, qt);
|
||||
MoveDown(board, node, qt);
|
||||
MoveLeft(board, node, qt);
|
||||
MoveRight(board, node, qt);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Steps
|
||||
// -----
|
||||
//
|
||||
// General : Handles all objects within the queue..
|
||||
//
|
||||
// Parameters :
|
||||
// SM * manager - pointer of simulation board
|
||||
// queue * qt - queue of objects
|
||||
//
|
||||
// Return value : Amount atoms explode.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned int Steps(SM * manager, queue * qt)
|
||||
{
|
||||
unsigned int booms_count = ZERO;
|
||||
SM * main_node;
|
||||
while (!IsEmptyQueue(qt))
|
||||
{
|
||||
main_node = (SM *)RemoveQueue(qt).pointer;
|
||||
switch (main_node->info.int_)
|
||||
{
|
||||
case NEUTRON_UP:
|
||||
MoveUp(manager, main_node, qt);
|
||||
break;
|
||||
case NEUTRON_DOWN:
|
||||
MoveDown(manager, main_node, qt);
|
||||
break;
|
||||
case NEUTRON_LEFT:
|
||||
MoveLeft(manager, main_node, qt);
|
||||
break;
|
||||
case NEUTRON_RIGHT:
|
||||
MoveRight(manager, main_node, qt);
|
||||
break;
|
||||
|
||||
default:
|
||||
if (!(main_node->info.int_ % TWO));
|
||||
{
|
||||
Explode(manager, main_node, qt);
|
||||
booms_count++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
RemoveItemSM(manager, main_node->row, main_node->col);
|
||||
}
|
||||
|
||||
return (booms_count);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Atomic explosion
|
||||
// -------------------
|
||||
//
|
||||
// General : The program is essentially an atomic explosion
|
||||
// simulation
|
||||
//
|
||||
// Input : A location where we want to blow up the atom
|
||||
//
|
||||
// Process : The program reads atoms the locations of the atoms
|
||||
// and presents them in the matrix. Then we choose a
|
||||
// place where we want to blow up the atom in the matrix
|
||||
// and count how many nuclear fission happened
|
||||
//
|
||||
// Output : None
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
SM * board;
|
||||
SM * temp;
|
||||
queue active_objects;
|
||||
char * chars_from_file;
|
||||
unsigned int length_chars_from_file;
|
||||
int * atoms_position;
|
||||
unsigned int length_atoms_position;
|
||||
unsigned int chosen_row;
|
||||
unsigned int chosen_col;
|
||||
unsigned int booms_count;
|
||||
ReadStringFromFile(&chars_from_file, &length_chars_from_file);
|
||||
FileDataToArray(chars_from_file, length_chars_from_file, atoms_position, &length_atoms_position);
|
||||
free(chars_from_file);
|
||||
InitSM(&board);
|
||||
InitBoard(board, SM_ROWS, SM_COLUMNS);
|
||||
InitQueue(&active_objects);
|
||||
ArrayDataToSM(board, atoms_position + ONE, length_atoms_position - ONE);
|
||||
free(atoms_position);
|
||||
while (!temp)
|
||||
{
|
||||
printf("Enter X of atom you want to explode: ");
|
||||
scanf(" %u", &chosen_col);
|
||||
printf("Enter Y of atom you want to explode: ");
|
||||
scanf(" %u", &chosen_row);
|
||||
temp = GetItemSM(board, chosen_row, chosen_col);
|
||||
}
|
||||
InsertQueue(&active_objects, (Data_Type)(void *)temp);
|
||||
booms_count = Steps(board, &active_objects);
|
||||
|
||||
printf("Energy: %d\n", booms_count);
|
||||
|
||||
}
|
||||
1
Projects/AtomBomb/test.txt
Normal file
1
Projects/AtomBomb/test.txt
Normal file
@@ -0,0 +1 @@
|
||||
idan cohen
|
||||
BIN
Projects/AtomBomb/בעיות.odg
Normal file
BIN
Projects/AtomBomb/בעיות.odg
Normal file
Binary file not shown.
3
Projects/AtomBomb/פונקציות.txt
Normal file
3
Projects/AtomBomb/פונקציות.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
1) לעשות פונקציה שתבדוק את מקום ההתנגשות ברגע ששני אטומים מתפוצצים באותו רגע.
|
||||
|
||||
2) ג
|
||||
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);
|
||||
}
|
||||
Binary file not shown.
BIN
Projects/MainProject/הצעת פרויקט.odt
Normal file
BIN
Projects/MainProject/הצעת פרויקט.odt
Normal file
Binary file not shown.
BIN
Projects/Projects1/project1
Normal file
BIN
Projects/Projects1/project1
Normal file
Binary file not shown.
345
Projects/Projects1/project1.c
Normal file
345
Projects/Projects1/project1.c
Normal file
@@ -0,0 +1,345 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define SIZE_NUMBER 4
|
||||
#define BOOLEAN unsigned short
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define TEN 10
|
||||
#define TWO 2
|
||||
#define ONE 1
|
||||
#define ZERO 0
|
||||
#define MAX(x, y) (x > y) ? x : y
|
||||
#define MIN(x, y) (x < y) ? x : y
|
||||
#define ABS(x) (x) * (((2 * (x)) + 1) % 2)
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Equality
|
||||
// --------
|
||||
//
|
||||
// General : Checks whether two numbers are equal in value.
|
||||
//
|
||||
// Parameters :
|
||||
// number1 - first number (int)
|
||||
// number2 - second number (int)
|
||||
//
|
||||
// Return value : If there is an equality between the two numbers (BOOLEAN).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
BOOLEAN Equality(int number1, int number2)
|
||||
{
|
||||
BOOLEAN answer = (ONE / (ABS(number1 - number2) + ONE));
|
||||
return answer;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// NumBBiggerThenNumA
|
||||
// ------------------
|
||||
//
|
||||
// General : Checks if the second number (B) is greater than the first number (A).
|
||||
//
|
||||
// Parameters :
|
||||
// number_a - first number (int)
|
||||
// number_b - second number (int)
|
||||
//
|
||||
// Return value : If the second number (B) is greater than the first number (A) (BOOLEAN).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
BOOLEAN NumBBiggerThenNumA(int number_a, int number_b)
|
||||
{
|
||||
int sub = number_a - number_b + ONE;
|
||||
BOOLEAN answer = (ONE / (ABS(sub) + (sub) + ONE));
|
||||
return answer;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CountADigitInNumber
|
||||
// -------------------
|
||||
//
|
||||
// General : Count the number of times her story appears in number.
|
||||
//
|
||||
// Parameters :
|
||||
// number - a number (int)
|
||||
// digit - a digit (unsigned short)
|
||||
//
|
||||
// Return value : The number of times the number appears in the number (unsigned short)
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned short CountADigitInNumber(int number, unsigned short digit)
|
||||
{
|
||||
int temp_num = number;
|
||||
unsigned count = FALSE;
|
||||
for (; temp_num; temp_num /= TEN)
|
||||
{
|
||||
count += Equality(temp_num % TEN, digit);
|
||||
}
|
||||
|
||||
return (count);
|
||||
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CountDigits
|
||||
// -----------
|
||||
//
|
||||
// General : COunt digit in number.
|
||||
//
|
||||
// Parameters :
|
||||
// num - first number (int)
|
||||
//
|
||||
// Return value : Number of digits count in a number.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.09.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned short CountDigits(int num)
|
||||
{
|
||||
unsigned short count;
|
||||
for (count = ZERO; num; num /= TEN, count++);
|
||||
|
||||
return (count);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// ForeignDigits
|
||||
// -------------
|
||||
//
|
||||
// General : Checks whether all digits are foreign to each other by number.
|
||||
//
|
||||
// Parameters :
|
||||
// number - first number (int)
|
||||
//
|
||||
// Return value : If all the numbers are foreign to each other in number (BOOLEAN)
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
BOOLEAN ForeignDigits(int number)
|
||||
{
|
||||
int temp_num = number;
|
||||
BOOLEAN answer = TRUE;
|
||||
for (; temp_num; temp_num /= TEN)
|
||||
{
|
||||
answer *= NumBBiggerThenNumA(CountADigitInNumber(temp_num, temp_num % TEN), TWO);
|
||||
}
|
||||
|
||||
return (answer);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CountOfIdenticalDigitsInSomeIndex
|
||||
// ---------------------------------
|
||||
//
|
||||
// General : Count the number of times there are identical digits between
|
||||
// two numbers and in the same location.
|
||||
//
|
||||
// Parameters :
|
||||
// number1 - first number (int)
|
||||
// number2 - second number (int)
|
||||
//
|
||||
// Return value : The number of times there are identical
|
||||
// digits between two numbers and in the same location (unsigned short)
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned short CountOfIdenticalDigitsInSomeIndex(int number1, int number2)
|
||||
{
|
||||
int temp_num1,
|
||||
temp_num2;
|
||||
unsigned short count = ZERO;
|
||||
for (temp_num1 = MIN(number1, number2), temp_num2 = MAX(number1, number2);
|
||||
temp_num1;
|
||||
temp_num1 /= TEN, temp_num2 /= TEN)
|
||||
{
|
||||
count += Equality(temp_num1 % TEN, temp_num2 % TEN);
|
||||
}
|
||||
|
||||
return (count);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CountOfIdenticalDigits
|
||||
// ----------------------
|
||||
//
|
||||
// General : Count the number of times there are identical digits
|
||||
// between two numbers.
|
||||
//
|
||||
// Parameters :
|
||||
// number1 - first number (int)
|
||||
// number2 - second number (int)
|
||||
//
|
||||
// Return value : The number of times there are identical digits
|
||||
// between two numbers (unsigned short)
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned short CountOfIdenticalDigits(int number1, int number2)
|
||||
{
|
||||
int temp_num1,
|
||||
temp_num2;
|
||||
unsigned short count = ZERO,
|
||||
digit_num1;
|
||||
for (temp_num1 = number1; temp_num1; temp_num1 /= TEN)
|
||||
{
|
||||
digit_num1 = temp_num1 % TEN;
|
||||
for (temp_num2 = number2; temp_num2; temp_num2 /= TEN)
|
||||
{
|
||||
count += Equality(digit_num1, temp_num2 % TEN);
|
||||
}
|
||||
temp_num2 = number2;
|
||||
}
|
||||
|
||||
return (count);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// ChangeBool
|
||||
// ----------
|
||||
//
|
||||
// General : Boolean variable inverse.
|
||||
//
|
||||
// Parameters :
|
||||
// bool1 - Boolean variable (BOOLEAN)
|
||||
//
|
||||
// Return value : Boolean variable (BOOLEAN).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
BOOLEAN ChangeBool(BOOLEAN bool1)
|
||||
{
|
||||
BOOLEAN answer = ++bool1 % TWO;
|
||||
return answer;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Game Bullseye
|
||||
// -------------
|
||||
//
|
||||
// General : There are two contestants. Each contestant chooses an integer
|
||||
// of 4 digits without declaring it
|
||||
// The number consists of foreign literature to each other.
|
||||
// Each participant is supposed to guess the number chosen by his or
|
||||
// her member in minimum steps.
|
||||
// For each guess, the contestant will receive a response according to
|
||||
// the following rules:
|
||||
// If you guess a book exists in a selected number and is not in its exact
|
||||
// location - this is an injury
|
||||
// If you guess a book exists in a number but not in its
|
||||
// exact location - it's a bullet.
|
||||
//
|
||||
// Input : Two numbers with 4 digits and Foreign digits.
|
||||
//
|
||||
// Process : The program receives two numbers per number, it checks their correctness.
|
||||
// If the number is foreign and has 4 digits.
|
||||
// The program then enters the loop where it stops only when one of the
|
||||
// players manages to reach four hits.
|
||||
// The program checks the accuracy of each player's guess.
|
||||
// The program then checks to see if the number has vulnerabilities and
|
||||
// projectiles with its opponent's number.
|
||||
// The program prints the player's results and moves to the next
|
||||
// player's queue. If one of the players wins the program comes
|
||||
// out of the loop and announces the winner
|
||||
//
|
||||
// Output : Who wins.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 23.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
unsigned short player1_number = ZERO,
|
||||
player2_number = ZERO,
|
||||
check_number,
|
||||
damage,
|
||||
projectiles,
|
||||
guess_number,
|
||||
guessing_amount = ZERO;
|
||||
BOOLEAN is_player1;
|
||||
|
||||
// Checking the correctness of the input (4 digits, foreign digits) for player1
|
||||
while ((CountDigits(player1_number) - SIZE_NUMBER) + !ForeignDigits(player1_number))
|
||||
{
|
||||
printf("Player 1 : : Enter number with foreign digits and 4 digits: ");
|
||||
scanf("%hu", &player1_number);
|
||||
}
|
||||
|
||||
// Checking the correctness of the input (4 digits, foreign digits) for player2
|
||||
while ((CountDigits(player2_number) - SIZE_NUMBER) + !ForeignDigits(player2_number))
|
||||
{
|
||||
printf("Player 2 : : Enter number with foreign digits and 4 digits: ");
|
||||
scanf("%hu", &player2_number);
|
||||
}
|
||||
|
||||
// First player starts his turn
|
||||
is_player1 = TRUE;
|
||||
|
||||
// As long as the vulnerability is not 4, the loop is running
|
||||
while (!Equality(damage, SIZE_NUMBER))
|
||||
{
|
||||
damage = ZERO;
|
||||
projectiles = ZERO;
|
||||
|
||||
// Checks that the correctness of the guess input is 4 digits
|
||||
while (CountDigits(guess_number) - SIZE_NUMBER)
|
||||
{
|
||||
printf("Player ");
|
||||
if (is_player1)
|
||||
{
|
||||
printf("1");
|
||||
guessing_amount++;
|
||||
}
|
||||
else
|
||||
printf("2");
|
||||
printf(" : : Enter your guess: ");
|
||||
scanf("%hu", &guess_number);
|
||||
}
|
||||
|
||||
// Checks the guess based on the player's turn
|
||||
check_number = (!is_player1) ? player1_number : player2_number;
|
||||
|
||||
// Checks the amount of vulnerability
|
||||
damage = CountOfIdenticalDigitsInSomeIndex(check_number, guess_number);
|
||||
|
||||
// Checking the amount of projectiles
|
||||
projectiles = CountOfIdenticalDigits(check_number, guess_number) - damage;
|
||||
|
||||
printf("Damage: %hu\nTarget Shooting: %hu\n\n", damage, projectiles);
|
||||
guess_number = ZERO;
|
||||
|
||||
// Changing the turn of the player to the next player
|
||||
is_player1 = ChangeBool(is_player1);
|
||||
}
|
||||
printf("The winner is \"Player ");
|
||||
if (!is_player1)
|
||||
printf("1");
|
||||
else
|
||||
printf("2");
|
||||
printf("\" with %hd guesses!\n", guessing_amount);
|
||||
}
|
||||
BIN
Projects/Shola/Lib
Normal file
BIN
Projects/Shola/Lib
Normal file
Binary file not shown.
2227
Projects/Shola/Lib.h
Normal file
2227
Projects/Shola/Lib.h
Normal file
File diff suppressed because it is too large
Load Diff
BIN
Projects/Shola/a.out
Normal file
BIN
Projects/Shola/a.out
Normal file
Binary file not shown.
BIN
Projects/Shola/game
Normal file
BIN
Projects/Shola/game
Normal file
Binary file not shown.
280
Projects/Shola/game.c
Normal file
280
Projects/Shola/game.c
Normal file
@@ -0,0 +1,280 @@
|
||||
#include "Lib.h"
|
||||
|
||||
#define MAT_SIZE 12
|
||||
#define SIGN_UNCOVER '.'
|
||||
#define ASCII_SPACE ' '
|
||||
#define ASCII_ZERO '0'
|
||||
#define STOP_GAME 999
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// InputString
|
||||
// -----------
|
||||
//
|
||||
// General : Input string from user.
|
||||
//
|
||||
// Parameters :
|
||||
// ptr_str - The pointer to save the string from user (char *).
|
||||
// size - Amount of char to inputs from user (unsigned int).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void InputString(char * ptr_str, unsigned int size)
|
||||
{
|
||||
fgets(ptr_str, size, stdin);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// StringToValue
|
||||
// -------------
|
||||
//
|
||||
// General : Convert string to value like "12" to 12
|
||||
//
|
||||
// Parameters :
|
||||
// ptr_start_string - pointer of strat string (char *).
|
||||
// ptr_end_string - pointer of end string (char *).
|
||||
//
|
||||
// Return value : The value (int).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned int StringToValue(char * ptr_start_string, char * ptr_end_string)
|
||||
{
|
||||
unsigned int value = ZERO;
|
||||
while (ptr_start_string < ptr_end_string)
|
||||
{
|
||||
value *= TEN;
|
||||
value += *ptr_start_string - ASCII_ZERO;
|
||||
ptr_start_string++;
|
||||
}
|
||||
|
||||
return (value);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CheckLocationMatrix
|
||||
// -------------------
|
||||
//
|
||||
// General : Tests how many bombs are located one square away from the desired spot.
|
||||
//
|
||||
// Parameters :
|
||||
// loc_x - Position on X axis (unsigned short).
|
||||
// lox_y - Position on Y axis (unsigned short).
|
||||
// row_count - Amount of matrix rows (unsigned short).
|
||||
// col_count - Amount of matrix column (unsigned short).
|
||||
// ptr_bomb_vec - A string with all the locations of the mines (char *).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
short CheckLocationMatrix(unsigned short loc_x, unsigned short loc_y, unsigned int row_count, unsigned int col_count, char * ptr_bomb_vec)
|
||||
{
|
||||
unsigned short bomb_x;
|
||||
unsigned short bomb_y;
|
||||
short count_bombs = ZERO;
|
||||
ptr_bomb_vec++;
|
||||
while (*(ptr_bomb_vec--) && count_bombs != -ONE)
|
||||
{
|
||||
bomb_x = StringToValue(ptr_bomb_vec, PtrCharInString(ptr_bomb_vec, ASCII_SPACE));
|
||||
ptr_bomb_vec = PtrCharInString(ptr_bomb_vec, ASCII_SPACE);
|
||||
bomb_y = StringToValue(ptr_bomb_vec, PtrCharInString(ptr_bomb_vec, ASCII_SPACE));
|
||||
ptr_bomb_vec = PtrCharInString(ptr_bomb_vec, ASCII_SPACE);
|
||||
(ABS(loc_x - bomb_x) <= ONE &&
|
||||
ABS(loc_y - bomb_y) <= ONE) ?
|
||||
count_bombs++ : ZERO;
|
||||
count_bombs = (loc_x == bomb_x && loc_y == bomb_y) ? -ONE : count_bombs;
|
||||
}
|
||||
|
||||
return (count_bombs);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// PrintChar
|
||||
// ---------
|
||||
//
|
||||
// General : print char.
|
||||
//
|
||||
// Parameters :
|
||||
// c - pointer of char (char *).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void PrintChar(char * c)
|
||||
{
|
||||
printf("%c", *c);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// PrintMatrix
|
||||
// -----------
|
||||
//
|
||||
// General : Print matrix.
|
||||
//
|
||||
// Parameters :
|
||||
// ptr_mat - pointer of matrix (void *).
|
||||
// col - Amount of matrix column (unsigned short).
|
||||
// row - Amount of matrix rows (unsigned short).
|
||||
// print - a function that print the type of matrix (void (void *)).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void PrintMatrix(void * ptr_mat, unsigned short col, unsigned short row, void print(void *))
|
||||
{
|
||||
unsigned int length = col * row;
|
||||
unsigned int counter;
|
||||
for (counter = ZERO; counter < length; counter++)
|
||||
{
|
||||
(!(counter % col)) ? printf("\n") : ZERO;
|
||||
print(ptr_mat++);
|
||||
printf(" ");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CopyChar
|
||||
// --------
|
||||
//
|
||||
// General : Copies the content from one character pointer to another character
|
||||
// pointer.
|
||||
//
|
||||
// Parameters :
|
||||
// ch - pointer of char (char *).
|
||||
// copy - pointer of char with to save content of ch (char *).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void CopyChar(char * ch, char * copy)
|
||||
{
|
||||
*copy = *ch;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// ResetMatrix
|
||||
// -----------
|
||||
//
|
||||
// General : Initializes the matrix.
|
||||
//
|
||||
// Parameters :
|
||||
// ptr_mat - pointer of matrix (void *).
|
||||
// col - Amount of matrix column (unsigned short).
|
||||
// row - Amount of matrix rows (unsigned short).
|
||||
// type - what to put in all the organs in the matrix (void *).
|
||||
// insert - a function that copies from one pointer to another
|
||||
// pointer (void (void *, void *)).
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void ResetMatrix(void * ptr_mat, unsigned short col, unsigned short row, void * type, void insert(void *, void *))
|
||||
{
|
||||
unsigned int length = col * row;
|
||||
unsigned int counter;
|
||||
for (counter = ZERO; counter < length; counter++)
|
||||
{
|
||||
insert(type, ptr_mat++);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Minesweeper
|
||||
// -----------
|
||||
//
|
||||
// General : Minesweeper is a single-player puzzle computer game. The objective of the
|
||||
// game is to clear a rectangular board containing hidden "mines" or bombs
|
||||
// without detonating any of them, with help from clues about the number
|
||||
// of neighboring mines in each field.
|
||||
//
|
||||
// Input : Strings of the points where the mines are. And throughout the game
|
||||
// guessing points (X, Y).
|
||||
//
|
||||
// Process : The program initially receives a string of mines locations, then it receives
|
||||
// point X and Y, and it checks how many mines around the point or if the point
|
||||
// is at all a mine, if the player is able to discover all the points, he is a
|
||||
// winner and if he discovers a mine before then He loses.
|
||||
//
|
||||
// Output : If the player is win or lose.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
char mat[MAT_SIZE][MAT_SIZE];
|
||||
string bombs_string;
|
||||
unsigned short place_without_bombs;
|
||||
short count_bombs = ZERO;
|
||||
char sign_uncover = SIGN_UNCOVER;
|
||||
unsigned short x_point;
|
||||
unsigned short y_point;
|
||||
|
||||
ResetMatrix(mat, MAT_SIZE, MAT_SIZE, &sign_uncover, CopyChar);
|
||||
|
||||
// Example for input "1 1 2 1 3 3 "
|
||||
printf("Enter bombs (\"x y \"): ");
|
||||
InputString(bombs_string, MAX_SIZE_STRING);
|
||||
place_without_bombs = (MAT_SIZE * MAT_SIZE) - (StringLenght(bombs_string)) / FOUR;
|
||||
while (place_without_bombs-- && count_bombs != -ONE)
|
||||
{
|
||||
printf("Enter X point: ");
|
||||
scanf("%hu", &x_point);
|
||||
printf("Enter Y point: ");
|
||||
scanf("%hu", &y_point);
|
||||
if (y_point == STOP_GAME)
|
||||
{
|
||||
count_bombs = -ONE;
|
||||
}
|
||||
// if the point is uncover
|
||||
else if (mat[y_point][x_point] != SIGN_UNCOVER)
|
||||
{
|
||||
count_bombs = CheckLocationMatrix(x_point, y_point, MAT_SIZE, MAT_SIZE, bombs_string);
|
||||
mat[y_point][x_point] = count_bombs + ASCII_ZERO;
|
||||
PrintMatrix(mat, MAT_SIZE, MAT_SIZE, PrintChar);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("You was enter this point in past!\n");
|
||||
}
|
||||
|
||||
}
|
||||
if (!place_without_bombs)
|
||||
{
|
||||
printf("You won!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("You lose!\n");
|
||||
}
|
||||
}
|
||||
2089
Projects/תרץ1/Lib.h
Normal file
2089
Projects/תרץ1/Lib.h
Normal file
File diff suppressed because it is too large
Load Diff
BIN
Projects/תרץ1/a.out
Normal file
BIN
Projects/תרץ1/a.out
Normal file
Binary file not shown.
BIN
Projects/תרץ1/ex13a
Normal file
BIN
Projects/תרץ1/ex13a
Normal file
Binary file not shown.
224
Projects/תרץ1/ex13a.c
Normal file
224
Projects/תרץ1/ex13a.c
Normal file
@@ -0,0 +1,224 @@
|
||||
#include "Lib.h"
|
||||
|
||||
#define APPEND_BINARY "a+b"
|
||||
#define FILE_NAME "starship.dat"
|
||||
#define PRINT "print"
|
||||
#define EXIT "exit"
|
||||
#define CREATE "create"
|
||||
#define UPDATE "update"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int id;
|
||||
char name[8];
|
||||
char star_name[15];
|
||||
int age;
|
||||
} alien;
|
||||
|
||||
|
||||
// -------------------------------------------------------
|
||||
|
||||
/* close file */
|
||||
int CloseFile(FILE * fp)
|
||||
{
|
||||
return (fclose(fp));
|
||||
}
|
||||
|
||||
/* print alien */
|
||||
void PrintAlien(alien * aln)
|
||||
{
|
||||
printf("ID : %d\nName : %s\nStar Name : %s\nAlien age : %d\n\n",
|
||||
aln->id, aln->name, aln->star_name, aln->age);
|
||||
}
|
||||
|
||||
/* get all data from file */
|
||||
void GetAllDataFromFile(alien ** aln_arr)
|
||||
{
|
||||
alien * aln_data;
|
||||
FILE * fp = fopen(FILE_NAME , APPEND_BINARY);
|
||||
*aln_arr = (alien *)malloc((ftell(fp) / sizeof(alien)) + (TWO * sizeof(alien)));
|
||||
aln_data = *aln_arr;
|
||||
rewind(fp);
|
||||
while (!feof(fp));
|
||||
{
|
||||
fread(aln_data, sizeof(alien), ONE, fp);
|
||||
aln_data++;
|
||||
}
|
||||
aln_data->id = ZERO;
|
||||
fclose(fp); // fix
|
||||
}
|
||||
|
||||
/* print aliens in arr */
|
||||
void PrintArrayAliens(alien * arr_alien)
|
||||
{
|
||||
while (arr_alien->id != ZERO)
|
||||
{
|
||||
PrintAlien(arr_alien);
|
||||
arr_alien++;
|
||||
}
|
||||
}
|
||||
|
||||
/* print data */
|
||||
void PrintAllAlienData()
|
||||
{
|
||||
alien * aliens_data;
|
||||
GetAllDataFromFile(&aliens_data);
|
||||
PrintArrayAliens(aliens_data);
|
||||
free(aliens_data);
|
||||
OutputErrorOpenReadFile();
|
||||
|
||||
}
|
||||
|
||||
/* write in file */
|
||||
void WriteInFile(alien * aln, unsigned int location)
|
||||
{
|
||||
FILE * fp = fopen(FILE_NAME , APPEND_BINARY);
|
||||
fseek(fp, (sizeof(alien) * location), SEEK_SET);
|
||||
fwrite(aln, sizeof(alien), ONE, fp);
|
||||
CloseFile(fp);
|
||||
}
|
||||
|
||||
/* check if the alien is exists in file */
|
||||
BOOLEAN ValueExistsInFile(alien * aln)
|
||||
{
|
||||
alien * temp_aln = (alien *)malloc(sizeof(alien));
|
||||
FILE * fp = fopen(FILE_NAME , APPEND_BINARY);
|
||||
fseek(fp, ZERO, SEEK_SET);
|
||||
while (fread(temp_aln, sizeof(alien), ONE, fp) > ZERO && temp_aln->id != aln->id);
|
||||
CloseFile(fp);
|
||||
return (temp_aln->id == aln->id);
|
||||
}
|
||||
|
||||
/* Get the pointer of alien in file */
|
||||
unsigned int GetLocationValueInFile(alien * aln)
|
||||
{
|
||||
FILE * fp;
|
||||
unsigned int count = -ONE;
|
||||
alien * temp_aln = (alien *)malloc(sizeof(alien));;
|
||||
fp = fopen(FILE_NAME , APPEND_BINARY);
|
||||
fseek(fp, ZERO, SEEK_SET);
|
||||
while (fread(temp_aln, sizeof(alien), ONE, fp) > ZERO && temp_aln->id != aln->id)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
CloseFile(fp);
|
||||
return count;
|
||||
}
|
||||
|
||||
void GetInputIdAlien(alien * aln)
|
||||
{
|
||||
int id;
|
||||
printf("Enter ID: ");
|
||||
scanf("%d", &id);
|
||||
aln->id = id;
|
||||
}
|
||||
|
||||
void GetInputNameAlien(alien * aln)
|
||||
{
|
||||
printf("Enter name: ");
|
||||
scanf("%s", (aln->name));
|
||||
}
|
||||
|
||||
void GetInputStarNameAlien(alien * aln)
|
||||
{
|
||||
printf("Enter star name: ");
|
||||
scanf("%s", (aln->star_name));
|
||||
}
|
||||
|
||||
void GetInputAgeAlien(alien * aln)
|
||||
{
|
||||
int age;
|
||||
printf("Enter age: ");
|
||||
scanf("%d", &age);
|
||||
aln->age = age;
|
||||
}
|
||||
|
||||
void GetInputUpdateAlien(alien * aln)
|
||||
{
|
||||
GetInputNameAlien(aln);
|
||||
GetInputStarNameAlien(aln);
|
||||
GetInputAgeAlien(aln);
|
||||
}
|
||||
|
||||
void GetInputAlien(alien * aln)
|
||||
{
|
||||
GetInputIdAlien(aln);
|
||||
GetInputUpdateAlien(aln);
|
||||
}
|
||||
|
||||
/* create data */
|
||||
void CreateData(alien * aln)
|
||||
{
|
||||
unsigned int location;
|
||||
alien temp = *aln;
|
||||
if (!ValueExistsInFile(&temp))
|
||||
{
|
||||
location = GetLocationValueInFile(aln) + ONE;
|
||||
WriteInFile(aln, location);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("The alien is exists already!\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* update data */
|
||||
void UpdateData(alien * aln)
|
||||
{
|
||||
if (ValueExistsInFile(aln))
|
||||
{
|
||||
GetInputUpdateAlien(aln);
|
||||
WriteInFile(aln, GetLocationValueInFile(aln));
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("The alien is not exists!\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Project
|
||||
// -------
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 19.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
string command = " ";
|
||||
alien aln;
|
||||
int id = ZERO;
|
||||
string exit_command = EXIT;
|
||||
string print_command = PRINT;
|
||||
string create_command = CREATE;
|
||||
string update_command = UPDATE;
|
||||
|
||||
while (!StringCompare(command, exit_command))
|
||||
{
|
||||
// print request for command
|
||||
printf("Enter command (\"print\", \"create\", \"update\" and \"exit\"): ");
|
||||
|
||||
// Input command
|
||||
scanf("%s", command);
|
||||
if (StringCompare(command, print_command))
|
||||
{
|
||||
PrintAllAlienData();
|
||||
}
|
||||
else if (StringCompare(command, create_command))
|
||||
{
|
||||
/* get input from user about new alien */
|
||||
/* if alien is not exists create new alien else output error */
|
||||
GetInputAlien(&aln);
|
||||
CreateData(&aln);
|
||||
}
|
||||
else if (StringCompare(command, update_command))
|
||||
{
|
||||
GetInputIdAlien(&aln);
|
||||
UpdateData(&aln);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
BIN
Projects/תרץ1/starship.dat
Normal file
BIN
Projects/תרץ1/starship.dat
Normal file
Binary file not shown.
Reference in New Issue
Block a user