# Thread: 3 of 8 options

1. ## 3 of 8 options

In my code, I have a cell in a matrix with 8 neighbours (horizontal, vertical and diagonal). I need to implement a condition such as

CONDITION 2 = When three of any of the 8 neighbors (V1, V2, V3, ... V8) are equal to some value like 'X'. But i dont wanna use a code like below when i have to write all the possibilities. How could I write this better ?

Code:
` #define COND2 (V1==V2==V3=='X') || (V1==V2==V4=='X') || (V1==V2==V5=='X') || (V1==V2==V6=='X') || (V1==V2==V7=='X') || (V1==V2==V8=='X') ||`

2. You could count the number of neighbors with a value equal to the given value, then just compare the count to 3. If you use an array, this could be easily simplified to a loop.

By the way, (V1==V2==V3=='X') is not quite what you want if you wanted to check that V1 is equal to V2, and V2 is equal to V3, and V3 is equal to 'X'.

3. It would be a lot easier to simply count how many of the 8 cells matched your criteria.
If the total is 3, do something, otherwise do something else.

What your tests don't do at the moment is exclude more than 3 cells having an 'X'.

3 of 8 sounds like Life
Conway's Game of Life - Wikipedia, the free encyclopedia

4. I use a for loop:

Code:
```for(row=0, match=0;rowi<8;row++) {
for(col=0; col<8;col++) {
if(a[row][col]== a[row-1][col])  //12 o'clock
match++;
//continue the above if, for all 8 directions```
You must either add logic to correctly handle the top and bottom row, and the leftmost and rightmost columns, and the four corner cells, or you must make your array larger than you need by two in each dimension, and fill it with an out bounds value on the edges.

Code:
```xxxxxxxxxx
xoooooooox
xoooooooox
xoooooooox
xxxxxxxxxx```
the "working" part of the array is the o's, and the x's have the "off the grid" value (maybe -9 or something impossible for the working array cells to ever have as a valid value.

And welcome to the forum!

5. Yeah, Salem, laserlight and adak, it's a life code. My teacher asked to do it, but without use the classic Conway's code. What is done by now: generate the 40x40 integer matrix with random numbers, create a char matrix based on the previous one respecting 40% of chances of cell be alive. But the function geracao that will control what happens on the next generation is not done yet. The main problem is define the conditions not using the i and j, but using V1 to V8. I thought in use adak suggestion but i couldnt understand it clearly. Anyway here's my code until now. Thanks for the help until now.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//Define the 8 possíbles neighbours of a cell
#define V1 (B[i-1][j])
#define V2 (B[i][j+1])
#define V3 (B[i+1][j])
#define V4 (B[i][j-1])
#define V5 (B[i-1][j-1])
#define V6 (B[i-1][j+1])
#define V7 (B[i+1][j-1])
#define V8 (B[i+1][j+1])

//Define conditions
//Condition 1: All neighbours are alive (X)
//Condição 2: Any three neighbours are alive (X)
//Condição 3: Two any neighbours are alive (X)
//Condição 4: Any dead cell (.) with three alive (X) neighbours, become alive;

#define COND1 (Write the first condition')
#define COND2 (Write the second condition'')
#define COND3 (Write the third condition')
#define COND4 (Write the fourth condition'')

//Global variables
int i,j,match=0; // Initialize the variables i,j (row and column) and match
int A[40][40]; // Define the 40x40 integer matrix
char B[40][40]; // Define the 40x40 char matrix

int main()
{

//This code only works with square matrix dimension 40.

printf("\nMatriz LIFE:\n\n");
srand(time(NULL));  //Activate function srand
for(i = 0; i < 40; i++)  //loop - i to 40
{
for(j = 0; j < 40; j++) //loop - j to 40
{
A[i][j] = rand() % 10;  // Aij value is between 0 and 9
printf("%1d", A[i][j]);  // Print each Aij
if(j==39) printf("\n"); // Organize the matrix
}

}
printf("\n");
printf("\n");
for(i = 0; i < 40; i++)  //loop - i to 40
{
for(j = 0; j < 40; j++) //loop - j to 40
{
if (A[i][j]<=3) B[i][j] = 'X';   //X for alive cells
else B[i][j] = '.'; // . for dead cells
printf("%1c", B[i][j]); // Print . or X
if(j==39) printf("\n"); // Organize the matrix
}

}
printf("\n");

geracao();   //Calls function geracao
imprimirmatriz(); //Calls function imprimirmatriz
getch();

}

//Funçtion geração - apply the necessary conditions.
int geracao(void)
{
printf("A funcao geracao aplica as condicoes de sobrevivencia\n");
\\Apply the conditions here using COND1, COND2, COND3 and COND4

}

//Function imprimir matriz (print the matrix after the geracao function)
int imprimirmatriz(void)
{
printf("A funcao imprimirmatriz imprime a nova matriz B após a aplicação das condicoes da geracao\n");
printf("\n");
printf("\n");
for(i = 0; i < 40; i++)  //Loop - i to 40
{
for(j = 0; j < 40; j++) //Loop - j to 40
{
printf("%1c", B[i][j]);
if(j==39) printf("\n");
}
}
}```

6. You can do it a couple of ways. You could take all those values V1 through V8, stick them in a list, sort it, and then start from high to low, subtracting values from X, until you reach zero, in which case your condition is true, or until you run out of array elements. Or, you could run permutations (Permutation - Wikipedia, the free encyclopedia) of V1 through V8, and stop on the first one that matches X.

Quzah.