-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI.cs
363 lines (340 loc) · 11.3 KB
/
AI.cs
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.Text.RegularExpressions;
namespace SLSGAI
{
enum State
{
greeting = 1,
askingForUserName = 2,
askingForOwnName = 3
}
class AI
{
private static String unnamedAIName = "Unnamed AI";
private String name;
private int language;
private String gameName;
private mainForm form;
private SqlCeConnection dbConn;
private bool greeted;
private State state;
private Regex dbEx;
public String Name
{
get { return name; }
}
/**
* In this constructor the AI saves the form in a member, so it can communicate with the form later
* As well it builds up the connection to the database/the brain and gets all settings for
* the AI in members
**/
public AI(mainForm form)
{
this.form = form;
// Create Database connection, or as the AI says if it fails connect to its brain
dbConn = new SqlCeConnection("Data Source=\"..\\..\\brain.sdf\";");
this.name = (String)this.getPropertie("name");
this.language = Convert.ToInt32((String)this.getPropertie("standardLanguage"));
dbEx = new Regex(@"^%(?<table>\w+)_(?<id>\w+)$", RegexOptions.IgnoreCase);
// Start Zustand definieren
showMessage(upperCaseFirstLetter(getPhrase("greeting")));
}
/**
* Analyses a given message
* the return value does tell the main program, whether the AI wants to start a game or not(if it should call startGame method
**/
public void analyseMessage(String msg)
{
msg.ToLower();
String meaning = getMeaning(msg);
// Here the AI should analyse the message and take its actions
if (meaning == "test" || messageAppliesPattern(msg, "%m_execute_smt test"))
{
test();
}
else
{
if (meaning != null)
{
analyseMeaning(meaning);
}
else
{
String[] sentences = msg.Split((new char[] { '?', '!', '.' }));
String[] outputs = new String[sentences.Length];
for (int i = 0; i < sentences.Length; i++)
{
outputs[i] = analyseSentence(sentences[i]);
showMessage(outputs[i]);
}
}
}
}
private String analyseSentence(String sentence)
{
String answer = "";
String[] parts = sentence.Trim().Split(' ');
return answer;
}
private void analyseMeaning(String meaning)
{
}
private void analyseMeaning(String[] meaning)
{
}
private void startGame()
{
}
private void stopGame()
{
}
private void playGame()
{
}
private Object getPropertie(String setting)
{
Object ret = null;
SqlCeDataReader dataReader = null;
openDBConnection();
try
{
// Create the SQLCommand we want to execute
SqlCeCommand cmd = new SqlCeCommand("SELECT p.value FROM properties AS p WHERE p.setting='" + setting + "'", dbConn);
// Run the command and put the result into the reader
dataReader = cmd.ExecuteReader();
// Run over ALL the data and save the requested propertie in ret
while (dataReader.Read())
{
if (dataReader[0] is DBNull)
{
ret = null;
}
else
{
ret = dataReader[0];
}
}
}
finally
{
if (dataReader != null)
{
dataReader.Close();
}
closeDBConnection();
}
return ret;
}
private void updatePropertie(String name, String value)
{
openDBConnection();
try
{
// Create the SQLCommand we want to execute
SqlCeCommand cmd = new SqlCeCommand("UPDATE properties SET value='" + value + "' WHERE setting='" + name + "'", dbConn);
cmd.ExecuteNonQuery();
}
finally
{
closeDBConnection();
}
}
private void openDBConnection()
{
try
{
// Open the connection to the database
dbConn.Open();
}
catch
{
showMessage("I can't find my brain. Have to shutdown.", "Confused AI");
System.Threading.Thread.Sleep(5000);
form.shutdown();
}
}
private void closeDBConnection()
{
dbConn.Close();
}
private void showMessage(String msg)
{
if (this.name == null)
{
form.showMessage(msg, AI.unnamedAIName);
}
else
{
form.showMessage(msg, this.name);
}
}
private void showMessage(String msg, String name)
{
form.showMessage(msg, name);
}
private String getPhrase(String meaning)
{
String ret = null;
SqlCeDataReader dataReader = null;
openDBConnection();
try
{
String sql = @"SELECT p.text
FROM phrase AS p
INNER JOIN meaning as m
ON m.id = p.meaning
WHERE m.meaning='" + meaning + @"'
AND p.language=" + this.language;
// Create the SQLCommand we want to execute
SqlCeCommand cmd = new SqlCeCommand(sql, dbConn);
// Run the command and put the result into the reader
dataReader = cmd.ExecuteReader();
// Run over ALL the data and save the requested propertie in ret
while (dataReader.Read())
{
if (!(dataReader[0] is DBNull))
{
ret = (String)dataReader[0];
break;
}
}
}
finally
{
if (dataReader != null)
{
dataReader.Close();
}
closeDBConnection();
}
return ret;
}
private String getMeaning(String phrase)
{
String ret = null;
SqlCeDataReader dataReader = null;
openDBConnection();
try
{
String sql = @"SELECT m.meaning
FROM phrase AS p
INNER JOIN meaning as m
ON m.id = p.meaning
WHERE p.text='" + phrase + @"'
AND p.language=" + this.language;
// Create the SQLCommand we want to execute
SqlCeCommand cmd = new SqlCeCommand(sql, dbConn);
// Run the command and put the result into the reader
dataReader = cmd.ExecuteReader();
// Run over ALL the data and save the requested propertie in ret
while (dataReader.Read())
{
if (!(dataReader[0] is DBNull))
{
ret = (String)dataReader[0];
break;
}
}
}
finally
{
if (dataReader != null)
{
dataReader.Close();
}
closeDBConnection();
}
return ret;
}
private List<String> getPhrases(String meaning)
{
List<String> ret = new List<String>();
SqlCeDataReader dataReader = null;
openDBConnection();
try
{
String sql = @"SELECT p.text
FROM phrase AS p
INNER JOIN meaning as m
ON m.id = p.meaning
WHERE m.meaning='" + meaning + @"'
AND p.language=" + this.language;
// Create the SQLCommand we want to execute
SqlCeCommand cmd = new SqlCeCommand(sql, dbConn);
// Run the command and put the result into the reader
dataReader = cmd.ExecuteReader();
// Run over ALL the data and save the requested propertie in ret
while (dataReader.Read())
{
if (!(dataReader[0] is DBNull))
{
ret.Add((String)dataReader[0]);
}
}
}
finally
{
if (dataReader != null)
{
dataReader.Close();
}
closeDBConnection();
}
return ret;
}
private Boolean messageAppliesPattern(String msg, String pattern)
{
String table, argument;
String[] partsMsg = msg.Split(' ');
String[] partsPattern = pattern.Split(' ');
if (partsMsg.Length != partsPattern.Length)
{
return false;
}
for (int i = 0; partsPattern.Length > i; i++)
{
Match match = dbEx.Match(partsPattern[i]);
if (match != null)
{
table = match.Groups["table"].Value;
argument = match.Groups["id"].Value;
switch (table)
{
case "m":
List<String> temp = getPhrases(argument);
if (!temp.Contains(partsMsg[i]))
{
return false;
}
break;
}
}
else
{
if (partsPattern[i] != partsMsg[i])
{
return false;
}
}
}
return true;
}
public String upperCaseFirstLetter(String text)
{
Char[] charText = text.ToCharArray();
charText[0] = char.ToUpper(charText[0]);
return new String(charText);
}
private void test()
{
showMessage("Test erreicht!");
}
}
}