-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscripts.js
More file actions
73 lines (67 loc) · 2.57 KB
/
scripts.js
File metadata and controls
73 lines (67 loc) · 2.57 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
70
71
72
73
function openTab(event, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
event.currentTarget.className += " active";
}
var bot = new Bot('Machine');
var user = new Player('Phuong Ngan');
var match;
function handleKeyPress(event,check = false) {
if (event.key === 'Enter' || check === true){
var messageInput = document.getElementById('message-input');
var message = messageInput.value.trim();
//console.log(match.checkAvailableWord(message));
if (message !== "" && match.checkAvailableWord(message)){
//User play
match.update(message,true);
addDivToContainer(message + ' :' + user.name,"message-container",'USER');
messageInput.value = "";
if (match.userPoint > match.pointLimit) {
window.alert(match.user.name + " is Winner");
resetGame();
}
else
{
//Bot play
var temp = match.bot.aWord(String.fromCharCode(match.currentWord.charCodeAt(match.currentWord.length-1)));
match.update(temp, false);//update match points
addDivToContainer(bot.name + ': '+ match.currentWord, 'message-container','BOT');
if (match.botPoint > match.pointLimit){
window.alert(match.bot.name + " is Winner");
resetGame();
}
}
}
messageInput.value = '';
}
document.getElementById('user-point').textContent = match.userPoint;
document.getElementById('bot-point').textContent = match.botPoint;
}
var pointLimit = 50;
function setPointLimit(event){
if (event.key == 'Enter'){
var pointInput = document.getElementById("point-limit").value.trim();
if (pointInput!== '') {
pointLimit = pointInput;
}
console.log(pointLimit);
window.alert("Đã set điểm giới hạn: " + pointLimit);
document.getElementById("point-limit").value = '';
}
}
function startGame(){
match = new Game(bot, user,pointLimit);
match.playGame();
}
function resetGame(){
document.getElementById('message-container').innerHTML = '';
startGame();
}