Procházet zdrojové kódy

Floodfill detection

Dorian Snowball před 2 roky
rodič
revize
3e833a4e8c
2 změnil soubory, kde provedl 107 přidání a 6 odebrání
  1. 1 0
      config.store
  2. 106 6
      lib/tron.rb

+ 1 - 0
config.store

@@ -3,3 +3,4 @@ name: tron.rb
 password: ForKarl!
 host: gpn-tron.duckdns.org
 port: 4000
+lookahead: 10

+ 106 - 6
lib/tron.rb

@@ -7,6 +7,7 @@ config.transaction do
   $password = config["password"]
   $host = config["host"]
   $port = config["port"]
+  $lookahead = config["lookahead"].to_i
 end
 
 def connect(host, port)
@@ -64,6 +65,7 @@ def cleanup(player)
 end
 
 $tries = 0
+
 def check_front
 
   # puts "#{$direction} at #{$myX},#{$myY} - try: #{$tries}"
@@ -92,6 +94,31 @@ def overflow(new, max)
   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"
@@ -105,19 +132,72 @@ def new_direction
   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()
 
-  $tries = 0
-  while check_front > -1 && !$dead.include?(check_front)
-    $direction = new_direction()
-    if $tries > 4
-      break
+  # check area left
+  left = check_area("left")
+  # check area right
+  right = check_area("right")
+
+  if check_front > -1 && !$dead.include?(check_front) || check_area("front") < 10
+    if left > right
+      $direction = turn_direction("left")
+    else
+      $direction = turn_direction("right")
     end
+    puts "#{left} > #{right}: turned #{$direction}"
   end
+  #   $direction = new_direction()
+  #   if $tries > 4
+  #     break
+  #   end
+  # end
 
   send("move", $direction)
 end
 
+# $tries = 0
+# while check_front > -1 && !$dead.include?(check_front)
+
 def chat(message)
   send("chat", message)
 end
@@ -143,7 +223,27 @@ def initmap()
   chat("Rock and Stone!")
 end
 
+def fill(x, y, depth)
+  x = overflow(x, $gameW)
+  y = overflow(y, $gameH)
 
+  tile = $fillmap[x][y]
 
-connect($host, $port)
+  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)