I have some C++ code that I use in my android application. It takes a string and converts each character to an int from this ascii table. I then renders the correct glyph based on this.
It works on most of the devices I tested a few different google pixels and samsung phones, but when I test in a 3.3" WQVGA emulator on api 24 - 30 it doesn't function properly.
I imagine I'm going at this all wrong. Is there a better way of doing this? Is there a fix to my code?
Thanks for your time.
void text::drawtexthorizontal( double x, double y, int textheight, string textin)
{
int n = textin.length();
// declaring character array
char char_array[n + 1];
// copying the contents of the string to char array
strcpy(char_array, textin.c_str());
for (int i = 0; i < n; i++){
//get ascii or "extended" ascii index and store it in symbol
int symbol;
if( (char_array[i] & ( 1 << 7 )) >> 7 == 1){
if( (char_array[i] & ( 1 << 6 )) >> 6 == 1){
//comb takes 2 byte representation and returns an int between 128 and 255 corresponding to extended ascii
symbol = comb(int(char_array[i]),int(char_array[i+1]));
i++;
}
else{
symbol = comb(int(char_array[i+1]),int(char_array[i]));
i++;
}
}
else symbol = int(char_array[i]);
//render chars[symbol]
}
}
int text::comb( int n, int m){
/*https://naveenr.net/unicode-character-set-and-utf-8-utf-16-utf-32-encoding/ for 2 byte encoding*/
int a[8] = { 0 },b[8] = { 0 };;
int i,k=0;
for (i = 0; n > 0; i++) {
a[i] = n % 2;
n /= 2;
}
for (i = 0; m > 0; i++){
b[i] = m % 2;
m /= 2;
}
for (i = 4; i >=0 ; i--){
k = 10 * k + a[i];
}
for (i = 5; i >=0 ; i--){
k = 10 * k + b[i];
}
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = k;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
if(dec_value > 255) dec_value = 32;
return dec_value;
}
I am able to print pyramid like this :
1
123
12345
1234567
Code i used to print the pyramid of numbers like above is :
int a = 1;
int b = 4;
for (int i = 1 ; i <= 4 ; i++){
for (int c = 1 ; c <= b - 1 ; c++){
text.append(" ");
}
for (int k = 1 ; k <= a ; k++){
String result = String.valueOf(k);
text.append(result);
}
a = a + 2;
b--;
text.append("\n");
}
but, the issue i am facing is i have to print the same pyramid, but in reverse order like this :
1234567
12345
123
1
Any help would be appreciated?
try this ...
for (int i = 7; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.print("\n");
if (i % 2 != 0) {
i = i - 1;
}
}
Try this
int a = 1;
int b = 4;
for (int i = 1; i <= 4; i++) {
for (int k = a; k >= 1; k--) {
String result = String.valueOf(k);
text.append(result);
}
for (int c = 1; c <= b - 1; c++) {
text.append(" ");
}
a = a + 2;
b--;
text.append("\n");
}
System.out.println(text.reverse());
Try this :)
int a = 1;
int b = 4;
int m = 4;
for (int i = 1; i <= 4; i++) {
for (int c = 1; c <= m; c++) {
text.append(" ");
}
for (int k = 1; k <= b; k++) {
String result = String.valueOf(k);
text.append(result);
}
a = a + 2;
b--;
m++;
text.append("\n");
}
int numberofdigits = 7;
int numberofdigitsforrow = 0;
int Emptyspace = 0;
for (int i = 0; i < numberofdigits; i++)
{
numberofdigitsforrow = numberofdigits - i * 2;
if (numberofdigitsforrow > 0)
{
Emptyspace = numberofdigits-numberofdigitsforrow;
if (Emptyspace > 0)
{
for (int b = 1; b <= Emptyspace/2; b++)
{
Console.Write(" ");
}
}
for (int c = 1; c <= numberofdigitsforrow; c++)
{
Console.Write(c.ToString());
}
if (Emptyspace > 0)
{
for (int b = 1; b <= Emptyspace / 2; b++)
{
Console.Write(" ");
}
}
Console.WriteLine();
}
}
int w = 7;
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= 5; i++)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = 1; j <=w; j++)
{
System.out.print(j+" ");
}
w = w - 2;
System.out.println();
}
I wanted to automate android gradle versioning my requirement is
Ver Code Version Name
So i have runned code in java application
Code:
public static void main(String[] args) {
int fileVersionCode = 1;
String fileVersionName;
for (int i = 0; i < 200; i++) {
if (fileVersionCode <= 10) {
fileVersionName = "1." + (fileVersionCode - 1);
} else if(fileVersionCode>=20 && fileVersionCode%10 ==0) {
fileVersionName = ( (fileVersionCode / 10)) + ".9";
}else {
fileVersionName = (1 + (fileVersionCode / 10)) + "." +( (fileVersionCode % 10)-1);
}
System.out.println(fileVersionCode+" "+fileVersionName);
fileVersionCode++;
}
}
And my code is working as expected but in gradle for the same code
task firstTask {
doFirst {
int fileVersionCode = 1;
String fileVersionName;
for (int i = 0; i < 200; i++) {
if (fileVersionCode <= 10) {
fileVersionName = "1." + (fileVersionCode - 1);
} else if(fileVersionCode>=20 && fileVersionCode%10 ==0) {
fileVersionName = ( (fileVersionCode / 10)) + ".9";
}else {
fileVersionName = (1 + (fileVersionCode / 10)) + "." +( (fileVersionCode % 10)-1);
}
System.out.println(fileVersionCode+" "+fileVersionName);
fileVersionCode++;
}
}
doLast{
// println 'firstTask doLast'
}
}
Run :> gradlew firstTask
output is different why i am getting like this
In groovy a division results in a BigDecimal if the operands are of type Integer, while in Java they are of the type int.
When fileVersionCode is 11, the result:
(fileVersionCode / 10)
translates to 1 in Java but to 1.1 in Groovy. In order to fix this, just add a (int) cast to trim the unnecessary decimal part.
* 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.
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);
}