Repository navigation
Expand file tree
/
Copy path01_rockPaperScissors.js
More file actions
55 lines (46 loc) Β· 2.09 KB
/
Copy path01_rockPaperScissors.js
File metadata and controls
55 lines (46 loc) Β· 2.09 KB
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
// ! λ¬Έμ :
/*
κ°μλ°μ보 κ²μμ 2μΈ μ΄μμ μ¬λμ΄ λμμ 'κ°μ, λ°μ, 보'λ₯Ό μΈμΉκ³ λμμ κ°μ, λ°μ λλ 보 μ€μμ ν κ°μ§λ₯Ό μλ―Ένλ μ λͺ¨μμ λ΄λ°μ΄ μΉλΆλ₯Ό κ²°μ μ§λ κ²μμ΄λ€.
μΈ νμ κ°μλ°μ보 κ²μμ ν κ²½μ°, ν μ¬λμ μΈ λ²μ μ ν(μ. κ°μ, κ°μ, 보)μ ν μ μμ΅λλ€. μΈ λ²μ μ νμΌλ‘ κ°λ₯ν λͺ¨λ κ²½μ°μ μλ₯Ό ꡬνλ ν¨μλ₯Ό μμ±ν©λλ€.
*/
// ! μ£Όμμ¬ν:
/*
1. μ΅μ’
μ μΌλ‘ 리ν΄λλ λ°°μ΄μ μμλ κ°μ€μΉ μ μ© μ λ ¬(Weighted Sort) μ λ°λ₯Έλ€.
2. μ€μλλ 'rock', 'paper', 'scissors' μμΌλ‘ λλ€.
3. μ½κ² μκ°ν΄ μ¬λ¦Όν½ μμ κ²°μ λ°©μμ μ°Έκ³ νλ©΄ λλ€.
4. κΈλ©λ¬('rock') μ΄ μλ©λ¬('paper') λ³΄λ€ μ°μ νκ³ , μλ©λ¬('paper') μ΄ λλ©λ¬('scissors') λ³΄λ€ μ°μ νλ€.
*/
// ! μ
μΆλ ₯ μμ:
/*
let output = rockPaperScissors();
console.log(output);
*/
// [
// ["rock", "rock", "rock"],
// ["rock", "rock", "paper"],
// ["rock", "rock", "scissors"],
// ["rock", "paper", "rock"],
// // ...etc ...
// ]
// ! λ΄ μ κ·Ό λ°©λ²:
// ? 1. 'κ°μ, λ°μ, 보'κ° λ΄κΈ΄ λ°°μ΄μ λ³μμ ν λΉν΄μ€λ€.
// ? 2. λ μ΄μ λ¨μ νμ΄ μμ λ, νμ¬κΉμ§ λΈ μλ€μ μλ‘μ΄ κ²°κ³Όμ λ΄μμ£Όκ³ λ¦¬ν΄νλ€.
// ? 3. permutate ν¨μμ λ°λ³΅λ¬Έ μμμ permutateλ₯Ό μ½λ°±ν¨μλ‘ μ¬μ©νλ€.
// ? 4. λΌμ΄λ μμ λΉ λ°°μ΄μ μΈμλ‘ νλ permutate ν¨μλ₯Ό μ€ννκ³ μ΅μ’
μ μΌλ‘ νΈμνκ³ λμ€λ κ²°κ³Όλ₯Ό 리ν΄νλ€.
const rockPaperScissors = function(rounds) {
rounds = rounds || 3;
const rps = ['rock', 'paper', 'scissors'];
const outcomes = [];
let permutate = function(roundsToGo, playedSoFar) {
if (roundsToGo === 0) {
outcomes.push(playedSoFar);
return;
}
for (let i = 0; i < rps.length; i++) {
let currentPlay = rps[i];
permutate(roundsToGo - 1, playedSoFar.concat(currentPlay));
}
};
permutate(rounds, []);
return outcomes;
};