-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphase2.js
More file actions
69 lines (62 loc) · 2.31 KB
/
phase2.js
File metadata and controls
69 lines (62 loc) · 2.31 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
const prompt = require("prompt-sync")();
const playersArray = [];
let winner = null;
const numberOfPlayers =
prompt("How many players you are (up to 7 and no less than 2)? ")
for (let i = 0; i < numberOfPlayers; i++) {
const playerName = prompt("Please enter a your name: ");
playersArray.push({ name: playerName, score: 0, wins: 0 });
}
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function playRound() {
const firstPlayer = playersArray[getRandomNumber(0, playersArray.length - 1)];
let secondPlayer = playersArray[getRandomNumber(0, playersArray.length - 1)];
while (firstPlayer.name === secondPlayer.name) {
secondPlayer = playersArray[getRandomNumber(0, playersArray.length - 1)];
}
firstPlayer.score = 0;
secondPlayer.score = 0;
for (let i = 0; i < numberOfPlayers; i++) {
const randomNumber = getRandomNumber(-5, 13);
if (randomNumber % 2 === 0) {
firstPlayer.score += 1;
console.log(
`Round #${i}, random number is ${randomNumber}, ${firstPlayer.name} scored!`
);
console.log(
`Status: ${firstPlayer.name} ${firstPlayer.score}, ${secondPlayer.name} ${secondPlayer.score}`
);
if (firstPlayer.score === Math.floor(numberOfPlayers / 2) + 1) {
console.log(`${firstPlayer.name} Wins!`);
firstPlayer.wins += 1;
if (firstPlayer.wins === Math.floor(numberOfPlayers / 2) + 1) {
winner = firstPlayer;
console.log(`the winner is ${firstPlayer.name} with ${firstPlayer.wins} wins`);
}
break;
}
} else if (randomNumber % 2 !== 0) {
secondPlayer.score += 1;
console.log(
`Round #${i}, random number is ${randomNumber}, ${secondPlayer.name} scored!`
);
console.log(
`Status: ${firstPlayer.name} ${firstPlayer.score}, ${secondPlayer.name} ${secondPlayer.score}`
);
if (secondPlayer.score === Math.floor(numberOfPlayers / 2) + 1) {
console.log(`${secondPlayer.name} Wins!`);
secondPlayer.wins += 1;
if (secondPlayer.wins === Math.floor(numberOfPlayers / 2) + 1) {
winner = secondPlayer;
console.log(`the winner is ${secondPlayer.name} with ${secondPlayer.wins} wins`);
}
break;
}
}
}
}
while (winner === null) {
playRound()
}