lib/coin_toss.rb in coin_toss-1.0.1 vs lib/coin_toss.rb in coin_toss-1.1.0
- old
+ new
@@ -1,83 +1,60 @@
require "coin_toss/version"
module CoinToss
system ('clear')
- puts "Best of how many games do you want to Play?
- Press 1 for 1 game
- Press 2 for best of 3 games
- Press 3 for best of 5 games"
-
- k = gets.to_i
- a = 0
- b = 0
- c = 1
-
- system ('clear')
-
- puts 'Player 1 -- TAILS
- Player 2 -- HEADS'
-
- if k == 1
+ def toss (a)
x = ["TAILS", "HEADS"]
y = x.sample
- puts "Toss number :#{c}"
puts "It's....."
- sleep(1)
+ sleep(2)
puts y
- sleep(1)
if y == "TAILS"
a = a + 1
else
- b = b + 1
+ a = a - 1
end
- if a > b
+ a
+ end
+
+ def win (a)
+ if a > 0
puts 'Player 1 wins'
- elsif b > a
+ elsif a < 0
puts 'Player 2 wins'
end
+ end
- elsif k == 2
- 3.times {x = ["TAILS", "HEADS"]
- y = x.sample
- puts "Toss number :#{c}"
- puts "It's....."
- sleep(1)
- puts y
- sleep(1)
- if y == "TAILS"
- a = a + 1
- else
- b = b + 1
- end
- c = c + 1}
- if a > b
- puts 'Player 1 wins'
- elsif b > a
- puts 'Player 2 wins'
- end
- c = 1
+ puts "Best of how many games do you want to Play?
+ Press 1 for 1 game
+ Press 2 for best of 3 games
+ Press 3 for best of 5 games"
- elsif k == 3
- 5.times {x = ["TAILS", "HEADS"]
- y = x.sample
- puts "Toss number :#{c}"
- puts "It's....."
- sleep(1)
- puts y
- sleep(1)
- if y == "TAILS"
- a = a + 1
- else
- b = b + 1
- end
- c = c + 1}
- if a > b
- puts 'Player 1 wins'
- elsif b > a
- puts 'Player 2 wins'
- end
- c = 1
- else
- puts 'invalid input'
- end
- end
+ k = gets.to_i
+
+ system ('clear')
+ puts 'Player 1 -- TAILS
+ Player 2 -- HEADS'
+
+ a = 0
+
+ if k == 1
+ b = toss 0
+ win b
+ elsif k == 2
+ b = toss 0
+ c = toss b
+ d = toss c
+ e = b + c + d
+ win e
+ elsif k == 3
+ b = toss 0
+ c = toss b
+ d = toss c
+ e = toss d
+ f = toss e
+ g = b + c + d + e + f
+ win g
+ else
+ puts 'invalid input'
+ end
+end
\ No newline at end of file