-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomuser.java
More file actions
49 lines (43 loc) · 1.67 KB
/
Randomuser.java
File metadata and controls
49 lines (43 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.io.IOException;
import java.io.InvalidClassException;
import java.util.*;
import java.io.Serializable;
public class Randomuser implements Serializable{
private static final long serialVersionUID = 1L;
public static User randomchooser(User username,ArrayList<String> userlist) throws ClassNotFoundException, IOException{
if(userlist.size()==0){
return username;
}
Collections.shuffle(userlist);
String randomname = userlist.get(0);
User randomUser = Handlefile.readUserFile(randomname);
if(!randomUser.validArmy()){
userlist.remove(randomname);
return randomchooser(username, userlist);
}else{
return randomUser;
}
}
public static User getrandomUser(User user) throws ClassNotFoundException, IOException,InvalidClassException{
ArrayList<String> userlist = Handlefile.readUserProfiles().getUserNames();
userlist.remove(user.getUserName());
if(userlist.size()==0){
return user;
}
User randomUser = randomchooser(user, userlist);
if(randomUser.getUserName().equals(user.getUserName())){
return user;
}
System.out.println("random opponent : "+randomUser.getName());
randomUser.getuserinfo();
System.out.print("\n1 > declare war with "+randomUser.getName()+"\n2 > show another player\nchoose correct number : ");
String choise = InputProcessor.getString();
System.out.println("\n");
switch (choise) {
case "1":
return randomUser;
default:
return getrandomUser(user);
}
}
}