[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1782
  • Last Modified:

VB - Conway's Game of Life - logic for counting neighbors


I don't have the code with me, but I am trying to figure out the code or pseudocode for determing how many neighbors I have in John Conway's - The Game of Life.

I have a grid that is 5 x 5, and an initialize button which places an "X" in random text boxes when clicked.  The boxes are in text boxes within a control array.  How the heck do I figure out how many neighbors each Text box has?  This is driving me crazy?  

......and yes, this is a homework assignment, but I am not asking for the program, just some help with the logic for counting the neightbors.  

T0   T1  T2   T3   T4
T5   T6  T7   T8   T9
T10 T11 T12 T13 T14
T15 T16 T17 T18 T19
T20 T21 T22 T23 T24

You see, if I'm trying to get the neighbors for T11, it has no idea which Textbox number is around him.....it could be anything.  This is where I'm confused.
  • 3
1 Solution
nothing8171Author Commented:
I would create an array (2 dimensional) to represent the state of the population, internally, and then figuring out the neighbors is quite easy.  Then you need a routine that scans the arry, and sets the corresponding textboxes on the UI, based on the contents of the array.

Dim arrLife(4,4) as Integer ' 5 elements by 5 elements

then T11 represents to contents of arrLife(2,1)

then you can figure out the realtionship between the Textbox number N and the row and column in the array:

for TN, the Row (0...4) = Int(N/5) and the Column = N mod 5

so for T18, Row = 3, and Column = 3

and sure enough (remembering that the rows and columns in the array are numbered 0..4), T18 is arrLife(3,3))

similarly, from the array to the textboxes, is equally simple, as arrLife(R,c) ==> T(R*5 + C)

that is multiply the row index (0..4) by 5, and add the column indec (also 0..4) to get the N, for TN

thus, arrLife(3,3) --> T(3*5 +3) = T18

why only a B?  If you needed a more complete explanation, then all you need do is to ask.


Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now