import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
class GameSimulator {
public static void main
(String[] args
) { int playerCount = 1000; //100K
int gameCount = 100000; //1M
GameSimulator simulator = new GameSimulator(playerCount, gameCount);
}
final int playerCount;
final int gameCount;
final Player[] playerList;
ArrayList<Player> game;
public GameSimulator(int playerCount, int gameCount) {
this.playerCount = playerCount;
this.gameCount = gameCount;
playerList = new Player[playerCount];
for(int currentGame = 0; currentGame < gameCount; currentGame++) {
game = new ArrayList<Player>();
while(game.size() < 24) {
//create a game of 24 players with no duplicates
int playerId = random.nextInt(playerCount);
if(!game.contains(getPlayer(playerId))) {
game.add(getPlayer(playerId));
}
}
int teamWins = random.nextInt(2);
//Team 0 or Team 1 wins
saveMatch(teamWins == 0);
}
//Print the distribution
printWinLossBuckets();
}
public void printWinLossBuckets() {
//Get everyone's W/L
final double[] winLoss = new double[playerCount];
for(int i = 0; i < playerCount; i++) {
getPlayer(i); //instantiate null players
winLoss[i] = playerList[i].getWinRate(); //get win rate
}
int bucketBracket = 0;
int bucketCount = 0;
double totalWinRate = 0;
int totalGamesPlayed = 0;
int mostGamesPlayed = 0;
for(int i = 0; i < playerCount; i++) {
totalWinRate += winLoss[i]; //calculate average WR
totalGamesPlayed += playerList[i].gamesPlayed; // calculate average games
mostGamesPlayed = mostGamesPlayed < playerList[i].gamesPlayed ? playerList[i].gamesPlayed : mostGamesPlayed;
while(winLoss[i] > bucketBracket) {
if(bucketCount > 0) {
System.
out.
println(bucketBracket
+ "\t" + bucketCount
); }
bucketBracket+=2;
bucketCount = 0;
}
bucketCount++;
}
System.
out.
println("Average WR: " + (totalWinRate
/ (double) playerCount
)); System.
out.
println("Average Games Played: " + (totalGamesPlayed
/ (double) playerCount
)); System.
out.
println("Most Games Played by One Player: " + mostGamesPlayed
); }
private void saveMatch(boolean firstTeamWins) {
for(int i = 0; i < 12; i++) {
game.get(i).playedGame(firstTeamWins);
}
for(int i = 12; i < 24; i++) {
game.get(i).playedGame(!firstTeamWins);
}
}
public Player getPlayer(int id) {
if(playerList[id] == null) {
playerList[id] = new Player();
}
return playerList[id];
}
class Player {
int gamesWon = 0;
int gamesPlayed = 0;
void lostGame() {
gamesPlayed++;
}
void wonGame() {
gamesPlayed++;
gamesWon++;
}
void playedGame(boolean won) {
if(won)
wonGame();
else
lostGame();
}
double getWinRate() {
return (double) gamesWon / (double) gamesPlayed * (double) 100;
}
}
}