forked from CODEScs3250/ConsoleGameHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameUtils.java
More file actions
32 lines (30 loc) · 964 Bytes
/
GameUtils.java
File metadata and controls
32 lines (30 loc) · 964 Bytes
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
import java.util.Random;
import java.util.List;
/**
* This utility class provides methods for use in multiple games.
* For example, it contains a method for handling random word selection.
*/
public final class GameUtils {
/**
* Static instance used to generate random strings.
*/
private static final Random RANDOM = new Random();
/**
* Prevent Instantiation.
*/
private GameUtils() {
throw new UnsupportedOperationException("class cannot be instantiated");
}
/**
* Returns a random word from given list.
*
* @param words A list of words from which the random word will be selected.
* @return A random word from the list or null if the list is null or empty.
*/
public static String getRandomWord(final List<String> words) {
if (words == null || words.isEmpty()) {
return null;
}
return words.get(RANDOM.nextInt(words.size()));
}
}