Procházet zdrojové kódy

We got a wingit add lib/tron.rb !

Dorian Snowball před 2 roky
rodič
revize
15e032ffd9
1 změnil soubory, kde provedl 94 přidání a 20 odebrání
  1. 94 20
      lib/tron.rb

+ 94 - 20
lib/tron.rb

@@ -1,31 +1,30 @@
 require 'socket'
 require 'yaml/store'
 
-config = YAML::Store.new "../config.store"
+config = YAML::Store.new "config.store"
 config.transaction do
-  $name =  config["name"]
+  $name = config["name"]
   $password = config["password"]
   $host = config["host"]
   $port = config["port"]
 end
 
-
-def connect(host,port)
-  $sock = TCPSocket.new(host,port)
-  #join($name,$password)
+def connect(host, port)
+  $sock = TCPSocket.new(host, port)
+  # join($name,$password)
   while line = $sock.gets
     puts line
     parse(line)
   end
 end
 
-def join(name,password)
-  send("join",name,password)
+def join(name, password)
+  send("join", name, password)
 end
 
 def parse(message)
   parts = message.split('|')
-  case parts[0]
+  case parts[0].strip
   when "error"
     puts "error occured " + parts[1]
 
@@ -36,40 +35,115 @@ def parse(message)
     initmap()
   when "pos"
     $map[parts[2].to_i][parts[3].to_i] = parts[1].to_i
-    #puts $map
+    # puts $map
+    if parts[1].to_i === $id
+      # store our position
+      $myX = parts[2].to_i
+      $myY = parts[3].to_i
+    end
   when "tick"
     decide()
   when "die"
-    
+    parts.drop(1).each { |player| cleanup(player) }
   when "message"
 
   when "win", "lose"
     reset()
   when "motd"
-    join($name,$password)
+    join($name, $password)
   end
+
 end
 
-def decide()
-  send("up")
+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
 
-def chat(message)
+$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 new_direction
+  case $direction
+  when "up"
+    "right"
+  when "down"
+    "left"
+  when "left"
+    "up"
+  when "right"
+    "down"
+  end
+end
+
+def decide()
 
+  $tries = 0
+  while check_front > -1 && !$dead.include?(check_front)
+    $direction = new_direction()
+    if $tries > 4
+      break
+    end
+  end
+
+  send("move", $direction)
 end
 
-def reset()
+def chat(message)
+  send("chat", message)
 end
 
-def send(msg,*param)
+def reset() end
+
+def send(msg, *param)
 
-  snd = "#{msg}|#{param.join("|")}"
+  if param.length > 0
+    snd = "#{msg}|#{param.join("|")}"
+  else
+    snd = msg
+  end
   $sock.puts(snd)
   puts(snd)
 end
+
 def initmap()
-  $map = Array.new($gameH) {Array.new($gameW)}
+  $map = Array.new($gameH) { Array.new($gameW) { -1 } }
+  $direction = "up"
+  $dead = Array.new(0)
+
+  chat("Rock and Stone!")
 end
-connect($host,$port)
+
+
+
+connect($host, $port)