-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameForNode.js
69 lines (60 loc) · 1.45 KB
/
gameForNode.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
const readline = require('readline-sync');
const MineField = require('./MineField.js');
const gridSize = 10;
let game = new MineField(gridSize);
const dataToDisplay = function(result){
return {lastStepResult:result,
nextPossibleMoves:game.currentPossibleMoves,
playerPreviousMoves:game.playerMoves,
playerChances:game.chances,
};
};
const stopGame = function(){
process.exit();
};
const executeResultCondition = function(currentMove,previousMessage) {
if (game.isGameOver()) {
console.log("you Lost!!!");
stopGame();
};
if (game.hasPlayerWon(currentMove)) {
console.log("you Won!!!");
stopGame();
};
return previousMessage;
};
const executeCurrentMove = function(){
if (game.isBomb()) {
game.actionOnBomb();
return dataToDisplay("bomb");
} else {
game.gameInPlay()
return dataToDisplay("your are safe");
};
};
const isFirstMove = function(move){
return game.isFirstMove() && move <= 10;
};
const executeGame = function(){
console.log("your move");
let move = +readline.prompt();
if (isNaN(move)) {
console.log("give Valid number\n\n");
executeGame();
};
let data = "";
game.updateCurrentMove(move);
if(isFirstMove(move)){
game.startGame(move);
data = dataToDisplay("your are safe");
} else {
data = executeCurrentMove();
};
data = executeResultCondition(move,data);
console.log(data);
executeGame();
};
const startGame = function(){
executeGame();
};
startGame();