-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtennis.rb
71 lines (57 loc) · 1.03 KB
/
tennis.rb
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
class Tennis
def initialize
@player1_score = 0
@player2_score = 0
end
def score
if someone_wins?
show_winner
elsif deuce?
show_deuce
elsif advantage?
show_advantage
else
show_score
end
end
def point! player
if player == 1
@player1_score = increase (@player1_score)
else
@player2_score = increase (@player2_score)
end
end
private
def deuce?
@player1_score == @player2_score && @player1_score >= 40
end
def show_advantage
"ADVANTAGE #{player_with_highest_score}"
end
def advantage?
@player1_score > 40 || @player2_score > 40
end
def someone_wins?
(@player1_score > 40 || @player2_score > 40) && ((@player1_score-@player2_score).abs > 15)
end
def player_with_highest_score
if @player1_score > @player2_score
"player 1"
else
"player 2"
end
end
def show_winner
"#{player_with_highest_score} wins"
end
def show_deuce
"DEUCE"
end
def show_score
"#{@player1_score}-#{@player2_score}"
end
def increase score
score += score==30? 10 : 15
score
end
end