Skip to content

Instantly share code, notes, and snippets.

@JEG2
Created August 19, 2010 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JEG2/538610 to your computer and use it in GitHub Desktop.
Save JEG2/538610 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -wKU
def program_moves(map, moves, modes)
%w[L L R R balance] # FIXME: replace with bot logic
end
require "socket"
# USAGE: bot [-r] [BOT_NAME [BOT_PASSWORD [HOST]]]
register = ARGV.delete("-r")
name = ARGV.shift || File.basename(__FILE__, ".rb")
password = ARGV.shift || "pass4#{name}"
host = ARGV.shift || "play.cobots.org"
played = false
reading_map = false
map = nil
moves = nil
modes = nil
server = TCPSocket.new(host, 23)
server.each do |line|
line.chomp!
if line =~ /\A!\s*ERROR\s+(.+?)\s*!\Z/
abort "ERROR: #{$1}"
elsif line =~ /\A\?\s*register\|login\b/
server.puts "#{register ? 'register' : 'login'} #{name} #{password}"
elsif line =~ /\A\?\s*fight\|quit\b/
if played
server.puts "quit"
else
server.puts "fight"
played = true
end
elsif line =~ /\A\?\s*move\b/
server.puts "move #{program_moves(map, moves, modes).join(' ')}"
elsif line =~ /\A!\s*BEGIN_MAP\b/
map = ""
reading_map = true
elsif line =~ /\A!\s*END_MAP\b/
puts map
reading_map = false
elsif reading_map
map << line << "\n"
elsif line =~ /\A!\s*PROGRAM\s+moves:(\S+)\s+modes:(\S+)\b/
puts line
moves, modes = [$1, $2].map { |list| list.split("|") }
elsif line =~ /\A!\s*(?:BATTLE|MOVES|TURN|WINNER)\b/
puts line
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment