Skip to content

Commit

Permalink
Finalized GameLanguage and added documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
faeornran committed May 3, 2011
1 parent 472367f commit 8799bc2
Show file tree
Hide file tree
Showing 2 changed files with 305 additions and 98 deletions.
175 changes: 77 additions & 98 deletions game-language.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# game-language.rb
# James Vaughan
# This module defines the language used by the GameParser.
# It is a list of actions defined by a verb, a target, and a list of items
# that can be used on that target.
Expand All @@ -11,66 +12,60 @@ def initialize()

# Returns the Action relating to the verb parameter.
def getAction(verb)
return @actions.find { |x| x.getVerb == verb }
return @actions.find { |x| x.getVerb == verb.to_sym }
end

# Returns the list of actions.
def getActions
return @actions
end

def getResult(action, target, item)
return action.getDefaultTarget if target.nil?
result = action.getTarget(target)
# Returns the method correlating to this combination of inputs as a String.
# Returns nil if the result is not found.
def getResult(verb, target, item)
action = getAction(verb.to_sym)
foundTarget = action.getTarget(target.to_sym) if !action.nil?
resultClass = foundTarget.getResult(item.to_sym) if !foundTarget.nil?
return resultClass.getResult.to_s if !resultClass.nil?
return nil
end

# Adds the specified action relating to the verb, target and items passed.
# Adds to the action if it already exists.
# Adds to the action if it already exists.5
# Returns the Action.
def addAction(verb, target, result, usableItems)
a = getAction(verb)
a = Action.new(verb) if a.nil?
a.addTarget(target, result, usableItems);
@actions.push(a)
verbSymbol = verb.to_sym
a = getAction(verbSymbol)
newA = false
if a.nil?
a = Action.new(verbSymbol) if a.nil?
newA = true
end

newItems = usableItems.to_a # Allows single strings to be passed
symbolItems = []
usableItems.each do |x|
symbolItems.push(x.to_sym)
end

a.addTarget(target.to_sym, result.to_sym, symbolItems)
@actions.push(a) if newA
a # return the Action
end

# Sorts the actions by each action's verb.
def sortActions()
@actions.sort_by { |x| x.getVerb.to_s }
end

###
=begin
class Result
def initialize(action, pointer, method)
@action = action
@pointer = pointer
@method = method
end
def getAction()
return @action
end
def getClassPointer()
return @pointer
end
def activate()
return @pointer.instance_eval(method)
end
end
=end
###

end # Language


# Associates a specific verb with a set of targets.
class Action
def initialize(verb)
# @verb: :string
@verb = verb
# @defaultTarget: Target
# @defaultTarget = nil
@verb = verb.to_sym
# @targets: list(Target)
@targets = []
end # initialize
Expand All @@ -80,53 +75,38 @@ def getVerb()
return @verb
end # getVerb

# Returns the default target.
# def getDefaultTarget()
# return @defaultTarget
# end # getDefaultTarget

# Returns the Target object corresponding to the input.
def getTarget(target)
return @targets.find { |x| x.getTarget == target }
return @targets.find { |x| x.getTarget == target.to_sym }
end

# Returns the possible targets for this Action.
def getTargets()
return @targets
end # getTargets

# Sets the default target for the current verb.
# target: Target
# def setDefaultTarget(target)
# t = @targets.find { |x| x.getTarget.object_id == target.object_id }
# return @defaultTarget = t if !t.nil?
# return nil
# end # setDefaultTarget

# Adds the specified target with the list of usable items.
# If the target already exists, the items specified will be added.
# target: :string
# result: :string
# usableItems: list(:string)
def addTarget(target, result, usableItems)
t = @targets.find{ |x| x.getTarget.to_s == target.to_s }
t = getTarget(target)
if t.nil?
t = Target.new(target)
t = Target.new(target.to_s)
@targets.push(t)
@targets.sort
end
t.addResult(result, usableItems)
#usableItems.each do |x|
# i = t.getItems.find { |y| x == y }
# t.addResult(x) if i.nil?
#end
end # addTarget

