/** * Returns the score for a single tile value. Tiles with value less * than 3
ID: 3736569 • Letter: #
Question
/**
* Returns the score for a single tile value. Tiles with value less
* than 3 have score zero. All other values result from starting with
* value 3 and doubling N times, for some N; the score is 3 to the power N + 1.
* For example: the value 48 is obtained from 3 by doubling N = 4
* times (48 / 3 is 16, which is 2 to the 4th), so the score is 3
* to the power 5, or 243.
* @param value
* tile value for which to compute the score
* @return
* score for the given gile value
*/
it will be tested with value 3 , 6, 12, 24, 48, 96, 192
public int getScoreForValue(int value)
{
// TODO
return 0;
}
/**
* Generate a new tile value using the given instance
* of Random. Values are generated such that there are
* 40% 1's, 40% 2's, 10% 3's, and 10% 6's.
* @param rand
* random number generator to use
* @return
* the value 1, 2, 3, or 6 with the specified probability
*/
public int generateRandomTileValue(Random rand)
{
// TODO
return 0;
}
Explanation / Answer
public int getScoreForValue(int value)
{
if(value < 3) return 0
else {
int div = value/3;
int sqrt = floorSqrt(div);
return 2^(sqrt + 1);
}
}
// Returns floor of square root of x
public int floorSqrt(int x)
{
// Base cases
if (x == 0 || x == 1)
return x;
// Staring from 1, try all numbers until
// i*i is greater than or equal to x.
int i = 1, result = 1;
while (result < x) {
if (result == x)
return result;
i++;
result = i * i;
}
return i - 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.