-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice_101.rb
More file actions
118 lines (104 loc) · 2.47 KB
/
Copy pathpractice_101.rb
File metadata and controls
118 lines (104 loc) · 2.47 KB
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
# This is a practice ruby 'game'
class Game
def initialize
@score_pc_player = [0, 0] unless defined?(@total)
@total = 0
puts '=========================================='
puts 'Let\'s take turns counting using numbers from 1 to 10.'\
' The first person to get to 100 wins'
sleep 0.5
who_goes_first
continue?
end
def who_goes_first
print 'Who goes first? You or me? '
starts = gets.chomp.downcase.strip
if starts == 'you'
game_pc_starts
elsif starts == 'me'
game_player_starts
else
puts 'Invalid answer'
who_goes_first
end
end
def pc_choice
puts "I choose the number #{@total.zero? ? 1 : 11 - @player_choice}"
@total += @total.zero? ? 1 : 11 - @player_choice
puts "The sum is now #{@total}"
end
def pc_won?
if @total >= 100
puts 'You lose!!! \\o/'
@score_pc_player[0] += 1
puts "SCOREBOARD: PC #{@score_pc_player[0]} x #{@score_pc_player[1]} You"
end
@total >= 100
end
def player_won?
if @total >= 100
puts 'You won!!! \\o/'
@score_pc_player[1] += 1
puts "SCOREBOARD: PC #{@score_pc_player[0]} x #{@score_pc_player[1]} You"
end
@total >= 100
end
def pc_choice_2
if @total % 11 == 1
rand(1..10)
elsif (@total % 11).zero?
1
else
12 - (@total % 11)
end
end
def player_choice
print 'Your turn, pick a number from 1 to 10: '
@player_choice = gets.chomp.to_i
check_player_answer
sleep 1
end
def check_player_answer
if @player_choice.between?(1, 10)
@total += @player_choice
puts "The sum is now #{@total}"
puts '=========================================='
else
puts 'Invalid answer, you should pick a number from 1 to 10'
player_choice
end
end
def game_pc_starts
puts 'Okay, I\'ll go first'
while @total < 100
pc_choice
break if pc_won?
player_choice
end
end
def game_player_starts
puts 'Okay, you go first'
while @total < 100
player_choice
break if player_won?
pc = pc_choice_2
puts "I choose the number #{pc}"
@total += pc
puts "The sum is now #{@total}"
break if pc_won?
end
end
def continue?
print 'Wanna play again?(yes or no) '
replay = gets.chomp.downcase.strip
if replay == 'yes'
initialize
elsif replay == 'no'
puts 'Okay, goodbye! \\o'
else
puts 'Invalid answer'
continue?
end
end
end
Game.new