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();
|
this.clientUserName = in.readLine();
|
||||||
// duplicate handling: if username already taken, assign random name to client
|
// duplicate handling: if username already taken, assign random name to client
|
||||||
if (AllClientNames.allNames("").contains(clientUserName)) {
|
if (AllClientNames.allNames("").contains(clientUserName)) {
|
||||||
clientUserName = NameGenerator.randomName();
|
clientUserName = NameGenerator.randomName(clientUserName);
|
||||||
}
|
}
|
||||||
// add username to list of all client names for future duplicate checking
|
// add username to list of all client names for future duplicate checking
|
||||||
AllClientNames.allNames(clientUserName);
|
AllClientNames.allNames(clientUserName);
|
||||||
|
|||||||
@ -5,16 +5,21 @@ import java.util.Random;
|
|||||||
// Creates a String beginning with "player_" followed by 4 random letters
|
// Creates a String beginning with "player_" followed by 4 random letters
|
||||||
|
|
||||||
public class NameGenerator {
|
public class NameGenerator {
|
||||||
static String randomName() {
|
static String randomName(String username) {
|
||||||
StringBuilder name = new StringBuilder();
|
StringBuilder name;
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
name = new StringBuilder();
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
char c = (char)(r.nextInt(26) + 'a');
|
int c = r.nextInt(10);
|
||||||
name.append(c);
|
name.append(c);
|
||||||
}
|
}
|
||||||
return "player_" + name;
|
if (!AllClientNames.allNames("").contains(username + name)) {
|
||||||
}
|
break;
|
||||||
public static void main (String[] args) {
|
|
||||||
System.out.println(randomName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return username + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user