Guest User

Untitled

a guest
Jan 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. require 'rubygems'
  2. require 'sinatra'
  3. require 'sinatra/reloader'
  4. require 'haml'
  5. #use Rack::Session::Pool
  6.  
  7. module HotColdApp
  8. def initialize
  9. guesses = 1
  10. i = rand(10)
  11. end
  12.  
  13. def play
  14. "Guess a number from 1 to 10"
  15. "You have 5 tries"
  16. "----------"
  17. guess = gets.to_i
  18. while guess != i and guesses < 5
  19. guesses = guesses + 1
  20. if guess < i
  21. "too cold"
  22. guess = gets.to_i
  23. else guess > i
  24. "too hot"
  25. guess = gets.to_i
  26. end
  27. end
  28. if guess == i
  29. "just right"
  30. else
  31. "try again next time"
  32. end
  33. end
  34. end
  35.  
  36. include HotColdApp
  37. get '/' do
  38. p initialize
  39. haml :index
  40. end
  41.  
  42. post '/' do
  43. guess = params[:guess]
  44. haml :index, :locals => {:name => guess}
  45. end
  46.  
  47. __END__
  48.  
  49. @@ index
  50. !!!!
  51. %html
  52. %head
  53. %title Hot/Cold
  54. %body
  55. %h1 hOt/cOld
  56. %p
  57. Guess a number from 1 to 10. You have 5 tries.
  58. %form{:action => "/", :method => "POST"}
  59. %p
  60. %input{:type => "textbox", :name => "guess", :class => "text"}
  61. %p
  62. %input{:type => "submit", :value => "GUESS!", :class => "button"}
  63. %p
  64.  
  65. require 'sinatra'
  66. require 'sinatra/reloader'
  67. require 'haml'
  68. #use Rack::Session::Pool
  69.  
  70. module HotColdApp
  71. def initialize
  72. @guesses = 5
  73. @i = rand(10)
  74. end
  75.  
  76. def play(guess)
  77. guess = guess.to_i
  78. if(@i != guess && @guesses > 1)
  79. @guesses -= 1
  80. if guess < @i
  81. return "#{@guesses} left. Too cold"
  82. else guess > @i
  83. return "#{@guesses} left. Too hot"
  84. end
  85. elsif(@i != guess && @guesses == 1)
  86. return "You lose!"
  87. elsif(@i == guess)
  88. return "You win!"
  89. end
  90. end
  91. end
  92.  
  93. include HotColdApp
  94. get '/' do
  95. p initialize
  96. haml :index
  97. end
  98.  
  99. post '/' do
  100. guess = params[:guess]
  101. @result = play(guess)
  102. haml :index, :locals => {:name => guess}
  103. end
  104.  
  105. __END__
  106.  
  107. @@ index
  108. !!!!
  109. %html
  110. %head
  111. %title Hot/Cold
  112. %body
  113. %h1 hOt/cOld
  114. %p
  115. Guess a number from 1 to 10. You get 5 tries.
  116. %form{:action => "/", :method => "POST"}
  117. %p
  118. %input{:type => "textbox", :name => "guess", :class => "text"}
  119. %p
  120. %input{:type => "submit", :value => "GUESS!", :class => "button"}
  121. %p
  122. %p= @result
Add Comment
Please, Sign In to add comment