I'm working on a strategy game. It's turn-based and card-based (think Dominion-style), done in a client, with eventual AI in the works. I've already implemented almost all of the game logic (methods for calculations and suchlike) and I'm starting to work on the actual game loop.

What is the "best" way to implement a game loop in such a game? Should I use a simple "while gameActive" loop that keeps running until gameActive is False, with sections that wait for player input? Or should it be managed through the UI with player actions determining what happens and when?

Any help is appreciated. I'm doing it in Python (for now at least) to get my Python skills up a bit, although the language shouldn't matter for this question.


2 Answers 2


Ok, if you don't care about the graphics, that's simpler. From a high-level pseudocode, what I suggest is like how any other game loop works:

while game has not ended yet:
   keep going

// we're out of the while loop; game has ended
show results, etc.
go back to main menu

Thing is, yours is turn-based, so we can do away with the loop part, or keep it and use coroutines. Let's keep it simple for now though. (Not sure if standard Python has coroutines built-in)

So instead of a loop, we'll basically use callback functions.

The entry point for starting our game would be like this (this is still pseudocode):

// call this at the start of the game
function OnStartTurn()

// call this function when the human player clicks on "End Turn" button, or if the AI decides it's finished.
function OnCurrentPlayerEndTurn()
   change currentPlayer variable to the next player in line
   OnStartTurn(); // will cause the next player to begin his turn

So from here, you can gather that we at least need a currentPlayer variable, which means we need some sort of Player class.

Also since we need to let the game pass control to the next player in line, we need to store all Player objects in a list (ordered in the way we want to). And on the OnCurrentPlayerEndTurn function, we simply cycle through that list so that the next player now begins his turn.

In OnStartTurn() we have a currentPlayer.OnBeginTurn(). You will want that for human players, this function will activate the GUI so the user can now act. For AI players you will want this function to start the AI's thinking process instead.

If you don't know how to do that, you have to learn polymorphism and object-oriented programming concepts.

Now for the human player, you'd provide some "End turn" button, which when clicked will call that OnCurrentPlayerEndTurn() function. For the AI, you'll want that immediately after the AI is done thinking and acting, it will likewise call OnCurrentPlayerEndTurn().

The idea is, either way, OnCurrentPlayerEndTurn() needs to get called in the end, so that the next player who needs to move gets his turn.

Unless a player won, in which case you don't need to bother calling that. Just go ahead and call your function to show results, etc.


The gamActive loop you discribed is the standard for making games, as far as your specific question goes you can do this a number of ways. I would recommend having a class that contains all the possible states of play, and in each of the player objects hold the status of each player. During your game loop you can skip through the active players putting them in a wait status, or a play status as you itterate through the players.

for(int activePlayer; activePlayer < playerArray.size(); activePlayer++) 
if (playerArray.get(activePlayer).activeTurn) 
    // Run code for this players turn
    // Give activeTurn to next player

This would work for a basic game but might not be efficient enough for a high level graphics based game. What language are you using to write your game in?

  • \$\begingroup\$ As I've said in the question, I'm writing it in Python. I'm planning on keeping it quite simple, it could even be text-based if I so desired. Nothing graphically intensive is planned for this particular project. \$\endgroup\$
    – Djentleman
    Oct 27, 2012 at 5:40
  • \$\begingroup\$ Did not see the mention of the language when I originally read it, thank you for the clarification. \$\endgroup\$
    – user22020
    Oct 29, 2012 at 4:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .