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