forked from areumsheep/javascript-baseball
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseballGame.js
More file actions
95 lines (84 loc) · 2.1 KB
/
BaseballGame.js
File metadata and controls
95 lines (84 loc) · 2.1 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
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
const { Console, Random } = require("@woowacourse/mission-utils");
const { GameConsole } = require("./GameConsole");
class BaseballGame {
#answer;
#inputNumbers;
#strikeCnt;
#ballCnt;
#console;
constructor() {
this.#answer = null;
this.#inputNumbers = null;
this.#strikeCnt = null;
this.#ballCnt = null;
this.#console = new GameConsole();
}
start() {
this.#makeRandomNumbers();
this.#console.printStart();
this.#getUserInput();
}
exit() {
Console.close();
}
restart() {
this.#answer = null;
this.#inputNumbers = null;
this.#strikeCnt = null;
this.#ballCnt = null;
this.#makeRandomNumbers();
this.#getUserInput();
}
#checkStrikeCnt() {
let cnt = 0;
for (let i = 0; i < 3; i += 1) {
if (this.#answer[i] === Number(this.#inputNumbers[i])) cnt += 1;
}
this.#strikeCnt = cnt;
}
#checkBallCnt() {
let cnt = 0;
this.#inputNumbers.forEach((v, i) => {
if (Number(v) === this.#answer[0] && i !== 0) cnt++;
if (Number(v) === this.#answer[1] && i !== 1) cnt++;
if (Number(v) === this.#answer[2] && i !== 2) cnt++;
});
this.#ballCnt = cnt;
}
#getUserInput() {
this.#console.inputNumbers(this.#userNumberAfterFunc.bind(this));
}
#userNumberAfterFunc(numbers) {
this.#inputNumbers = numbers.split("");
this.#matchWithAnswer();
}
#cmdAfterFunc(cmd) {
if (cmd === "1") this.restart();
if (cmd === "2") this.exit();
}
#makeRandomNumbers() {
const computer = [];
while (computer.length < 3) {
const number = Random.pickNumberInRange(1, 9);
if (!computer.includes(number)) {
computer.push(number);
}
}
this.#answer = computer;
console.log(this.#answer); // 확인용
}
#matchWithAnswer() {
this.#checkStrikeCnt();
this.#checkBallCnt();
this.#console.printResult(this.#strikeCnt, this.#ballCnt);
if (this.#strikeCnt === 3) {
this.#console.printEnd();
this.#console.inputCmd(this.#cmdAfterFunc.bind(this));
return;
}
this.#getUserInput();
}
}
module.exports = {
BaseballGame,
};