Pascal: Calculating darts result

I believe that all of us seen or maybe even played darts. Everyone knows the circles which are numbered with points which you get by throwing the arrow. How could we calculate the result of our game if we get 10 points by hitting the center and then a minus one on each other circle on the darts board. And if we do not hit the board, we do not get any points which simply corresponds to zero.

You can see an example at this picture:

Darts board example

Our input information is the number of taken shots and the coordinates of shoots in coordinate system. And the main point of this program is to calculate a sum of our taken shots and output it on screen.

Our first step should be making a function which will count points given for one shot. We will be using a mathematical equation of circle. The equation:  x*x + y*y = r*r where x and y are the coordinates of point in coordinates system. So we are checking where our dart is pointed (the right circle) and how many points we need to give for that circle. For example, if we hit the seventh circle, we get 7 points and so on. I left two variables to determine this action for learning purposes. You can see whole function below:

function calc_points(r,x, y:integer):integer;
  var points,               // the count of points
  which:integer;      // circle number
    points := 10; which := 10; // initial values
    while which >= 1 do
        if x*x + y*y <= r*r
         then begin
           calc_points := points;
        end else begin
          points := points - 1;
          r := r * 2;
         which := which - 1;
    if points <= 0
      then calc_points := 0;

Now we just need to add all shots to one sum variable and that’s all. Here is the main program code:

WriteLn('Input y: ');
ReadLn(y); // initial circle radius
WriteLn('Write the count of shots: ');
WriteLn('Input coordinates: ');
for i := 1 to shots do
    Read(a, b);
    points := points + calc_points(y, a, b);
WriteLn('You get: ', points);

All variables are integers.

This program example also gets an initial circle radius from the person using the program, so it is handy for different types of dart’s boards. In addition, as I mentioned earlier we can abandon which variable which is the same as points in our calc_points function.

Leave a comment

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. Cookies are only used for statistical purposes.