Skip to content

Instantly share code, notes, and snippets.

@lexun
Last active January 4, 2016 07:29
Show Gist options
  • Save lexun/8589428 to your computer and use it in GitHub Desktop.
Save lexun/8589428 to your computer and use it in GitHub Desktop.
Tron Code Challenge -- codingame.com
# Work in progress
STDOUT.sync = true
def log(text, obj)
STDERR.puts "#{text}: #{obj.to_s}"
end
def time
(Time.now - $start) * 1000
end
class Game
attr_accessor :board, :hero_id, :players
def initialize
log 'init game', time
input = parse_input
@board = Board.new self
@hero_id = input.shift
@players = input.collect.with_index do |pos, id|
Player.new id, pos, board
end
hero.move
end
def step
$start = Time.now
log 'start step', time
player_moves = parse_input.drop 1
# board.update player_moves
hero.move
end
def hero
players[hero_id]
end
private
def parse_input
meta = STDIN.gets.split(' ')
input = [meta[1].to_i]
meta[0].to_i.times do
input << STDIN.gets.split(' ').pop(2)
end
log 'parsed input', input
input
end
end
class Board
attr_accessor :cells, :game
HEIGHT = 20
WIDTH = 30
def initialize(game)
log 'init board', time
@game = game
@cells = WIDTH.times.collect do |col|
HEIGHT.times.collect do |row|
Cell.new col, row, self
end
end
log 'finish board', time
end
def get_cell(x, y)
@cells[x.to_i][y.to_i]
end
def update(player_moves)
end
end
class Cell
attr_accessor :col, :row, :content, :board
def initialize(col, row, board)
@col, @row = col, row
@content = :empty
@board = board
end
def surrounding
{ up: up,
down: down,
left: left,
right: right }
end
def up
log 'up', board.get_cell(row+1, col)
board.get_cell(row+1, col)
end
def down
board.get_cell(row-1, col)
end
def left
board.get_cell(row, col+1)
end
def right
board.get_cell(row, col-1)
end
end
class Player
attr_accessor :id, :cell, :board
def initialize(id, position, board)
log 'init player', time
@id = id
@board = board
@cell = board.get_cell(*position)
update_board
end
def move
log 'surroundings', @cell.surrounding
puts 'LEFT'
end
def update_board
@cell.content = self
end
end
$start = Time.now
log 'start', time
game = Game.new
loop { game.step }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment