-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
271 additions
and
408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# A sample Guardfile | ||
# More info at https://github.com/guard/guard#readme | ||
|
||
## Uncomment and set this to only include directories you want to watch | ||
# directories %w(app lib config test spec features) \ | ||
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")} | ||
|
||
## Note: if you are using the `directories` clause above and you are not | ||
## watching the project directory ('.'), then you will want to move | ||
## the Guardfile to a watched dir and symlink it back, e.g. | ||
# | ||
# $ mkdir config | ||
# $ mv Guardfile config/ | ||
# $ ln -s config/Guardfile . | ||
# | ||
# and, you'll have to watch "config/Guardfile" instead of "Guardfile" | ||
|
||
# Note: The cmd option is now required due to the increasing number of ways | ||
# rspec may be run, below are examples of the most common uses. | ||
# * bundler: 'bundle exec rspec' | ||
# * bundler binstubs: 'bin/rspec' | ||
# * spring: 'bin/rspec' (This will use spring if running and you have | ||
# installed the spring binstubs per the docs) | ||
# * zeus: 'zeus rspec' (requires the server to be started separately) | ||
# * 'just' rspec: 'rspec' | ||
|
||
guard :rspec, cmd: "bundle exec rspec" do | ||
require "guard/rspec/dsl" | ||
dsl = Guard::RSpec::Dsl.new(self) | ||
|
||
# Feel free to open issues for suggestions and improvements | ||
|
||
# RSpec files | ||
rspec = dsl.rspec | ||
watch(rspec.spec_helper) { rspec.spec_dir } | ||
watch(rspec.spec_support) { rspec.spec_dir } | ||
watch(rspec.spec_files) | ||
|
||
# Ruby files | ||
ruby = dsl.ruby | ||
dsl.watch_spec_files_for(ruby.lib_files) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,43 @@ | ||
require 'goby' | ||
|
||
module Goby | ||
|
||
# Representation of a fight between two Fighters. | ||
class Battle | ||
|
||
# @param [Entity] entity_a the first entity in the battle | ||
# @param [Entity] entity_b the second entity in the battle | ||
def initialize(entity_a, entity_b) | ||
@entity_a = entity_a | ||
@entity_b = entity_b | ||
@pair = [entity_a, entity_b] | ||
end | ||
|
||
# Determine the winner of the battle | ||
# | ||
# @return [Entity] the winner of the battle | ||
def determine_winner | ||
type("#{entity_a.name} enters a battle with #{entity_b.name}!\n\n") | ||
until someone_dead? | ||
#Determine order of attacks | ||
attackers = determine_order | ||
type("#{@entity_a.name} enters a battle with #{@entity_b.name}!\n\n") | ||
until @pair.any?(&:dead?) | ||
# Determine order of attacks | ||
total_agility = @pair.sum { |entity| entity.stats[:agility] } | ||
attackers = Random.rand(0..total_agility - 1) < @entity_a.stats[:agility] ? @pair : @pair.reverse | ||
|
||
# Both choose an attack. | ||
attacks = attackers.map { |attacker| attacker.choose_attack } | ||
attacks = attackers.zip(attackers.reverse).map do |attacker, enemy| | ||
[attacker.choose_attack, attacker, enemy] | ||
end | ||
|
||
2.times do |i| | ||
attacks.each do |attack, attacker, enemy| | ||
# The attacker runs its attack on the other attacker. | ||
attacks[i].run(attackers[i], attackers[(i + 1) % 2]) | ||
attack.run(attacker, enemy) | ||
|
||
if (attackers[i].escaped) | ||
attackers[i].escaped = false | ||
if attacker.escaped | ||
attacker.escaped = false | ||
return | ||
end | ||
|
||
break if someone_dead? | ||
return @pair.detect { |entity| !entity.dead? } if @pair.any?(&:dead?) | ||
end | ||
end | ||
|
||
#If @entity_a is dead return @entity_b, otherwise return @entity_a | ||
entity_a.stats[:hp] <=0 ? entity_b : entity_a | ||
end | ||
|
||
private | ||
|
||
# Determine the order of attack based on the entitys' agilities | ||
# | ||
# @return [Array] the entities in the order of attack | ||
def determine_order | ||
sum = entity_a.stats[:agility] + entity_b.stats[:agility] | ||
random_number = Random.rand(0..sum - 1) | ||
|
||
if random_number < entity_a.stats[:agility] | ||
[entity_a, entity_b] | ||
else | ||
[entity_b, entity_a] | ||
end | ||
end | ||
|
||
# Check if either entity is is dead | ||
# | ||
# @return [Boolean] whether an entity is dead or not | ||
def someone_dead? | ||
entity_a.stats[:hp] <= 0 || entity_b.stats[:hp] <= 0 | ||
end | ||
|
||
attr_reader :entity_a, :entity_b | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,25 @@ | ||
require 'goby' | ||
|
||
module Goby | ||
|
||
# Allows an Entity to try to escape from the opponent. | ||
class Escape < BattleCommand | ||
|
||
# Text for successful escape. | ||
SUCCESS = "Successful escape!\n\n" | ||
SUCCESS = "Successful escape!\n\n".freeze | ||
# Text for failed escape. | ||
FAILURE = "Unable to escape!\n\n" | ||
FAILURE = "Unable to escape!\n\n".freeze | ||
|
||
# Initializes the Escape command. | ||
def initialize | ||
super(name: "Escape") | ||
super(name: 'Escape') | ||
end | ||
|
||
# Samples a probability to determine if the user will escape from battle. | ||
# | ||
# @param [Entity] user the one who is trying to escape. | ||
# @param [Entity] enemy the one from whom the user wants to escape. | ||
def run(user, enemy) | ||
|
||
# Higher probability of escape when the enemy has low agility. | ||
if (user.sample_agilities(enemy)) | ||
user.escaped = true | ||
type(SUCCESS) | ||
return | ||
end | ||
|
||
# Should already be false. | ||
user.escaped = false | ||
type(FAILURE) | ||
user.escape_from(enemy) | ||
type(user.escaped ? SUCCESS : FAILURE) | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.