Ja-oon Solo
Ja-oon Solo

Reputation: 93

Nested for loop inside do while to keep player's turns in order

This is a beginning attempt to create a turn counter for a simple turn-based game. Although it's currently an infinite loop, I'm curious if there's a better or more elegant way to do what I want to do - keep players turns in order throughout a complete game.

public class Main {
    public static void main(String args[]){

        int i;     // player turn counter
        int j = 6; // number of players created + 1

        do {
            for (i=1; i < j; i++){
                System.out.println("Player " + i);  // just for visualization
            }
        }while (true);
    }
}

I'm extremely new with java. I have found that basic for loops need +1 to avoid counting -1 lower.

My while condition will be a check for a winner, say:

while(winner == false) 

or

while(!winner)

Upvotes: 1

Views: 668

Answers (2)

Yashovardhan99
Yashovardhan99

Reputation: 938

Your method seems to be the best case scenario right now. Other methods involve using arrays which will just complicate things here. Though to save the nested loop, you can just add the condition of !winner to the for loop

for (i=1; i < j && winner == false; i++)
    {
        System.out.println("Player " + i);
        if(i==j)
            i=1;
    }

this will work as good and will save the other loop.

Upvotes: 1

Aimee Borda
Aimee Borda

Reputation: 842

That is completely fine, just a few observations while(winner == false) as a single = means assignment and == means comparison.

Upvotes: 0

Related Questions