Created
September 21, 2017 10:42
-
-
Save dnivra26/a85599d5ccbee04c3819712a57059f46 to your computer and use it in GitHub Desktop.
Game Of Trust - Stage 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Action { | |
COOPERATE, | |
CHEAT | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Game { | |
public static void main(String[] args) { | |
Machine machine = new Machine(); | |
Player player2 = new Player("Player-1", 1); | |
Player player1 = new Player("Player-2", 1); | |
for (int i = 0; i < 1; i++) { | |
machine.execute(player1, player2); | |
} | |
System.out.println("Player 1 has: " + player1.getCoin() + " coins."); | |
System.out.println("Player 2 has: " + player2.getCoin() + " coins. "); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Machine { | |
public Machine() { | |
} | |
public void execute(Player player1, Player player2) { | |
Action player1Action = player1.getAction(); | |
Action player2Action = player2.getAction(); | |
player1.update(getRewardForAction(player2Action) - getContributionForAction(player1Action)); | |
player2.update(getRewardForAction(player1Action) - getContributionForAction(player2Action)); | |
} | |
private int getRewardForAction(Action action) { | |
if (action.equals(Action.COOPERATE)) { | |
return 3; | |
} else { | |
return 0; | |
} | |
} | |
private int getContributionForAction(Action action) { | |
if (action.equals(Action.COOPERATE)) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Player { | |
private String name; | |
private int coin; | |
public Player(String name, int coin) { | |
this.name = name; | |
this.coin = coin; | |
} | |
public int getCoin() { | |
return coin; | |
} | |
public Action getAction() { | |
Scanner reader = new Scanner(System.in); | |
System.out.println(name +" Enter 1 to co-operate 0 to cheat: "); | |
int n = reader.nextInt(); | |
if (n == 1) { | |
return Action.COOPERATE; | |
} else { | |
return Action.CHEAT; | |
} | |
} | |
public void update(int delta) { | |
coin += delta; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment