tron.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. end
  10. def connect(host, port)
  11. $sock = TCPSocket.new(host, port)
  12. # join($name,$password)
  13. while line = $sock.gets
  14. puts line
  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. $gameW = parts[1].to_i
  28. $gameH = parts[2].to_i
  29. $id = parts[3].to_i
  30. initmap()
  31. when "pos"
  32. $map[parts[2].to_i][parts[3].to_i] = parts[1].to_i
  33. # puts $map
  34. if parts[1].to_i === $id
  35. # store our position
  36. $myX = parts[2].to_i
  37. $myY = parts[3].to_i
  38. end
  39. when "tick"
  40. decide()
  41. when "die"
  42. parts.drop(1).each { |player| cleanup(player) }
  43. when "message"
  44. when "win", "lose"
  45. reset()
  46. when "motd"
  47. join($name, $password)
  48. end
  49. end
  50. def cleanup(player)
  51. id = player.to_i
  52. puts "Removing tiles from player ##{id}"
  53. $dead.append(id)
  54. # $map.each { |row| row.each { |element| if element == id then element = -1 end}}
  55. end
  56. $tries = 0
  57. def check_front
  58. # puts "#{$direction} at #{$myX},#{$myY} - try: #{$tries}"
  59. case $direction
  60. when "up"
  61. value = $map[overflow($myX, $gameW)][overflow($myY - 1, $gameH)]
  62. when "down"
  63. value = $map[overflow($myX, $gameW)][overflow($myY + 1, $gameH)]
  64. when "left"
  65. value = $map[overflow($myX - 1, $gameW)][overflow($myY, $gameH)]
  66. when "right"
  67. value = $map[overflow($myX + 1, $gameW)][overflow($myY, $gameH)]
  68. end
  69. $tries = $tries + 1
  70. puts "check_front: #{value}"
  71. return value
  72. end
  73. def overflow(new, max)
  74. if new >= max
  75. return 0
  76. end
  77. if new < 0
  78. return max - 1
  79. end
  80. new
  81. end
  82. def new_direction
  83. case $direction
  84. when "up"
  85. "right"
  86. when "down"
  87. "left"
  88. when "left"
  89. "up"
  90. when "right"
  91. "down"
  92. end
  93. end
  94. def decide()
  95. $tries = 0
  96. while check_front > -1 && !$dead.include?(check_front)
  97. $direction = new_direction()
  98. if $tries > 4
  99. break
  100. end
  101. end
  102. send("move", $direction)
  103. end
  104. def chat(message)
  105. send("chat", message)
  106. end
  107. def reset() end
  108. def send(msg, *param)
  109. if param.length > 0
  110. snd = "#{msg}|#{param.join("|")}"
  111. else
  112. snd = msg
  113. end
  114. $sock.puts(snd)
  115. puts(snd)
  116. end
  117. def initmap()
  118. $map = Array.new($gameH) { Array.new($gameW) { -1 } }
  119. $direction = "up"
  120. $dead = Array.new(0)
  121. chat("Rock and Stone!")
  122. end
  123. connect($host, $port)