-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGameChat.js
More file actions
46 lines (45 loc) · 2.13 KB
/
GameChat.js
File metadata and controls
46 lines (45 loc) · 2.13 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
function addDivToContainer(source,container,className) {
var messageContainer = document.getElementById(container);
var newDiv = document.createElement('div');
newDiv.innerHTML = source;
newDiv.className = className;
if (className === "USER") newDiv.style.textAlign = "right";
if (className === "BOT") newDiv.style.textAlign = "left";
messageContainer.appendChild(newDiv);
}
class Game {
constructor(bot, user, pointLimit) {
this.dictionary = new Dictionary();
this.dictionary.init("./words_alpha.txt");
this.usedWords = new Dictionary();
this.bot = bot;
this.user = user;
this.botPoint = 0;
this.userPoint = 0;
this.currentWord = "";
this.pointLimit = pointLimit;
}
playGame() {
this.currentWord = bot.aWord(String.fromCharCode('a'.charCodeAt(0) + rand(0, 25)));
this.botPoint += this.currentWord.length;
this.usedWords.addWord(this.currentWord);
addDivToContainer(this.bot.name + ': ' + this.currentWord,'message-container','BOT');
document.getElementById('bot-point').textContent = match.botPoint;
document.getElementById('user-point').textContent = match.userPoint;
}
checkAvailableWord(word){
// console.log(this.usedWords.findWord(word) === false );
// console.log(this.currentWord.charCodeAt(this.currentWord.length - 1) === word.charCodeAt(0));
// console.log(this.dictionary.findWord(word) === true);
return this.usedWords.findWord(word) === false
&& this.currentWord.charCodeAt(this.currentWord.length - 1) === word.charCodeAt(0)
&& this.dictionary.findWord(word) === true;
}
update(message, pointForUser){
this.currentWord = message;
this.usedWords.addWord(message);
if (pointForUser === true) this.userPoint += message.length;
else this.botPoint += message.length;
this.bot.brain.deleteWord(message);
}
}