r/learnruby • Beginner • Sep 11 '14

First completed app. Feels like I'm not doing this "the Ruby way", looking for feedback.

This is part of my practice as I go through Learn Ruby the Hard Way. Exercise 36 is to go off and create a game, working on it for a week or so.

The code below feels sloppy, especially for the reason that I'm passing the "player, monster, kills" variables through everything. Seems like there's probably a more "Rubyist" way to be doing some of this. Even just some pointers such as "Look into this technique" would be very helpful, thank you.

This is a simple game where the player chooses a name, is assigned HP, and has three simple choices as actions. As the user kills monsters, the game tallies kills and shows the number to the user after they die. The point is to kill as many as possible before dying. Thanks very much for any feedback!!

Edit: reworked a bit for some edge cases. Edit 2: reworked some more, added magic, leveling, and critical hit systems.

class Player
	attr_accessor :name, :hp, :shield, :xp, :level, :mp

	def initialize(name, hp, shield, xp, level, mp)
		@name = name
		@hp = hp
		@shield = shield
		@xp = xp
		@level = level
		@mp = mp
	end

	def self.level_up(player, kills)
		if player.xp >= 100 && player.level < 2
			puts "#{player.name} has leveled up!  Spells can now be cast!"

			player.level += 1
			player.mp += 30
			player.hp += 15
			player.xp = 0

			arena(player, kills)
		elsif player.xp >= 100
			puts "#{player.name} has leveled up!"

			player.level += 1
			player.mp += 30
			player.hp += 15
			player.xp = 0

			arena(player, kills)
		else
			arena(player, kills)
		end
	end

	def name
		@name
	end

	def hp
		@hp
	end

	def shield
		@shield
	end

	def xp
		@xp
	end

	def level
		@level
	end

	def mp
		@mp
	end
end

class Monster < Player
end

#Where spells are called from
module Spells
	#Checking if user has enough MP to cast spell
	def self.mp_check(player, monster, kills, req)
		req_mp = req
		if player.mp < req_mp
			puts "#{player.name} doesn't have enough MP!"
			action(player, monster, kills)
		else
		end
	end

	def self.heal(player, monster, kills)
		req_mp = 3
		Spells.mp_check(player, monster, kills, req_mp)

		player.mp -= req_mp
		amt = rand(3..10)
		player.hp += amt
		puts "#{player.name} has been healed by #{amt} HP!"
		action(player, monster, kills)
	end

	def self.fireball(player, monster, kills)
		req_mp = 5
		Spells.mp_check(player, monster, kills, req_mp)

		player.mp -= req_mp
		dmg = rand(5..10)
		monster.hp -= dmg

		if monster.hp >= 1
			puts "#{player.name}'s fireball burns the #{monster.name} for #{dmg} HP!"
			action(player, monster, kills)
		else
			puts "#{player.name} launches a mighty fireball at the #{monster.name}!"
			monster_death(player, monster, kills)
		end
	end

	def self.earthquake(player, monster, kills)
		req_mp = 8
		Spells.mp_check(player, monster, kills, req_mp)

		player.mp -= req_mp
		dmg = rand(5..15)
		monster.hp -= dmg

		if monster.hp >= 1
			puts "#{player.name}'s earthquake crushes the #{monster.name} for #{dmg} HP!"
			action(player, monster, kills)
		else
			puts "The ground violently lurches and cracks open beneath he #{monster.name}!"
			monster_death(player, monster, kills)
		end
	end
end

def prompt
	print "> "
end

def outline
	puts "********************************************"
end

kills = 0

#Where the player makes his choices.  Initial if-then checking for Shield status in order to display SP for user
def action(player, monster, kills)
	if player.shield >= 1 && player.level >= 2 #Show MP and SP if player is of adequate level
		outline
		puts "\tOur Hero: #{player.name} \(Lvl #{player.level}\) \:\: #{player.hp} HP \:\: #{player.mp} MP \:\: #{player.shield} SP" 
	elsif player.level >= 2
		outline
		puts "\tOur Hero: #{player.name} \(Lvl #{player.level}\) \:\: #{player.hp} HP \:\: #{player.mp} MP"
	elsif player.shield >= 1
		outline
		puts "\tOur Hero: #{player.name} \(Lvl #{player.level}\) \:\: #{player.hp} HP \:\: #{player.shield} SP"		
	else
		outline
		puts "\tOur Hero: #{player.name} \(Lvl #{player.level}\) \:\: #{player.hp} HP"
	end
		puts "\t \t \tvs"
		puts "\tEnemy: #{monster.name} \(Lvl #{monster.level}\) \:\: #{monster.hp} HP"
		outline

