r/javahelp • u/TegstBesged • Jan 15 '19
Help to structure simple java game to MVC
I am doing this project and want to structure it as the MVC model, i just cant to seem to understand how, help would be appreciated
my code here : https://github.com/tegstbesged/snake-simpel.git
16
Upvotes
3
u/Crazed8s Jan 16 '19
Well you can do it however you want really. The point of it is to simply separate the logic into smaller organized pieces.
So break it up into pieces.
What are the things you need to know: -the board (size, shape, etc) -the snake (position, size, shape, direction, etc) -the orbs (position..) -the game state( turn counter, maybe score, level...)
These are all properties of the model. Being a snapshot of the current state of things. At a very basic level it just needs getters and setters for all these things because it doesn’t do much processing at all.
What else do we need: -a way to advance the state ( timer probably ) -collision detection -input parsing
These are part of the model. Every “turn” the timer ticks and the snake moves one square in its current direction. Then the controller asks the model of the snake and an orb are in he same spot and what to do. (Or a boundary of some variety) update size/score/end game/etc...
Lastly we need: -input handling -game state updating
These would be methods of the view. It says “hey this guy/gal clicked a button. Controller do something”. Alternatively whenever the controller says “hey the turn changed” the view reads the model and outputs whatever the model says is the correct game state.