Are there any string functions in Renderscript? Like vsprintf, for example?
Specifically, I'd like to convert a float to a string. Do I have to write that from scratch?
Thanks!
Sorry, here's a better one. It'll work for integers as well but they have ".000" added on.
char stringBuffer[50];
static const int MAX_STRING_LENGTH = sizeof(stringBuffer) - 1;
void drawFloat(float value, int x, int y) {
int index = 0;
int scaledValue = (int)(value * 1000);
index = MAX_STRING_LENGTH;
stringBuffer[index] = 0;
while(scaledValue > 0 || index > MAX_STRING_LENGTH - 4) {
index--;
if(index == MAX_STRING_LENGTH - 4) {
stringBuffer[index--] = '.';
}
int digitValue = scaledValue % 10;
stringBuffer[index] = '0' + digitValue;
scaledValue /= 10;
}
if(value < 0) {
stringBuffer[index--] = '-';
}
rsgDrawText(&stringBuffer[index], x - 10, y + 5);
}
Couldn't find a simple way so...
void drawInteger(int value, int x, int y) {
char text[50] = "0";
int index = 0;
if(value != 0) {
index = 49;
text[index] = 0;
while(value > 0) {
index--;
int digitValue = value % 10;
text[index] = '0' + digitValue;
value /= 10;
}
if(value < 0) {
text[index--] = '-';
}
}
rsgDrawText(&text[index], x - 10, y + 5);
}
void drawFloat(float value, int x, int y) {
char text[50] = "0.000";
int index = 0;
if(value != 0) {
int integerPart = (int)(value * 1000);
index = 49;
text[index] = 0;
while(integerPart > 0) {
index--;
if(index == 45) {
text[index--] = '.';
}
int digitValue = integerPart % 10;
text[index] = '0' + digitValue;
integerPart /= 10;
}
if(value < 0) {
text[index--] = '-';
}
}
rsgDrawText(&text[index], x - 10, y + 5);
}
Related
Someone to help me? I'm a beginner. I'm trying to get the controller numbers from a textfield. For this I tried to convert the characters to int. Note: in the textfield the user will enter numbers up to three digits
This error appears: RangeError (index): Invalid value: Not in inclusive range 0..13: 14
validate(textController)
{
List textCode = List();
List messageInCode = List();
int validateRepeticions;
var stringText = textController;
for (var b = 0; b < stringText.length; b++)
{
textCode.insert(b, stringText[b]);
}
validateRepeticions = textCode.length;
print(validateRepeticions);
int g = 0;
int k = 0;
do
{
bool comma = true;
List tryToParseInt = List();
int d = 0;// 'd' is responsible for identifying the positional value of a digit
int a = 1;// 'a' increment values to k
int c = 0;// 'c' defines the positions used in tryToParseInt
int completeNumber;
tryToParseInt.insert(c, int.tryParse(textCode[k]));
if (tryToParseInt[c] == null)
{
validateRepeticions--;
}
else
{
d++;
c++;
do
{
tryToParseInt.insert(c, int.tryParse(textCode[k+a]));
if (tryToParseInt[c] == null)
{
completeNumber = tryToParseInt[0];
messageInCode.insert(g, completeNumber);
g++;
comma = false;
validateRepeticions -= 2;
}
else
{
d++;
a++;
c++;
tryToParseInt.insert(c, int.tryParse(textCode[k+a]));
if (tryToParseInt[c] == null)
{
completeNumber = tryToParseInt[0]*10 + tryToParseInt[1];
messageInCode.add(completeNumber);
g++;
comma = false;
validateRepeticions -= 3;
d++;
}
else
{
d++;
completeNumber = tryToParseInt[0]*100 + tryToParseInt[1]*10 + tryToParseInt[2];
messageInCode.add(completeNumber);
g++;
comma = false;
validateRepeticions -= 3;
}
}
} while (comma);
}
d++;
k += d;
} while (validateRepeticions >= 0);
}
Updated code
validate(textEditingController) {
int validateRepeticions = textEditingController.text.length;
List textString = textEditingController.text.split('');
List messageInCode = List();
int k = 0;
do {
bool comma = true;
List tryToParseInt = List();
int d = 0; // 'd' is responsible for identifying the value of a number that has already been verified
int a = 1; // 'a' increment values to k
int c = 0; // 'c' defines the positions used in tryToParseInt
int completeNumber;
tryToParseInt.insert(c, int.tryParse(textString[k]));
if (tryToParseInt[c] == null) {
validateRepeticions--;
} else {
d++; //1
c++; //1
do {
tryToParseInt.insert(c, int.tryParse(textString[k + a]));
if (tryToParseInt[c] == null) {
completeNumber = tryToParseInt[0];
messageInCode.add(completeNumber);
comma = false;
validateRepeticions -= 2;
} else {
d++; //2
a++; //2
c++; //2
tryToParseInt.insert(c, int.tryParse(textString[k + a]));
if (tryToParseInt[c] == null) {
completeNumber = tryToParseInt[0] * 10 + tryToParseInt[1];
messageInCode.add(completeNumber);
comma = false;
validateRepeticions -= 3;
d++;
} else {
d++;
completeNumber = tryToParseInt[0] * 100 + tryToParseInt[1] * 10 + tryToParseInt[2];
messageInCode.add(completeNumber);
comma = false;
validateRepeticions -= 3;
}
}
} while (comma);
}
d++;
k += d;
} while (validateRepeticions > 0);
}
Now it's almost working, although an error still appears:
The method '_addFromInteger' was called on null.
Receiver: null
Tried calling: _addFromInteger(0)
validate(textEditingController) {
int validateRepeticions = textEditingController.text.length;
List textString = textEditingController.text.split('');
List messageInCode = List();
int k = 0;
messageInCode.clear();
do {
bool comma = true;
List tryToParseInt = List();
int d = 0; // 'd' is responsible for identifying the positional value of a digit
int a = 0; // 'a' increment values to k
int c = 0; // 'c' defines the positions used in tryToParseInt
int completeNumber;
if (k < textEditingController.text.length) {
tryToParseInt.insert(c, int.tryParse(textString[k]));
if (tryToParseInt[c] == null) {
validateRepeticions--;
} else {
d++; //1 these comments are examples
c++; //1
a++; //1
do {
if ((k + a) < textEditingController.text.length) {
tryToParseInt.insert(c, int.tryParse(textString[k + a]));
if (tryToParseInt[c] == null) {
completeNumber = tryToParseInt[0];
messageInCode.add(completeNumber);
comma = false;
validateRepeticions -= 2;
} else {
d++; //2
a++; //2
c++; //2
if ((k + a) < textEditingController.text.length) {
tryToParseInt.insert(c, int.tryParse(textString[k + a]));
if (tryToParseInt[c] == null) {
completeNumber = tryToParseInt[0] * 10 + tryToParseInt[1];
messageInCode.add(completeNumber);
comma = false;
validateRepeticions -= 3;
d++;
} else {
d++;
completeNumber = tryToParseInt[0] * 100 + tryToParseInt[1] * 10 + tryToParseInt[2];
messageInCode.add(completeNumber);
comma = false;
validateRepeticions -= 3;
}
} else {
completeNumber = tryToParseInt[0] * 10 + tryToParseInt[1];
messageInCode.add(completeNumber);
comma = false;
validateRepeticions -= 3;
d++;
}
}
} else {
completeNumber = tryToParseInt[0];
messageInCode.add(completeNumber);
comma = false;
validateRepeticions -= 2;
}
} while (comma);
}
} else {
validateRepeticions--;
}
d++;
k += d;
} while (validateRepeticions > 0 && k < textEditingController.text.length);
}
In your function, I'm assuming the param textController is the TextEditingController. In that case you need to check the length with textController.text.length
To create a list of characters from the string, you just need to use split() instead of creating a new list and loop through the string. For example: textController.text.split('')
Replace textCode.insert(b, stringText[b]); with textCode.add(stringText[b]);
in order to interpolate 2 values, I can use
lerp(int a, int b) {
return (a + b) / 2;
}
Now imagine I've an array(1, 30, 100, 300) and I want to interpolate it to array in size N (N=10 for example).
If N == 7, then:
1,15,30,65,100,200,300
I've no idea how to interpolate 4 values to be 10. I need a method that looks like:
interpolate(fina int[] input, final int newSize) {
int[] res = new int[newSize];
...
return res;
}
that works even on my example above with newSize of 7, 10 or whatever.
Any idea how to implement it?
SOLVED.
public static double[] interpolate(double[] x, int newLength) {
double[] y = null;
if (newLength > 0) {
int N = x.length;
if (N == 1) {
y = new double[1];
y[0] = x[0];
return y;
} else if (newLength == 1) {
y = new double[1];
int ind = (int) Math.floor(N * 0.5 + 0.5);
ind = Math.max(1, ind);
ind = Math.min(ind, N);
y[0] = x[ind - 1];
return y;
} else {
y = new double[newLength];
double Beta = ((double) newLength) / N;
double newBeta = 1.0;
if (newLength > 2)
newBeta = (N - 2.0) / (newLength - 2.0);
y[0] = x[0];
y[1] = x[1];
y[newLength - 1] = x[N - 1];
double tmp, alpha;
int i, j;
for (i = 2; i <= newLength - 2; i++) {
tmp = 1.0 + (i - 1) * newBeta;
j = (int) Math.floor(tmp);
alpha = tmp - j;
y[i] = (1.0 - alpha) * x[Math.max(0, j)] + alpha * x[Math.min(N - 1, j + 1)];
}
}
}
return y;
}
/**
* Find the maximum of all elements in the array, ignoring elements that are NaN.
* #param data
* #return
*/
public static double max(double[] data) {
double max = Double.NaN;
for (int i = 0; i < data.length; i++) {
if (Double.isNaN(data[i]))
continue;
if (Double.isNaN(max) || data[i] > max)
max = data[i];
}
return max;
}
public static int max(int[] data) {
int max = data[0];
for (int i = 1; i < data.length; i++) {
if (data[i] > max)
max = data[i];
}
return max;
}
I'm trying to implement adaptive thresholding algorithm by Derek Bradley using Android. But it is returning black pixels all the time. Here is my code snippet. Please suggest me about what should I do. Thanks in advance.
public static Bitmap GrayscaleToBin(Bitmap bm2)
{
Bitmap bm;
bm=bm2.copy(Config.ARGB_8888, true);
final int width = bm.getWidth();
final int height = bm.getHeight();
int[] pixels;
pixels = new int[width*height];
bm.getPixels(pixels,0,width,0,0,width,height);
//Bradley AdaptiveThrsholdging
int []intImg= new int[width*height];
int sum=0;
for(int i=0;i<width;++i){
sum=0;
for(int j=0;j<height;++j)
{
sum=sum+pixels[i+j*width];
if(i==0){intImg[i+j*width]=sum;}
else
{
intImg[i+j*width]= intImg[i-1+j*width]+sum;
}
}
}
int x1,x2,y1,y2=0,count=0;
int s=width >> 3;
int t=15;
for(int i=0;i<width;++i)
{
for(int j=0;j<height;++j)
{
x1=i-s/2;
x2=i+s/2;
y1=j-s/2;
y2=j+s/2;
if (x1 <0) x1 = 0;
if (x2>= width) x2 = width-1;
if (y1 <0) y1 = 0;
if (y2>= height) y2 = height-1;
count = (x2-x1) * (y2-y1);
sum = intImg [y2 * width + x2] -
intImg [y1 * width + x2] -
intImg [y2 * width + x1] +
intImg [y1 * width + x1];
if((pixels[i+j*width]*count)<=(sum*(100-t)/100))
{
pixels[i+j*width]=0;
}
else
{
pixels[i+j*width]=255;
}
}
}
/*---------------------------------------------------------------------------*/
bm.setPixels(pixels,0,width,0,0,width,height);
// Log.d("cdsfss","afterloop");
return bm;
}
After a Long struggle I have solved the issue with the following code.
public static Bitmap GrayscaleToBin(Bitmap bm2)
{
Bitmap bm;
bm=bm2.copy(Config.RGB_565, true);
final int width = bm.getWidth();
final int height = bm.getHeight();
int pixel1,pixel2,pixel3,pixel4,A,R;
int[] pixels;
pixels = new int[width*height];
bm.getPixels(pixels,0,width,0,0,width,height);
int size=width*height;
int s=width/8;
int s2=s>>1;
double t=0.15;
double it=1.0-t;
int []integral= new int[size];
int []threshold=new int[size];
int i,j,diff,x1,y1,x2,y2,ind1,ind2,ind3;
int sum=0;
int ind=0;
while(ind<size)
{
sum+=pixels[ind] & 0xFF;
integral[ind]=sum;
ind+=width;
}
x1=0;
for(i=1;i<width;++i)
{
sum=0;
ind=i;
ind3=ind-s2;
if(i>s)
{
x1=i-s;
}
diff=i-x1;
for(j=0;j<height;++j)
{
sum+=pixels[ind] & 0xFF;
integral[ind]=integral[(int)(ind-1)]+sum;
ind+=width;
if(i<s2)continue;
if(j<s2)continue;
y1=(j<s ? 0 : j-s);
ind1=y1*width;
ind2=j*width;
if (((pixels[ind3]&0xFF)*(diff * (j - y1))) < ((integral[(int)(ind2 + i)] - integral[(int)(ind1 + i)] - integral[(int)(ind2 + x1)] + integral[(int)(ind1 + x1)])*it)) {
threshold[ind3] = 0x00;
} else {
threshold[ind3] = 0xFFFFFF;
}
ind3 += width;
}
}
y1 = 0;
for( j = 0; j < height; ++j )
{
i = 0;
y2 =height- 1;
if( j <height- s2 )
{
i = width - s2;
y2 = j + s2;
}
ind = j * width + i;
if( j > s2 ) y1 = j - s2;
ind1 = y1 * width;
ind2 = y2 * width;
diff = y2 - y1;
for( ; i < width; ++i, ++ind )
{
x1 = ( i < s2 ? 0 : i - s2);
x2 = i + s2;
// check the border
if (x2 >= width) x2 = width - 1;
if (((pixels[ind]&0xFF)*((x2 - x1) * diff)) < ((integral[(int)(ind2 + x2)] - integral[(int)(ind1 + x2)] - integral[(int)(ind2 + x1)] + integral[(int)(ind1 + x1)])*it)) {
threshold[ind] = 0x00;
} else {
threshold[ind] = 0xFFFFFF;
}
}
}
/*-------------------------------
* --------------------------------------------*/
bm.setPixels(threshold,0,width,0,0,width,height);
return bm;
}
You can use Catalano Framework. There's an example using Bradley for Android in samples folder.
FastBitmap fb = new FastBitmap(bitmap);
fb.toGrayscale();
BradleyLocalThreshold bradley = new BradleyLocalThreshold();
bradley.applyInPlace(fb);
bitmap = fb.toBitmap();
* Coded Edited with Fix * I found an open source project that has a few bugs (I have fixed a couple so far) but this one has me stumped. The issue that I am trying to solve... If I have 3 sets of pairs (isThreePair) and it is also a 4 of a Kind (example: 4ea - 3's and 2ea 4's) it always chooses the 4 of a kind (isFourOfAKind), I should be able to pick either. Can someone please tell me what is wrong with the code.
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
final class Scorer {
private Scorer() {
}
public final static int calculate(int[] array, boolean ignoreExtraDice,
Context c) {
int length = array.length;
if (length == 0)
return 0;
int[] newArray = count(array);
if (length == 6) {
int straightStatus = isStraight(newArray, c, false);
if (straightStatus > 0) {
return straightStatus;
}
int threePairStatus = isThreePair(newArray, c, false);
if (threePairStatus > 0)
return threePairStatus;
int sixOfAKindStatus = isSixOfAKind(newArray, c);
if (sixOfAKindStatus > 0)
return sixOfAKindStatus;
int doubleTripletStatus = isDoubleTriplet(newArray, c);
if (doubleTripletStatus > 0)
return doubleTripletStatus;
}
int fiveOfAKindStatus = 0;
if (length == 5)
fiveOfAKindStatus = isFiveOfAKind(newArray, c, length)[0];
if (fiveOfAKindStatus > 0)
return fiveOfAKindStatus;
int fourOfAKindStatus = 0;
if (length == 4)
fourOfAKindStatus = isFourOfAKind(newArray, c, length)[0];
if (fourOfAKindStatus > 0)
return fourOfAKindStatus;
int threeOfAKindStatus = 0;
if (length == 3)
threeOfAKindStatus = isThreeOfAKind(newArray, c, length)[0];
if (threeOfAKindStatus > 0)
return threeOfAKindStatus;
int combinationScore = scoreCombinations(newArray, c, length);
if (combinationScore >= 0 && !ignoreExtraDice)
return 0;
else
return Math.abs(combinationScore);
}
private final static int[] count(int[] array) {
int numOf1 = 0;
int numOf2 = 0;
int numOf3 = 0;
int numOf4 = 0;
int numOf5 = 0;
int numOf6 = 0;
for (int j = 0; j < array.length; j++) {
switch (array[j]) {
case (1):
numOf1++;
break;
case (2):
numOf2++;
break;
case (3):
numOf3++;
break;
case (4):
numOf4++;
break;
case (5):
numOf5++;
break;
case (6):
numOf6++;
break;
}
}
int[] newArray = { numOf1, numOf2, numOf3, numOf4, numOf5, numOf6 };
return newArray;
}
public final static int isStraight(int[] array, Context c, boolean calledPublicly) {
if (array.length < 6)
return 0;
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(c);
boolean toScoreStraight = prefs.getBoolean("toScoreStraightPref", true);
if (!toScoreStraight)
return 0;
int straightScore = 0;
if (toScoreStraight)
straightScore = Integer.valueOf(prefs.getString("straightPref",
"1500"));
if (calledPublicly)
array = count(array);
boolean isStraight = (array[0] == 1 && array[1] == 1 && array[2] == 1
&& array[3] == 1 && array[4] == 1 && array[5] == 1);
if (isStraight)
return straightScore;
else
return 0;
}
public final static int isThreePair(int[] array, Context c, boolean calledPublicly) {
if (array.length < 6)
return 0;
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(c);
int threePairScore = Integer.valueOf(prefs.getString("threePairPref",
"750"));
if (calledPublicly)
array = count(array);
int count = 0;
if (array[0] >= 2)
count += array[0] / 2;
if (array[1] >= 2)
count += array[1] / 2;
if (array[2] >= 2)
count += array[2] / 2;
if (array[3] >= 2)
count += array[3] / 2;
if (array[4] >= 2)
count += array[4] / 2;
if (array[5] >= 2)
count += array[5] / 2;
if (count == 3)
return threePairScore;
else
return 0;
}
private final static int isSixOfAKind(int[] array, Context c) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(c);
String sixKindPref = prefs.getString("sixOfAKindPref",
"4x the 3 Of A Kind Value");
int sixOfAKindMult = (sixKindPref.contains("3000")) ? 3000 : Integer
.valueOf("" + sixKindPref.charAt(0));
int score = 0;
if (array[0] == 6)
score = 10000;
if (array[1] == 6)
score = 200 * sixOfAKindMult;
if (array[2] == 6)
score = 300 * sixOfAKindMult;
if (array[3] == 6)
score = 400 * sixOfAKindMult;
if (array[4] == 6)
score = 500 * sixOfAKindMult;
if (array[5] == 6)
score = 600 * sixOfAKindMult;
if (sixOfAKindMult > 100)
return sixOfAKindMult;
if (score != 0)
return score;
else
return 0;
}
private final static int isDoubleTriplet(int[] array, Context c) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(c);
boolean toVariateTwoTripsScore = prefs.getBoolean(
"toScoreTwoTripsPref", false);
if (!toVariateTwoTripsScore)
return 0;
int twoTripsScore = 0;
if (toVariateTwoTripsScore)
twoTripsScore = Integer.valueOf(prefs.getString(
"twoTripletScorePref", "2500"));
int count = 0;
if (array[0] == 3)
count++;
if (array[1] == 3)
count++;
if (array[2] == 3)
count++;
if (array[3] == 3)
count++;
if (array[4] == 3)
count++;
if (array[5] == 3)
count++;
if (count == 2)
return twoTripsScore;
else
return 0;
}
private final static int scoreCombinations(int[] array, Context c, int length) {
int score = 0;
int[] fiveArray = isFiveOfAKind(array, c, length);
int[] fourArray = isFourOfAKind(array, c, length);
int[] threeArray = isThreeOfAKind(array, c, length);
int[] temp = null;
if (fiveArray[0] != 0)
temp = fiveArray;
else if (fourArray[0] != 0)
temp = fourArray;
else if (threeArray[0] != 0)
temp = threeArray;
if (temp != null) {
for (int i = 1; i <= 6; i++) {
if (temp[i] == 0)
array[i - 1] = 0;
}
}
score += fiveArray[0];
score += fourArray[0];
score += threeArray[0];
int scoreOfRemainders = scoreRemainders(array);
if (scoreOfRemainders > 0)
score *= -1;
score += scoreOfRemainders;
return score;
}
private final static int[] isFiveOfAKind(int[] array, Context c, int length) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(c);
String fiveKindPref = prefs.getString("fiveOfAKindPref",
"3x the 3 Of A Kind Value");
int fiveOfAKindMult = (fiveKindPref.contains("2000")) ? 2000 : Integer
.valueOf("" + fiveKindPref.charAt(0));
int score = 0;
if (array[0] == 5) {
score = 1000 * fiveOfAKindMult;
array[0] = 0;
}
if (array[1] == 5) {
score = 200 * fiveOfAKindMult;
array[1] = 0;
}
if (array[2] == 5) {
score = 300 * fiveOfAKindMult;
array[2] = 0;
}
if (array[3] == 5) {
score = 400 * fiveOfAKindMult;
array[3] = 0;
}
if (array[4] == 5) {
score = 500 * fiveOfAKindMult;
array[4] = 0;
}
if (array[5] == 5) {
score = 600 * fiveOfAKindMult;
array[5] = 0;
}
if (score != 0) {
if (fiveOfAKindMult > 100)
score = fiveOfAKindMult;
else if (length > 5)
score *= -1;
}
int[] returnArray = { score, array[0], array[1], array[2], array[3],
array[4], array[5] };
return returnArray;
}
private final static int[] isFourOfAKind(int[] array, Context c, int length) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(c);
String fourKindPref = prefs.getString("fourOfAKindPref",
"2x the 3 Of A Kind Value");
int fourOfAKindMult = (fourKindPref.contains("1000")) ? 1000 : Integer
.valueOf("" + fourKindPref.charAt(0));
int score = 0;
if (array[0] == 4) {
score = 1000 * fourOfAKindMult;
array[0] = 0;
}
if (array[1] == 4) {
score = 200 * fourOfAKindMult;
array[1] = 0;
}
if (array[2] == 4) {
score = 300 * fourOfAKindMult;
array[2] = 0;
}
if (array[3] == 4) {
score = 400 * fourOfAKindMult;
array[3] = 0;
}
if (array[4] == 4) {
score = 500 * fourOfAKindMult;
array[4] = 0;
}
if (array[5] == 4) {
score = 600 * fourOfAKindMult;
array[5] = 0;
}
if (score != 0) {
if (fourOfAKindMult > 100)
score = fourOfAKindMult;
else if (length > 4)
score *= -1;
}
int[] returnArray = { score, array[0], array[1], array[2], array[3],
array[4], array[5] };
return returnArray;
}
private final static int[] isThreeOfAKind(int[] array, Context c, int length) {
int score = 0;
if (array[0] == 3) {
score += 1000;
array[0] = 0;
}
if (array[1] == 3) {
score += 200;
array[1] = 0;
}
if (array[2] == 3) {
score += 300;
array[2] = 0;
}
if (array[3] == 3) {
score += 400;
array[3] = 0;
}
if (array[4] == 3) {
score += 500;
array[4] = 0;
}
if (array[5] == 3) {
score += 600;
array[5] = 0;
}
if (score != 0 && length > 3) {
score *= -1;
}
int[] returnArray = { score, array[0], array[1], array[2], array[3],
array[4], array[5] };
return returnArray;
}
private final static int scoreRemainders(int[] array) {
boolean thereAreExtra = (array[1] != 0 || array[2] != 0
|| array[3] != 0 || array[5] != 0);
int score = 0;
if (thereAreExtra) {
score += array[0] * 100;
score += array[4] * 50;
} else {
score += array[0] * -100;
score += array[4] * -50;
}
return score;
}
}
My Issue was answered on the Code Review. The Above Code reflects the Working Code. But here is the Section that needed to be modified.
public final static int isThreePair(int[] array, Context c, boolean calledPublicly) {
if (array.length < 6)
return 0;
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(c);
int threePairScore = Integer.valueOf(prefs.getString("threePairPref",
"750"));
if (calledPublicly)
array = count(array);
int count = 0;
if (array[0] >= 2)
count += array[0] / 2;
if (array[1] >= 2)
count += array[1] / 2;
if (array[2] >= 2)
count += array[2] / 2;
if (array[3] >= 2)
count += array[3] / 2;
if (array[4] >= 2)
count += array[4] / 2;
if (array[5] >= 2)
count += array[5] / 2;
if (count == 3)
return threePairScore;
else
return 0;
}
Walk through the code and think what would happen if it was four of a kind.
int fourOfAKindStatus = 0;
if (length == 4)
fourOfAKindStatus = isFourOfAKind(newArray, c, length)[0];
if (fourOfAKindStatus > 0)
return fourOfAKindStatus;
int threeOfAKindStatus = 0;
if (length == 3)
threeOfAKindStatus = isThreeOfAKind(newArray, c, length)[0];
if (threeOfAKindStatus > 0)
return threeOfAKindStatus;
If the result is four of a kind, you are returning from the method before checking for 3 of a kind. Therefore, the three of a kind check never gets performed.
You could have calculate() return a HashMap instead of an int, and instead of immediately returning the status when you find a match, you could add an entry to the map and keep checking, returning it at the end.
This question already has answers here:
Android Tile Bitmap
(8 answers)
Closed 8 years ago.
To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?
You can use this class as a equivalent "TiledLayer class of jme":
package net.obviam.walking;
import java.util.ArrayList;
import com.kilobolt.framework.Graphics;
import com.kilobolt.framework.Image;
import com.kilobolt.framework.implementation.AndroidImage;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Rect;
public class TiledLayer {
public static final int DIR_LEFT = 0;
public static final int DIR_RIGHT = 1;
public static final int DIR_UP = 2;
public static final int DIR_DOWN = 3;
private int cellWidth;
private int cellHeight;
private int yPosition = 0, xPosition = 0;
private int[][] grid;
private Image image;
private Bitmap tileArray[];
private int[] tileXPositions;
private int[] tileYPositions;
private ArrayList animatedTiles;
private int numberOfTiles;
private int numberOfColumns;
private int numberOfRows;
private int gridColumns;
private int gridRows, width, height;
int tileCounter;
public TiledLayer(Image image, int columns, int rows, int tileWidth,
int tileHeight, int width, int height) {
this.grid = new int[columns][rows];
this.gridColumns = columns;
this.gridRows = rows;
this.width = columns * tileWidth;
this.height = rows * tileHeight;
this.animatedTiles = new ArrayList();
this.cellWidth = tileWidth;
this.cellHeight = tileHeight;
int r = image.getHeight() / tileHeight;
int c = image.getWidth() / tileWidth;
tileArray = new Bitmap[(r * c) + 1];
setStaticTileSet(image, tileWidth, tileHeight);
}
public TiledLayer(Bitmap image, int columns, int rows, int tileWidth,
int tileHeight, int width, int height) {
this.grid = new int[columns][rows];
this.gridColumns = columns;
this.gridRows = rows;
this.width = columns * tileWidth;
this.height = rows * tileHeight;
this.animatedTiles = new ArrayList();
this.cellWidth = tileWidth;
this.cellHeight = tileHeight;
int r = image.getHeight() / tileHeight;
int c = image.getWidth() / tileWidth;
tileArray = new Bitmap[(r * c) + 1];
setStaticTileSet(image, tileWidth, tileHeight);
}
public void setStaticTileSet(Image image, int tileWidth, int tileHeight) {
tileArray[0] = Bitmap.createBitmap(tileWidth, tileHeight,
Config.ARGB_4444);
int rows = image.getHeight() / tileHeight;
int columns = image.getWidth() / tileWidth;
numberOfTiles = rows * columns;
for (int i = 0; i < rows; i++) {
yPosition = i * tileHeight;
for (int j = 0; j < columns; j++) {
xPosition = j * tileWidth;
tileCounter++;
tileArray[tileCounter] = Bitmap.createBitmap(
((AndroidImage) image).bitmap, xPosition, yPosition,
tileWidth, tileHeight);
}
}
if (gridColumns * gridRows < this.numberOfTiles) {
// clear the grid, when there are not as many tiles as in the
// previous set:
for (int i = 0; i < this.grid.length; i++) {
for (int j = 0; j < this.grid[i].length; j++) {
this.grid[i][j] = 0;
}
}
}
}
public void setStaticTileSet(Bitmap image, int tileWidth, int tileHeight) {
tileArray[0] = Bitmap.createBitmap(tileWidth, tileHeight,
Config.ARGB_4444);
int rows = image.getHeight() / tileHeight;
int columns = image.getWidth() / tileWidth;
numberOfTiles = rows * columns;
for (int i = 0; i < rows; i++) {
yPosition = i * tileHeight;
for (int j = 0; j < columns; j++) {
xPosition = j * tileWidth;
tileCounter++;
tileArray[tileCounter] = Bitmap.createBitmap(image, xPosition,
yPosition, tileWidth, tileHeight);
}
}
if (gridColumns * gridRows < this.numberOfTiles) {
// clear the grid, when there are not as many tiles as in the
// previous set:
for (int i = 0; i < this.grid.length; i++) {
for (int j = 0; j < this.grid[i].length; j++) {
this.grid[i][j] = 0;
}
}
}
}
public int createAnimatedTile(int staticTileIndex) {
if (staticTileIndex >= this.numberOfTiles) {
throw new IllegalArgumentException("invalid static tile index: "
+ staticTileIndex + " (there are only ["
+ this.numberOfTiles + "] tiles available.");
}
this.animatedTiles.add(new Integer(staticTileIndex));
return -1 * (this.animatedTiles.size() - 1);
}
public void setAnimatedTile(int animatedTileIndex, int staticTileIndex) {
if (staticTileIndex >= this.numberOfTiles) {
}
int animatedIndex = (-1 * animatedTileIndex) - 1;
this.animatedTiles.set(animatedIndex, new Integer(staticTileIndex));
}
public int getAnimatedTile(int animatedTileIndex) {
int animatedIndex = (-1 * animatedTileIndex) - 1;
Integer animatedTile = (Integer) this.animatedTiles.get(animatedIndex);
return animatedTile.intValue();
}
public void setCell(int col, int row, int tileIndex) {
if (tileIndex >= this.numberOfTiles) {
throw new IllegalArgumentException("invalid static tile index: "
+ tileIndex + " (there are only [" + this.numberOfTiles
+ "] tiles available.");
}
this.grid[col][row] = tileIndex;
}
public int getCell(int col, int row) {
return this.grid[col][row];
}
public boolean checkTileCollision(Sprite sp, int dir, int speed) {
int curRow, curCollumn;
int leftTile = 0, rightTile = 0, upTile = 0, downTile = 0;
curRow = sp.getY() / cellHeight;
curCollumn = sp.getX() / cellWidth;
leftTile = grid[curCollumn - 1][curRow];
rightTile = grid[curCollumn + 1][curRow];
upTile = grid[curCollumn][curRow - 1];
downTile = grid[curCollumn][curRow + 1];
if (dir == DIR_RIGHT
&& (sp.getX() + sp.getWidth() + speed) <= (curCollumn + 1)
* cellWidth)
return false;
else if (dir == DIR_RIGHT
&& ((sp.getX() + sp.getWidth() + speed)) > (curCollumn + 1)
* cellWidth && rightTile != 1)
return true;
else if (dir == DIR_LEFT
&& (sp.getX() - speed) >= (curCollumn) * cellWidth)
return false;
else if (dir == DIR_LEFT
&& (sp.getX() - speed) < (curCollumn) * cellWidth
&& leftTile != 1)
return true;
else if (dir == DIR_UP && (sp.getY() - speed) >= (curRow) * cellHeight)
return false;
else if (dir == DIR_UP && (sp.getY() - speed) < (curRow) * cellHeight
&& upTile != 1)
return true;
else if (dir == DIR_DOWN
&& (sp.getY() + sp.getHeight() + speed) <= (curRow + 1)
* cellHeight)
return false;
else if (dir == DIR_DOWN
&& (sp.getY() + sp.getHeight() + speed) > (curRow + 1)
* cellHeight && downTile != 1)
return true;
else
return false;
}
public void fillCells(int col, int row, int numCols, int numRows,
int tileIndex) {
if (tileIndex >= this.numberOfTiles) {
throw new IllegalArgumentException("invalid static tile index: "
+ tileIndex + " (there are only [" + this.numberOfTiles
+ "] tiles available.");
}
int endCols = col + numCols;
int endRows = row + numRows;
for (int i = col; i < endCols; i++) {
for (int j = row; j < endRows; j++) {
this.grid[i][j] = tileIndex;
}
}
}
public final int getCellWidth() {
return this.cellWidth;
}
public final int getCellHeight() {
return this.cellHeight;
}
public final int getColumns() {
return this.gridColumns;
}
public final int getRows() {
return this.gridRows;
}
public final void paint(Graphics g) {
for (int i = 0; i < this.gridRows; i++) {
for (int j = 0; j < this.gridColumns; j++) {
Bitmap bmp = tileArray[grid[j][i]];
g.drawImage(bmp, j * cellWidth, i * cellHeight);
}
}
}
public final void paint(Canvas c) {
for (int i = 0; i < this.gridRows; i++) {
for (int j = 0; j < this.gridColumns; j++) {
Bitmap bmp = tileArray[grid[j][i]];
c.drawBitmap(bmp, j * cellWidth, i * cellHeight, null);
// g.drawImage(bmp, j*cellWidth,i*cellHeight);
}
}
}
}