require 'socket' require 'yaml/store' config = YAML::Store.new "config.store" config.transaction do $name = config["name"] $password = config["password"] $host = config["host"] $port = config["port"] $lookahead = config["lookahead"].to_i end $dirs = Queue.new $ticks = 0 def connect(host, port) $sock = TCPSocket.new(host, port) # join($name,$password) while line = $sock.gets parse(line) end end def join(name, password) send("join", name, password) end def parse(message) parts = message.split('|') case parts[0].strip when "error" puts "error occured " + parts[1] when "game" puts "#{Time.now.strftime('%M%S%L')}: #{message}" $gameW = parts[1].to_i $gameH = parts[2].to_i $id = parts[3].to_i initmap() when "pos" $map[parts[2].to_i][parts[3].to_i] = parts[1].to_i # puts $map if parts[1].to_i === $id # store our position $myX = parts[2].to_i $myY = parts[3].to_i end when "tick" puts "#{Time.now.strftime('%M%S%L')}: #{message}" puts "tick took: #{Time.now.strftime('%M%S%L').to_i - $lasttick}" $lasttick = Time.now.strftime('%M%S%L').to_i decide() when "die" puts "#{Time.now.strftime('%M%S%L')}: #{message}" parts.drop(1).each { |player| cleanup(player) } when "message" puts "#{Time.now.strftime('%M%S%L')}: #{message}" when "win", "lose" puts "#{Time.now.strftime('%M%S%L')}: #{message}" reset() when "motd" puts "#{Time.now.strftime('%M%S%L')}: #{message}" join($name, $password) end end def cleanup(player) id = player.to_i puts "Removing tiles from player ##{id}" $dead.append(id) # $map.each { |row| row.each { |element| if element == id then element = -1 end}} end $tries = 0 def check_front # puts "#{$direction} at #{$myX},#{$myY} - try: #{$tries}" case $direction when "up" value = $map[overflow($myX, $gameW)][overflow($myY - 1, $gameH)] when "down" value = $map[overflow($myX, $gameW)][overflow($myY + 1, $gameH)] when "left" value = $map[overflow($myX - 1, $gameW)][overflow($myY, $gameH)] when "right" value = $map[overflow($myX + 1, $gameW)][overflow($myY, $gameH)] end $tries = $tries + 1 puts "check_front: #{value}" return value end def overflow(new, max) if new >= max return 0 end if new < 0 return max - 1 end new end def turned_position(turn_direction) dir = 0 front = 0 case turn_direction when "left" dir = -1 when "right" dir = 1 when "front" front = 1 dir = 0 end case $direction when "up" return overflow($myX + dir, $gameW), overflow($myY - front, $gameH) when "down" return overflow($myX - dir, $gameW), overflow($myY + front, $gameH) when "left" return overflow($myX - front, $gameW), overflow($myY - dir, $gameH) when "right" return overflow($myX + front, $gameW), overflow($myY + dir, $gameH) end end def new_direction case $direction when "up" "right" when "down" "left" when "left" "up" when "right" "down" end end def turn_direction(dir) case $direction when "up" if dir == "right" "right" else "left" end when "down" if dir == "right" "left" else "right" end when "left" if dir == "right" "up" else "down" end when "right" if dir == "right" "down" else "up" end end end def check_area(direction) $fillmap = Array.new($gameH) { Array.new($gameW) { -1 } } (0...$gameH).each do |i| (0...$gameW).each { |j| $fillmap[i][j] = $map[i][j] } end x, y = turned_position(direction) fill(x, y, 0) end def decide() $ticks = $ticks + 1 lastdirection = $direction if $dirs.length > 0 puts "in queue" $direction = $dirs.pop if check_front > -1 && !$dead.include?(check_front) #chat("You crossed my Plans!") $direction = lastdirection $dirs.clear() else send("move", $direction) return end end # check area front front = check_area("front") # check area left left = check_area("left") # check area right right = check_area("right") if($ticks < 30) if left > front && left > right puts "#{left} > #{front} && #{left} > #{right} -> left" $direction = turn_direction("left") $dirs << "left" puts "qued left" else if right > front && right > left puts "#{right} > #{front} && #{right} > #{left} -> right" $direction = turn_direction("right") $dirs << "right" puts "qued right" end end else if check_front > -1 && !$dead.include?(check_front) || check_area("front") < 10 if left > right $direction = turn_direction("left") else if left == right case $lastturn when "left" $direction = turn_direction("right") $lastturn = "right" $dirs << "right" when "right" $direction = turn_direction("left") $lastturn = "left" $dirs << "left" end else $direction = turn_direction("right") end end puts "#{left} > #{right}: turned #{$direction}" end end #$direction = new_direction() #if $tries > 4 # break #end if check_front > -1 && !$dead.include?(check_front) chat("To honor the Fallen") $direction = lastdirection end send("move", $direction) end # $tries = 0 # while check_front > -1 && !$dead.include?(check_front) def chat(message) send("chat", message) end def reset() end def send(msg, *param) if param.length > 0 snd = "#{msg}|#{param.join("|")}" else snd = msg end $sock.puts(snd) puts("#{Time.now.strftime('%M%S%L')}: #{snd}") end def initmap() $map = Array.new($gameH) { Array.new($gameW) { -1 } } $direction = "up" $lastturn = "left" $dead = Array.new(0) $lasttick = 0 chat("Rock and Stone!") end def fill(x, y, depth) x = overflow(x, $gameW) y = overflow(y, $gameH) tile = $fillmap[x][y] if depth > $lookahead return 0 end if tile > -1 && !$dead.include?(tile) return 0 end $fillmap[x][y] = 42 # mark as visited sum = 1 sum += fill(x - 1, y, depth + 1) sum += fill(x + 1, y, depth + 1) sum += fill(x, y - 1, depth + 1) sum += fill(x, y + 1, depth + 1) return sum end connect($host, $port)