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