-
Notifications
You must be signed in to change notification settings - Fork 1
/
CHaserConnect.rb
433 lines (384 loc) · 10.7 KB
/
CHaserConnect.rb
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# -*-coding: utf-8 -*-
# CHaserConnect.rb
# CHaserクライアント通信プログラム
# Author:: Nobuyuki SAKAI
# Date:: 2013/06
# Update:: 2013/09/08 Nobuyuki SAKAI Toメソッドを追加
# これは書き換えない
require 'socket'
# CHaserConnect
# CHaserクライアント通信クラス
class CHaserConnect
# 定数
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
WALK = 0
SEARCH = 1
LOOK = 2
PUT = 3
# インスタンス生成
# Param:: name(名前), port(ポート番号), host(IPアドレス,ホスト名)
def initialize(name, port, host = 'localhost')
begin
if port == 2009 || port == 2010
@name = name #.encode("Shift_JIS")
else
@name = name.encode("Shift_JIS")
end
@host = host
@port = port
@socket = TCPSocket.open(host, port) # TCPソケットを開く
@socket.puts(@name) # 名前を送信
rescue
puts "\"#{@name.encode("UTF-8")}\"はサーバに接続出来ませんでした"
puts "サーバが起動しているかどうか or ポート番号、IPアドレスを確認してください"
else
puts "\"#{@name.encode("UTF-8")}\"はサーバに接続しました"
end
end
# 準備信号
# Return:: 制御情報と周囲情報
def getReady()
begin
puts "\"#{@name.encode("UTF-8")}\"はgetReadyをサーバに送信"
msg = @socket.gets
@socket.write("gr\r\n")
msg = @socket.gets
results = parseInt(msg)
return results
rescue
puts "\"#{@name.encode("UTF-8")}\"はgetReadyをサーバに送信できませんでした"
end
end
# 右に進む
# Return:: 制御情報と周囲情報
def walkRight()
puts "\"#{@name.encode("UTF-8")}\"はwalkRightをサーバに送信"
@socket.write("wr\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 左に進む
# Return:: 制御情報と周囲情報
def walkLeft()
puts "\"#{@name.encode("UTF-8")}\"はwalkLeftをサーバに送信"
@socket.write("wl\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 上に進む
# Return:: 制御情報と周囲情報
def walkUp()
puts "\"#{@name.encode("UTF-8")}\"はwalkUpをサーバに送信"
@socket.write("wu\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 下に進む
# Return:: 制御情報と周囲情報
def walkDown()
puts "\"#{@name.encode("UTF-8")}\"はwalkDownをサーバに送信"
@socket.write("wd\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 右を見る
# Return:: 制御情報と周囲情報
def lookRight()
puts "\"#{@name.encode("UTF-8")}\"はlookRightをサーバに送信"
@socket.write("lr\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 左を見る
# Return:: 制御情報と周囲情報
def lookLeft()
puts "\"#{@name.encode("UTF-8")}\"はlookLeftをサーバに送信"
@socket.write("ll\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 上を見る
# Return:: 制御情報と周囲情報
def lookUp()
puts "\"#{@name.encode("UTF-8")}\"はlookUpをサーバに送信"
@socket.write("lu\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 下を見る
# Return:: 制御情報と周囲情報
def lookDown()
puts "\"#{@name.encode("UTF-8")}\"はlookDownをサーバに送信"
@socket.write("ld\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 右を見る(直線9マス)
# Return:: 制御情報と周囲情報
def searchRight()
puts "\"#{@name.encode("UTF-8")}\"はsearchRightをサーバに送信"
@socket.write("sr\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 左を見る(直線9マス)
# Return:: 制御情報と周囲情報
def searchLeft()
puts "\"#{@name.encode("UTF-8")}\"はsearchLeftをサーバに送信"
@socket.write("sl\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 上を見る(直線9マス)
# Return:: 制御情報と周囲情報
def searchUp()
puts "\"#{@name.encode("UTF-8")}\"はsearchUpをサーバに送信"
@socket.write("su\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 下を見る(直線9マス)
# Return:: 制御情報と周囲情報
def searchDown()
puts "\"#{@name.encode("UTF-8")}\"はsearchDownをサーバに送信"
@socket.write("sd\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 右にブロックを置く
# Return:: 制御情報と周囲情報
def putRight()
puts "\"#{@name.encode("UTF-8")}\"はputRightをサーバに送信"
@socket.write("pr\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 左にブロックを置く
# Return:: 制御情報と周囲情報
def putLeft()
puts "\"#{@name.encode("UTF-8")}\"はputLeftをサーバに送信"
@socket.write("pl\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 上にブロックを置く
# Return:: 制御情報と周囲情報
def putUp()
puts "\"#{@name.encode("UTF-8")}\"はputUpをサーバに送信"
@socket.write("pu\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 下にブロックを置く
# Return:: 制御情報と周囲情報
def putDown()
puts "\"#{@name.encode("UTF-8")}\"はputDownをサーバに送信"
@socket.write("pd\r\n")
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 引数_direction_で指定した方向へ進む
# Param:: direction(方向)
# Return:: 制御情報と周囲情報を返す
def walkTo(direction)
tmp = Array.new(10)
case direction
when "up", 0
tmp = walkUp
when "down", 1
tmp = walkDown
when "left", 2
tmp = walkLeft
when "right", 3
tmp = walkRight
else
puts "指定された引数は使えません"
end
return tmp
end
# 引数_direction_で指定した方向へlookする
# Param:: direction(方向)
# Return:: 制御情報と周囲情報を返す
def lookTo(direction)
tmp = Array.new(10)
case direction
when "up", 0
tmp = lookUp
when "down", 1
tmp = lookDown
when "left", 2
tmp = lookLeft
when "right", 3
tmp = lookRight
else
puts "指定された引数は使えません"
end
return tmp
end
# 引数_direction_で指定した方向へsearchする
# Param:: direction(方向)
# Return:: 制御情報と周囲情報を返す
def searchTo(direction)
tmp = Array.new(10)
case direction
when "up", 0
tmp = searchUp
when "down", 1
tmp = searchDown
when "left", 2
tmp = searchLeft
when "right", 3
tmp = searchRight
else
puts "指定された引数は使えません"
end
return tmp
end
# 引数_direction_で指定した方向へputする
# Param:: direction(方向)
# Return:: 制御情報と周囲情報を返す
def putTo(direction)
tmp = Array.new(10)
case direction
when "up", 0
tmp = putUp
when "down", 1
tmp = putDown
when "left", 2
tmp = putLeft
when "right", 3
tmp = putRight
else
puts "指定された引数は使えません"
end
return tmp
end
# 引数_kind_でコマンドを指定し引数_direction_で方向を指定する
# Param:: kind(コマンドの種類), direction(方向)
# Return:: 制御情報と周囲情報を返す
def command(kind, direction)
case kind
when "walk", 0
case direction
when "up", 0
@socket.write("wu\r\n")
when "down", 1
@socket.write("wd\r\n")
when "left", 2
@socket.write("wl\r\n")
when "right", 3
@socket.write("wr\r\n")
end
when "search", 1
case direction
when "up", 0
@socket.write("su\r\n")
when "down", 1
@socket.write("sd\r\n")
when "left", 2
@socket.write("sl\r\n")
when "right", 3
@socket.write("sr\r\n")
end
when "look", 2
case direction
when "up", 0
@socket.write("lu\r\n")
when "down", 1
@socket.write("ld\r\n")
when "left", 2
@socket.write("ll\r\n")
when "right", 3
@socket.write("lr\r\n")
end
when "put", 3
case direction
when "up", 0
@socket.write("pu\r\n")
when "down", 1
@socket.write("pd\r\n")
when "left", 2
@socket.write("pl\r\n")
when "right", 3
@socket.write("pr\r\n")
end
end
msg = @socket.gets
results = parseInt(msg)
@socket.write("\#\r\n")
return results
end
# 文字列を整数配列に変換
# Param:: str(サーバから返ってきた制御情報と周囲情報の文字列)
# Return:: 文字列を整数列に変換した配列
def parseInt(str)
results = [9,9,9,9,9,9,9,9,9,9]
for i in 0..9
results[i] = str[i].to_i
print results[i]
end
print "\n"
return results
end
# ソケットを閉じる
def close
@socket.close
end
end
# テスト用
# "CHaserConnect.rb"自体を実行すると先攻としてサーバに接続
if __FILE__ == $0
#----必要に合わせてコメントアウトで切り替える
# 1byte文字版
target = CHaserConnect.new("test", 2010, 'localhost')
# 2byte文字版 エンコードの問題のチェック(文字化け等々)
#target = CHaserConnect.new("テスト", 40000, 'localhost')
#----
values = Array.new(10)
loop do
values = target.getReady
if values[0] == 0
break
end
values = target.command(CHaserConnect::SEARCH, CHaserConnect::UP)
if values[0] == 0
break
end
end
target.close
end