Skip to content

Commit

Permalink
Merge pull request #105 from ryanb/syntax-tree-format
Browse files Browse the repository at this point in the history
Format with Syntax Tree
  • Loading branch information
ryanb authored Mar 27, 2024
2 parents b1af18b + 6758e98 commit a062c16
Show file tree
Hide file tree
Showing 92 changed files with 657 additions and 679 deletions.
3 changes: 3 additions & 0 deletions .streerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--print-width=100
--plugins=plugin/trailing_comma
--ignore-files='rubywarrior/*'
9 changes: 5 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
source "https://rubygems.org"

gem 'base64'
gem "base64"
gem "syntax_tree"

group :test do
gem 'rake'
gem 'rspec', '~> 3.13.0'
gem 'cucumber'
gem "rake"
gem "rspec", "~> 3.13.0"
gem "cucumber"
end
20 changes: 8 additions & 12 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
require 'rake'
require 'rspec'
require 'rspec/core/rake_task'
require 'cucumber'
require 'cucumber/rake/task'
require "rake"
require "rspec"
require "rspec/core/rake_task"
require "cucumber"
require "cucumber/rake/task"

desc "Run specs"
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = "spec/**/*_spec.rb"
end
RSpec::Core::RakeTask.new(:spec) { |t| t.pattern = "spec/**/*_spec.rb" }

Cucumber::Rake::Task.new(:features) do |t|
t.cucumber_opts = %w[features --format progress]
end
Cucumber::Rake::Task.new(:features) { |t| t.cucumber_opts = %w[features] }

task :default => [:spec, :features]
task default: %i[spec features]
6 changes: 6 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby

# loading the spec_helper file even though this isn't necessarily for specs
# it's just a convenient, quick way to load all the requirements.
puts "Loading console"
exec "irb -r #{File.dirname(__FILE__) + '/../lib/ruby_warrior.rb'} -r #{File.dirname(__FILE__) + '/../features/support/mockio.rb'} --simple-prompt"
2 changes: 2 additions & 0 deletions bin/format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
bundle exec stree write '**/*.rb' bin/rubywarrior Gemfile Rakefile
2 changes: 1 addition & 1 deletion bin/rubywarrior
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env ruby
require_relative '../lib/ruby_warrior'
require_relative "../lib/ruby_warrior"

runner = RubyWarrior::Runner.new(ARGV, STDIN, STDOUT)
runner.run
1 change: 1 addition & 0 deletions cucumber.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default: --publish-quiet --format progress
12 changes: 3 additions & 9 deletions features/step_definitions/interaction_steps.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Given /^a profile named "([^\"]*)" on "([^\"]*)"$/ do |name, tower|
step 'I run rubywarrior'
step "I run rubywarrior"
step 'I answer "y" to "create one?"'
step 'I choose "' + tower + '" for "tower"'
step 'I answer "' + name + '" to "name"'
Expand Down Expand Up @@ -27,9 +27,7 @@
When /^I run rubywarrior with options "([^\"]*)"$/ do |options|
RubyWarrior::Config.reset
@io = MockIO.new
@io.start do |io|
RubyWarrior::Runner.new(options.split, io, io).run
end
@io.start { |io| RubyWarrior::Runner.new(options.split, io, io).run }
end

When /^I answer "([^\"]*)" to "([^\"]*)"$/ do |answer, question|
Expand All @@ -40,11 +38,7 @@
When /^I choose "([^\"]*)" for "([^\"]*)"$/ do |choice, phrase|
answer = nil
content = @io.gets_until_include(phrase)
content.split("\n").each do |line|
if line.include?(choice) && line =~ /\[(\d)\]/
answer = $1
end
end
content.split("\n").each { |line| answer = $1 if line.include?(choice) && line =~ /\[(\d)\]/ }
if answer
@io.puts(answer)
else
Expand Down
14 changes: 5 additions & 9 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
require 'cucumber'
require 'rspec'
require "cucumber"
require "rspec"

require File.dirname(__FILE__) + '/../../lib/ruby_warrior'
require File.dirname(__FILE__) + "/../../lib/ruby_warrior"

