class AresHackathon

Constants

CONNECTION_LOST
DEFEAT
ONGOING
VICTORY

Attributes

field[R]
id[R]
name[R]
status[R]

Public Class Methods

new() click to toggle source
# File lib/ares-hackathon.rb, line 14
def initialize
  @status = ONGOING
end

Public Instance Methods

add_unit(c) click to toggle source
# File lib/ares-hackathon.rb, line 58
def add_unit(c)
  if units_remained > 0 then
    @units << c
  end
end
add_units(c, count) click to toggle source
# File lib/ares-hackathon.rb, line 70
def add_units(c, count)
  for i in 1..count
    add_unit c
  end
end
add_units_list(cells) click to toggle source
# File lib/ares-hackathon.rb, line 64
def add_units_list(cells)
  cells.each do | cell |
    add_unit cell
  end
end
attack(fromX, fromY, toX, toY) click to toggle source
# File lib/ares-hackathon.rb, line 35
def attack(fromX, fromY, toX, toY)
  buffer = [fromX, fromY, toX, toY].pack('C*')
  @conn.send buffer, 0

  if fromX == 255 && fromY == 255 && toX == 255 && toY == 255 then
    return
  end

  @field = AresHackathon::Field.read_from_socket @conn
end
cell(x,y) click to toggle source
# File lib/ares-hackathon.rb, line 100
def cell(x,y)
  return @field.cell(x,y)
end
connect(url, name) click to toggle source
# File lib/ares-hackathon.rb, line 18
def connect(url, name)
  s = url.split ":"
  ip = s[0]
  port = s.length > 1 ? s[1].to_i : 1337

  @conn = TCPSocket.new ip, port
  name = name[0..24]
  name = name+"\0"

  @conn.send name, 0
  id = @conn.recv(1)

  @id = id.ord
  @name = name[0..24]
end
end_adding_units() click to toggle source
# File lib/ares-hackathon.rb, line 76
def end_adding_units
  buffer = []
  for i in 0..(@total_units-1)
    if i < @units.length then
      buffer << @units[i].x
      buffer << @units[i].y
    else
      buffer << 255
      buffer << 255
    end
  end
  send_buffer = buffer.pack("C*")
  @conn.write send_buffer

  @field = AresHackathon::Field.read_from_socket @conn
end
end_attacks() click to toggle source
# File lib/ares-hackathon.rb, line 46
def end_attacks
  attack(255,255,255,255)

  buffer = @conn.recv 1
  @total_units = buffer[0].ord
  @units = []
end
map() click to toggle source
# File lib/ares-hackathon.rb, line 108
def map
  return @field
end
my_cells() click to toggle source
# File lib/ares-hackathon.rb, line 104
def my_cells
  return @field.owned_by @id
end
next_turn() click to toggle source
# File lib/ares-hackathon.rb, line 93
def next_turn
  @field = AresHackathon::Field.read_from_socket @conn

  @status = VICTORY if @field.has_win @id
  @status = DEFEAT if @field.has_lost @id
end
units_remained() click to toggle source
# File lib/ares-hackathon.rb, line 54
def units_remained
  return @total_units - @units.length
end