MVC pattern in a simple game

Started by
2 comments, last by Zahlman 15 years, 4 months ago
Problem: I've been reading articles about the MVC-pattern a lot but whenever I try to implement it in a game, I fail miserably. Solution: I was hoping someone could give an example of the roles of model, view and controller in a simple game, like Tic-Tac-Toe. Here's what I think it is like: - The model holds the data for the game ( like a 3x3 matrix ) - The model holds the logic for the game ( like what to do with a mouse click ) - The controller checks for any input the game should respond to and lets the model know when that happens ( like "Hmm I noticed a mouse click.. Hey model! A mouse click occurred at (x,y)!" ) - The view retrieves the data it needs from the model to present it to the screen ( like turning the 3x3 matrix into a nice Tic-Tac-Toe board ) Questions: - Do I have a good idea of how this pattern works? - If no, could anyone give a correction of the above? - Maybe someone could add an extra example for how it works in some other simple game? Thank you in advance, Arjan
Advertisement
Quote:Original post by Arjan B
- The model holds the logic for the game ( like what to do with a mouse click )
- The controller checks for any input the game should respond to and lets the model know when that happens ( like "Hmm I noticed a mouse click.. Hey model! A mouse click occurred at (x,y)!" )


First, the model doesn't know what a mouse is (after all, a mouse is just one of many means of controlling the model, such as an AI). But even assuming that it knew what a mouse was, it doesn't know where the tic-tac-toe graphics appear on the screen (because that's what the view does), so it couldn't map the mouse position to a tile anyway.

Therefore, the input should be converted to a specific manipulation ("draw a cross in the top left corner") by the controller.
Thanks for your reply!

Sounds good, that way the Model part is really your game itself and Controllers and Views can be like "plugged in and out" right ^^?

My new idea of MVC in Tic-Tac-Toe:

Model
Holds data like a 3x3 board, who's turn it is, the result of a game etc. Also holds logic like "Put a cross/circle at [column], [row]" and "Check if there's a winner".

Controller for player
When a mouseclick occurs  Map click to a tile  Retrieve board from Model  If empty tile    Model: Put a cross at [column], [row]


So it would need a way to check for mouse clicks, some data and a function to map a click to a tile, a way to retrieve the data from the model to check if the mapped tile is empty and finally make it's move.

Controller for AI
Retrieve board from ModelDetermine moveModel: Put a circle at [column], [row]


So it would need a way to retrieve the data from the model, use it to determine it's move and make it.

View
Retrieve board from ModelPresent the board to the screen using nice looking images


So it needs a way to retrieve the data from the model, map the values of the board to their corresponding images and draw those on the screen.


Is this a better idea ^^?
Quote:Original post by Arjan B
Model
Holds data like a 3x3 board, who's turn it is, the result of a game etc. Also holds logic like "Put a cross/circle at [column], [row]" and "Check if there's a winner".

Controller for player
When a mouseclick occurs  Map click to a tile  Retrieve board from Model  If empty tile    Model: Put a cross at [column], [row]



Nonononono. The controller handles the logic of transforming the player's actual input into some kind of request. The model is in charge of figuring out whether that request is valid.

It'd be something more like:

ModelTo handle input at (column, row):  If the tile is occupied: return # not a valid move.  Figure out whose turn it is, and update the board accordingly.  Switch to the other player's turn.  If this is game is versus the computer:    Decide on AI's move and make it.ControllerWhen a mouseclick occurs:  Map click to a tile.  Model: handle input at (column, row).ViewTo draw the board:  Get board data from Model.  Model: Has game ended? If so:    Indicate the winner (if any) or draw condition (otherwise).


This topic is closed to new replies.

Advertisement