-
Notifications
You must be signed in to change notification settings - Fork 1
/
rock_paper_scissors.js
146 lines (122 loc) · 3.41 KB
/
rock_paper_scissors.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
class Player {
constructor(name) {
this.name = name;
this.move = null;
}
}
class ComputerPlayer extends Player {
static get EASY_DIFFICULTY() { return 'facil';}
static get NORMAL_DIFFICULTY() { return 'normal';}
constructor(name, difficulty) {
super(name);
this.difficulty = difficulty;
}
async nextMove() {
if (this.difficulty === ComputerPlayer.EASY_DIFFICULTY) {
this.move = new Move(Move.ROCK);
} else if (this.difficulty === ComputerPlayer.NORMAL_DIFFICULTY) {
this.move = new Move();
}
console.log(`${this.name}: ${this.move.name}`);
}
}
class HumanPlayer extends Player {
async nextMove() {
this.move = await Move.readMove(this.name);
}
}
class Move {
static get PAPER() { return 'papel'; }
static get ROCK() { return 'piedra'; }
static get SCISSORS () { return 'tijera'; }
constructor(name) {
this.options = [Move.ROCK, Move.PAPER, Move.SCISSORS];
this.name = name ? name : this.random();
}
win() {
switch (this.name) {
case Move.ROCK:
return Move.SCISSORS;
case Move.PAPER:
return Move.ROCK;
case Move.SCISSORS:
return Move.PAPER;
}
}
random() {
return this.options[Math.floor(Math.random() * this.options.length)];
}
static async readMove(PlayerName) {
return new Promise((resolve, reject) => {
readline.question(PlayerName+': ', answer => {
resolve(new Move(answer));
})
});
}
}
class Rule {
static get TIE () { return null; }
constructor(players) {
this.players = players;
}
getWinner() {
const moves = this.players.map(player => player.move);
if (moves[0].name === moves[1].name) return Rule.TIE;
return moves[0].win() === moves[1].name ? this.players[0] : this.players[1];
}
}
class Game {
constructor(players, mode, rule) {
this.players = players;
this.mode = mode;
this.rule = rule;
}
async play() {
for (const player of this.players) {
await player.nextMove();
}
return this.rule.getWinner();
}
}
class RockPaperScissorsGame extends Game {
static get MULTIPLAYER() { return 'multijugador'; }
static get SINGLEPLAYER() { return 'solo'; }
constructor(mode, difficulty) {
const players = RockPaperScissorsGame.definePlayers(mode, difficulty);
const rule = new Rule(players);
super(players, mode, rule);
}
static definePlayers(mode, difficulty) {
let players = [];
if (mode === RockPaperScissorsGame.MULTIPLAYER) {
players[0] = new HumanPlayer('Jugador1');
players[1] = new HumanPlayer('Jugador2');
}
else if (mode === RockPaperScissorsGame.SINGLEPLAYER) {
players[0] = new HumanPlayer('Jugador1');
players[1] = new ComputerPlayer('Computador', difficulty);
}
return players;
}
}
async function main() {
/*node index.js --modo=solo --dificultad=norma */
let mode = '';
let difficulty = '';
process.argv.forEach(arg => {
if (arg.includes('--modo')) {
mode = arg.split('=')[1];
} else if (arg.includes('--dificultad')) {
difficulty = arg.split('=')[1];
}
});
const rockPaperScissors = new RockPaperScissorsGame(mode, difficulty);
const winner = await rockPaperScissors.play();
console.log(winner === Rule.TIE ? `Empate` : `Ganador: ${winner.name}`);
readline.close();
}
main();