Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:25:31+00:00 2026-06-14T18:25:31+00:00

I am writing a method to switch the player number after each player’s turn.

  • 0

I am writing a method to switch the player number after each player’s turn. I am using a boolean array that keeps track of the players still playing (that have not been eliminated). So the array at the beginning of the game is initialized to true and is the size of the number of players in the game. As players get eliminated, the corresponding value of the index gets set to false (For example if Player 2 gets eliminated, the third index of the array gets set to false). (Note: Player 0 does exist, so if two players are playing they are Players 0 and Player 1.) If the player number is equal to the last player, then it needs to start back at the beginning and find the first player that is still in the game. Otherwise, the player number increments to the first player that is still playing. Here is what I have:

public static int switchPlayer(int currentPlayer, boolean[] playerList) {
    if(currentPlayer == playerList.length) {
        for(int i = 0; i < playerList.length; i++) {
            if(playerList[i] == true) {
                currentPlayer = i;
                break;
            }
        }
    }
    else {
        for(int i = (currentPlayer+1); i < playerList.length; i++) {
            if(playerList[i] == true) {
                currentPlayer = i;
                break;
            }
        }
    }
    return currentPlayer;
}

Any changes or suggestions? It is not quite working and cannot see where it is going wrong.

I have tried to implement one of the answers, but I cannot figure out how to get it implemented. Does anyone have a solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:25:32+00:00Added an answer on June 14, 2026 at 6:25 pm

    If you have Players 0,1,2,3. Then length is 4.
    But the argument currentPlayer can only have 0-3 value, since thats the player numbers, so try changing this:

    if(currentPlayer == playerList.length) {
        for(int i = 0; i < playerList.length; i++) {
            if(playerList[i] == true) {
                currentPlayer = i;
                break;
            }
        }
    }
    

    to:

    if(currentPlayer + 1 == playerList.length) {
        for(int i = 0; i < playerList.length; i++) {
            if(playerList[i] == true) {
                currentPlayer = i;
                break;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an interpreter that parses an array of String and assigns each
I'm writing a method that will resize the columns in a datagridview so that
I'm using StreamWriter to write log file. And this is the writing log method,
I just create a app for writing the text on Screen using TouchEvent method.
I'm writing a 100% JavaScript webapp that manages a catalog of products. I'm using
I have a base method that I'm writing in order to not repeat the
I am writing a music player that uses a custom Adapter extending BaseAdapter (efficiency
Writing a vocabulary trainer for Arabic, my program shall automatically switch the input method
I’m writing an application that prints addresses directly onto envelopes. I’m using QTextDocument and
I have a doubt about orkut's new URL Writing method. Earlier the home page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.