# target: :string
# item: :string
def addItem(target, item)
newTarget = @targets.find { |x| x.getTarget.object_id == target.object_id }
return newTarget.addItem(item) if !newTarget.nil?
return nil
end # addItem
# def addItem(target, item)
# newTarget = getTarget(target)
# return newTarget.addItem(item) if !newTarget.nil?
# return nil
# end # addItem

end # Action

Expand All @@ -137,66 +117,67 @@ class Target
def initialize(target)
# @target: :string
# Tracks the entity's name that is targetable by the preceding verb.
@target = target
@target = target.to_sym
# @results: list(Result)
# Tracks the usable items and resulting method calls.
# Duplicate items are not allowed.
@results = []
# @items: list(:string)
# Stores what items have already been used.
@items = []
end
end # initialize

# Used for sorting Targets.
def <=>(other)
return @target.to_s <=> other.getTarget().to_s
end
return @target.to_s <=> other.getTarget.to_s
end # <=>

# Returns the target's name.
def getTarget()
return @target
end
end # getTarget

# Returns the possible Results for this verb/target combo.
def getResults()
return @results
end
end # getResults

# Returns the Result caused by a specific item.
# Returns nil if not found.
def getResult(item)
return @results.find { |x| x.hasItem(item) } if @items.find { |x| x == item }
return nil
end # getResult

# Returns items usable on the target.
def getItems()
return @items
end

def getResult(item)
return @results.find { |x| x.getResult == result }
end
end # getItems

# Adds the specified list of items to the specifed result.
def addResult(result, usableItems)
r = @results.find { |x| x.getResult == result }
if r.nil?
r = Result.new(result)
@results.push(r)
@results.sort
end
usableItems.each do |x|

newItems = usableItems.to_a # guarantee array form
newItems.each do |x|
if !@items.find { |y| x == y }
@items.push(x)
@items.sort_by { |z| x.to_s == z.to_s }
@items.sort_by { |z| z.to_s }
r.addItem(x)
end
end
end
end # addResult


end # Target


# Add item(:string) to @usableItems if not already there.
# def addItem(item)
# x = nil
# x = @usableItems.push(item) if @usableItems.find { |x| x == item }
# sortItems if !x.nil?
# return x
# end # addItem

# Tracks what items can accomplish the result method specified.
# For this specific verb, there cannot be repeat items.
# For each specific verb, there cannot be repeat items.
class Result
def initialize(result)
# @result: :string
Expand All @@ -205,11 +186,12 @@ def initialize(result)
# @usableItems: list(:string)
# Lists the items that can be used with this result.
@usableItems = []
end
end # initialize

# Used for sorting sets of results.
def <=>(other)
return @result.to_s <=> other.to_s
end
return @result.to_s <=> other.getResult.to_s
end # <=>

# Returns the result.
def getResult()
Expand All @@ -221,6 +203,11 @@ def getItems()
return @usableItems
end # getItems

# Determines if the result uses the specified item.
def hasItem(item)
return @usableItems.find { |x| x == item.to_sym }
end # hasItem

# Sorts the items usable with this specific verb on this specific target.
def sortItems()
@usableItems.sort_by { |x| x.to_s }
Expand All @@ -229,17 +216,9 @@ def sortItems()
# Add item(:string) to @usableItems. Should be guaranteed item is unique.
def addItem(item)
@usableItems.push(item)
@usableItems.sort
#x = nil
#x = @usableItems.push(item) if !@usableItems.find { |y| y == item }
#sortItems if !x.nil?
#return x
@usableItems.sort_by { |x| x.to_s }
end # addItem

# def hasItem(item)
# return @usableItems.find { |x| x.to_s == item.to_s }
# end

end # Result

end
end # GameLanguage
Loading

0 comments on commit 8799bc2

Please sign in to comment.