-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
193 lines (168 loc) · 4.52 KB
/
app.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict";
var request = require('request');
function App(){
var self = this;
this.game = null;
this.player = 0;
this.speech = function( speech ) {
speech.triggers.forEach(function(trigger){
if (trigger.id == 'startTictactoe') {
self.game = new Game();
self.speak("Starting new game, X, go ahead!");
self.player = 1;
} else if (trigger.id == 'moveOne') {
self.move(1);
} else if (trigger.id == 'moveTwo') {
self.move(2);
} else if (trigger.id == 'moveThree') {
self.move(3);
} else if (trigger.id == 'moveFour') {
self.move(4);
} else if (trigger.id == 'moveFive') {
self.move(5);
} else if (trigger.id == 'moveSix') {
self.move(6);
} else if (trigger.id == 'moveSeven') {
self.move(7);
} else if (trigger.id == 'moveEight') {
self.move(8);
} else if (trigger.id == 'moveNine') {
self.move(9);
} else if( trigger.id == 'giveinfo' ) {
var transcript = speech.transcript;
getInformation(transcript.replace("give information about ", ""));
}
});
}
this.move = function(field) {
if (self.game == null) {
self.speak("You must construct additional pylons, or start a game first.");
return;
}
if (self.game.doMove(self.game.convertField(field).x, self.game.convertField(field).y, self.player)) {
self.speak("Affirmative");
self.player = (self.player == 1) ? 2 : 1;
} else {
self.speak("I can't let you do that Dave!");
}
if (self.game.checkWin(1)) {
self.speak("X wins");
self.game = null;
} else if (self.game.checkWin(2)) {
self.speak("O wins");
self.game = null;
} else if (self.game.checkFull()) {
self.speak("Tie. Game over.");
self.game = null;
} else {
if (self.player == 1) {
Homey.manager('ledring').animate({
name: 'pulse',
color: 'red'
});
self.speak("X, go ahead!");
} else {
Homey.manager('ledring').animate({
name: 'pulse',
color: 'green'
});
self.speak("O, go ahead!");
}
}
}
this.speak = function(text) {
Homey.log(text);
Homey.manager('speech-output').say(text);
}
}
function speakAndLog(text, log){
Homey.manager('speech-output').say(text);
if (log){
Homey.log(text);
}
}
function toTitleCase(str)
{
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
function rollDie(numberOfSides){
return Math.floor(Math.random()*numberOfSides + 1, 0)
}
function getInformation(name){
name = toTitleCase(name);
request.get("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles="+name
, function(error, response, body){
var object = JSON.parse(body);
if(object.query != undefined){
if(object.query.pages != undefined){
if(Object.keys(object.query.pages).length > 0){
var extract = object.query.pages[Object.keys(object.query.pages)[0]].extract;
if(extract != undefined){
speakAndLog("Some information about "+ name + ". \n" + extract.split("\n")[0], true);
} else {
speakAndLog("Some information about "+ name + ". \n" + extract, true);
}
} else {
speakAndLog("No info found!", true);
}
} else {
speakAndLog("No info found!", true);
}
} else {
speakAndLog("No info found!", true);
}
});
}
function Game() {
var self = this;
this.field = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
this.convertField = function(field) {
field -= 1;
return { 'x': field % 3, 'y': Math.floor(field / 3) };
}
this.doMove = function(x, y, value) {
var returnval = false;
if (returnval = (self.field[x][y] == 0)) self.field[x][y] = value;
return returnval;
}
this.checkWin = function(player) {
var f = self.field;
for (var y = 0; y < f.length; y++) {
var row = true;
for (var x = 0; x < f[y].length; x++) {
row = row && (f[y][x] == player);
}
if (row) return true;
}
for (var x = 0; x < f.length; x++) {
var col = true;
for (var y = 0; y < f[x].length; y++) {
col = col && (f[y][x] == player);
}
if (col) return true;
}
var diagltrb = true;
var diagrtlb = true;
for (var a = 0; a < f.length; a++) {
diagltrb = diagltrb && (f[a][a] == player);
diagrtlb = diagrtlb && (f[a][f.length - (a + 1)] == player);
}
if (diagltrb) return true;
if (diagrtlb) return true;
return false;
}
this.checkFull = function() {
var f = self.field;
for (var y = 0; y < f.length; y++) {
for (var x = 0; x < f[y].length; x++) {
if (f[y][x] == 0) return false;
}
}
return true;
}
}
module.exports = App;