@cgousley/

RockPaperScissors

JavaScript

Uses new ES6 Classes to create a RPS game. Keeps score too.

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8594721432006238","path":"main.js","file":{"path":"main.js","content":{"asEncoding":{"base64":"Y2xhc3MgUlBTIHsKICBjb25zdHJ1Y3RvcihzZWxlY3QpewogICAgdGhpcy5yZWNvcmQgPSB7CiAgICAgIHdpbjogMCwKICAgICAgbG9zZTogMCwKICAgICAgdGllOiAwCiAgICB9LAogICAgdGhpcy5jaG9pY2VMaXN0ID0gWydyb2NrJywgJ3BhcGVyJywgJ3NjaXNzb3JzJ10sCiAgICB0aGlzLmNob2ljZXMgPSB7CiAgICAgIHIgOiB7CiAgICAgICAgbmFtZTogInJvY2siLAogICAgICAgIHJpdmFscyA6IHsKICAgICAgICAgIHJvY2sgOiAiSXQncyBhIHRpZSIsCiAgICAgICAgICBwYXBlciA6ICJZb3UgbG9zZSIsCiAgICAgICAgICBzY2lzc29ycyAgOiAiWW91IHdpbiIKICAgICAgICB9LAogICAgICB9LAogICAgICBwIDogewogICAgICAgIG5hbWU6ICJwYXBlciIsCiAgICAgICAgcml2YWxzIDogewogICAgICAgICAgcm9jayA6ICJZb3Ugd2luIiwKICAgICAgICAgIHBhcGVyIDogIkl0J3MgYSB0aWUiLAogICAgICAgICAgc2Npc3NvcnMgIDogIllvdSBsb3NlIgogICAgICAgIH0KICAgICAgfSwKICAgICAgcyA6IHsKICAgICAgICBuYW1lOiAic2Npc3NvcnMiLAogICAgICAgIHJpdmFscyA6IHsKICAgICAgICAgIHJvY2sgOiAiWW91IGxvc2UiLAogICAgICAgICAgcGFwZXIgOiAiWW91IHdpbiIsCiAgICAgICAgICBzY2lzc29ycyAgOiAiSXQncyBhIHRpZSIKICAgICAgICB9CiAgICAgIH0KICAgIH0KICB9CiAgZW5lbXlDaG9zZSgpewogICAgcmV0dXJuICh0aGlzLmVuZW15Q2hvaWNlKSA/IHRoaXMuZW5lbXlDaG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nKSwgNTAwMCk7CnNldFRpbWVvdXQoKCkgPT4geC5nYW1lKCdzJyksIDEwMDAwKTs="},"asBuffer":null},"loaded":true}}
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class RPS {
  constructor(select){
    this.record = {
      win: 0,
      lose: 0,
      tie: 0
    },
    this.choiceList = ['rock', 'paper', 'scissors'],
    this.choices = {
      r : {
        name: "rock",
        rivals : {
          rock : "It's a tie",
          paper : "You lose",
          scissors  : "You win"
        },
      },
      p : {
        name: "paper",
        rivals : {
          rock : "You win",
          paper : "It's a tie",
          scissors  : "You lose"
        }
      },
      s : {
        name: "scissors",
        rivals : {
          rock : "You lose",
          paper : "You win",
          scissors  : "It's a tie"
        }
      }
    }
  }
  enemyChose(){
    return (this.enemyChoice) ? this.enemyChoice : this.choiceList[Math.floor(Math.random() * (3-0))]
  }
  youChose(select){
    // return select
    return this.choices[select].name;
  }
  getOutcomeMsg(y,e,select){
    return this.choices[select].rivals[e]
  }
  game(select){
    //your choice
    const y = this.youChose(select);
    //ememy's choice
    const e =  this.enemyChose();
    //who won msg based on choices
    const o = this.getOutcomeMsg(y,e,select)
    
    this.record[o.split(" ")[o.split(" ").length - 1]]++;
    var i = 0;
    let next = () => {
      // console.log message
      console.log(["You chose "+y, "Your enemy chose "+e, o, this.record][i])
      i++;
    }
    setInterval(() => {(i<4) ? next() : "";}, 1000);
  }
}
//Declare a new game
let x = new RPS();
//Play game.
setTimeout(() => x.game('r'), 0);
setTimeout(() => x.game('p'), 5000);
setTimeout(() => x.game('s'), 10000);
Native Browser JavaScript