main.js
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