Before do
RubyWarrior::Config.reset
end
Before { RubyWarrior::Config.reset }

After do
FileUtils.rm_rf "towers/short"
end
After { FileUtils.rm_rf "towers/short" }
14 changes: 7 additions & 7 deletions features/support/mockio.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class MockIO
class Timeout < Exception
end

def initialize(receiver = nil)
@buffer = ""
@receiver = receiver || MockIO.new(self)
end

def gets
1000.times do |n|
sleep 0.01 if n > 800 # throttle if it seems to be taking a while
Expand All @@ -18,7 +18,7 @@ def gets
end
raise Timeout, "MockIO Timeout: No content was received for gets."
end

def gets_until_include(phrase)
content = gets
while !content.include?(phrase)
Expand All @@ -30,20 +30,20 @@ def gets_until_include(phrase)
end
content
end

# TODO make this thread safe
def puts(str)
@receiver << "#{str}\n"
end

def print(str)
@receiver << str.to_s
end

def <<(str)
@buffer << str
end

def start
main_thread = Thread.current
Thread.new do
Expand Down
82 changes: 41 additions & 41 deletions lib/ruby_warrior.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
$: << File.dirname(__FILE__)

require 'set'
require "set"

require 'ruby_warrior/core_additions'
require "ruby_warrior/core_additions"

require 'ruby_warrior/runner'
require 'ruby_warrior/game'
require 'ruby_warrior/profile'
require 'ruby_warrior/ui'
require 'ruby_warrior/config'
require 'ruby_warrior/player_generator'
require 'ruby_warrior/level_loader'
require 'ruby_warrior/tower'
require 'ruby_warrior/level'
require 'ruby_warrior/turn'
require 'ruby_warrior/floor'
require 'ruby_warrior/space'
require 'ruby_warrior/position'
require "ruby_warrior/runner"
require "ruby_warrior/game"
require "ruby_warrior/profile"
require "ruby_warrior/ui"
require "ruby_warrior/config"
require "ruby_warrior/player_generator"
require "ruby_warrior/level_loader"
require "ruby_warrior/tower"
require "ruby_warrior/level"
require "ruby_warrior/turn"
require "ruby_warrior/floor"
require "ruby_warrior/space"
require "ruby_warrior/position"

require 'ruby_warrior/units/base'
require 'ruby_warrior/units/warrior'
require 'ruby_warrior/units/sludge'
require 'ruby_warrior/units/archer'
require 'ruby_warrior/units/thick_sludge'
require 'ruby_warrior/units/captive'
require 'ruby_warrior/units/wizard'
require 'ruby_warrior/units/golem'
require "ruby_warrior/units/base"
require "ruby_warrior/units/warrior"
require "ruby_warrior/units/sludge"
require "ruby_warrior/units/archer"
require "ruby_warrior/units/thick_sludge"
require "ruby_warrior/units/captive"
require "ruby_warrior/units/wizard"
require "ruby_warrior/units/golem"

require 'ruby_warrior/abilities/base'
require 'ruby_warrior/abilities/walk'
require 'ruby_warrior/abilities/attack'
require 'ruby_warrior/abilities/feel'
require 'ruby_warrior/abilities/rest'
require 'ruby_warrior/abilities/health'
require 'ruby_warrior/abilities/look'
require 'ruby_warrior/abilities/shoot'
require 'ruby_warrior/abilities/rescue'
require 'ruby_warrior/abilities/pivot'
require 'ruby_warrior/abilities/distance_of'
require 'ruby_warrior/abilities/bind'
require 'ruby_warrior/abilities/listen'
require 'ruby_warrior/abilities/direction_of_stairs'
require 'ruby_warrior/abilities/direction_of'
require 'ruby_warrior/abilities/explode'
require 'ruby_warrior/abilities/detonate'
require 'ruby_warrior/abilities/form'
require "ruby_warrior/abilities/base"
require "ruby_warrior/abilities/walk"
require "ruby_warrior/abilities/attack"
require "ruby_warrior/abilities/feel"
require "ruby_warrior/abilities/rest"
require "ruby_warrior/abilities/health"
require "ruby_warrior/abilities/look"
require "ruby_warrior/abilities/shoot"
require "ruby_warrior/abilities/rescue"
require "ruby_warrior/abilities/pivot"
require "ruby_warrior/abilities/distance_of"
require "ruby_warrior/abilities/bind"
require "ruby_warrior/abilities/listen"
require "ruby_warrior/abilities/direction_of_stairs"
require "ruby_warrior/abilities/direction_of"
require "ruby_warrior/abilities/explode"
require "ruby_warrior/abilities/detonate"
require "ruby_warrior/abilities/form"
4 changes: 2 additions & 2 deletions lib/ruby_warrior/abilities/attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class Attack < Base
def description
"Attacks a unit in given direction (forward by default)."
end

