How to Create Turn-Based Games

i have programmed java game where only one player kann play and hit the enemy an then i want to make it turn-based game.
I recherched in the internet but i don’t find any help. Hier is a detail to my code (see below).
I used the java.util.Timer class to create Turn-Based Game but it doesn’t work
correctly. I tested it with 2 player. the first player get 1000 ms time but the second only 100 ms.
is my method correct?
Can you tell me wehre can i find help or better concept, tutorials etc to make my game turn-based.
Thanks!

1)GamePanel.java

void init() {

new GameLevel1State();

running = true;
}

public void run() {
init();

	// Timers
	long start;
	long elapsed;
	long wait;

	// Game loop
	while (running) {
		start = System.nanoTime();

		update();
		draw();

		elapsed = System.nanoTime() - start;
		wait = targetTime - elapsed / 1000000;
		if (wait < 0)
			wait = 0;

		try {
			Thread.sleep(wait);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

2)GameLevel1State.java


Timer timer;
public GameLevel1State(…) {
…;
init();
}

public void init() {


timer = new Timer();
timer.schedule(new TurnBasedTask(), 0,1000);

}

class TurnBasedTask extends TimerTask {
public void run() {
setActiveSpieler();
}

}
}

Would be really nice if you put your code into code tags to make it easier to read.

[.code][./code]

without .

About your question, I really don’t think there is a correct or incorrect way to do something. If it works, its a good way.
I wouldn’t really want to tell you how to make such a game, because it wouldn’t be your game anymore, if someone told you how to make it.

I think the best way to answer this would be with an explanation of the idea of how things would work, letting moon1 do the code completely himself.

I would give my two cents to answer the actual question, but I am in no way experienced enough.
Never mind the fact I haven’t even tackled that kind of game myself.

It’s a bit difficult to answer your question with the little information that you provide.
Also I have never used a timer before.

But it seems as you are trying to keep a steady fps in your gameloop, so you could use this to your advantage.
Say if you are running 60 fps you could just have an int that counted up or down every tick, and then use that for timing. (Counting 60 for every second)
I personally would think that as simpler.

I have only made tick-tack-toe as an turn-based game, so I can’t really help much more :slight_smile:

I would do some tick-based architecture :).

The basic idea is that you have entities that implements Tickable. This will allow it to have a tick(float delta) method. Above this, there will be a Ticker. The Ticker will contain all of the Tickables. The Ticker will have a tick(float delta) method. This will loop through all of the Tickables and tick(float delta) them. This method will continue to be run over and over again until all Tickables are finished ticking.

Sorry if that was difficult to understand, but hopefully you get the idea.

-wes

I have made turn based games. The most basic of these is tic-tac-toe.

Tic-tac-toe game

You need states for each player. Then you can set a timer to each state to change the turns. If I were you, I’d build this game and fully understand it before implementing a timer. The problem stated is logic based and is universal to all languages. The best way to solve is probably imitating a state based structure. Good luck.