Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Working with objects (http://www.programmingforums.org/showthread.php?t=10166)

Eric the Red Jun 3rd, 2006 7:16 PM

Working with objects
 
1 Attachment(s)
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.

Harakim Jun 3rd, 2006 7:47 PM

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.

Arevos Jun 3rd, 2006 8:43 PM

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.

Eric the Red Jun 3rd, 2006 10:52 PM

Thanks for your help, but I still can't get this to work.

titaniumdecoy Jun 4th, 2006 1:32 AM

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).

Eric the Red Jun 4th, 2006 1:59 AM

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.

lectricpharaoh Jun 4th, 2006 2:15 AM

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.

Eric the Red Jun 4th, 2006 2:24 AM

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.

Harakim Jun 4th, 2006 3:48 AM

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.

Eric the Red Jun 4th, 2006 4:20 PM

Thanks Harakim. That helped a lot!


All times are GMT -5. The time now is 8:07 AM.

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