-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
42 lines (39 loc) · 2.08 KB
/
Driver.java
File metadata and controls
42 lines (39 loc) · 2.08 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
package game_theory_bimatrix;
public class Driver {
static boolean DEBUG = false;
static int totalRounds = 1_000_000;
public static void main(String[] args) {
Game game = new GameRPS();
Player player1 = new Player(new StrategyHMC(new double[] {0.33,0.33,0.33}, game, 1), game, 1);
Player player2 = new Player(new StrategyHMC(new double[] {0.33,0.33,0.33}, game, 2), game, 2);
int p1Wins = 0, p2Wins = 0;
double p1TotalUtility = 0, p2TotalUtility = 0;
for (int i = 0; i < totalRounds; i++) {
int p1Choice = player1.chooseAction();
int p2Choice = player2.chooseAction();
double p1Utility = game.utility(p1Choice, p2Choice, 1);
double p2Utility = game.utility(p2Choice, p1Choice, 2);
p1TotalUtility += p1Utility;
p2TotalUtility += p2Utility;
if (DEBUG) System.out.println("\nPlayer 1 plays " + game.actionAsString(p1Choice) + ", player 2 plays " + game.actionAsString(p2Choice));
if (p1Utility > p2Utility) {
p1Wins++;
if (DEBUG) System.out.println("Player 1 wins");
}
else if (p1Utility < p2Utility) {
p2Wins++;
if (DEBUG) System.out.println("Player 2 wins"); }
else {
if (DEBUG) System.out.println("Tie");
}
player1.update(p1Choice, p2Choice);
player2.update(p2Choice, p1Choice);
}
System.out.println("Player1 wins: " + p1Wins + " Player2 wins: " + p2Wins); System.out.println("Player1 utility per game: " + (p1TotalUtility/totalRounds) +
" Player2 utility per game: " + (p2TotalUtility/totalRounds)); System.out.println("Player 1 Strategy:");
double[] s= player1.getStrategy().getStrategy(); for (int i = 0; i < s.length; i++) {
System.out.print(" " + s[i]); }
System.out.println("\nPlayer 2 Strategy:"); s= player2.getStrategy().getStrategy(); for (int i = 0; i < s.length; i++) {
System.out.print(" " + s[i]); }
System.out.println(); }
}