Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7637945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:03:26+00:00 2026-05-31T08:03:26+00:00

Following the tutorial Zed A. Shaw , I’m writing a soccer game. My code

  • 0

Following the tutorial Zed A. Shaw, I’m writing a soccer game. My code so far is as follows:

$team_a_players = ["Basar", "Mehmet", "Abdullah", "Alpaslan", "Salih", "Recep", "Ibrahim", "Orhan", "Hakki", "Yakup", "Serdar"]
$team_a_substitutes = ["Hasan", "Turgay", "Umit"]
$team_b_players = ["Habib", "Erkan", "Sahin", "Cemal", "Ahmet", "Fikret", "Yucel", "Pergel", "Ali", "Sabri", "Yilmaz"]
$team_b_substitutes = ["Abdulkadir", "Gokhan", "Mustafa"]
$yellow = []
$red = []
$reasons = ["corner", "direct attack", "free kick", "side attack", "speed kick"]
$team_a_attack = 90.0
$team_a_defense = 80.0
$team_b_attack = 70.0
$team_b_defense = 60.0
$team_a_goals = 0
$team_b_goals = 0

def prompt()
  print "> "
end

def dice()
  if rand(2) == 0
    round_team_a()

  else
    round_team_b()
  end
end

def fauls()
  if rand(0) > 0.95 and rand(10) % 2 == 0
    faul_player = $team_a_players[rand(11)]
    if $yellow.include?(faul_player)
      $red.push(faul_player)
      $team_a_players.delete("faulplayer")
      puts "#{faul_player} of Team A gets a red card in #{$i}. minute!"
      puts "Who would you like to substitute in place of #{faul_player}?"
      list_subs_a()
      prompt()
      substitute = STDIN.gets.chomp()
      $team_a_players.push("substitute")
      $yellow.delete(faul_player)

    else
      $yellow.push(faul_player)
      puts "#{faul_player} of Team A gets a yellow card in #{$i}. minute!"
    end

  elsif rand(0) > 0.95 and rand(10) % 2 == 1
    faul_player = $team_b_players[rand(11)]
    if $yellow.include?(faul_player)
      $red.push(faul_player)
      $team_b_players.delete("faulplayer")
      puts "#{faul_player} of Team B gets a red card in #{$i}. minute!"
      puts "Who would you like to substitute in place of #{faul_player}?"
      list_subs_b()
      prompt()
      substitute = STDIN.gets.chomp()
      $team_b_players.push("substitute")
      $yellow.delete(faul_player)

    else
      $yellow.push(faul_player)
      puts "#{faul_player} of Team B gets a yellow card in #{$i}. minute!"
    end

  else
    faul_player = nil
  end
end

def list_subs_a()
  $team_a_substitutes.each {|p| puts p}
end

def list_subs_b()
  $team_b_substitutes.each {|p| puts p}
end

def list_yellow()
  $yellow.each {|p| puts p}
end

def list_red()
  $red.each {|p| puts p}
end

def round_team_a()
  score = $team_a_attack / $team_b_defense * rand(0)
  if score > 1
    goal = 1
    $team_a_goals += 1
    reason = $reasons[rand(5)]
    puts "Team A scored #{goal} goal through a #{reason} in #{$i}. minute!"

  else
    goal = 0
  end
end

def round_team_b()
  score = $team_b_attack / $team_a_defense * rand(0)
  if score > 1
    goal = 1
    $team_b_goals += 1
    reason = $reasons[rand(5)]
    puts "Team B scored #{goal} goal through a #{reason} in #{$i}. minute!"

  else
    goal = 0
  end
end

def match()
  $i = 0
  until $i > 59 do
    dice()
    fauls()
    $i += 1
  end
  puts "Team A scored a total of #{$team_a_goals} goals and Team B scored a total of #{$team_b_goals} goals."
  if $team_a_goals > $team_b_goals
    puts "Team A won against Team B by #{$team_a_goals}:#{$team_b_goals}!"

  elsif $team_b_goals > $team_a_goals
    puts "Team B won against Team A by #{$team_b_goals}:#{$team_b_goals}!"

  else
    puts "It's a tie!"
  end

  if $yellow.length > 0
    puts "Players shown a yellow card are:"
    list_yellow() 

  else
    puts "No yellow cards in the end of the game"
  end

  if $red.length > 0
    puts "Players shown a red card are:"
    list_red()

  else
    puts "No red cards in the end of the game"
  end
end

match()

From here, I would like to do the following:

  • Replace the arrays $yellow and $red with hashes so that I can also report minutes and teams of yellow- and red-cards.
  • Replace the arrays starting with the name $team_ with hashes so that I can add individualized attack- and defense-powers to players so that substitutions mean sth. but before the code gets any more complex, I have to solve sth. This looks similar to this question concerning php.
  • Define the functions list, round and faul in a way that can be used common to a_players and b_players . I tried doing team_#{team}_players instead of team_a_players etc, but cannot achieve it.

What I seek is a guide to that problem, not a solution, so that I can fix this myself. A link or long explanation in clear words is very much more welcome than a fixed code. And please note that the tutorial has not mentioned classes yet, so this is not an option yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:03:27+00:00Added an answer on May 31, 2026 at 8:03 am

    The basic idea you haven’t seemed to grasp is passing arguments to functions.
    Assume we have two global variables and we wish to perform identical operations on them – say multiply elements of arrays by 2.
    You write:

    $a = [1,2,3]
    $b = [2,3,4]
    
    def multiply_a
      result = []
      for element in $a do
        result << element * 2
      end
      result
    end
    
    def multiply_b
      result = []
      for element in $b do
        result << element * 2
      end
      result
    end
    

    But this code is very bad. First of all, you should note that in Ruby $a is a special variable – a global variable. You should never need to use them – writing code containing them means that there is something wrong with it. So how to fix it?
    The basic idea is to pass an argument to a function, instead of hard coding the variable, the function operates on. So you can transform the code as follows:

    a = [1, 2, 3]
    b = [2, 3, 4]
    def multiply(argument)
      result = []
      for element in argument do
        result << element * 2
      end
      result
    end
    # and then call
    multiply(a) # instead of multiply_a
    multiply(b) # instead of multiply_b 
    

    This is the way you should fix your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my following tutorial code from Learning PHP MYSQL and Javascript by OReilly.
Using the code at the following tutorial, http://www.zenbrains.com/blog/en/2010/05/detectar-cuando-se-selecciona-una-anotacion-mkannotation-en-mapa-mkmapview/ , I was able to add
I have been playing with the source code available from the following tutorial .
Following StockWatcher tutorial, http://code.google.com/webtoolkit/doc/latest/tutorial/debug.html When I run the app in debug mode, my Safari
I am following the following tutorial ( http://www.highoncoding.com/Articles/642_Creating_a_Stock_Widget_in_ASP_NET_MVC_Application.aspx ) on using ajax to render
I am doing the following tutorial http://msdn.microsoft.com/en-us/library/ms731835%28v=vs.100%29.aspx and the program is working perfectly fine.
I'm following tutorial, installed RanvenDB embeeded and write: public static IDocumentStore archives = new
I'm using the following tutorial http://developer.android.com/resources/tutorials/views/hello-mapview.html in order to create a map view and
Following this tutorial and running into trouble. [TestMethod] [ExpectedException(typeof(Exception))] public void VerifyPropertyNameMethod_NonExistentPropertyString_ThrowsException() { var
Following this tutorial (http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application), I learned how to save data and do concurrency checks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.