4 replies
As someone fairly new to OO programming, I was wondering how more experienced programmers follow the design process. As an example, let's say you want to code a simple tetris game clone in Java or Python, how do you go about determining which classes to define and whether it makes sense to declare some as abstract classes, etc.

Letting my inexperience show, I would identify the following classes:

1.) Screen or playable area on screen
2.) A square
3.) A shape (consisting of more than one square as in 2.)
4.) a keyboard key (should this even be a class?)
5.) Some text (title, scoreline, etc.)

How will you approach this design?
#design
  • Profile picture of the author PhilHardaker
    Ok, tetris! There are not a lot of 'things' in it, but here is my high level. And don't forget, there is always more than one way to program something...

    You will have your main program class.

    Inside you can instantiate your playable-area class.
    The title is an attribute of the playable-area, as are the width, height, color. The grid is an attribute too, a 2 dimensional array.

    You could have a little scoreboard class, with attributes for user name, score, clock. The playable-area class also instantiates this as a member.

    You can have a shape abstract class. It will have properties like color, horizonal position, vertical position. I has methods like move left one square, move right one square, move down one square, and spin-90-degrees.

    Then you need 7 objects derived from the shape class. I will call them bar, box, left-zigzag, right-zigzag, left-ell, right-ell, and tee classes. They will have some kind of layout attribute to describe the squares they fill.

    That's all the objects.

    When you run the program, the main program constructor instantiates the playable area, and the grid and score are blank.
    It then runs the play method.

    The play method waits for keys to be pressed.
    A drop key instantiates a random shape and gives it a random column value. Then the play method goes into a loop with a pause timer. Every time the timer completes, it executes the shape.move-down method. The shape checks the grid to see if there is room for it to move down.
    Meanwhile, the play method is still detecting keys that might spin or move the shape sideways.
    When the shape can't move down in the grid anymore, or the user presses the stop key, the play method ends.
    That should get you started!
    {{ DiscussionBoard.errors[9539186].message }}
  • Profile picture of the author Himanshu1988
    I was also having same problem before. How much classes? Which class will do what? How much function in a class? How many variables? Visibility? The answer is you need to learn UML.
    Signature

    Web Design | SEO | WordPress | Mobile App @ ZOTO Solutions

    {{ DiscussionBoard.errors[9539530].message }}
  • Profile picture of the author pinkwhale
    Banned
    Hi, first object oriented analysis is done after this oo design is start.oo design have many steps like user interface,use case and conceptual model
    {{ DiscussionBoard.errors[9594094].message }}
  • Profile picture of the author BDazzler
    I'd use the MVC (Model/View/Controller) design pattern. I'd build the model from an abstract class (like Playing Piece) with the methods turn, fall etc. I'd put the main game loop into the base model class, along with the core motion and rotation functions.

    I'd use an base class for a view with a concrete class that interfaced to various devices, so you could play the game on whatever screen you happen to have.

    Then, I'd have a controller. Again, starting with an abstract class, I'd let it accept input from a generic device. At least two methods to the abstract class, "Rotate Right" and "Rotate Left". The concrete class would connect to a keyboard, mouse, joy stick, game controller ... whatever.
    {{ DiscussionBoard.errors[9613153].message }}

Trending Topics