-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
53 lines (46 loc) · 1.3 KB
/
game.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
#Array that stores a hash of the players info.
@players = [
{
id: 1,
life: 3,
score: 0
},
{
id: 2,
life: 3,
score: 0
}
]
def generate_question(players)
turn = 0
counter = 0
while players[turn][:life] != 0 #|| players[1][:life] != 0
num1 = rand(1..20)
num2 = rand(1..20)
turn = counter % 2 # If divisible is 0 turn is player 1, if divisible is 1 it is player 2's turn.
#if turn == 0
# puts "turn 0"
#else puts "turn 1"
#end
print "Player #{players[turn][:id]}: what is #{num1} + #{num2}? "
answer = num1 + num2
response = gets.chomp.to_i
counter += 1
if response == answer
players[turn][:score] += 1
#puts "The score is #{players[turn][:score]}"
elsif response != answer
players[turn][:life] -= 1
puts "That was the wrong answer. The score is: \n Player 1: #{players[0][:score]} \n Player 2: #{players[1][:score]}"
end
#if players[turn][:life]
end
if players[0][:life] == 1
puts "Player # 1 won with a score of #{players[0][:score]}"
puts "Player # 2 lost with a score was #{players[1][:score]}"
else
puts "Player # 2 won with a score of #{players[1][:score]}"
puts "Player # 1 lost with a score of #{players[1][:score]}"
end
end
generate_question(@players)