Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
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 3086 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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
// 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():

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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!