?
Solved

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

Posted on 2005-03-31
4
Medium Priority
?
1,779 Views
Last Modified: 2008-02-26
Hello,

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.
0
Comment
Question by:nothing8171
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 1

Author Comment

by:nothing8171
ID: 13676574
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 1500 total points
ID: 13676720
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))

AW
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 13676737
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

AW
0
 
LVL 44

Expert Comment

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

AW
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question