tron.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. require 'socket'
  2. require 'yaml/store'
  3. config = YAML::Store.new "config.store"
  4. config.transaction do
  5. $name = config["name"]
  6. $password = config["password"]
  7. $host = config["host"]
  8. $port = config["port"]
  9. $lookahead = config["lookahead"].to_i
  10. end
  11. def connect(host, port)
  12. $sock = TCPSocket.new(host, port)
  13. # join($name,$password)
  14. while line = $sock.gets
  15. parse(line)
  16. end
  17. end
  18. def join(name, password)
  19. send("join", name, password)
  20. end
  21. def parse(message)
  22. parts = message.split('|')
  23. case parts[0].strip
  24. when "error"
  25. puts "error occured " + parts[1]
  26. when "game"
  27. puts message
  28. $gameW = parts[1].to_i
  29. $gameH = parts[2].to_i
  30. $id = parts[3].to_i
  31. initmap()
  32. when "pos"
  33. $map[parts[2].to_i][parts[3].to_i] = parts[1].to_i
  34. # puts $map
  35. if parts[1].to_i === $id
  36. # store our position
  37. $myX = parts[2].to_i
  38. $myY = parts[3].to_i
  39. end
  40. when "tick"
  41. puts message
  42. decide()
  43. when "die"
  44. puts message
  45. parts.drop(1).each { |player| cleanup(player) }
  46. when "message"
  47. puts message
  48. when "win", "lose"
  49. puts message
  50. reset()
  51. when "motd"
  52. puts message
  53. join($name, $password)
  54. end
  55. end
  56. def cleanup(player)
  57. id = player.to_i
  58. puts "Removing tiles from player ##{id}"
  59. $dead.append(id)
  60. # $map.each { |row| row.each { |element| if element == id then element = -1 end}}
  61. end
  62. $tries = 0
  63. def check_front
  64. # puts "#{$direction} at #{$myX},#{$myY} - try: #{$tries}"
  65. case $direction
  66. when "up"
  67. value = $map[overflow($myX, $gameW)][overflow($myY - 1, $gameH)]
  68. when "down"
  69. value = $map[overflow($myX, $gameW)][overflow($myY + 1, $gameH)]
  70. when "left"
  71. value = $map[overflow($myX - 1, $gameW)][overflow($myY, $gameH)]
  72. when "right"
  73. value = $map[overflow($myX + 1, $gameW)][overflow($myY, $gameH)]
  74. end
  75. $tries = $tries + 1
  76. puts "check_front: #{value}"
  77. return value
  78. end
  79. def overflow(new, max)
  80. if new >= max
  81. return 0
  82. end
  83. if new < 0
  84. return max - 1
  85. end
  86. new
  87. end
  88. def turned_position(turn_direction)
  89. dir = 0
  90. front = 0
  91. case turn_direction
  92. when "left"
  93. dir = -1
  94. when "right"
  95. dir = 1
  96. when "front"
  97. front = 1
  98. dir = 0
  99. end
  100. case $direction
  101. when "up"
  102. return overflow($myX + dir, $gameW), overflow($myY - front, $gameH)
  103. when "down"
  104. return overflow($myX - dir, $gameW), overflow($myY + front, $gameH)
  105. when "left"
  106. return overflow($myX - front, $gameW), overflow($myY - dir, $gameH)
  107. when "right"
  108. return overflow($myX + front, $gameW), overflow($myY + dir, $gameH)
  109. end
  110. end
  111. def new_direction
  112. case $direction
  113. when "up"
  114. "right"
  115. when "down"
  116. "left"
  117. when "left"
  118. "up"
  119. when "right"
  120. "down"
  121. end
  122. end
  123. def turn_direction(dir)
  124. case $direction
  125. when "up"
  126. if dir == "right"
  127. "right"
  128. else
  129. "left"
  130. end
  131. when "down"
  132. if dir == "right"
  133. "left"
  134. else
  135. "right"
  136. end
  137. when "left"
  138. if dir == "right"
  139. "up"
  140. else
  141. "down"
  142. end
  143. when "right"
  144. if dir == "right"
  145. "down"
  146. else
  147. "up"
  148. end
  149. end
  150. end
  151. def check_area(direction)
  152. $fillmap = Array.new($gameH) { Array.new($gameW) { -1 } }
  153. (0...$gameH).each do |i|
  154. (0...$gameW).each { |j| $fillmap[i][j] = $map[i][j] }
  155. end
  156. x, y = turned_position(direction)
  157. fill(x, y, 0)
  158. end
  159. def decide()
  160. lastdirection = $direction
  161. # check area front
  162. front = check_area("front")
  163. # check area left
  164. left = check_area("left")
  165. # check area right
  166. right = check_area("right")
  167. if left > front && left > right
  168. puts "#{left} > #{front} && #{left} > #{right} -> left"
  169. $direction = turn_direction("left")
  170. else
  171. if right > front && right > left
  172. puts "#{right} > #{front} && #{right} > #{left} -> right"
  173. $direction = turn_direction("right")
  174. end
  175. end
  176. # if check_front > -1 && !$dead.include?(check_front) || check_area("front") < 10
  177. #
  178. # if left > right
  179. # $direction = turn_direction("left")
  180. # else
  181. # if left == right
  182. # case $lastturn
  183. # when "left"
  184. # $direction = turn_direction("right")
  185. # $lastturn = "right"
  186. # when "right"
  187. # $direction = turn_direction("left")
  188. # $lastturn = "left"
  189. # end
  190. # else
  191. # $direction = turn_direction("right")
  192. # end
  193. # end
  194. #
  195. # puts "#{left} > #{right}: turned #{$direction}"
  196. # end
  197. # $direction = new_direction()
  198. # if $tries > 4
  199. # break
  200. # end
  201. # end
  202. if check_front > -1 && !$dead.include?(check_front)
  203. chat("To honor the Fallen")
  204. $direction = lastdirection
  205. end
  206. send("move", $direction)
  207. end
  208. # $tries = 0
  209. # while check_front > -1 && !$dead.include?(check_front)
  210. def chat(message)
  211. send("chat", message)
  212. end
  213. def reset() end
  214. def send(msg, *param)
  215. if param.length > 0
  216. snd = "#{msg}|#{param.join("|")}"
  217. else
  218. snd = msg
  219. end
  220. $sock.puts(snd)
  221. puts(snd)
  222. end
  223. def initmap()
  224. $map = Array.new($gameH) { Array.new($gameW) { -1 } }
  225. $direction = "up"
  226. $lastturn = "left"
  227. $dead = Array.new(0)
  228. chat("Rock and Stone!")
  229. end
  230. def fill(x, y, depth)
  231. x = overflow(x, $gameW)
  232. y = overflow(y, $gameH)
  233. tile = $fillmap[x][y]
  234. if depth > $lookahead
  235. return 0
  236. end
  237. if tile > -1 && !$dead.include?(tile)
  238. return 0
  239. end
  240. $fillmap[x][y] = 42 # mark as visited
  241. sum = 1
  242. sum += fill(x - 1, y, depth + 1)
  243. sum += fill(x + 1, y, depth + 1)
  244. sum += fill(x, y - 1, depth + 1)
  245. sum += fill(x, y + 1, depth + 1)
  246. return sum
  247. end
  248. connect($host, $port)