def perform(direction = :forward)
verify_direction(direction)
receiver = unit(direction)
if receiver
@unit.say "attacks #{direction} and hits #{receiver}"
if direction == :backward
power = (@unit.attack_power/2.0).ceil
power = (@unit.attack_power / 2.0).ceil
else
power = @unit.attack_power
end
Expand Down
26 changes: 15 additions & 11 deletions lib/ruby_warrior/abilities/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,40 @@ class Base
def initialize(unit)
@unit = unit
end

def offset(direction, forward = 1, right = 0)
case direction
when :forward then [forward, -right]
when :backward then [-forward, right]
when :right then [right, forward]
when :left then [-right, -forward]
when :forward
[forward, -right]
when :backward
[-forward, right]
when :right
[right, forward]
when :left
[-right, -forward]
end
end

def space(direction, forward = 1, right = 0)
@unit.position.relative_space(*offset(direction, forward, right))
end

def unit(direction, forward = 1, right = 0)
space(direction, forward, right).unit
end

def damage(receiver, amount)
receiver.take_damage(amount)
@unit.earn_points(receiver.max_health) unless receiver.alive?
end

def description
end

def pass_turn
# callback which is triggered every turn
end

def verify_direction(direction)
unless Position::RELATIVE_DIRECTIONS.include? direction
raise "Unknown direction #{direction.inspect}. Should be :forward, :backward, :left or :right."
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_warrior/abilities/bind.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Bind < Base
def description
"Binds a unit in given direction to keep him from moving (forward by default)."
end

def perform(direction = :forward)
verify_direction(direction)
receiver = unit(direction)
Expand Down
8 changes: 3 additions & 5 deletions lib/ruby_warrior/abilities/detonate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ class Detonate < Base
def description
"Detonate a bomb in a given direction (forward by default) which damages that space and surrounding 4 spaces (including yourself)."
end

def perform(direction = :forward)
verify_direction(direction)
if @unit.position
@unit.say "detonates a bomb #{direction} launching a deadly explosion."
bomb(direction, 1, 0, 8)
[[1, 1], [1, -1], [2, 0], [0, 0]].each do |x, y|
bomb(direction, x, y, 4)
end
[[1, 1], [1, -1], [2, 0], [0, 0]].each { |x, y| bomb(direction, x, y, 4) }
end
end

def bomb(direction, x, y, damage_amount)
if @unit.position
receiver = space(direction, x, y).unit
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_warrior/abilities/direction_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class DirectionOf < Base
def description
"Pass a Space as an argument, and the direction (:left, :right, :forward, :backward) to that space will be returned."
end

def perform(space)
@unit.position.relative_direction_of(space)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_warrior/abilities/direction_of_stairs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class DirectionOfStairs < Base
def description
"Returns the direction (:left, :right, :forward, :backward) the stairs are from your location."
end

def perform
@unit.position.relative_direction_of_stairs
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_warrior/abilities/distance_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class DistanceOf < Base
def description
"Pass a Space as an argument, and it will return an integer representing the distance to that space."
end

def perform(space)
@unit.position.distance_of(space)
end
Expand Down
Loading

0 comments on commit a062c16

Please sign in to comment.