Adjusted Random Name generator to include wanted username and switched to numbers instead of character for readability
This commit is contained in:
parent
4a5bc49aa7
commit
6654280990
@ -31,7 +31,7 @@ public class ClientHandler implements Runnable {
|
||||
this.clientUserName = in.readLine();
|
||||
// duplicate handling: if username already taken, assign random name to client
|
||||
if (AllClientNames.allNames("").contains(clientUserName)) {
|
||||
clientUserName = NameGenerator.randomName();
|
||||
clientUserName = NameGenerator.randomName(clientUserName);
|
||||
}
|
||||
// add username to list of all client names for future duplicate checking
|
||||
AllClientNames.allNames(clientUserName);
|
||||
|
||||
@ -5,16 +5,21 @@ import java.util.Random;
|
||||
// Creates a String beginning with "player_" followed by 4 random letters
|
||||
|
||||
public class NameGenerator {
|
||||
static String randomName() {
|
||||
StringBuilder name = new StringBuilder();
|
||||
Random r = new Random();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
char c = (char)(r.nextInt(26) + 'a');
|
||||
name.append(c);
|
||||
static String randomName(String username) {
|
||||
StringBuilder name;
|
||||
while (true) {
|
||||
|
||||
name = new StringBuilder();
|
||||
Random r = new Random();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int c = r.nextInt(10);
|
||||
name.append(c);
|
||||
}
|
||||
if (!AllClientNames.allNames("").contains(username + name)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return "player_" + name;
|
||||
}
|
||||
public static void main (String[] args) {
|
||||
System.out.println(randomName());
|
||||
return username + name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user