Thread: grid counting

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    9

    grid counting

    hello
    i'm writing the code for "The Game of Life".
    i'll explain, i get grid dimensions from the user, and in every cell there's either
    '-','R' or 'G'.
    now i need to go threw the grid and count how many R's and G's are adjacent to that cell.
    the problem arises when the i'm going threw the edge rows (1st and last) and columns (1st and last).
    i KNOW i can solve it somehow using a modulu, but i'm not sure how.

    Thank you for your attention, have a nice day

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by casper7 View Post
    hello
    i'm writing the code for "The Game of Life".
    i'll explain, i get grid dimensions from the user, and in every cell there's either
    '-','R' or 'G'.
    now i need to go threw the grid and count how many R's and G's are adjacent to that cell.
    the problem arises when the i'm going threw the edge rows (1st and last) and columns (1st and last).
    i KNOW i can solve it somehow using a modulu, but i'm not sure how.

    Thank you for your attention, have a nice day
    Leaving a border row and column is helpful. Don't use the zero'th row or zero'th column. That gives you a left and top side border. Make the array two rows more than the user requests, and two columns, also. That would give you a border all the way around the life board array.

    Modulus can be used like so:
    Code:
    for(row = 1; (row % row_num+1) > 0; row++)   {
       for(col = 1; (col % col_num+1) > 0; col++)   {
          //your sqr checking logic, here - sqr is dead, sqr is alive, etc.
    
       }
    }

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by casper7 View Post
    i KNOW i can solve it somehow using a modulu, but i'm not sure how.
    Hmm, if I'm not mistaken, modulo would "wrap-around" the board edges. I doubt that's what you want. It's been a while since I've played the Game of Life, but I don't recall the bottom row counting towards a top-row-square's neighbours.
    You could just use an if condition that doesn't perform the check for the squares below the current square if the current square is on the bottom row, and so on for all edges. That'll take care of the corners automatically for you.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tips for drawing a grid
    By countchocula in forum C Programming
    Replies: 12
    Last Post: 04-19-2008, 07:47 AM
  2. More Windows Trouble
    By samGwilliam in forum Windows Programming
    Replies: 9
    Last Post: 04-12-2005, 10:02 AM
  3. Cleaning up a Grid of Memory
    By Epo in forum C++ Programming
    Replies: 9
    Last Post: 02-25-2005, 10:23 AM
  4. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 05:38 PM
  5. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM