-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
160 lines (114 loc) · 2.51 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require 'colorize'
require_relative 'player'
#game state values####
@whos_turn = 1
@playing = true
def create_players
ask_for_name
@p1 = Player.new(collect_name)
update_turn
ask_for_name
@p2 = Player.new(collect_name)
update_turn
end
def ask_for_name
p "Please enter your name, player #{@whos_turn}?"
end
def collect_name
gets.chomp
end
def generate_question
@number_1 = rand(20)
@number_2 = rand(20)
@answer = @number_1 + @number_2
end
def turn_name #Player's name who's turn it is.
@whos_turn == 1 ? @p1.name : @p2.name
end
def ask_question
puts "\n#{turn_name}, what is #{@number_1} + #{@number_2}?"
end
def gather_response
resonse = gets.chomp.to_i
end
def evalutate_response(response)
response == @answer
end
def notify_right_or_wrong(correct_response)
if correct_response
puts "\n#{turn_name}, that is a correct response!".colorize(:green)
else
puts "\n#{turn_name}, that's wrong! Better luck next time.".colorize(:red)
end
end
def update_turn
@whos_turn == 1 ? @whos_turn = 2 : @whos_turn = 1
end
def update_lives
@whos_turn == 1 ? @p1.lose_life : @p2.lose_life
end
def update_wins(player)
player == 1 ? @p1.add_win : @p2.add_win
end
def check_for_winner
if @p1.lives == 0
2
elsif @p2.lives == 0
1
else
nil
end
end
def announce_winner(player)
if player == 1
puts "\n#{@p1.name} wins!\n".colorize(:green)
else
puts "\n#{@p2.name} wins!\n".colorize(:green)
end
end
def play_again
puts "Play again? (Y/N)"
response = gets.chomp.downcase
response == 'y' ? true : false
end
def quit_or_restart(play_again)
if play_again
reset_game_state
@playing = true
else
@playing = false
end
end
def wins_scoreboard
puts "#{@p1.name} : #{@p1.wins} total wins."
puts "#{@p2.name} : #{@p2.wins} total wins."
end
def show_scoreboard
puts "#{@p1.name} : #{@p1.lives}pts | #{@p2.name} : #{@p2.lives}pts"
end
def reset_game_state
@p1.reset_lives
@p2.reset_lives
@whos_turn = 1
end
def play
while @playing
create_players if !@p1
generate_question
ask_question
correct_response = evalutate_response(gather_response)
notify_right_or_wrong(correct_response)
update_lives if !correct_response
update_turn
show_scoreboard
if check_for_winner #checks if there is a winner.
winner = check_for_winner
announce_winner(winner)
update_wins(winner)
wins_scoreboard
quit_or_restart(play_again)
end
end
puts "Thank you for playing!"
end
play