Offside Detection Algorithm

In a game of football, one of the most complex rule is the offside rule. One of the roles of the assistant referees is to detect when a player is offside.

A player is in an offside position if, when the ball is played by a team-mate, they are nearer to the opposition’s goal line than both the ball and the second last defender (the last defender being in most cases the goal keeper).

Note that players cannot be offside in their own half of the field.

In this challenge, we assume that the blue team has the ball and want to identify if any of the players of the blue team is in a position of off-side. We will assume that the ball is being played by a team-mate positioned behind them.

Python Challenge


In this challenge you will design an algorithm to detect if a player is offside. You will implement this algorithm in Python by completing the code provided below.

Your algorithm will need to:

  • Loop through all the players of the red team (away team) to find out the y coordinate of the second last defender,
  • Loop through all the players of the blue team (home team) to find out the y coordinate of the most forward player,
  • Compare both these y coordinates to decide if there is a position of offside,
  • Output a message on screen or draw a horizontal line to show which player is offside.


unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.8 / 5. Vote count: 4

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!