Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Let's imagine game like Heroes of Might and Magic, or Master of Orion, or your turn-based game of choice. What is the game logic behind making next turn? Are there any materials or books to read about the topic? To be specific, let's imagine game loop:

void eventsHandler(); //something that responds to input
void gameLogic(); //something that decides whats going to be output on the screen
void render(); //this function outputs stuff on screen

All those are getting called say 60 times a second. But how turn-based enters here? I might imagine that in gameLogic() there is a function like endTurn() that happens when a player clicks that button, but how do I handle it all? Need insights.

share|improve this question

2 Answers 2

up vote 19 down vote accepted

A turn based game is going to be governed by a state machine. Basically, you would lay out a series of states that can occur in a logical order.

At a high level, a player's turn could be the start of a new state, followed by all the possible actions that are allowed during that turn.

For instance

  • State - change player
    • it is now player 1's turn
  • Actions allowed
    • attack
      • select enemy to attack
    • defend
      • select unit to defend
    • move unit
      • select unit to move
      • check to ensure movement is allowed
    • etc

Obviously this will balloon quite quickly, as I've only sketched out an extremely limited plan. Having a good grasp on possible states early on will mean that you should be in a good position to implement. I'd highly stress sketching out exactly how you want the game to run....a good turn-based game requires a lot of planning IMO.

share|improve this answer
    
A fairly old school project of mine that was a 3month development attempt at Final Fantasy Tactics :) Uses a state machine (a few of em actually) as described above. sourceforge.net/projects/bpfat –  James Feb 8 '11 at 1:46

I have recently built a turn based strategy game similar to Master of Orion (it's only single player right now, multi-player would be a bit more complex, but a similar idea), here is what my endTurn function logic looks like (in my main Game Controller class):

//for all computer players:
computerTakeTurn(player)

//for all players
moveShips(player)

//for all players
endTurn(player)//see this function below

//for all planets
planet.repairFleet()

//for all players
resolvePlanetaryConflicts(player)

//check to see if any players were destroyed or if the game is over
checkPlayerDestroyedAndGameOver()

My player end turn function will perform functions specific to that player that isn't affected by the order it takes place between players:

eatAndStarve()

generatePlayerResources()

buildPlayerPlanetImprovements()

growPlayerPlanetPopulation()

Where appropriate, these functions return status messages and/or trigger events so that the GUI can show what happened during the turn or let the player know a computer was destroyed, etc...

share|improve this answer
    
could you please provide source code just to check out? Would be great help. Just for educational purposes. –  Dvole Feb 8 '11 at 18:59
    
@Dvole I'm currently working on converting the code from Silverlight/C# to JavaScript using the HTML5 canvas tag. Once I finish I plan on open sourcing the code and at that time you are more than welcome to see exactly how I've implemented this function. If it helps to get context you can play my Space Strategy game here: Astriarch - Ruler of the Stars –  Matt Palmerlee Feb 8 '11 at 21:45
1  
@Dvole It's taken me quite some time, but I just open sourced the Silverlight version of Astriarch. If you are still interested you can download the source code from github here: github.com/mpalmerlee/Astriarch/tree/Silverlight –  Matt Palmerlee Nov 14 '12 at 15:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.