How to remove a User from a rotation of inputs?









up vote
0
down vote

favorite












I'm trying to make a multiplayer Random Number Game on java.



I have already successfully built a single player version of this game but I'm struggling to figure out how I would skip over a user that guessed correctly in a Multiplayer setting.



The idea is that each user will take one turn at guessing their respective number stored in the array respective to their order.



If all the users are wrong it loops back again and asks them.



If a user is correct, when the "round" restarts it skips over the user that guessed correctly and continues to asks the remaining users.



Basically the scope of which I'm requesting you look is in the for loop followed by a SysOut saying "Ok " + name[i] + " Please guess a number." I hope my code is readable and any/all advice would be appreciated. Thank you!



while(run == true)
{
System.out.println("Welcome to the Random Number Game!");
System.out.print("How many players are playing?: ");

while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();


int numPlayers = sc.nextInt();

String name = new String[numPlayers];
int randomTarget = new int[numPlayers];
int minNum = new int[numPlayers];
int maxNum = new int[numPlayers];

System.out.println("What are their names?");

for(int i = 0; i<numPlayers; i++)

name[i] = sc.next();


for(int i = 0; i<numPlayers; i++)

System.out.print("Ok, " + name[i] + " Please select a minimum number: ");
while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();

int min = sc.nextInt();
minNum[i] = min;

System.out.print("Ok, " + name[i] + " Please select a maximum number: ");
while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();

int max = sc.nextInt();
maxNum[i] = max;

randomTarget[i] = (int) ranNum(min,max);


System.out.println("OK! I've picked a number for you.");

for(int i = 0; i<numPlayers; i++)

System.out.print("Ok, " + name[i] + " Please guess the number for me: ");

while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();


int guess = sc.nextInt();
int x=0;
int counter = 0;
int size = maxNum[i] - minNum[i] + 1;
int record = new int[size];

if(guess != randomTarget[i])











share|improve this question























  • Assign each user a unique name and then use set to keep the users who have guessed it correctly. Next time looping, check the set and if it contains that user then skip it! Doesn't it work???
    – Ketan
    Nov 11 at 23:26






  • 1




    One solution might be to use a List to maintain all the "active" users. So when a user makes a correct guess, you can simply remove them from the List
    – MadProgrammer
    Nov 11 at 23:37














up vote
0
down vote

favorite












I'm trying to make a multiplayer Random Number Game on java.



I have already successfully built a single player version of this game but I'm struggling to figure out how I would skip over a user that guessed correctly in a Multiplayer setting.



The idea is that each user will take one turn at guessing their respective number stored in the array respective to their order.



If all the users are wrong it loops back again and asks them.



If a user is correct, when the "round" restarts it skips over the user that guessed correctly and continues to asks the remaining users.



Basically the scope of which I'm requesting you look is in the for loop followed by a SysOut saying "Ok " + name[i] + " Please guess a number." I hope my code is readable and any/all advice would be appreciated. Thank you!



while(run == true)
{
System.out.println("Welcome to the Random Number Game!");
System.out.print("How many players are playing?: ");

while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();


int numPlayers = sc.nextInt();

String name = new String[numPlayers];
int randomTarget = new int[numPlayers];
int minNum = new int[numPlayers];
int maxNum = new int[numPlayers];

System.out.println("What are their names?");

for(int i = 0; i<numPlayers; i++)

name[i] = sc.next();


for(int i = 0; i<numPlayers; i++)

System.out.print("Ok, " + name[i] + " Please select a minimum number: ");
while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();

int min = sc.nextInt();
minNum[i] = min;

System.out.print("Ok, " + name[i] + " Please select a maximum number: ");
while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();

int max = sc.nextInt();
maxNum[i] = max;

randomTarget[i] = (int) ranNum(min,max);


System.out.println("OK! I've picked a number for you.");

for(int i = 0; i<numPlayers; i++)

System.out.print("Ok, " + name[i] + " Please guess the number for me: ");

while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();


int guess = sc.nextInt();
int x=0;
int counter = 0;
int size = maxNum[i] - minNum[i] + 1;
int record = new int[size];

if(guess != randomTarget[i])











share|improve this question























  • Assign each user a unique name and then use set to keep the users who have guessed it correctly. Next time looping, check the set and if it contains that user then skip it! Doesn't it work???
    – Ketan
    Nov 11 at 23:26






  • 1




    One solution might be to use a List to maintain all the "active" users. So when a user makes a correct guess, you can simply remove them from the List
    – MadProgrammer
    Nov 11 at 23:37












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to make a multiplayer Random Number Game on java.



I have already successfully built a single player version of this game but I'm struggling to figure out how I would skip over a user that guessed correctly in a Multiplayer setting.



The idea is that each user will take one turn at guessing their respective number stored in the array respective to their order.



If all the users are wrong it loops back again and asks them.



If a user is correct, when the "round" restarts it skips over the user that guessed correctly and continues to asks the remaining users.



Basically the scope of which I'm requesting you look is in the for loop followed by a SysOut saying "Ok " + name[i] + " Please guess a number." I hope my code is readable and any/all advice would be appreciated. Thank you!



while(run == true)
{
System.out.println("Welcome to the Random Number Game!");
System.out.print("How many players are playing?: ");

while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();


int numPlayers = sc.nextInt();

String name = new String[numPlayers];
int randomTarget = new int[numPlayers];
int minNum = new int[numPlayers];
int maxNum = new int[numPlayers];

System.out.println("What are their names?");

for(int i = 0; i<numPlayers; i++)

name[i] = sc.next();


for(int i = 0; i<numPlayers; i++)

System.out.print("Ok, " + name[i] + " Please select a minimum number: ");
while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();

int min = sc.nextInt();
minNum[i] = min;

System.out.print("Ok, " + name[i] + " Please select a maximum number: ");
while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();

int max = sc.nextInt();
maxNum[i] = max;

randomTarget[i] = (int) ranNum(min,max);


System.out.println("OK! I've picked a number for you.");

for(int i = 0; i<numPlayers; i++)

System.out.print("Ok, " + name[i] + " Please guess the number for me: ");

while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();


int guess = sc.nextInt();
int x=0;
int counter = 0;
int size = maxNum[i] - minNum[i] + 1;
int record = new int[size];

if(guess != randomTarget[i])











share|improve this question















I'm trying to make a multiplayer Random Number Game on java.



I have already successfully built a single player version of this game but I'm struggling to figure out how I would skip over a user that guessed correctly in a Multiplayer setting.



The idea is that each user will take one turn at guessing their respective number stored in the array respective to their order.



If all the users are wrong it loops back again and asks them.



If a user is correct, when the "round" restarts it skips over the user that guessed correctly and continues to asks the remaining users.



Basically the scope of which I'm requesting you look is in the for loop followed by a SysOut saying "Ok " + name[i] + " Please guess a number." I hope my code is readable and any/all advice would be appreciated. Thank you!



while(run == true)
{
System.out.println("Welcome to the Random Number Game!");
System.out.print("How many players are playing?: ");

while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();


int numPlayers = sc.nextInt();

String name = new String[numPlayers];
int randomTarget = new int[numPlayers];
int minNum = new int[numPlayers];
int maxNum = new int[numPlayers];

System.out.println("What are their names?");

for(int i = 0; i<numPlayers; i++)

name[i] = sc.next();


for(int i = 0; i<numPlayers; i++)

System.out.print("Ok, " + name[i] + " Please select a minimum number: ");
while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();

int min = sc.nextInt();
minNum[i] = min;

System.out.print("Ok, " + name[i] + " Please select a maximum number: ");
while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();

int max = sc.nextInt();
maxNum[i] = max;

randomTarget[i] = (int) ranNum(min,max);


System.out.println("OK! I've picked a number for you.");

for(int i = 0; i<numPlayers; i++)

System.out.print("Ok, " + name[i] + " Please guess the number for me: ");

while(!sc.hasNextInt())

System.out.print("Incorrect format, please try again! ");
sc.next();


int guess = sc.nextInt();
int x=0;
int counter = 0;
int size = maxNum[i] - minNum[i] + 1;
int record = new int[size];

if(guess != randomTarget[i])








java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 23:44









lucascaro

3,38611530




3,38611530










asked Nov 11 at 23:21









Skizzers

1




1











  • Assign each user a unique name and then use set to keep the users who have guessed it correctly. Next time looping, check the set and if it contains that user then skip it! Doesn't it work???
    – Ketan
    Nov 11 at 23:26






  • 1




    One solution might be to use a List to maintain all the "active" users. So when a user makes a correct guess, you can simply remove them from the List
    – MadProgrammer
    Nov 11 at 23:37
















  • Assign each user a unique name and then use set to keep the users who have guessed it correctly. Next time looping, check the set and if it contains that user then skip it! Doesn't it work???
    – Ketan
    Nov 11 at 23:26






  • 1




    One solution might be to use a List to maintain all the "active" users. So when a user makes a correct guess, you can simply remove them from the List
    – MadProgrammer
    Nov 11 at 23:37















Assign each user a unique name and then use set to keep the users who have guessed it correctly. Next time looping, check the set and if it contains that user then skip it! Doesn't it work???
– Ketan
Nov 11 at 23:26




Assign each user a unique name and then use set to keep the users who have guessed it correctly. Next time looping, check the set and if it contains that user then skip it! Doesn't it work???
– Ketan
Nov 11 at 23:26




1




1




One solution might be to use a List to maintain all the "active" users. So when a user makes a correct guess, you can simply remove them from the List
– MadProgrammer
Nov 11 at 23:37




One solution might be to use a List to maintain all the "active" users. So when a user makes a correct guess, you can simply remove them from the List
– MadProgrammer
Nov 11 at 23:37












2 Answers
2






active

oldest

votes

















up vote
0
down vote













One way you could go about doing this is having an additional boolean array that is parallel to the "name" array. Start by creating a boolean array that is the same size as the "name" array:



boolean correctGuesses = new boolean[name.size];


If a player guesses correctly, the same index of the boolean array will be flipped to true. Before asking a player to guess, first check the corresponding index in the boolean array.



Or, consider an object-oriented approach. You could create a player class like this:



class Player 
private String name;
private boolean guessedCorrectly;

Player(String name)
this.name = name;
guessedCorrectly = false;


public String getName()
return name;


public void setName(String name)
this.name = name;


public boolean hasGuessedCorrectly()
return guessedCorrectly;


public boolean setGuessedCorrectly(boolean guessedCorrectly)
this.guessedCorrectly = guessedCorrectly;







share|improve this answer






















  • Thanks so much! I didn't think to use either option. Appreciate the help!
    – Skizzers
    Nov 11 at 23:49

















up vote
0
down vote













For me the best to make what tou want is to use a class like failedProgrammer said.
By this way, you can first add other attributs to your player like : their scores, their names. After this you only have to use a "if" boucle to ask to each player to play or not.






share|improve this answer




















  • I agree I'm going to try that now. Thank you!
    – Skizzers
    Nov 11 at 23:49










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254241%2fhow-to-remove-a-user-from-a-rotation-of-inputs%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













One way you could go about doing this is having an additional boolean array that is parallel to the "name" array. Start by creating a boolean array that is the same size as the "name" array:



boolean correctGuesses = new boolean[name.size];


If a player guesses correctly, the same index of the boolean array will be flipped to true. Before asking a player to guess, first check the corresponding index in the boolean array.



Or, consider an object-oriented approach. You could create a player class like this:



class Player 
private String name;
private boolean guessedCorrectly;

Player(String name)
this.name = name;
guessedCorrectly = false;


public String getName()
return name;


public void setName(String name)
this.name = name;


public boolean hasGuessedCorrectly()
return guessedCorrectly;


public boolean setGuessedCorrectly(boolean guessedCorrectly)
this.guessedCorrectly = guessedCorrectly;







share|improve this answer






















  • Thanks so much! I didn't think to use either option. Appreciate the help!
    – Skizzers
    Nov 11 at 23:49














up vote
0
down vote













One way you could go about doing this is having an additional boolean array that is parallel to the "name" array. Start by creating a boolean array that is the same size as the "name" array:



boolean correctGuesses = new boolean[name.size];


If a player guesses correctly, the same index of the boolean array will be flipped to true. Before asking a player to guess, first check the corresponding index in the boolean array.



Or, consider an object-oriented approach. You could create a player class like this:



class Player 
private String name;
private boolean guessedCorrectly;

Player(String name)
this.name = name;
guessedCorrectly = false;


public String getName()
return name;


public void setName(String name)
this.name = name;


public boolean hasGuessedCorrectly()
return guessedCorrectly;


public boolean setGuessedCorrectly(boolean guessedCorrectly)
this.guessedCorrectly = guessedCorrectly;







share|improve this answer






















  • Thanks so much! I didn't think to use either option. Appreciate the help!
    – Skizzers
    Nov 11 at 23:49












up vote
0
down vote










up vote
0
down vote









One way you could go about doing this is having an additional boolean array that is parallel to the "name" array. Start by creating a boolean array that is the same size as the "name" array:



boolean correctGuesses = new boolean[name.size];


If a player guesses correctly, the same index of the boolean array will be flipped to true. Before asking a player to guess, first check the corresponding index in the boolean array.



Or, consider an object-oriented approach. You could create a player class like this:



class Player 
private String name;
private boolean guessedCorrectly;

Player(String name)
this.name = name;
guessedCorrectly = false;


public String getName()
return name;


public void setName(String name)
this.name = name;


public boolean hasGuessedCorrectly()
return guessedCorrectly;


public boolean setGuessedCorrectly(boolean guessedCorrectly)
this.guessedCorrectly = guessedCorrectly;







share|improve this answer














One way you could go about doing this is having an additional boolean array that is parallel to the "name" array. Start by creating a boolean array that is the same size as the "name" array:



boolean correctGuesses = new boolean[name.size];


If a player guesses correctly, the same index of the boolean array will be flipped to true. Before asking a player to guess, first check the corresponding index in the boolean array.



Or, consider an object-oriented approach. You could create a player class like this:



class Player 
private String name;
private boolean guessedCorrectly;

Player(String name)
this.name = name;
guessedCorrectly = false;


public String getName()
return name;


public void setName(String name)
this.name = name;


public boolean hasGuessedCorrectly()
return guessedCorrectly;


public boolean setGuessedCorrectly(boolean guessedCorrectly)
this.guessedCorrectly = guessedCorrectly;








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 23:36

























answered Nov 11 at 23:28









4dc0

42539




42539











  • Thanks so much! I didn't think to use either option. Appreciate the help!
    – Skizzers
    Nov 11 at 23:49
















  • Thanks so much! I didn't think to use either option. Appreciate the help!
    – Skizzers
    Nov 11 at 23:49















Thanks so much! I didn't think to use either option. Appreciate the help!
– Skizzers
Nov 11 at 23:49




Thanks so much! I didn't think to use either option. Appreciate the help!
– Skizzers
Nov 11 at 23:49












up vote
0
down vote













For me the best to make what tou want is to use a class like failedProgrammer said.
By this way, you can first add other attributs to your player like : their scores, their names. After this you only have to use a "if" boucle to ask to each player to play or not.






share|improve this answer




















  • I agree I'm going to try that now. Thank you!
    – Skizzers
    Nov 11 at 23:49














up vote
0
down vote













For me the best to make what tou want is to use a class like failedProgrammer said.
By this way, you can first add other attributs to your player like : their scores, their names. After this you only have to use a "if" boucle to ask to each player to play or not.






share|improve this answer




















  • I agree I'm going to try that now. Thank you!
    – Skizzers
    Nov 11 at 23:49












up vote
0
down vote










up vote
0
down vote









For me the best to make what tou want is to use a class like failedProgrammer said.
By this way, you can first add other attributs to your player like : their scores, their names. After this you only have to use a "if" boucle to ask to each player to play or not.






share|improve this answer












For me the best to make what tou want is to use a class like failedProgrammer said.
By this way, you can first add other attributs to your player like : their scores, their names. After this you only have to use a "if" boucle to ask to each player to play or not.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 23:44









Molokay

1




1











  • I agree I'm going to try that now. Thank you!
    – Skizzers
    Nov 11 at 23:49
















  • I agree I'm going to try that now. Thank you!
    – Skizzers
    Nov 11 at 23:49















I agree I'm going to try that now. Thank you!
– Skizzers
Nov 11 at 23:49




I agree I'm going to try that now. Thank you!
– Skizzers
Nov 11 at 23:49

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254241%2fhow-to-remove-a-user-from-a-rotation-of-inputs%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3