PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/poignant.coffee

http://github.com/jashkenas/coffee-script
CoffeeScript | 181 lines | 50 code | 32 blank | 99 comment | 8 complexity | 6b4f0487d97df620985989e03c63a772 MD5 | raw file
  1# Examples from the Poignant Guide.
  2# These are examples of syntax differences between CoffeeScript and Ruby,
  3# they won't run.
  4
  5# ['toast', 'cheese', 'wine'].each { |food| print food.capitalize }
  6
  7print food.capitalize() for food in ['toast', 'wine', 'cheese']
  8
  9
 10
 11
 12# class LotteryTicket
 13#   def picks;           @picks;            end
 14#   def picks=(var);     @picks = var;      end
 15#   def purchased;       @purchased;        end
 16#   def purchased=(var); @purchased = var;  end
 17# end
 18
 19LotteryTicket =
 20  get_picks:     -> @picks
 21  set_picks:     (@picks) ->
 22  get_purchased: -> @purchase
 23  set_purchased: (@purchased) ->
 24
 25
 26
 27# class << LotteryDraw
 28#   def play
 29#     result = LotteryTicket.new_random
 30#     winners = {}
 31#     @@tickets.each do |buyer, ticket_list|
 32#       ticket_list.each do |ticket|
 33#         score = ticket.score( result )
 34#         next if score.zero?
 35#         winners[buyer] ||= []
 36#         winners[buyer] << [ ticket, score ]
 37#       end
 38#     end
 39#     @@tickets.clear
 40#     winners
 41#   end
 42# end
 43
 44LotteryDraw =
 45  play: ->
 46    result  = LotteryTicket.new_random()
 47    winners = {}
 48    for buyer, ticketList of @tickets
 49      for ticket in ticketList when (score = ticket.score result) isnt 0
 50        (winners[buyer] or= []).push [ticket, score]
 51    @tickets = {}
 52    winners
 53
 54
 55
 56# module WishScanner
 57#   def scan_for_a_wish
 58#     wish = self.read.detect do |thought|
 59#       thought.index( 'wish: ' ) == 0
 60#     end
 61#     wish.gsub( 'wish: ', '' )
 62#   end
 63# end
 64
 65WishScanner =
 66  scan_for_a_wish: ->
 67    wish = @read().detect (thought) -> thought.indexOf('wish: ') is 0
 68    wish.replace 'wish: ', ''
 69
 70
 71
 72# class Creature
 73#
 74#   # This method applies a hit taken during a fight.
 75#   def hit( damage )
 76#     p_up = rand( charisma )
 77#     if p_up % 9 == 7
 78#       @life += p_up / 4
 79#       console.log "[#{ self.class } magick powers up #{ p_up }!]"
 80#     end
 81#     @life -= damage
 82#     console.log "[#{ self.class } has died.]" if @life <= 0
 83#   end
 84#
 85#   # This method takes one turn in a fight.
 86#   def fight( enemy, weapon )
 87#     if life <= 0
 88#       console.log "[#{ self.class } is too dead to fight!]"
 89#       return
 90#     end
 91#
 92#     # Attack the opponent
 93#     your_hit = rand( strength + weapon )
 94#     console.log "[You hit with #{ your_hit } points of damage!]"
 95#     enemy.hit( your_hit )
 96#
 97#     # Retaliation
 98#     p enemy
 99#     if enemy.life > 0
100#       enemy_hit = rand( enemy.strength + enemy.weapon )
101#       console.log "[Your enemy hit with #{ enemy_hit } points of damage!]"
102#       self.hit( enemy_hit )
103#     end
104#   end
105#
106# end
107
108Creature =
109
110  # This method applies a hit taken during a fight.
111  hit: (damage) ->
112    p_up = Math.rand @charisma
113    if p_up % 9 is 7
114      @life += p_up / 4
115      console.log "[#{@name} magick powers up #{p_up}!]"
116    @life -= damage
117    if @life <= 0 then console.log "[#{@name} has died.]"
118
119  # This method takes one turn in a fight.
120  fight: (enemy, weapon) ->
121    return console.log "[#{@name} is too dead to fight!]" if @life <= 0
122
123    # Attack the opponent.
124    your_hit = Math.rand @strength + weapon
125    console.log "[You hit with #{your_hit}points of damage!]"
126    enemy.hit your_hit
127
128    # Retaliation.
129    console.log enemy
130    if enemy.life > 0
131      enemy_hit = Math.rand enemy.strength + enemy.weapon
132      console.log "[Your enemy hit with #{enemy_hit}points of damage!]"
133      @hit enemy_hit
134
135
136
137# # Get evil idea and swap in code words
138# print "Enter your new idea: "
139# idea = gets
140# code_words.each do |real, code|
141#   idea.gsub!( real, code )
142# end
143#
144# # Save the jibberish to a new file
145# print "File encoded.  Please enter a name for this idea: "
146# idea_name = gets.strip
147# File::open( "idea-" + idea_name + ".txt", "w" ) do |f|
148#   f << idea
149# end
150
151# Get evil idea and swap in code words
152print "Enter your new idea: "
153idea = gets()
154code_words.each (real, code) -> idea.replace(real, code)
155
156# Save the jibberish to a new file
157print "File encoded. Please enter a name for this idea: "
158idea_name = gets().strip()
159File.open "idea-#{idea_name}.txt", 'w', (file) -> file.write idea
160
161
162
163# def wipe_mutterings_from( sentence )
164#   unless sentence.respond_to? :include?
165#     raise ArgumentError,
166#       "cannot wipe mutterings from a #{ sentence.class }"
167#   end
168#   while sentence.include? '('
169#     open = sentence.index( '(' )
170#     close = sentence.index( ')', open )
171#     sentence[open..close] = '' if close
172#   end
173# end
174
175wipe_mutterings_from = (sentence) ->
176  throw new Error "cannot wipe mutterings" unless sentence.indexOf
177  while '(' in sentence
178    open     = sentence.indexOf('(')
179    close    = sentence.indexOf(')')
180    sentence = "#{sentence[0...open]}#{sentence[close + 1..]}"
181    sentence