#rolling a d20 to see who takes a turn
	turn = rand(1..100)

	if turn <= 20
		monster_attack(player, monster, kills)
	else
		puts "What would you like to do?"
		puts "1. Attack!"
		puts "2. Defend!"
		puts "3. Run away!"
		#Give the player magic if they're at least level 2
		if player.level >= 2
			puts "4. Cast spell"
		else
		end

		prompt; action = gets.chomp
		
		if action == "1"
			attack(player, monster, kills)
		elsif action == "2"
			defend(player, monster, kills)
		elsif action == "3"
			flee(player, monster, kills)
		elsif action == "4" && player.level >= 2
			magic(player, monster, kills)
		else
			action(player, monster, kills)
		end
	end
end

def magic(player, monster, kills)
	puts "What magic would you like to cast?"
	puts "1. Heal"
	puts "2. Fireball"
	puts "3. Earthquake"
	prompt; magic = gets.chomp

	if magic == "1"
		Spells.heal(player, monster, kills)
	elsif magic == "2"
		Spells.fireball(player, monster, kills)
	elsif magic == "3"
		Spells.earthquake(player, monster, kills)
	else
		magic(player, monster, kills)
	end
end

#20% chance of monster attacking - this checks for a hit or a miss by the monster
def monster_attack(player, monster, kills)
	puts "The #{monster.name} attacks #{player.name}!"
	mscore = rand(1..20)

	if mscore >= 8
		monster_attack_success(player, monster, kills)
	else
		puts "The #{monster.name} misses with its attack!"
		action(player, monster, kills)
	end
end

#The monster was successful in the attack
def monster_attack_success(player, monster, kills)
	damage = rand(1..6) + monster.level

	#Handing damage, taking Shield Points into accounts
	if player.hp >= 1  && player.shield >= damage
		puts "#{monster.name} whomps #{player.name} for #{damage} HP, but #{player.name}'s shield absorbs it!"
		player.shield -= damage
	elsif player.hp >= 1 && player.shield >= 1
		puts "#{monster.name} whomps #{player.name} for #{damage} HP, but #{player.name}'s shield absorbs #{player.shield} damage!"
		player.hp -= (damage - player.shield)
		player.shield = 0
	elsif player.hp >= 1 
		puts "#{monster.name} whomps #{player.name} for #{damage} HP!"
		player.hp -= damage
	else
		puts "#{monster.name} slays #{player.name} with a wicked blow!"
		player_death(player, monster, kills)
	end

	if player.hp <= 0
		player_death(player, monster, kills)
	else
		action(player, monster, kills)
	end
end

#The user chose to attack, checking for hit or miss
def attack(player, monster, kills)
	puts "#{player.name} attacks!"
	pscore = rand(1..20)
	if pscore >= 9
		d6(player, monster, kills)
	else
		puts "#{player.name} missed with a wild swing!"
		action(player, monster, kills)
	end
end

#The user's attack was successful, now rolling for damage and death of monster
def d6(player, monster, kills)
	damage = rand(1..6)
	crit_hit = rand(1..100)

	#Determine if the player's attack lands a critical hit for a damage modifier of 1.5x
	if crit_hit <= 15
		puts "CRITICAL HIT!"
		damage *= 2
	else
		damage = damage
	end

	monster.hp -= damage

	if monster.hp >= 1
		puts "#{player.name} slices #{monster.name} for #{damage} HP!"
		action(player, monster, kills)
	else
		puts "#{player.name} slays #{monster.name} with a wicked blow!"
		monster_death(player, monster, kills)
	end
end

#User chose to defend - checking for max shield value of 5, adding SP if not.  Allows user to go above 5 on addition of SP.
def defend(player, monster, kills)
	if player.shield >= (5 + player.level)
		puts "#{player.name} already has maximum defense!"
		action(player, monster, kills)
	else
		player.shield += rand(1..3)
		puts "#{player.name} prepares their defenses!"
		action(player, monster, kills)
	end
