-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
110 lines (101 loc) · 3.39 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const argv = yargs(hideBin(process.argv)).argv;
class Player {
constructor(name, move) {
this.move = move;
this.name = name;
console.log(`${name} plays ${move}`);
}
static ruleSets = [
{
rock: ["paper"],
paper: ["scissors"],
scissors: ["rock"],
},
{
rock: ["paper", "spock"],
paper: ["scissors", "lizard"],
scissors: ["rock", "spock"],
lizard: ["scissors", "rock"],
spock: ["paper", "lizard"],
},
{
tree: ["lightning", "gun", "rock", "fire", "scissors", "snake", "human"],
wolf: ["gun", "rock", "fire", "scissors", "snake", "human", "tree"],
sponge: ["rock", "fire", "scissors", "snake", "human", "tree", "wolf"],
paper: ["fire", "scissors", "snake", "human", "tree", "wolf", "sponge"],
air: ["scissors", "snake", "human", "tree", "wolf", "sponge", "paper"],
water: ["snake", "human", "tree", "wolf", "sponge", "paper", "air"],
dragon: ["human", "tree", "wolf", "sponge", "paper", "air", "water"],
devil: ["tree", "wolf", "sponge", "paper", "air", "water", "dragon"],
lightning: ["wolf", "sponge", "paper", "air", "water", "dragon", "devil"],
gun: ["sponge", "paper", "air", "water", "dragon", "devil", "lightning"],
rock: ["paper", "air", "water", "dragon", "devil", "lightning", "gun"],
fire: ["air", "water", "dragon", "devil", "lightning", "gun", "rock"],
scissors: [
"water",
"dragon",
"devil",
"lightning",
"gun",
"rock",
"fire",
],
snake: [
"dragon",
"devil",
"lightning",
"gun",
"rock",
"fire",
"scissors",
],
human: ["devil", "lightning", "gun", "rock", "fire", "scissors", "snake"],
},
];
static listRules(complexity = 0) {
Object.entries(Player.ruleSets[complexity]).forEach((move) => {
console.log(`${move[0]} is beat by ${move[1].join(", ")}.`);
});
}
static compareMoves(player1, player2, complexity = 0) {
if (player1 instanceof Player || player2 instanceof Player) {
throw new TypeError("Invalid Player!");
}
const moveSet = Player.ruleSets[complexity];
if (player1.move === player2.move) {
console.log("~This round is a tie~");
} else if (moveSet[player2.move].includes(player1.move)) {
console.log(`~${player1.name} wins.~`);
} else if (moveSet[player1.move].includes(player2.move)) {
console.log(`~${player2.name} wins.~`);
} else {
throw new Error("Unexpected Error occured!");
}
}
static [Symbol.hasInstance](obj) {
if (obj.ruleSets) return true;
}
}
class HumanPlayer extends Player {
constructor(move, complexity = 0) {
if (!(move.toLowerCase() in Player.ruleSets[complexity]))
throw new TypeError("Invalid move!");
super("Player", move.toLowerCase());
}
}
class ComputerPlayer extends Player {
constructor(complexity = 0) {
const moves = Object.keys(Player.ruleSets[complexity]);
super("Computer", moves[Math.floor(Math.random() * moves.length)]);
}
}
if (argv.listRules || argv.listrules) {
Player.listRules(argv.complexity);
}
if (argv.move) {
const human = new HumanPlayer(argv.move, argv.complexity);
const computer = new ComputerPlayer(argv.complexity);
Player.compareMoves(human, computer, argv.complexity);
}