-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
131 lines (100 loc) · 3.65 KB
/
example.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
A simple example implementation of Rock, Paper, Scissors.
It subclasses TextGame and implements a new handleInput method. Users should
do the same for their own games.
*/
import { TextGame, ChoicePool } from './textgame.js';
const ROCK = "rock";
const PAPER = "paper";
const SCISSORS = "scissors";
const options = new ChoicePool([ ROCK, PAPER, SCISSORS ]);
const buildShorthandTable = (iterable) => {
/*
Build table mapping short form -> full command.
currently only uses the first character.
*/
const table = {};
iterable.forEach(command => {
table[command[0]] = command;
});
return table;
};
const shorthandTable = buildShorthandTable(options);
class RockPaperScissors extends TextGame {
chooseNextPlay() {
this.computerChoice = options.choose();
}
constructor(containerElement) {
super(containerElement);
this.chooseNextPlay();
this.computerScore = 0;
this.playerScore = 0;
this.addLine("Welcome to Rock, Paper, Scissors!");
this.showPrompt();
}
showPrompt() {
if ( this.computerScore || this.playerScore ) {
this.addLine(`Player: ${this.playerScore} Computer: ${this.computerScore}`);
}
this.addLine("Choose your move:")
}
showHelp() {
this.addLine(`You can choose from the following options:
rock - select rock as your move
paper - select paper as your move
scissors - select scissors as your move
clear - clear the screen
help - show this screen again
You can also type the first letter of rock, paper, or scissors as a shorthand way of declaring a move.
`);
}
playerWinsWhen(winCondition) {
if(winCondition) {
this.addLine("Player won!");
this.playerScore += 1;
} else {
this.addLine("Computer won!");
this.computerScore += 1;
}
}
handleInput(input) {
this.addLine(input);
let playerChoice = input.toLowerCase();
//expand shorthand to full form
if ( playerChoice in shorthandTable) {
playerChoice = shorthandTable[playerChoice];
}
if ( ! options.has(playerChoice) ) {
if (playerChoice === "help" ) {
this.showHelp();
} else if (playerChoice === "clear") {
this.clearLines();
} else {
this.addLine(`'${playerChoice}' is not a valid move. type 'help' to see available options.`);
}
} else {
this.chooseNextPlay();
this.addLine(`Player chose ${playerChoice}, Computer chose ${this.computerChoice}`);
if ( playerChoice === this.computerChoice ) {
this.addLine("DRAW!");
}
else { // someone will win.
switch(this.computerChoice) {
case ROCK:
this.playerWinsWhen(playerChoice === PAPER);
break;
case PAPER:
this.playerWinsWhen(playerChoice === SCISSORS);
break;
case SCISSORS:
this.playerWinsWhen(playerChoice === ROCK);
break;
default:
this.addLine("Something impossible happened.");
}
}
}
this.showPrompt();
}
}
const game = new RockPaperScissors(document.body);