473,659 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programming the game of life..Some help needed.

8 New Member
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 4778
horace1
1,510 Recognized Expert Top Contributor
ios::eof() is a function, try
Expand|Select|Wrap|Line Numbers
  1.     while (!infile.eof)
  2.  
Mar 1 '07 #2
CapMaster
8 New Member
What do you mean by that? Where should I put it?
Mar 1 '07 #3
horace1
1,510 Recognized Expert Top Contributor
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 Recognized Expert Specialist
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 New Member
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 Recognized Expert Contributor
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
6513
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 yeah,-that's-non-obvious dept. theodp writes "As if there weren't enough dodgy patents, here's an excerpt from one granted to Microsoft Tuesday for a 'Method and apparatus for displaying information regarding stored data in a gaming system': 'When saving a game,...
9
3875
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" http://www.gamedev.net/reference/design/features/makegames/. the article suggests first programming a tetris clone, then moving on to "breakout", "pacman", and a super mario style game.. My question is, how long should it realistically take me to be able to...
34
2789
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 we think about is apache/oracle/php. Now, I can do oracle no problem but I'm pretty wet behind the ears about everything else. What books could you recommend to me so that I can learn: - what all this apache stuff is about, the mod_* - what...
134
7964
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 increasingly been using scripting languages (especially Python and Bourne shell) which offer the same speed and yet are far more simple and safe to use. I can no longer understand why anyone would willingly use C to program anything but the lowest...
2
2586
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 keyboard. In the field where your snake is, there are candies that you must eat. The more candies you eat, the longer you get. You win if you eat all 10 candies without dying more than 3 times in one level. In that case a time bonus is added to your...
2
1861
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 (BugEaterForm) that has a module, has the following objects: - Snake, for drawing and being controlled; - Player, with player information, such as name, score, number of lives; - PlayField, containing the matrix on which you are playing; - BugEaterForm.
3
1642
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 game but when I am compiling this it yields an error.... Why is that #include<iostream>
4
4430
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 computer game. Its a game with up to 4 players, so it seems like it would be hard to do. After all, a computer doesn't have up to 4 mouses. And its a board game where pieces move on the board, so I'd have to keep redrawing the board but with squares...
5
4183
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 the hang of how python works but to put my thoughts into the program to make it run is the confusing part for me. The task is to implement a Hangman game and the Program Specifications are: 1) Output a brief description of the game of hangman...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8535
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.