/
main.dart
104 lines (82 loc) · 3.17 KB
/
main.dart
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
import 'dart:html';
import 'package:english_words/english_words.dart' as engWords;
import 'package:hangman/src/hangman.dart';
const List<String> imageList = const [
"https://i.imgur.com/kReMv94.png",
"https://i.imgur.com/UFP8RM4.png",
"https://i.imgur.com/9McnEXg.png",
"https://i.imgur.com/vNAW0pa.png",
"https://i.imgur.com/8UFWc9q.png",
"https://i.imgur.com/rHCgIvU.png",
"https://i.imgur.com/CtvIEMS.png",
"https://i.imgur.com/Z2mPdX0.png"
];
const String winImage = "https://i.imgur.com/QYKuNwB.png";
//const List<String> wordList = const ["PLENTY","ACHIEVE","CLASS","STARE","AFFECT","THICK","CARRIER","BILL","SAY","ARGUE","OFTEN","GROW","VOTING","SHUT","PUSH","FANTASY","PLAN","LAST","ATTACK","COIN","ONE","STEM","SCAN","ENHANCE","PILL","OPPOSED","FLAG","RACE","SPEED","BIAS","HERSELF","DOUGH","RELEASE","SUBJECT","BRICK","SURVIVE","LEADING","STAKE","NERVE","INTENSE","SUSPECT","WHEN","LIE","PLUNGE","HOLD","TONGUE","ROLLING","STAY","RESPECT","SAFELY"];
final List<String> wordList = engWords.all.where((String word) => word.length > 2 && word.length < 8).map((String word) => word.toUpperCase()).toList();
HangmanGame game = new HangmanGame(wordList);
void main() {
// set up event listeners
game.onChange.listen(updateWordDisplay);
game.onWrong.listen(updateGallowsImage);
game.onWin.listen(win);
game.onLose.listen(gameOver);
newGameRef.onClick.listen(newGame);
// put the letter buttons on the screen
createLetterButtons();
// start the first game
newGame();
}
void newGame([_]) {
// set up the game model
game.newGame();
// reset the letter buttons
enableLetterButtons();
// hide the New Game button
newGameRef.hidden = true;
// show the first gallows image
updateGallowsImage(0);
}
void updateWordDisplay(String word) {
wordRef.text = word;
}
void updateGallowsImage(int wrongGuesses) {
gallowsRef.src = imageList[wrongGuesses];
}
void win([_]) {
gallowsRef.src = winImage;
gameOver();
}
void gameOver([_]) {
enableLetterButtons(false);
newGameRef.hidden = false;
}
void createLetterButtons() {
// add letter buttons to the DOM
generateAlphabet().forEach((String letter) {
lettersRef.append(new ButtonElement()
..classes.add("letter-btn")
..text = letter
..onClick.listen((MouseEvent event) {
(event.target as ButtonElement).disabled = true;
game.guessLetter(letter);
})
);
});
}
void enableLetterButtons([bool enable = true]) {
// enable/disable all letter buttons
lettersRef.children.forEach((Element btn) => (btn as ButtonElement).disabled = !enable);
}
List<String> generateAlphabet() {
// get the character code for a capital 'A'
int startingCharCode = 'A'.codeUnits.first;
// create a list of 26 character codes (from 'A' to 'Z')
List<int> charCodes = new List<int>.generate(26, (int index) => startingCharCode + index);
// map character codes into a list of actual strings
return charCodes.map((int code) => new String.fromCharCode(code)).toList();
}
ImageElement get gallowsRef => querySelector("#gallows");
DivElement get wordRef => querySelector("#word");
DivElement get lettersRef => querySelector("#letters");
ButtonElement get newGameRef => querySelector("#new-game");