Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 3rd, 2006, 7:16 PM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Working with objects

I've made this huge Tetris game. I'm a high school student learning programming in school. So I'm stuck on some OOP concepts.

Here's my problem in simplest terms:

I have 5 classes. there's a class to read in from a file all the Tetris data (shape of pieces, color of pieces, shape of grid and so on...). I have another class which is the grid of where the Tetris pieces fall, it's has a 20x10 array of smallbox objects. Note: The small box objects (from the duh! smallbox class) contain the data of each of the individual squares on the Tetris grid (the color and if there's anything moving down in that smallbox).

In addition, I have another class which is similar to the Tetris grid. It's a Piece class which is a 4x4 grid holding a different set of smallbox object data.

Now down to the important stuff:
What I'm trying to do is use the FileRead class to bring in information from the file. So I create an array of pieces in the FileRead class. When this is done, how can I access this data in another class? If I create a object in another class with the same name, it will create a new object local to that class and all my piece data from the file will be stuck in the FileRead object.

Please check out my project. There's an error in the FileRead Class on line 277, which I'm 100% that anyone with the least bit of OOP skills (in java) can fix with a quick glance.

Here's the attachment (be sure to run tetris.java)

Any help, would be 110% appreciated.
Attached Files
File Type: zip tetris2.zip (11.3 KB, 16 views)
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Jun 3rd, 2006, 7:47 PM   #2
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
I don't have time right now to look at your code, but this sounds like a problem that could be solved by implementing Serializable for your class. Serializable objects are easy to write to and read from file.

Constraint: All objects in the class must be serializable before it can be serializable.

Java API
Java API

Anyway you just wrap them using FileInput/FileOutput streams and it's cake.

If I have time I will look into this though.
Harakim is offline   Reply With Quote
Old Jun 3rd, 2006, 8:43 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
The problem is a classic mistake:
tetPiece = new Piece [totalPiece];
The above line doesn't do what you think it does. It doesn't create an array full of Piece objects. It creates an empty array that can potentially hold Piece objects. That's where your error's creeping in, I suspect.

As an aside:
	catch (IOException e)
	{
	}
It's very bad practise to ignore exceptions.
Arevos is offline   Reply With Quote
Old Jun 3rd, 2006, 10:52 PM   #4
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Thanks for your help, but I still can't get this to work.
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Jun 4th, 2006, 1:32 AM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Why not create the FileRead object inside the class you want to use the data in? Another possibility is to create a method inside that object to receive the data; for instance, setData(String[] fileData).
titaniumdecoy is offline   Reply With Quote
Old Jun 4th, 2006, 1:59 AM   #6
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by Arevos
The problem is a classic mistake:
tetPiece = new Piece [totalPiece];
The above line doesn't do what you think it does. It doesn't create an array full of Piece objects. It creates an empty array that can potentially hold Piece objects. That's where your error's creeping in, I suspect.
You're absolutely correct about that. If I created an object that's not an array

Piece testing = new Piece();
testing.whatever(); // works

However, creating an array of Pieces objects doesn't work. I tried everything. I just need someone to fix this for me once and I'll remember for the rest of my life.
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Jun 4th, 2006, 2:15 AM   #7
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,123
Rep Power: 5 lectricpharaoh will become famous soon enough
Why not use an array of some fundamental type (like byte or int) for the grid? Having an object for each square seems a little bit inefficient. Using objects for the actual pieces is good, though, but the game grid could best be thought of as a single cohesive unit, and it could track what was 'stored' in each square of the grid (empty, solid, and for solid squares, what color it was). As an example, imagine how clumsy it would be to use Java's String class if it stored the strings as an array of java.lang.Character objects.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old Jun 4th, 2006, 2:24 AM   #8
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by lectricpharaoh
Why not use an array of some fundamental type (like byte or int) for the grid? Having an object for each square seems a little bit inefficient. Using objects for the actual pieces is good, though, but the game grid could best be thought of as a single cohesive unit, and it could track what was 'stored' in each square of the grid (empty, solid, and for solid squares, what color it was). As an example, imagine how clumsy it would be to use Java's String class if it stored the strings as an array of java.lang.Character objects.
Sounds like a great idea. However, I'm trying to develop my Object Oriented Programming skills by doing this project. Therefore, I have to stick with my method.

Creating the game like this also allows me to add things to the game when I'm done such as have multiple pieces falling at the same time and other crazzy additions.
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Jun 4th, 2006, 3:48 AM   #9
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
I couldn't get your code to compile because of a lot of things, namely the import statements reference libraries that don't exist.


For one, I think this line of code:
268:   tetPiece [pieceCounter].setFilledPiece (xLen, yLen, true);
will give a NullPointerException for reasons mentioned by TitaniumDecoy.


It is the same as this:
246:   tetPiece = new Piece [totalPiece];
for ( Piece p : tetPiece )
	p = null;

You need to fill tetPiece with Piece Objects, so change it to this:
246:   tetPiece = new Piece [totalPiece];
for ( Piece p : tetPiece )
	p = new Piece();


An even better solution, in my opinion, would be to change line 268:
268:   tetPiece [pieceCounter] = new Piece(xLen, yLen, true);
although many people would disagree for a whole lot of reasons.


I hope this helps. If not and you really need to finish this, change 246: like I said and leave it at that.
Harakim is offline   Reply With Quote
Old Jun 4th, 2006, 4:20 PM   #10
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Thanks Harakim. That helped a lot!
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:16 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC