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

All rules fixed and working

This commit is contained in:
Antoine Bartuccio 2016-12-28 16:28:07 +01:00
parent 581e2e60e8
commit 30bac5cdfe
3 changed files with 180 additions and 87 deletions

View File

@ -475,101 +475,120 @@ void FreeBooleanMatrix(BooleanMatrix matrix){
} }
Matrix basicRule(Matrix m, int incrh, int incrv){ Matrix matrixFromRules(Matrix m, int n, int rules[]){
int i; int i;
int j; int j;
bool a;
bool b; bool * bools = NULL;
Matrix result = CreateMatrix(); Matrix result = CreateMatrix();
if (rules == NULL){
result.colCount = 0;
result.rowCount = 0;
return result;
}
result.colCount = m.colCount; result.colCount = m.colCount;
result.rowCount = m.rowCount; result.rowCount = m.rowCount;
for (i=0; i < m.rowCount; i++){ for (i=0; i < m.rowCount; i++){
for (j = 0; j < m.colCount; j++){ for (j = 0; j < m.colCount; j++){
a = GetCellValue(m, j, i); bools = GetFromRules(m, j, i, n, rules);
b = ErrorToFalse(GetCellValue(m, j + incrh, i + incrv)); if (bools != NULL){
if (XOR(a, b)){ if (MXOR(n, bools)){
SetCellValue(result, j, i, true); SetCellValue(result, j, i, true);
} }
free(bools);
}
} }
} }
return result; return result;
} }
Matrix leftRule(Matrix m){ bool MXOR(int n, bool bools[]){
Matrix result = CreateMatrix();
int i; int i;
int j; bool r = false;
for (i=0;i<n;i++){
result.colCount = m.colCount; r = XOR(r, bools[i]);
result.rowCount = m.rowCount;
for (i=1;i<m.colCount;i++){
for (j=0;j<m.rowCount;j++){
if (GetCellValue(m, i, j)){
SetCellValue(result, i - 1, j, true);
}
} }
return r;
} }
return result; bool firstRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos, RowPos));
} }
Matrix rightRule(Matrix m){ bool leftRule(Matrix m, int ColPos, int RowPos){
Matrix result = CreateMatrix(); return ErrorToFalse(GetCellValue(m, ColPos + 1, RowPos));
}
bool rightRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos - 1, RowPos));
}
bool topRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos, RowPos -1));
}
bool bottomRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos, RowPos + 1));
}
bool top_leftRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos + 1, RowPos + 1));
}
bool top_rightRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos - 1, RowPos + 1));
}
bool bottom_leftRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos + 1, RowPos - 1));
}
bool bottom_rightRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos - 1, RowPos - 1));
}
bool * GetFromRules(Matrix m, int ColPos, int RowPos, int n, int rules[]){
bool * bools = (bool *)malloc(sizeof(bool) * n);
int i; int i;
int j;
result.colCount = m.colCount; if (bools != NULL){
result.rowCount = m.rowCount; for (i=0;i<n;i++){
switch (rules[i]){
for (i=0;i<m.colCount - 1;i++){ case 8:
for (j=0;j<m.rowCount;j++){ bools[i] = topRule(m, ColPos, RowPos);
if (GetCellValue(m, i, j)){ break;
SetCellValue(result, i + 1, j, true); case 128:
} bools[i] = bottomRule(m, ColPos, RowPos);
} break;
} case 2:
return result; bools[i] = leftRule(m, ColPos, RowPos);
} break;
case 32:
Matrix topRule(Matrix m){ bools[i] = rightRule(m, ColPos, RowPos);
Matrix result = CreateMatrix(); break;
int i; case 4:
int j; bools[i] = top_leftRule(m, ColPos, RowPos);
break;
result.colCount = m.colCount; case 16:
result.rowCount = m.rowCount; bools[i] = top_rightRule(m, ColPos, RowPos);
break;
for (i=1;i<m.rowCount;i++){ case 256:
for (j=0;j<m.colCount;j++){ bools[i] = bottom_leftRule(m, ColPos, RowPos);
if (GetCellValue(m, j, i)){ break;
SetCellValue(result, j, i - 1, true); case 34:
bools[i] = bottom_rightRule(m, ColPos, RowPos);
break;
case 1:
bools[i] = firstRule(m, ColPos, RowPos);
} }
} }
} }
return result; return bools;
}
Matrix downRule(Matrix m){
Matrix result = CreateMatrix();
int i;
int j;
result.colCount = m.colCount;
result.rowCount = m.rowCount;
for (i=0;i<m.rowCount - 1;i++){
for (j=0;j<m.colCount;j++){
if (GetCellValue(m, j, i)){
SetCellValue(result, j, i + 1, true);
}
}
}
return result;
} }
/* todos : /* todos :

View File

@ -232,40 +232,113 @@ Matrix andRowSequenceOnMatrix(Matrix m);
Matrix orRowSequenceOnMatrix(Matrix m); Matrix orRowSequenceOnMatrix(Matrix m);
/** /**
* Apply XOR on a whole matrix * Apply a given set of rules
* @param m a matrix * @param m a matrix
* @param incrh the horizontal increment * @param n the number of rules
* @param incrv the vertical increment * @param rules an array of rules
* @return matrix a new modified matrix * @return matrix a new modified matrix
*/ */
Matrix basicRule(Matrix m, int incrh, int incrv); Matrix matrixFromRules(Matrix m, int n, int rules[]);
/** /**
* Apply right rule on a matrix * Apply XOR to a given number of bools
* @param m a matrix * @param n the number of bool
* @return matrix a new matrix * @param bools an array of bool
* @return bool the processed bool
*/ */
Matrix rightRule(Matrix m); bool MXOR(int n, bool bools[]);
/** /**
* Apply left rule on a matrix * Get a cell with first rule
* @param m a matrix * @param m a matrix
* @return matrix a new matrix * @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/ */
Matrix leftRule(Matrix m); bool firstRule(Matrix m, int ColPos, int RowPos);
/**
* Apply top rule on a matrix
* @param m a matrix
* @return matrix a new matrix
*/
Matrix topRule(Matrix m);
/** /**
* Apply down rule on a matrix * Get a cell with right rule
* @param m a matrix * @param m a matrix
* @return matrix a new matrix * @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/ */
Matrix downRule(Matrix m); bool rightRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with left rule
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/
bool leftRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with top rule
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/
bool topRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with bottom rule
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/
bool bottomRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with top_left rule
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/
bool top_leftRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with top_right rule
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/
bool top_rightRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with bottom_left rule
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/
bool bottom_leftRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with bottom_right rule
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/
bool bottom_rightRule(Matrix m, int ColPos, int RowPos);
/**
* Get a list of bool from a given set of rules
* @param m a Matrix
* @param ColPos colon position of the point
* @param RowPos colon position of the point
* @param n number of rules
* @param rules[] an array of rules
* @return bool[] an array of bool
*/
bool * GetFromRules(Matrix m, int ColPos, int RowPos, int n, int rules[]);
Matrix sumMatrix(Matrix matrix1,Matrix matrix2); Matrix sumMatrix(Matrix matrix1,Matrix matrix2);

3
main.c
View File

@ -13,6 +13,7 @@
int main(int argc, char **argv){ int main(int argc, char **argv){
int Rule = 256; int Rule = 256;
int N = 1; int N = 1;
int rules[] = {1, 2};
Matrix matrix = CreateMatrix(); Matrix matrix = CreateMatrix();
Matrix m2; Matrix m2;
Matrix m3; Matrix m3;
@ -23,7 +24,7 @@ int main(int argc, char **argv){
m3 = newMatrix(bmatrix); m3 = newMatrix(bmatrix);
FreeBooleanMatrix(bmatrix); FreeBooleanMatrix(bmatrix);
BasicPrintMatrix(m3); BasicPrintMatrix(m3);
m4 = downRule(m3); m4 = matrixFromRules(m3, 2, rules);
BasicPrintMatrix(m4); BasicPrintMatrix(m4);
freeMatrix(m4); freeMatrix(m4);
freeMatrix(m3); freeMatrix(m3);