HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyMinor

Cleaning up code and making better loops for soccer shoot-out game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
soccerandloopsbettergameforcodemakingcleaningshoot

Problem

I'll start by saying I've just started with programming, and Ruby is my first language. I've made a little soccer shoot-out game. I'm having problems with the end of the code looping back to where I want it.

If there's a tie, I want it to keep the same score and players, but add another round of shooting. Should I do this by creating an array to store the points?

If there's anything else I can simplify, I'd really appreciate your input.

```
class Shoot
def initialize
shoot
end

def shoot
@kick = 1 + rand(6)
end

def kick
@kick
end
end

response = ''
until response == 'N'
play = "Have another shoot-out? Put Y or N."

puts "Player 1's name is:"
player_1_name = gets.chomp

puts "Player 2's name is?"
player_2_name = gets.chomp

puts "#{player_1_name} and #{player_2_name}, are you ready to play sudden death (ENTER)?!"
gets.chomp

p1_score = 0
p2_score = 0
result_1 = 0
result_2 = 0

until result_1 >= 1 || result_2 >= 1
puts "#{player_1_name}, please take your kick (ENTER)."
gets.chomp!
shot_1 = Shoot.new.kick
result_1 = shot_1*0.2
puts "#{player_1_name} takes the shot, and it's looking good..."
puts "(ENTER)"
gets.chomp

if result_1 >= 1
puts "...And it's a goal!!!"
p1_score += 1
else
puts "...Oh! So close, but it's no good!!!"
end

puts "#{player_2_name}, please take your kick (ENTER)."
gets.chomp!

shot_2 = Shoot.new.kick
result_2 = shot_2*0.2
puts "#{player_2_name} takes the shot, and it's looking good..."
puts "(ENTER)"
gets.chomp

if result_2 >= 1
puts "...And it's a goal!!!"
p2_score += 1
else
puts "...Oh! So close, but it's no good!!!"
end

puts "#{player_1_name}: #{p1_score}"
puts "#{player_2_name}: #{p2_score}"

if p1_score > p2_score
puts "#{player_1_name}, you're the winner! Congratulations!"
elsif p1_score < p2_score
puts "#{player_2_name}, you're the winner! Congratulations!"
else
puts "Looks like we've gotta go another round!"
return
end
end

put

Solution

Not a bad start. However here's a few things that immediately pops out when I look at the code.

  • Avoid duplication: The code for both players to kick is exactly the same. You should try to move that into a function instead of writing it out twice.



  • Make a player class: By moving all the code for a player into a class, you avoid having to keep each variable twice. Instead of shot_1/shot_2, result_1/result_2 etc, you could have player[0].shot, player[0].result etc.



  • Make a game class: While you're at it, why not also turn the game itself into a class. I think this would solve your looping problem all by itself.



Something like this:

class Game
  def initialize
    # set up for a new game
  end

  def play_round
    # play one round of the game
  end
end

Code Snippets

class Game
  def initialize
    # set up for a new game
  end

  def play_round
    # play one round of the game
  end
end

Context

StackExchange Code Review Q#33828, answer score: 4

Revisions (0)

No revisions yet.