Thread: 3 of 8 options

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    6

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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'.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    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()                                     
    {
        
        printf("Este codigo trabalha apenas com matrizes LIFE quadradas de 40x40:\n");        
        //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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 01:24 PM
  2. What's the best way to handle many program options?
    By stickmangumby in forum C Programming
    Replies: 19
    Last Post: 06-06-2007, 04:06 PM
  3. Explorer options
    By Magos in forum Windows Programming
    Replies: 1
    Last Post: 04-02-2005, 09:23 AM
  4. Options for a Server
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-10-2004, 09:10 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM