I'm a Java beginner trying to understand OOP and Design Pattern. I'm trying to implement the Model-View-Controller Pattern in Java for a videogame transposition of a board game.
The game have:
- 1 map made by cells
- different kind of player
- some "item" cards
Every turn one player can:
- move by one or two cells (depend of which type of player is)
- use an item card
- attack another player in the same cell in every order but he can move or attack only once and use only one card.
First question: I have some difficulties understanding the separation of functions between Model and Controller.
For example the control for the rule:
Player can move by one cell if it's TYPEA and by two if TYPEB
has to be in the model? (just like a more specific "setter")
class PlayerA {
public move(destination){
if( distance(this.position, destination) <=2)
this.position=destination
else
error
}
or in the controller?
So in the controller I will have something like
class PlayerController {
public move(destination){
currPlayer = Model.getCurrentPlayer()
if(player instanceof PlayerA){
if( distance(currPlayer.position, destination) <=2)
currPlayer.setPosition(destination);
else
error
}
}
if(player instanceof PlayerB){
[...]
}
Wich one is better? and why?
Second question:
How to implement the "Turn Controller"? Can I use the State Pattern?
One "State" interface that force all class to implement the transitions (from Attack State Player can go to UseItem State, but from Move State he cannot go to Attack for example)
At the beginning of the turn i start in "Waiting State" if the Player click on "attack" Game Controller call TurnController.attack(). If it's possible, it change state and re-throw the action, otherwise print an error
class TurnController{
[...]
public void attack(){
currentStatus.attack()
}
------------------------
class WaitingState
public void attack(){
if(canAttack=true)
TurnController.setState(AttackState)
TurnController.currentStatus.attack();
......
else
error
}
Is this Correct?
Last question: (I promise!!! :)
Every Item Card do something special to the gamestate for example move a player in some specific position or remove the player from game. Is it correct implementing this with the Strategy Pattern?
drow card from deck -> strategy = card -> strategy.doAction()
Thank you in advance
Aucun commentaire:
Enregistrer un commentaire