Skip to content

Instantly share code, notes, and snippets.

@zoolu-got-rhythm
Last active November 25, 2022 04:13
Show Gist options
  • Save zoolu-got-rhythm/840299d8f17ffa15be6d081c24c9b82b to your computer and use it in GitHub Desktop.
Save zoolu-got-rhythm/840299d8f17ffa15be6d081c24c9b82b to your computer and use it in GitHub Desktop.
rock, paper, scissors game logic modeled and written in typescript
// rock paper scissors business logic/application state/model data
enum Hand{
ROCK,
PAPER,
SCISSORS
}
const hands = [Hand.ROCK, Hand.PAPER, Hand.SCISSORS];
class Player{
name: string;
id: string; // could be uuid
currentHand: Hand;
constructor(name: string, id: string, currentHand: Hand){
this.name = name;
this.id = id;
this.currentHand = currentHand;
}
public setCurrentHand(hand: Hand){
this.currentHand = hand;
}
}
interface MatchScore{
[key: string]: number; //uuid of player and their score for Match
}
// first to 3
class Match{
playerA: Player;
playerB: Player;
score: MatchScore;
onWinningPlayer: (player: Player) => void;
gameIsFinished: boolean;
constructor(playerA: Player, playerB: Player, onWinningPlayer: (player: Player) => void){
this.playerA = playerA;
this.playerB = playerB;
this.score = {
[this.playerA.id]: 0,
[this.playerB.id]: 0
}
this.onWinningPlayer = onWinningPlayer;
this.gameIsFinished = false;
}
public getWinnerOfRound(){
if(this.gameIsFinished){
return;
}
if(this.playerA.currentHand === Hand.ROCK && this.playerB.currentHand === Hand.SCISSORS){ // p-a rock > p-b scissors
this.score[this.playerA.id] = this.score[this.playerA.id] + 1;
console.log("player a wins round");
}else if(this.playerA.currentHand === Hand.ROCK && this.playerB.currentHand === Hand.PAPER){ // p-a rock < p-b paper
this.score[this.playerB.id] = this.score[this.playerB.id] + 1;
console.log("player b wins round");
}else if(this.playerA.currentHand === Hand.SCISSORS && this.playerB.currentHand === Hand.PAPER){ // p-a scissors > p-b paper
this.score[this.playerA.id] = this.score[this.playerA.id] + 1;
console.log("player a wins round");
}else if(this.playerB.currentHand === Hand.ROCK && this.playerA.currentHand === Hand.SCISSORS){ // p-a scissors < p-b rock
this.score[this.playerB.id] = this.score[this.playerB.id] + 1;
console.log("player b wins round");
}else if(this.playerB.currentHand === Hand.ROCK && this.playerA.currentHand === Hand.PAPER){ // p-a paper > p-b rock
this.score[this.playerA.id] = this.score[this.playerA.id] + 1;
console.log("player a wins round");
}else if(this.playerB.currentHand === Hand.SCISSORS && this.playerA.currentHand === Hand.PAPER){ // p-a paper < p-b scissors
this.score[this.playerB.id] = this.score[this.playerB.id] + 1;
console.log("player b wins round");
}else{ // tie
console.log("tie");
}
if(this.score[this.playerA.id] === 3 || this.score[this.playerB.id] === 3){
this.getWinnerOfGame();
}
}
private getWinnerOfGame(){
this.gameIsFinished = true;
const winningPlayer: Player = this.score[this.playerA.id] === 3 ? this.playerA : this.playerB;
this.onWinningPlayer(winningPlayer);
}
}
// game test
let playerA = new Player("bob", Math.random().toString(), hands[Math.floor(Math.random() * hands.length)]);
let playerB = new Player("max", Math.random().toString(), hands[Math.floor(Math.random() * hands.length)]);
let matchA = new Match(playerA, playerB, (player: Player) => {
console.log(player.name + " wins!");
});
console.log("playerA", playerA);
console.log("playerB", playerB);
// first to 3 round wins
playerA.setCurrentHand(Hand.ROCK);
playerB.setCurrentHand(Hand.SCISSORS);
matchA.getWinnerOfRound(); // round 1: 1 - 0
matchA.getWinnerOfRound(); // round 2: 2 - 0
playerB.setCurrentHand(Hand.PAPER);
matchA.getWinnerOfRound(); // round 3: 2 - 1
matchA.getWinnerOfRound(); // round 4: 2 - 2
playerA.setCurrentHand(Hand.SCISSORS);
matchA.getWinnerOfRound(); // round 5: 3 - 2
matchA.getWinnerOfRound(); // void round: 3 - 2
matchA.getWinnerOfRound(); // void round: 3 - 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment