-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_player_math_game.rb
More file actions
95 lines (81 loc) · 1.92 KB
/
Copy pathtwo_player_math_game.rb
File metadata and controls
95 lines (81 loc) · 1.92 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
#2 player math game
require 'colorize'
require_relative 'player'
class Game
def initialize(players)
@num_players = players
@turns = 0
@players = [] #an array of players
end
def create_players
@num_players.times do |player|
puts "Player #{player + 1}, enter your name: "
name = gets.chomp
@players << Player.new(name)
end
puts "Okay perfect, lets get started!"
end
def generate_question
x = rand(1..20)
y = rand(1..20)
syms = [:+, :-, :*, :**, :%]
operator = syms.sample
puts "#{@players[@turns].name} What does #{x} #{operator} #{y} equal?"
x.send(operator, y)
end
def get_answer
gets.chomp.to_i
end
def verify_answer(answer, user_answer)
if answer == user_answer
puts "CORRECT!!!".green
else
puts "WRONG! The correct answer was #{answer}.".red
puts "#{@players[@turns].name} loses a life".red
@players[@turns].lose_life
end
end
def change_turn
if @turns >= @players.size - 1
@turns = 0
else
@turns += 1
end
end
def is_dead?
unless @players[@turns].alive?
puts "#{@players[@turns].name} is dead!".red
@turns -= 1
end
end
def output_lives
@players.each do |player|
puts "#{player.name} has #{player.lives} lives"
end
end
def game_still_going?
@players.length > 1
end
def remove_losers
@players.select! do |player|
player.alive?
end
end
def declare_winner
puts "#{@players[0].name} is the winner!!!".green
end
end #end of class
print "How many players? "
num_players = gets.chomp.to_i
main_game = Game.new(num_players)
main_game.create_players
while main_game.game_still_going?
answer = main_game.generate_question
u_answer = main_game.get_answer
main_game.verify_answer(answer, u_answer)
main_game.is_dead?
main_game.remove_losers
main_game.output_lives
main_game.change_turn
end
main_game.declare_winner