class PlayItSafe extends Strategy
{
    public int count, othercount, pot, bestBid, myShares, totalShares;
    public int potentialTake, potentialShares, biddingOpps, bidOneOpps;
    public int safetyScore, bestSafetyScore;
    public int equalOpps[], smallerOpps[], skip[];

    void priv_init()
    {
        equalOpps = new int[opponents];
        smallerOpps = new int[opponents];
        skip = new int[opponents];
    }

    // Bid 3 to start.
    public int initial_turn()
    {
        return 3;
    }

    public int turn(int last_turns[], int scores[], int carryOver, int turn_no)
    {
        // Assume everyone will bid the same amount as they did last time.  Calculate
        // the pot + shares, and determine the best bid in terms of take divided by risk
        pot = carryOver + 1;
        biddingOpps = 0;
        bidOneOpps = 0;

        // Clear the arrays
        for (count = 0; count < opponents; count++)
        {
            equalOpps[count] = 0;
            smallerOpps[count] = 0;
            skip[count] = 0;
        }

        for (count = 1; count < opponents; count++)
        {
            // If this strategy didn't bid, skip it to save time...
            if (last_turns[count] == 0)
                skip[count] = 1;

            // Otherwise, take the data
            else
            {
                // Calculate the anticipated pot
                pot = pot + last_turns[count];

                // Count the number of opponents who bid
                biddingOpps++;

                // If the bid was one, note that someone bid that.
                if (last_turns[count] == 1) bidOneOpps++;

                // Find how many bids are smaller (or equal), and how many are
                // one larger
                for (othercount = (count+1); othercount < opponents; othercount++)
                {
                    if (last_turns[othercount] > 0)
                    {
                        // Credit the appropriate boxes for each comparison
                        if (last_turns[count] == last_turns[othercount])
                        {
                            equalOpps[count]++;
                            equalOpps[othercount]++;
                            skip[othercount] = 1;
                        }
                        else if (last_turns[count] > last_turns[othercount])
                            smallerOpps[count]++;
                        else if (last_turns[count] < last_turns[othercount])
                            smallerOpps[othercount]++;
                    }
                }
            }
        }

        // Count the number of shares before I bid (the +1 is the point for the strategy itself)
        totalShares = 0;
        for (count = 1; count < opponents; count++)
        {
            if (last_turns[count] > 0)
                totalShares = totalShares + smallerOpps[count] + equalOpps[count] + 1;
        }

        // Now decide what to bid
        bestSafetyScore = 0;
        bestBid = 0;
        for (count = 1; count < opponents; count++)
        {
            // Don't check this case if zero, or if we checked an equal case
            if (skip[count] == 0)
            {
                // Calculate the total number of shares if I bid the same
                potentialShares = totalShares + (biddingOpps - smallerOpps[count]) + equalOpps[count] + 2;

                // Calculate the number of shares I would get
                myShares = equalOpps[count] + smallerOpps[count] + 2;

                // Calculate my take if I bid this
                potentialTake = ((myShares * (pot + last_turns[count]))/potentialShares) - last_turns[count];

		// Calculate the safety score = 100 * potentialTake / myBid
		safetyScore = ((100 * potentialTake) / last_turns[count]);

                // If this is the safety score record the bid
                if (safetyScore > bestSafetyScore)
                {
                    bestSafetyScore = safetyScore;
                    bestBid = last_turns[count];
                }
            }
        }

        // If no one bid 1, check that bid...
        if (bidOneOpps == 0)
        {
            // Calculate the total number of shares if I bid one
            potentialShares = totalShares + biddingOpps + 1;

            // Calculate the number of shares I would get
            myShares = 1;

            // Calculate my take if I bid this
            potentialTake = ((myShares * (pot + 1)) / potentialShares) - 1;

	    // Calculate the safety score = 100 * potentialTake
	    safetyScore = (100 * potentialTake);

            // If this is the best safety score, record the bid
            if (safetyScore > bestSafetyScore) bestBid = 1;
        }

        // OK, we have the best bid now - so bid it
        return (bestBid);

    }
}

