Turn based card game, also using MVC - design questions

Hi all,

I’m having a design issue using the MVC. I’ve written the game so that the “game” loop lives inside the Controller.

The Controller is initialised a with a Model and a View.
The Controller is started (enters the “game” loop)

The game loop looks like this:


// this is the "main" or "game" loop
while (running)
	{
	// the engine is the model
	engine.stepOver();
	// the ui is the view
	ui.update();

	// let the OS do it's thing
	Thread.yield();
	}

… and here is engine.stepOver():


public void stepOver()
	{
	// dont do anything if we aren't playing a game!
	if (currentGame != null && !currentGame.isGameOver())
		{
		// what goes here?
		}

I’m trying to decide on the mechanism for actually polling players for their moves. The implementation I am following is to have a queue of Command objects in the Model, that encapsulate a method call on a Game object using the Command pattern. These Commands would then be executed in the stepOver() method, in the order they were received. When this list is cleared, the method does some more calls on the Game to check for a win condition or to move to the next player, and then finishes.

What I can’t work is how to get the Commands into the queue. At the moment I have a PlayerInterface interface, that is implemented by an AIPlayerInterface and runs in a separate thread, vaguely checking if a move is required of it and queueing one up if so. But I think I might run into concurrency issues and am not sure how to structure that. I’m also not to sure how to implement that interface for a human player, at the moment it is implemented directly by the View (the UserInterface).

Is the thread based idea a good one, or can anyone throw in two cents about a better way of queueing commands by a set of players?

I think I understand what you explaining.
If this is a “turn” based game, then I would think each player would only be able to have input on his turn. If its not the player’s turn, then the user should be able to do anything. Is this loop for each turn? Or is it for updating the graphics?
I’m not sure what you are trying to queue.

Try this idea: Have the loop for updating the graphics. You would have a player object that contains the logic for what cards the player has. A board object for the playing area. (Note: Swing inputs are handled in a separate thread by the JVM) When the user has an input, the player would check if it is his turn. (If not it can queue his action in a List.) If it is his turn, the player object would perform the action. The loop doesn’t need code for handling user input. When the loop goes to update the graphics, it will just get the current state of the players and board draw the graphics appropriately.

I hope that makes sense. :slight_smile:

As a matter of fact I’m writing a turn based card game also using MVC. You can see the game here: storytube.kilu.de/durak10 and the source (not the newest version though, but the general structure is the same) http://storytube.kilu.de/Durak_0.8.tar.gz

My model is called Game. I also have an abstract Player class (I hope in the version of the source too) that the AIPlayer / HumanPlayer inherit (actually there is a class in between, the ControllablePlayer class). The Game class holds the game play logic of the turn based card game. It has references to the players. Also, the round logic is in its own thread.
When the Game class is in the state “Attacking” / “Defending” it evaluates the attack() / defend() method of the AIPlayer / HumanPlayer (but of course they are of the type “Player” for the Game class -> Polymorphism). It is then the task of the player to return the right cards. The round logic thread is “stopped” until the HumanPlayer / AIplayer made its decision. That way you don’t need a separate thread for the aiplayer.
All events that happenend in the model (like a card transer from the deck to a hand of a player) are saved in a queue. The view is responsible to process the events in the queue & for example show appropriate animations.

That’s basically my design and it works, but I think it can be done a little bit differently and better, which is what I’m trying to do now.