1
0
mirror of https://gitlab.com/klmp200/LO27.git synced 2024-11-22 16:53:22 +00:00

Merge branch 'sli' into 'master'

Ajout d'une gui et correction de l'apply rules

See merge request !20
This commit is contained in:
Antoine Bartuccio 2016-12-29 02:16:22 +00:00
commit 758e64f6cd
5 changed files with 129 additions and 27 deletions

View File

@ -7,7 +7,7 @@ INCLUDEDIR=-I/usr/include -I. -I../LibMatrix
#Library variables
LIBTARGET=libGui.so
LIBSOURCE=pixel
LIBSOURCE=gui
LIBSOURCECFILE=$(LIBSOURCE:=.c)
LIBSOURCEOFILE=$(LIBSOURCE:=.o)

View File

@ -2,15 +2,17 @@
* @Author: klmp200
* @Date: 2016-12-27 19:59:21
* @Last Modified by: klmp200
* @Last Modified time: 2016-12-29 00:03:19
* @Last Modified time: 2016-12-29 02:51:25
*/
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <pixel.h>
#include <gui.h>
#include <matrix.h>
#include <CellElement.h>
#include <termios.h>
#include <unistd.h>
void SetPixel(SDL_Renderer *renderer, int x, int y, bool value){
@ -88,3 +90,51 @@ int NewWindowFromMatrix(Matrix m){
return 0;
}
bool YesOrNo(char message[]){
char response;
printf("%s (Y/n)\n", message);
response = getchar();
ClearBuffer();
if (response == 'n'){
return false;
} else {
return true;
}
}
void ClearBuffer(){
char c;
while ((c = getchar()) != '\n' && c != EOF) { }
}
void DisplayMatrixGUI(Matrix m, bool useSDL){
if (useSDL){
printf("Press ENTER or the red cross to exit the window and continue.\n");
NewWindowFromMatrix(m);
} else {
BasicPrintMatrix(m);
}
}
int SafeNumberInput(int min, int max){
char str[30];
int input;
fgets(str, 29, stdin);
ClearBuffer();
input = atoi(str);
while (input < min || input > max){
printf("The number should be between %d and %d\n", min, max);
fgets(str, 29, stdin);
ClearBuffer();
input = atoi(str);
}
return input;
}

View File

@ -45,4 +45,33 @@ void WaitUntilEnter(SCREEN * screen);
*/
int NewWindowFromMatrix(Matrix m);
/**
* Display a message and ask for yes or no to the user
* @param message the message to display
* @return bool the answer
*/
bool YesOrNo(char message[]);
/**
* Display matrix choosing the right function
* @param m a Matrix
* @param useSDL a bool
* @return
*/
void DisplayMatrixGUI(Matrix m, bool useSDL);
/**
* Get a number from the user safely
* @param min the minimal authorized number
* @param max the maximal authorized number
* @return int the input number
*/
int SafeNumberInput(int min, int max);
/**
* Clears the buffer of stdin
* @return
*/
void ClearBuffer();
#endif

View File

@ -9,16 +9,15 @@ Matrix applyRules (Matrix matrix,int Rules, int N){
int power = 2;
int sum = 0;
int j = 0;
Matrix tempMatrix;
Matrix tempMatrix1;
Matrix tempMatrix2;
if (Rules <= 0 || N < 1){
return matrix;
}
tempMatrix = CreateMatrix();
}
while(power<=512){
RulesMatrix[i] = Rules%power - sum;
sum = Rules%power;
@ -29,17 +28,20 @@ Matrix applyRules (Matrix matrix,int Rules, int N){
power*=2;
}
/* test code : print decomposition */
/*for (j=0;j<i;j++){
printf("%d +",RulesMatrix[j]);
}*/
tempMatrix1 = matrixFromRules(matrix, i, RulesMatrix);
for (j = 0;j<N;j++){
freeMatrix(tempMatrix);
tempMatrix = matrixFromRules(matrix,i, RulesMatrix);
for (j=1;j<N;j++){
printf("Tourne\n");
tempMatrix2 = matrixFromRules(tempMatrix1,i, RulesMatrix);
freeMatrix(tempMatrix1);
tempMatrix1.colCount = tempMatrix2.colCount;
tempMatrix1.rowCount = tempMatrix2.rowCount;
tempMatrix1.cols = tempMatrix2.cols;
tempMatrix1.rows = tempMatrix2.rows;
}
return tempMatrix;
return tempMatrix1;
}

View File

@ -10,31 +10,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <matrix.h>
#include <pixel.h>
#include <gui.h>
int main(){
int Rule34 = 3;
int rule;
int times;
bool useSDL;
bool cont = true;
int col;
int row;
Matrix m1;
Matrix m2;
BooleanMatrix bmatrix = CreateBooleanMatrix(5, 5);
BooleanMatrix bmatrix;
useSDL = YesOrNo("Do you want to use SDL library for matrix display ?");
printf("A random matrix will be generated\n");
printf("Enter the number of columns of this matrix\n");
col = SafeNumberInput(1, 30000);
printf("Enter the number of rows of this matrix\n");
row = SafeNumberInput(1, 30000);
bmatrix = CreateBooleanMatrix(col, row);
bmatrix = RandomizeBooleanMatrix(bmatrix);
m1 = newMatrix(bmatrix);
FreeBooleanMatrix(bmatrix);
NewWindowFromMatrix(m1);
DisplayMatrixGUI(m1, useSDL);
m2 = applyRules(m1,Rule34,3);
while (cont){
printf("What rule do you want to apply to this matrix ?\n");
rule = SafeNumberInput(1, 481);
printf("How many times do you want the rule to be applied ?\n");
times = SafeNumberInput(1, 100000);
m2 = applyRules(m1,rule,times);
freeMatrix(m1);
NewWindowFromMatrix(m2);
DisplayMatrixGUI(m2, useSDL);
cont = YesOrNo("Do you want to apply other rules on this matrix ?");
m1 = m2;
}
freeMatrix(m1);
freeMatrix(m2);
return 0;
}