Created
February 4, 2016 20:39
-
-
Save Ethan826/ccf5935dbd427c4aae20 to your computer and use it in GitHub Desktop.
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
/** | |
* Enums and interfaces | |
*/ | |
enum Choices { | |
cooperate, | |
defect | |
} | |
interface Results { | |
p1Points: number; | |
p2Points: number; | |
} | |
interface Player { | |
playTurn(): Choices; | |
receiveResult(Choices): void; | |
} | |
interface Round { | |
myChoice: Choices; | |
theirChoice: Choices; | |
} | |
/** | |
* Game running classes | |
*/ | |
class GameRunner { | |
private VALUES = { | |
sucker: 10.0, | |
punishment: 6.0, | |
reward: 0.5, | |
temptation: 0.0 | |
} | |
constructor(private player1: Player, private player2: Player) { } | |
playGame(): Results { | |
let p1Move = this.player1.playTurn(); | |
let p2Move = this.player2.playTurn(); | |
let p1Result: number, p2Result: number; | |
if(p1Move === Choices.cooperate) { | |
if(p2Move === Choices.cooperate) { // both cooperate | |
p1Result = this.VALUES.reward; | |
p2Result = this.VALUES.reward; | |
} else { // p1 cooperates, p2 defects | |
p1Result = this.VALUES.sucker; | |
p2Result = this.VALUES.temptation; | |
} | |
} else { | |
if(p2Move === Choices.cooperate) { // p1 defects, p2 cooperates | |
p1Result = this.VALUES.temptation; | |
p2Result = this.VALUES.sucker; | |
} else { // both defect | |
p1Result = this.VALUES.punishment; | |
p2Result = this.VALUES.punishment; | |
} | |
} | |
this.player1.receiveResult(p2Move); | |
this.player2.receiveResult(p1Move); | |
return {p1Points: p1Result, p2Points: p2Result}; | |
} | |
} | |
class SeriesRunner { | |
private scores: Results = {p1Points: 0, p2Points: 0}; | |
private gameRunner: GameRunner; | |
constructor( | |
private player1, | |
private player2, | |
private numRounds: number, | |
private callback: (results: Results) => any) { | |
this.gameRunner = new GameRunner(this.player1, this.player2); | |
this.runSeries(); | |
} | |
runSeries() { | |
for(let round = 0; round < this.numRounds; ++round) { | |
let result = this.gameRunner.playGame(); | |
this.scores.p1Points += result.p1Points; | |
this.scores.p2Points += result.p2Points; | |
} | |
this.callback(this.scores); | |
} | |
} | |
/** | |
* Specific player implementations | |
*/ | |
class AllC implements Player { | |
playTurn() { | |
return Choices.cooperate; | |
} | |
receiveResult(theirChoice) { } | |
} | |
class AllD implements Player { | |
playTurn() { | |
return Choices.defect; | |
} | |
receiveResult(theirChoice) { } | |
} | |
class TfT implements Player { | |
private nextMove = Choices.cooperate; | |
playTurn() { | |
return this.nextMove; | |
} | |
receiveResult(theirChoice) { | |
this.nextMove = theirChoice; | |
} | |
} | |
let sr = new SeriesRunner(new TfT, new AllD, 10000, (results) => { | |
$("#player1").html("P1 score: " + results.p1Points); | |
$("#player2").html("P2 score: " + results.p2Points); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment