-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.mjs
44 lines (39 loc) · 1.54 KB
/
main.mjs
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
/*
A game of rock-scissors-paper. Two strategies are available:
* Random Strategy: showing a random hand signal.
* Mirror Strategy: showing a hand signal from the previous opponent's hand signal.
*/
'use strict';
import { Player } from './player.mjs';
import { RandomStrategy } from './random-strategy.mjs';
import { MirrorStrategy } from './mirror-strategy.mjs';
import { GameResultType } from './game-result-type.mjs';
const player1 = new Player(`Emily`, new RandomStrategy());
const player2 = new Player(`James`, new MirrorStrategy());
for (let i = 0; i < 100; i++) {
const handOfPlayer1 = player1.showHandSignal();
const handOfPlayer2 = player2.showHandSignal();
// Judge win, loss, or draw
let resultOfPlayer1;
let resultOfPlayer2;
if (handOfPlayer1.isStrongerThan(handOfPlayer2)) {
console.log(`Winner: ${player1.toString()}`);
resultOfPlayer1 = GameResultType.Win;
resultOfPlayer2 = GameResultType.Loss;
}
else if (handOfPlayer2.isStrongerThan(handOfPlayer1)) {
console.log(`Winner: ${player2.toString()}`);
resultOfPlayer1 = GameResultType.Loss;
resultOfPlayer2 = GameResultType.Win;
}
else {
console.log(`Draw...`);
resultOfPlayer1 = GameResultType.Draw;
resultOfPlayer2 = GameResultType.Draw;
}
player1.notifyGameResult(resultOfPlayer1, handOfPlayer1, handOfPlayer2);
player2.notifyGameResult(resultOfPlayer2, handOfPlayer2, handOfPlayer1);
}
console.log(`RESULT:`);
console.log(player1.toString());
console.log(player2.toString());