![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
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.
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
The problem is a classic mistake:
tetPiece = new Piece [totalPiece]; As an aside: catch (IOException e)
{
} |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#5 |
|
Expert Programmer
|
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).
|
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
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. |
|
|
|
|
|
|
#7 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,123
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
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. |
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
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); 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); I hope this helps. If not and you really need to finish this, change 246: like I said and leave it at that. |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Thanks Harakim. That helped a lot!
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|