end

#User chose to flee; small chance of flight.  If they lose roll, monster wins attack.
def flee(player, monster, kills)
	chance = rand(0..20)

	if chance >= 15
		puts "#{player.name} flees the battle!"
		arena(player, kills)
	else
		puts "Oh no!  The #{monster.name} blocks #{player.name}'s path and attacks!"
		monster_attack(player, monster, kills)
	end
end

#User has defeated the monster.  Kill is added to tally, user starts over again.
def monster_death(player, monster, kills)
	puts "#{monster.name} thrashes about for a bit before gurgling its last breath.  You win!"
	kills += 1

	if monster.name == "Griffin"
		player.xp += rand(5..30)
	else
		player.xp += rand(20..50)
	end

	Player.level_up(player, kills)
end

#User has been defeated.  User is shown how many kills they tallied.
def player_death(player, monster, kills)
	puts "#{player.name} has died at the hands of the mighty #{monster.name}."
	puts "Before dying, #{player.name} slew #{kills} foul beasts!"
	exit(0)
end

#Beginning of game, getting user information and rolling for HP.
def start(kills)

print '''
 ____  __.___.____    .____        _________ ___ ___ .______________
|    |/ _|   |    |   |    |      /   _____//   |   \|   __    ___/
|      < |   |    |   |    |      _____  \/    ~    \   | |    |   
|    |  \|   |    |___|    |___   /        \    Y    /   | |    |   
|____|__ ___|_______ _______ \ /_______  /___|_  /|___| |____|   
        \/           \/       \/         \/       \/                '''

	puts "\n\tKill as many monsters as you can before you die!"

	puts "\nAfter the collapse of the rebellion and being on the run for months, you have finally been captured.  You will be sent to the arena to fight to the death...your death. \nHow long can you last?"

	puts "\nWhat is your name, rebel?"
	prompt; pname = gets.chomp

	if pname.length >= 1
		player = Player.new(pname, rand(20..30), 0, 0, 1, 0)
		arena(player, kills)
	else
		start(kills)
	end
end

#Generating initial monster and sending user to action choices.
def arena(player, kills)

	outline
	puts "\n#{player.name} takes a breath, looks around the arena, and prepares for battle.\n \n"
	
	monster_type = rand(1..2)

	if monster_type == 1
		monster = Monster.new("Griffin", rand(10..30), 0, 0, (player.level + rand(0..2)), 0)
		puts "A mighty Griffin swoops down from above!\n"
		action(player, monster, kills)
	else
		monster = Monster.new("Cyclops", rand(20..30), 0, 0, (player.level + rand(0..2)), 0)
		puts "A huge Cyclops comes crashing into the arena!\n"
		action(player, monster, kills)
	end
end

start(kills)
6 Upvotes

4 comments sorted by

3

u/materialdesigner Sep 12 '14

Hey, so I really enjoyed the game so I decided to try to go through a series of incremental refactorings with the code to help you understand those smells you were getting intuitively.

You should be able to follow along with the commits in the git repo I added to github.

1

u/MinervaDreaming Beginner Sep 12 '14

Thank you so much, this is very helpful! One of the items that I'd been thinking about was about how to break up some of the modules into their own .rb and include them - I hadn't gotten that far in my learning yet and your implementation sheds light on it.

As you can see, right around the time that you posted this I'd updated my game to include magic, critical hits, and leveling. I think I'll extract the spells into their own .rb like you did with the player and monster classes and just call them in to keep things clean.

I like the way you thought about the game and how that ended up with you splitting off the room and player components, and what it meant for the kills.

Thanks again, this is a big help!

2

u/Digital-Ghost Sep 16 '14

This may be more of a question aimed at the more experienced developers here:

When a method takes arguments, why would you not opt to take a hash instead of a list that requires order to be exact? This seems like one area you could improve the code so that its not as brittle.

Please correct me if I am wrong.

1

u/MinervaDreaming Beginner Sep 16 '14

This is also something that I was thinking about, especially because as I've fleshed out this particular little game I now have 12 arguments for the Player class.

http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/

Right now I'm completing the refactor to get it split up into different .rbs and making it a bit cleaner - my next plan is to do exactly what you're talking about. If you don't get an answer here and I figure it out, I'll post my code if/when I can get it to work :)