tron.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 left
  162. left = check_area("left")
  163. # check area right
  164. right = check_area("right")
  165. if check_front > -1 && !$dead.include?(check_front) || check_area("front") < 10
  166. if left > right
  167. $direction = turn_direction("left")
  168. else
  169. if left == right
  170. case $lastturn
  171. when "left"
  172. $direction = turn_direction("right")
  173. $lastturn = "right"
  174. when "right"
  175. $direction = turn_direction("left")
  176. $lastturn = "left"
  177. end
  178. else
  179. $direction = turn_direction("right")
  180. end
  181. end
  182. puts "#{left} > #{right}: turned #{$direction}"
  183. end
  184. # $direction = new_direction()
  185. # if $tries > 4
  186. # break
  187. # end
  188. # end
  189. if check_front > -1 && !$dead.include?(check_front)
  190. chat("To honor the Fallen")
  191. $direction = lastdirection
  192. end
  193. send("move", $direction)
  194. end
  195. # $tries = 0
  196. # while check_front > -1 && !$dead.include?(check_front)
  197. def chat(message)
  198. send("chat", message)
  199. end
  200. def reset() end
  201. def send(msg, *param)
  202. if param.length > 0
  203. snd = "#{msg}|#{param.join("|")}"
  204. else
  205. snd = msg
  206. end
  207. $sock.puts(snd)
  208. puts(snd)
  209. end
  210. def initmap()
  211. $map = Array.new($gameH) { Array.new($gameW) { -1 } }
  212. $direction = "up"
  213. $lastturn = "left"
  214. $dead = Array.new(0)
  215. chat("Rock and Stone!")
  216. end
  217. def fill(x, y, depth)
  218. x = overflow(x, $gameW)
  219. y = overflow(y, $gameH)
  220. tile = $fillmap[x][y]
  221. if depth > $lookahead
  222. return 0
  223. end
  224. if tile > -1 && !$dead.include?(tile)
  225. return 0
  226. end
  227. $fillmap[x][y] = 42 # mark as visited
  228. sum = 1
  229. sum += fill(x - 1, y, depth + 1)
  230. sum += fill(x + 1, y, depth + 1)
  231. sum += fill(x, y - 1, depth + 1)
  232. sum += fill(x, y + 1, depth + 1)
  233. return sum
  234. end
  235. connect($host, $port)