Hi !
Featured games (88)
games approved by the League of Dukes
Games in Showcase (681)
Games in Android Showcase (196)
games submitted by our members
Games in WIP (744)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Turn based card game, also using MVC - design questions  (Read 4098 times)
0 Members and 1 Guest are viewing this topic.
Offline DrunkenLizard

Junior Newbie

« Posted 2009-06-18 16:22:17 »

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
   // the ui is the view

   // let the OS do it's thing

... 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?
Offline aazimon
« Reply #1 - Posted 2009-06-18 21:52:38 »

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.  Smiley
Offline newgame

Junior Newbie

« Reply #2 - Posted 2009-08-30 14:25:53 »

As a matter of fact I'm writing a turn based card game also using MVC. You can see the game here: and the source (not the newest version though, but the general structure is the same)

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.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Hydroque (44 views)
2016-06-22 02:17:53

SwampChicken (45 views)
2016-06-20 13:22:57

SwampChicken (44 views)
2016-06-20 13:22:49

SwampChicken (38 views)
2016-06-20 13:22:26

Hydroque (81 views)
2016-06-15 08:22:50

Hydroque (72 views)
2016-06-13 06:40:55

DarkCart (192 views)
2016-05-29 02:30:33

Hydroque (161 views)
2016-05-26 14:45:46

Mac70 (168 views)
2016-05-24 21:16:33

theagentd (151 views)
2016-05-14 18:38:35
Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42

FPS Camera Tutorial
by Hydroque
2016-05-22 05:40:58

Website offering 3D Models specifically for games for free
by vusman
2016-05-18 17:23:09

Website offering 3D Models specifically for games for free
by vusman
2016-05-09 08:50:56

Website offering 3D Models specifically for games for free
by vusman
2016-05-06 11:10:21

Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17

List of Learning Resources
by SilverTiger
2016-02-05 09:39:47 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!