473,378 Members | 1,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Programming the game of life..Some help needed.

I'm having some trouble programming the game of life. In the version my teacher gave us, it involves a class with a private grid variable for the game. Here's the class she gave us:
Expand|Select|Wrap|Line Numbers
  1. ..
  2. const int maxrow = 20, maxcol = 60;    //  grid dimensions
  3. class Life {
  4. public:
  5.     Life ();
  6.     void initialize();
  7.     void print();
  8.     void update();
  9. private:
  10. // allows for two extra rows and columns
  11.     int grid[maxrow + 2][maxcol + 2];  
  12.     int neighbor_count(int row, int col);
  13. ..
Now for the initializiation, she wants some #s or *s in pre-determined places that are to be read in from a data file, like this:
8 6
8 9
9 7
19 5

And for everything else, a blank space. Here's what I have, including a constructor to try to make the blank spaces:
Expand|Select|Wrap|Line Numbers
  1. ..
  2. Life::Life()
  3. {
  4.     int x, y;
  5.     for (x=0; x<maxrow; x++)
  6.     {
  7.         for (y=0; y<maxcol; y++)
  8.         {
  9.             grid[x][y]=' ';
  10.         }
  11.     }
  12. }
  13. void Life::initialize()
  14. {
  15.     ifstream infile;
  16.     infile.open("lifedata.txt");
  17.     int x, y;
  18.     while (!infile.eof)
  19.     {
  20.         infile>>x>>y;
  21.         grid[x][y]='#';
  22.     }
  23. ..
Here's my problem though. It says that I can't use !infile.eof in a class function. I have it there because what I'm trying to do is as long as there's numbers in the file, set those coordinates in the grid to be a # sign. Everything else, bet set to a blank space. I also have a question concerning filling arrays with classes, functions, and large programs like this. After I fill it in with the x and y, I want it to be like that everywhere in the program, that in every class function grid[x][y] retains the same values. How can I get the compiler to do those two things?
Mar 1 '07 #1
6 4768
horace1
1,510 Expert 1GB
ios::eof() is a function, try
Expand|Select|Wrap|Line Numbers
  1.     while (!infile.eof)
  2.  
Mar 1 '07 #2
What do you mean by that? Where should I put it?
Mar 1 '07 #3
horace1
1,510 Expert 1GB
What do you mean by that? Where should I put it?
forgot to put the correct code
Expand|Select|Wrap|Line Numbers
  1. while (!infile.eof())
  2.  
Mar 2 '07 #4
Ganon11
3,652 Expert 2GB
Also, your 2D array holding the values is declared as an integer array- but you're giving it character values such as ' ' and '#'. Should grid be a char array?
Mar 2 '07 #5
rd2d2
1
i have the same exercice in my class to implement the game of life can i see please how you did this i dont found any solution
Jan 22 '08 #6
Studlyami
464 Expert 256MB
Wow, raising up an old thread and asking for a solution. Post up what you have tried and where you are stuck at and we would be glad to help you out.
Jan 22 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
9
by: the_philospher | last post by:
I am a beginner programmer learning c++. i have read some useful information about programming games (which is my goal) on gamedev.net, particularly "how do i program games"...
34
by: Volker Hetzer | last post by:
Hi! I've done lots of programming for CAD, which was basically C/C++ and tcl/tk. Now, we are thinking about introducing more web based tools, programming them ourselves and right now the toolchain...
134
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've...
2
by: Hans Kamp | last post by:
As a programming exercise I try to program a game. In my case it is called BugEater. The purpose is that you are a snake. It moves automatically but you can control it with the keys on your...
2
by: Hans Kamp | last post by:
I am programming an easy game (not with a commercial purpose, just with learning purpose), but I doubt whether the following way of programming is proper or advisable. The main form...
3
by: ThaDoctor | last post by:
Hi. I am quite new to C++ so I think I would ask here what I am doing wrong with this code. I am writing a little game in a text console, but here is something that is in no way related to the...
4
by: COHENMARVIN | last post by:
Are there any good sources on video game programming in vb.net? Is "DirectX" a set of libraries for video game programmers? The reason I ask is that I'd like to convert a board game into a...
5
by: av3rage | last post by:
I have never done any programming in my life but I have decided to go into engineering and in doing so we have to take this intro to programming course and I am pretty clueless. I am starting to get...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.