Created
April 29, 2016 23:08
-
-
Save garrettgsb/4fc1afce098dbc7283251ac5f4746956 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "pry-nav" | |
##TODO: Refactor such that there is a "Player" class that has attributes for | |
## 'lives' and 'score,' put players in a "players" array and iterate through | |
## it to pass turns. | |
class Game | |
def initialize | |
@player_1_lives, | |
@player_2_lives, | |
@player_1_score, | |
@player_2_score = 3,3,0,0 | |
@whose_turn = 2 # Will toggle when the game loop initiates so that P1 goes first. | |
game_loop | |
end #initialize | |
def generate_question | |
@first_digit = (1..20).to_a.sample | |
@second_digit = (1..20-@first_digit).to_a.sample # I THINK this will guarantee | |
# that the digits don't add up | |
# to more than 20. | |
@correct_answer = @first_digit.to_i + @second_digit.to_i | |
end #generate_question | |
def game_loop | |
while nobody_is_dead | |
@whose_turn = turn_toggle(@whose_turn) | |
generate_question | |
puts "Player #{@whose_turn}: What's the answer to #{@first_digit.to_s} plus #{@second_digit.to_s}?" | |
input = gets.chomp | |
break if input == "stahp" | |
(input == @correct_answer.to_s) ? woot_you_scored(@whose_turn) : yeah_sorry_mate_that_is_wrong(@whose_turn) | |
# puts " | |
# Player 1: | |
# Score: #{@player_1_score} | |
# Lives: #{@player_1_lives} | |
# ........ | |
# Player 2: | |
# Score: #{@player_2_score} | |
# Lives: #{@player_2_lives} | |
# #{@player_2_score}" | |
end #while loop | |
game_over(@whose_turn) | |
end #game_loop | |
def turn_toggle(num) | |
num == 1 ? 2 : 1 | |
end #turn_toggle | |
def woot_you_scored(player) | |
if player == 1 | |
@player_1_score += 1 | |
elsif player == 2 | |
@player_2_score += 1 | |
end #if/elsif | |
puts " *************************************** | |
***********Yeah you got it!************ | |
************----------***************** | |
************--Score:--***************** | |
**P1: #{@player_1_score}-------------------------P2: #{@player_2_score}** | |
*************************************** | |
************----------***************** | |
************--Lives:--***************** | |
**P1: #{@player_1_lives}-------------------------P2: #{@player_2_lives}** | |
***************************************" | |
end #woot_you_scored | |
def yeah_sorry_mate_that_is_wrong(player) | |
puts " ==================================== | |
Yeah sorry mate. You lost a life :\\ | |
==================================== | |
Player 1: #{@player_1_lives}---Remaining---Player 2: #{@player_2_lives}" | |
if player == 1 | |
@player_1_lives -= 1 | |
elsif player == 2 | |
@player_2_lives -= 1 | |
end #if/elsif | |
end #yeah_sorry_mate_that_is_wrong | |
def game_over(player) | |
puts ".\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n. Player #{player} is bad at math :(.\n.\n. Do you wanna play again though?\n.\n.\n.\n.\n.\n)" | |
restart = gets.chomp | |
restart == "yes" ? Game.new : (puts "\n\nKay bye!\n\n\n\n........") | |
end #game_over | |
def nobody_is_dead | |
@player_1_lives > 0 && @player_2_lives > 0 | |
end #nobody_is_dead | |
end #class | |
Game.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment