A game called Bocce played on a Cartesian grid You may know Bocce, so you should know that I've tweaked the rules a little. Three types of balls are thrown: a red type, a blue type, and single black ball called the jack.

These are all on the Cartesian coordinate system. The coordinates of the jack are given, and also distances of an equal amount of red and blue balls from the origins of the system (0, 0). If a ball's distance is larger than the distance of the jack, and if the distance of this ball is larger than the distance of its peer, it's a score. The team with the highest score wins.

Here's the code:

#include &lt;iostream&gt;
#include &lt;map&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;cmath&gt;
#include &lt;array&gt;
#include &lt;algorithm&gt;

typedef std::map&lt;std::string, std::vector&lt;int&gt;&gt; bocce_balls;
typedef std::vector&lt;int&gt; int_vec;
typedef std::array&lt;int, 2&gt; int_arr;

std::string bocce(bocce_balls balls, int_arr jack)
{
    auto distance = std::sqrt(std::pow(jack[0], 2)+std::pow(jack[1], 2));
    int red_count = 0;
    int blue_count = 0;

    int_vec red = balls["red"];
    int_vec blue = balls["blue"];

    if (blue.size() != red.size())
    {
        std::cerr &lt;&lt; "Balls were different numbers!" &lt;&lt; std::endl;
        return "Fail!";
    }

    std::sort(blue.begin(), blue.end());
    std::sort(red.begin(), red.end());

    for (int i = 0; i &lt; blue.size(); ++i)
    {
        if (red[i] &gt; distance &amp;&amp; red[i] &gt; blue[i])
            ++red_count;
        else if (blue[i] &gt; distance &amp;&amp; blue[i] &gt; red[i])
            ++blue_count;
    }


    if (red_count &gt; blue_count)
        return "Red Wins!";
    else if (blue_count &gt; red_count)
        return "Blue Wins!";
    else
        return "Draw!";

}



int main() {

    bocce_balls insert;
    insert["blue"] = {2, 9, 3, 4, 5, 1, 10, 2};
    insert["red"] = {20, 11, 3, 4, 3, 1, 2, 3};
    int_arr dist = {2, 2};

    std::cout &lt;&lt; bocce(insert, dist) &lt;&lt; std::endl;
    return 0;
}
Bocce, as far as I know it, counts all the balls of one player that are closer to the jack than the closest one of his opponent.

That would mean that the distances from origin are irrelevant, instead, you need the distances from the balls to the jack:

auto dx = ball.x - jack.x;
auto dy = ball.y - jack.y;
auto distance2 = dx*dx + dy*dy;


Note that I avoided std::pow in favour to multiplying with itself, as std::pow inefficient for this purpose.

Note, too, that as the root function is strictly monotonely rising, you don't need to calculate it, but you can compare the squares as well and still will get the same results.

I personally strongly recommend that you create a class Ball containing three members: x, y for the coordinates and distance for the distance to jack:

class Ball
{
     int x, y;
     int distanceToJack2;
};


I'm not too happy with int as data type - it depends on the range of valid coordinates, though, if it is suitable or not. Assuming we have 32-bit int, then if the valid ranges for x and y can be covered with 16 bit, you will be safe from overflow while multiplying (this would yield undefined behaviour!), otherwise you'd need to calculate in next larger data type. For selecting the correct data types more safely, you might prefer the data types from &lt;cstdint&gt; header, e. g.:

 int32_t x, y;
 int64_t distanceToJack2;


Or you simply have coordinates and distance in double right away. Be aware that with double, you can quickly get issues due to rounding, so if you compare for one value being less than the other, you should consider them only so if difference is larger than some yet to be defined epsilon value (minimum distance for two values for not considering them equal). You could get some hints to from this question. Sure, it is java, but as long as both C++ and Java follow IEEE 754 for floating point values, it applies for both languages equally. On the other hand, you could then use std::hypot for calculating the distances in a very safe manner (thanks Toby for the hint).

Then you might add:

 void Ball::calculateDistance(Ball const&amp; jack)
 {
     uint64_t dx = static_cast&lt;uint64_t&gt;(x) - jack.x;
     uint64_t dy = static_cast&lt;uint64_t&gt;(y) - jack.y;
     distanceToJack2 = dx*dx + dy*dy;
 }


Finally, you can sort your balls according to the distances to jack and select all balls from one vector of which the distance is smaller than the distance of the first element in the respective other vector.

One last point: std::string is a rather bad key type for the map if you only have two fix values (beware of typos, additionally, map mangement is rather expensive). You might prefer an enum instead: enum class Color { RED, BLUE }; and use this one as key instead. Even better: don't use a map at all, instead:

std::string bocce(std::vector&lt;Ball&gt;&amp; red, std::vector&lt;Ball&gt;&amp; blue, Ball const&amp; jack);


Note that as you need to calculate the distances, which are stored in the balls, you cannot have const references for the two vectors (which otherwise would have been preferrable). From point of view of design, this is at least questionable (but at least, it's quite some improvement already). Ideally, the design would allow to pass in const vectors, but that would require some overhead, so for pragmatism, we might decide to live with the current design...