Hello all,
My background is in C, but I'm relatively new to Java and to Android.
I have a question as to whether I'm implementing code in the best way:

As a learning exercise in Java and in meeting the Model-View-
Controller pattern, I wrote a simple game. It plays out on a 2D grid
owned by the game class, and is drawn to the display by an extended
View class. I have it working, but I'm wondering after the fact if I'm
going about it the right way. Here's a simplified version of what I
have:


public class GameClass {

        // game is played out on this 2D field
        private final int[][] mGameGrid;

        public int[][] getGameGrid() {
                return mGameGrid;
        }
}



public class TileView extends View {

        // reference to what i want the view to draw
        private int[][] mGrid;

        // accept reference to some external grid
        public void setGrid(int[][] externalGrid) {
                mGrid = externalGrid;
        }

        @Override
        protected void onDraw(Canvas canvas) {
                // reads mGrid contents to draw the array to the canvas
        }
}




public class GameMVC extends Activity {

        private TileView mGameView;
        private GameClass mGame;


        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.game_layout);

                mGame = new GameClass();

                mGameView = (TileView) findViewById(R.id.GameTileView);
                mGameView.setGrid(mGame.getGameGrid());
        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
                // which updates the game and triggers screen redraws
        }

}



Let me add, I am trying to meet these 2 goals as well:
1. I am hoping to keep the game class completely independent of the
android platform, in the spirit of fully keeping the M-V-C model (i.e.
no import Android.anything in this class)
2. I would like define the view as a resource (I understand this is
more efficient) and so inflate it via
setContentView(R.id.TileView)

So, I'm wondering, are there better ways to connect the game to the
View? And, are my goals reasonable/appropriate?
Comments welcome, thanks.

I learn a lot from reading threads here, thanks much everyone for
contributing.
Best

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to