-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgame.js
More file actions
102 lines (102 loc) · 3.6 KB
/
game.js
File metadata and controls
102 lines (102 loc) · 3.6 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
96
97
98
99
100
101
102
import { compareNumbers } from './compareNumbers';
import { formatDate } from './formatDate';
import { generateRandomNum } from './randomNumber';
export const createGame = () => {
// 게임 상태
let randomNumber = [];
let chances = 3;
let isGameStarted = false;
let gameRecord = [];
// 게임 시작
const startGame = () => {
randomNumber = generateRandomNum();
chances = 3;
isGameStarted = true;
const game = {
id: gameRecord.length + 1,
start: formatDate(new Date()),
end: undefined,
attempt: 0,
detailResult: {
inputData: [],
resultList: [],
},
isSuccess: false,
message: '',
};
gameRecord.push(game);
};
// 게임 리셋
const resetGame = () => {
const currentGame = gameRecord[Math.max(0, gameRecord.length - 1)];
currentGame.end = formatDate(new Date());
isGameStarted = false;
};
// 게임이 시작됐는지 여부
const getIsGameStarted = () => isGameStarted;
// 입력 값 검사
const checkNum = (input) => {
const currentGame = gameRecord[Math.max(0, gameRecord.length - 1)];
if (!/^\d{3}$/.test(input.trim())) {
return '3자리 숫자를 입력하세요';
}
const gameResult = compareNumbers(randomNumber, input);
chances--;
currentGame.attempt++;
currentGame.detailResult.inputData.push(input);
currentGame.detailResult.resultList.push(`strike: ${gameResult.strike}, ball: ${gameResult.ball}`);
if (gameResult.strike === 3) {
isGameStarted = false;
currentGame.isSuccess = true;
currentGame.end = formatDate(new Date());
currentGame.message = '3개의 숫자를 모두 맞히셨습니다.';
return `3개의 숫자를 모두 맞히셨습니다.\n-------게임 종료-------`;
}
if (chances === 0) {
isGameStarted = false;
currentGame.isSuccess = false;
currentGame.end = formatDate(new Date());
currentGame.message = '3개의 숫자를 모두 맞히지 못하셨습니다.';
return `3개의 숫자를 모두 맞히지 못하셨습니다.\n-------게임 종료-------`;
}
return gameResult.ball === 0 ? '낫싱' : `strike: ${gameResult.strike}, ball: ${gameResult.ball}`;
};
const getGameRecords = () => {
return gameRecord.map((record) => ({
id: record.id,
start: record.start,
end: record.end,
attempt: record.attempt,
}));
};
const getDetailGameRecords = (id) => {
const record = gameRecord.find((record) => record.id === id);
if (!record) {
return '기록이 없습니다.';
}
let detail = `${record.id}번 게임 결과 \n `;
record.detailResult.inputData.forEach((input, index) => {
const result = record.detailResult.resultList[index];
detail += `숫자를 입력해주세요: ${input}\n${result}\n`;
});
if (record.message) {
detail += `\n${record.message}\n-------기록 종료-------`;
}
return detail;
};
const getTotalRecords = () => {
return gameRecord.map((record) => ({
id: record.id,
attempt: record.attempt,
}));
};
return {
startGame,
resetGame,
checkNum,
getIsGameStarted,
getGameRecords,
getDetailGameRecords,
getTotalRecords,
};
};