Turned based game design/architecture

Hi,

I’m currently making a turned base game (risk style game) where there are 2-4 players (single/multi + computers).

I’ve been thinking about how to design the game so that:
a) Only one player makes his moves at a time
b) Screen only updated when something has happened, there is no animation going on (that is, it’s not as a traditional game loop where it’s updated as fast a possible)
c) When player is making his moves I want to display messages on the screen like “Player A attacks Player B”.
d) I want there to be some user interaction even though it’s not the users turn, like highlighting objects of interest, display help. If the user tries to do something when it’s not his turn, he gets a message “wait for your turn”.

I was thinking about having the “Screen”, that is the graphics that are displayed, running as a seperate thread, and then the “Logic”, doing all the players moves, running as a seperate thread. Logic would tell Screen to refresh itself when something has happened. But this only covers the logic in the game, not when the user has interaction with objects of interest (such as highlighting). So I’ve begun to wonder if there aren’t some standard ways of doing this?

Thanks!

why can’t you do the highlighting with the method you describe? just let the thread which determines if something should be highlighted tell the screen to refresh ???

Maybe I’m asking for references to “Turn Based Game Architecture & Design” documents, sites. I need to learn how to make games change by events, not by time (delta). And I need to learn how turn based games are implemented, I haven’t thought much of them since I became interested in game programming.

Although I could probably get my game to work (some of it’s functionality works already), I just want to do it properly and in the “recommended” manner.

This is all (believe it or not) a very good Software Engineering exercise. I’ve spent a few minutes wondering how I could explain it to you but it’s hard without giving you every step.

One thing I would say is that if you make your game event based and manage it with listeners (MVC), and do the right design of how it should be and worry about implementation later then you may have an easier task.

You could probably knock out the necessary architecture in a few hours that (a) would not need to be changed and (b) is sufficient to cater for not only network play, but a text console and a GUI. Take it in iterations:

  • Step 1: develop a text console version of the game, I can lend you a nice little text console using a TextField
  • Step 2: develop the game so that you are still inputting commands using the console but it is displaying a gui output
  • Step 3: add gui input

Start thinking about how you will be loading in data too. You don’t want your data tied into your code, use input files whenever you can!

[quote=“appel,post:1,topic:30385”]
Why not? It’s the best way… How can you highlight things under the pointer without a framerate?

-1 on this :wink:
one day, you’ll want animations even in your turn-based game. And then you’ll curse yourself for not having a “classic” game loop.
As Keldon said above, the model-view-controller pattern should be able to help you out, here.

the model is your actual game. It has methods to assign players, change turns, make moves, create units, etc… This object will apply game logic and raise an error when someone tries to do something out of their turn. It also has a lot of getter methods to figure out the current state.

the view is your standard 60 fps game loop. it keeps track of a camera, and asks the model where all the units are, and which units are active (need highlighting), etc.

the controller is the object that will call methods on the model to create players, start the game, etc. It does this based on input. Like Keldon suggested, you can start with a really simple console, and later move your code to the game loop.

The reason I wanted to make my game re-render itself only when required to do so is:
To reduce CPU overhead, no need to render a static screen at 60fps. I often hear complaints from laptop users that their fans went into overdrive because of small games.

I mean, how would you feel like if Minesweeper in Windows caused 100% CPU load?

I can live without some nice highlight stuff.

I see your point. Reducing the maximum frame rate may be enough to make the problem disappear, though. After all, you need barely enough fps to make your game somewhat responsive. I played a quick action game on my portable (the things I don’t do for java-gaming.org! ::slight_smile: ) and my fan never kicked in.

Do you plan to use AWT/Swing, or any of the more gaming-oriented libs like JME/LWGL/JOGL?

In case of AWT/Swing, I guess the main question becomes: in the MVC pattern, how does the viewer object become aware that the screen needs an update because the model has changed?

  • polling? You’re back to 100% cpu usage if you’re not careful.
  • a callback? If your model would define an event “GameUpdateChangedEvent”, and allowed for GameUpdateListeners to be registered, I think you’re getting closer.

Yes, just AWT/Swing, and just Java2D.

I have KeyListener and MouseListener to make the game react to local user inputs, so whenever he clicks on the screen with his mouse I can check if that x,y actually hit a country, and if it did, then I can highlight that country, thus refreshing the screen. If he did not hit anything, I could still refresh the screen with a message “You must select a country!”, or simply do nothing.

And then I have the computer AI doing it’s move (probably skipping multiplayer in 1st version to get things going).

So, how I make the screen aware is not what I would do. I would simply tell the screen to update whenever my logic says, or user inputs says, I need to update it.

Not sure if reducing the framerate is really the solution. Even at whatever framerate I would still have to “animate” things that are happening. Let me explain: When it’s the Computers move, it can finish it’s logic in 1ms (or 1 game logic update loop), and in that time it could have invaded 4 countries, moved armies around etc, and the user watching the game just missed that, all he saw was the screen before all these changes and then the results of the computers move. I want the user to witness the computers move, although they would playback relatively faster than normal, the player would still see and realize what the computer was doing, just like the user was playing against another human opponent. I don’t want the user to think “Wow, I lost half of my countries, what the heck happened?”

So, in that sense, it would probably be more difficult to simulate this in a 60fps-game-loop. While in a static screen, and it’s the computers turn, I could simply refresh the screen when the computer made some action, let the thread sleep for 100ms, and then off to the next action.

I’m going to focus on this part :wink:
If your “game” class is listening to keys and mouse events directly, it sounds like you’re not following the MVC pattern. I’m not sure if you’re familiar with the pattern?

Yes :slight_smile: But mostly with web-MVC which is different than gui-MVC. Haven’t done it for a while, I’ll read some article to refresh my memory. Let you know if that’s the solution :slight_smile:

I’m doing some similar stuff - it would be interesting to hear how your design ended up?

Scrapped the project, so I ended up doing nothing but fancy graphics for the game :-\

I figured that the risk game was too different, e.g. event based MVC, from what I consider “normal” games. I’ll probably try it again, but with something much simpler than what I originally had in mind.

then were in the same position, i also got carried away with something risk-like and it definietly looks like Im trying to swollow a little bit to much at once.

I hink I’ll go back to something more “monopoly” like - moving a piece and buying a house … or something… But this time I’ll write me a model and will try to really stick to it, and hopefully get a good design from the beginning. I don’t really get it with passing my actionevents around in a good way

have a look at http://filthyrichclients.org/

and note how animations can play nice with the edt.

Bought!! :wink: