Trying to assign card pictures, but it gives me wrong cards - java

I have this method:
I am trying to assign picture of a card into their iconCards[][] profiles. Pictures are located in images/ folder as e.g. images/AS.gif (Ace of Spades), images/AD.gif (Ace of Diamonds) etc.
static void loadCardIcons(){
int l =0;
int k =0;
while (k < 14){
for (l = 0; l < 4; l++){
String card = "images/" + Character.toString(valueRanks[k]) + "C.gif";
iconCards[k][l] = new ImageIcon(card);
System.out.println(k + " " + l + " " + card);
card = "images/" + Character.toString(valueRanks[k]) + "D.gif";
iconCards[k][l++] = new ImageIcon(card);
System.out.println(k + " " + l + " " + card);
card = "images/" + Character.toString(valueRanks[k]) + "H.gif";
iconCards[k][l++] = new ImageIcon(card);
System.out.println(k + " " + l + " " + card);
card = "images/" + Character.toString(valueRanks[k]) + "S.gif";
iconCards[k][l++] = new ImageIcon(card);
System.out.println(k + " " + l + " " + card);
k++;
}
}
iconBack = new ImageIcon("images/BK.gif");
}
...12 3 images/KS.gif
13 0 images/XC.gif
13 1 images/XD.gif
13 2 images/XH.gif
13 3 images/XS.gif
*13
2
images/XS.gif*
You can see that value = 13 and suit = 2. According to output above the card should be images/XH (JokerHearts), but it prints me XS. Why? Here's the method that runs it. Can't find out what's going on for weeks.
static public Icon getIcon(Card card){
loadCardIcons();
return iconCards[valueAsInt(card)][suitAsInt(card)];
}

Problem is here
iconCards[k][l++] = new ImageIcon(card);
l++ is the post increment operator on l. Therefore given l = 0 and k = 0, you would access
iconCards[0][0]
and then l would go to 1. You might want to use the pre increment ++l version.
So your l (as an index to iconCards) only goes up to value 2 (for images/XH.gif), not 3 (for images/XS.gif).
See the Oracle tutorial here on increment/decrement operators for an explanation on their use.

Related

How to redistribute the side pots in poker?

It's a poker game with bluetooth and I encounter some difficulties to redistribute the side pots. Does someone have any experiences with that?
for(int k = 0; k < numberOfPlayer; k++)
{
canWinSidePotUpTo[k] = -1;
}
for(int i = 0 ; i < sidePot.size(); i++) {
if (sideTempToRaiseListSorted.get(i) != sideTempToRaiseListSorted.get(i + 1)) {
for (int k = 0; k < numberOfPlayer; k++) {
print("All in ToRaiseList[" + k + "] = " + toRaiseList[k]);
print("All in TempToRaise[" + k + "] = " + tempToRaise[k]);
if (sideTempToRaiseListSorted.get(i) == max(toRaiseList) - max(tempToRaise)) {
continue;
}
if (sideTempToRaiseListSorted.get(i) == (toRaiseList[k] - tempToRaise[k])) {
canWinSidePotUpTo[k] = j;
}
if (sideTempToRaiseListSorted.get(i + 1) == (toRaiseList[k] - tempToRaise[k])) {
canWinSidePotUpTo[k] = j;
}
print("All In canWinSidePotUpTo[" + k + "] " + canWinSidePotUpTo[k] + " + i = " + i);
}
print("All In sideTempToRaiseListSorted.get(" + i + ") " + sideTempToRaiseListSorted.get(i) + " + i = " + i);
print("All In sideTempToRaiseListSorted.get(" + (i + 1) + ") " + sideTempToRaiseListSorted.get(i + 1) + " + i + 1 = " + i + 1);
}
j++;
}
The expected result is to be able to set the array canWinSidePotUpTo[player]
for each player.
The side pot start at index 0 and if the player can win only the pot then canWinSidePotUpTo[player] = -1. All player which are allin have canWinSidePotUpTo[player] = -1 and then canWinSidePotUpTo[player] should be set according to the stack at allin...
the actual result is:
All In canWinSidePotUpTo[0] -1 + i = 1
All In canWinSidePotUpTo[1] 1 + i = 1
All In canWinSidePotUpTo[2] 1 + i = 1
All In canWinSidePotUpTo[3] -1 + i = 1
That the result for:
player:hand:stack allin
0:AA:900
1:KK:1100
2:QQ:1300
3:JJ:1500
pot = 3600
sidepot(0)= 600
sidepot(1) = 400
Flop:AKQJ9
Any help would be welcome!
In software, I think it's simpler to do it in the opposite order from how a live-game dealer would. In a casino, the side-pots are awarded first, then the main--mainly because the pots are consolidated during betting and sometimes that's the only way to do it.
But in software, you can keep track during betting of each player's total contribution to the pot, including antes and blinds, as the betting happens. You don't have to calculate it after-the-fact. So to award the pots, then, you just start with the best hand. Award him his contribution, plus up to that amount from each of the other players, then remove him (and any other players with no contribution left) from the list. Then repeat: find the best remaining hand, award him his remaining contribution plus up to that amount from each of the others, then remove from list, etc.
I've been answering a variety of these questions, because often they come with clues and nothing super concrete. If you'd like to see the algorithm I wrote, assuming you still care, I'm linking to the code here:
https://dotnetfiddle.net/P0wgR5
It doesn't care about bets, it's at the point where everyone's committment has been tallied and it's time to distribute to the winners. It handles folded hands, all-ins/overbets, and I haven't found a bug yet.

How to calcucate a product of n adjecent numbers using ArrayList and subList?

Im trying to solve EulerProblem8 https://projecteuler.net/problem=8 and i just don't get it , what am i doing wrong ? I tried before with a file and ArrayList but couldn't pull it off ... Whats wrong , the subtsrings , the loops , the *= ... i dont know what to do anymore?
package largestproductinaseries_ep8;
//The four adjacent digits in the 1000-digit number
//that have the greatest product are 9 × 9 × 8 × 9 = 5832.
//Find the thirteen adjacent digits in the 1000-digit number
//that have the greatest product. What is the value of this product?
public class LargestProductInASeries_EP8 {
public static void main(String[] args){
String bigNum = "73167176531330624919225119674426574742355349194934" +
"96983520312774506326239578318016984801869478851843" +
"85861560789112949495459501737958331952853208805511" +
"12540698747158523863050715693290963295227443043557" +
"66896648950445244523161731856403098711121722383113" +
"62229893423380308135336276614282806444486645238749" +
"30358907296290491560440772390713810515859307960866" +
"70172427121883998797908792274921901699720888093776" +
"65727333001053367881220235421809751254540594752243" +
"52584907711670556013604839586446706324415722155397" +
"53697817977846174064955149290862569321978468622482" +
"83972241375657056057490261407972968652414535100474" +
"82166370484403199890008895243450658541227588666881" +
"16427171479924442928230863465674813919123162824586" +
"17866458359124566529476545682848912883142607690042" +
"24219022671055626321111109370544217506941658960408" +
"07198403850962455444362981230987879927244284909188" +
"84580156166097919133875499200524063689912560717606" +
"05886116467109405077541002256983155200055935729725" +
"71636269561882670428252483600823257530420752963450";
int a = 0;
String peace = "";
String onePeace = "";
int onePeaceNum = 0;
int multi = 1;
int maxMulti = 0;
while(a<bigNum.length()-12){
peace = bigNum.substring(a, a+13);
if(!peace.contains("0")){
for(int i = 12; i>=0; i--){
onePeace = peace.substring(i, i+1);
onePeaceNum = Integer.parseInt(onePeace);
multi *= onePeaceNum;
if(multi>maxMulti){
maxMulti = multi;
}
}
multi = 1;
}
a++;
}
System.out.println(maxMulti);
}
}
//23514624000 this is Euler answer
// 2091059712 this is my output
You have a problem in your implementation, you never reset the mult value so you keep multiplying and you don't stop after 13 numbers.
Your code should be:
if (mult > maxMult) {
maxMult = mult;
mult = 1;
} else {
mult = 1;
}

String with 1000 digits, find the biggest 5 digits without an array

I know my code can be simpler and more efficient... My code is supposed to grab the biggest set of 5 digits. It works, except it only is grabbing 3 digits, what would i need to modify to change that?
public class thousandDigits {
public static void main(String[] args) {
int greatest = 0;
String num = ("73167176531330624919225119674426574742355349194934"
+ "96983520312774506326239578318016984801869478851843"
+ "85861560789112949495459501737958331952853208805511"
+ "12540698747158523863050715693290963295227443043557"
+ "66896648950445244523161731856403098711121722383113"
+ "62229893423380308135336276614282806444486645238749"
+ "30358907296290491560440772390713810515859307960866"
+ "70172427121883998797908792274921901699720888093776"
+ "65727333001053367881220235421809751254540594752243"
+ "52584907711670556013604839586446706324415722155397"
+ "53697817977846174064955149290862569321978468622482"
+ "83972241375657056057490261407972968652414535100474"
+ "82166370484403199890008895243450658541227588666881"
+ "16427171479924442928230863465674813919123162824586"
+ "17866458359124566529476545682848912883142607690042"
+ "24219022671055626321111109370544217506941658960408"
+ "07198403850962455444362981230987879927244284909188"
+ "84580156166097919133875499200524063689912560717606"
+ "05886116467109405077541002256983155200055935729725"
+ "71636269561882670428252483600823257530420752963450");
for (int n = 0; n < num.length() - 5; n++) {
greatest = ((num.charAt(n)) + (num.charAt(n+1)) + (num.charAt(n+2)) + (num.charAt(n+3))
+ (num.charAt(n+4)));
if (greatest > n) {
n = greatest;
}
}
System.out.print(greatest);
}
}
OUTPUT:
357
I think you want to use String.substring(int, int) to iterate all possible 5 character substrings, and then you might use Math.max(int, int) to update greatest. Something like
int greatest = Integer.MIN_VALUE;
for (int i = 0; i < num.length() - 4; i++) {
// int value = Integer.parseInt(num.substring(i, i + 5));
int value = Integer.parseInt(String.valueOf(num.charAt(i))
+ num.charAt(1 + i) + num.charAt(2 + i) + num.charAt(3 + i)
+ num.charAt(4 + i));
greatest = Math.max(greatest, value);
}
System.out.println(greatest);
I get 99890.
I think you are trying to add 5 consecutive characters to get sum, and store starting index of highest sum.
But you should be using Character.getNumricValue(char) to convert (num.charAt(n)) to numeric value and then add.
greatest = Character.getNumericValue((num.charAt(n)) + Character.getNumericValue((num.charAt(n+1)) + Character.getNumericValue((num.charAt(n+2)) +
Character.getNumericValue((num.charAt(n+3)) +
Character.getNumericValue((num.charAt(n+4));
You need a valirable to store old value to compare and index
if(greatest > oldGreatest) {
index = n;
}
Then finally print using index out side loop:
System.out.print((num.charAt(index)) + (num.charAt(index+1) + (num.charAt(index +2)) + (num.charAt(index +3)) + (num.charAt(index +)));
Although #ElliottFrisch and #dave provides more elegant answer, I tried to modify from your original version and here is my code (I have tested it):
public class ThousandDigits {
public static void main(String[] args) {
int greatest = 0;
String num = ("73167176531330624919225119674426574742355349194934"
+ "96983520312774506326239578318016984801869478851843"
+ "85861560789112949495459501737958331952853208805511"
+ "12540698747158523863050715693290963295227443043557"
+ "66896648950445244523161731856403098711121722383113"
+ "62229893423380308135336276614282806444486645238749"
+ "30358907296290491560440772390713810515859307960866"
+ "70172427121883998797908792274921901699720888093776"
+ "65727333001053367881220235421809751254540594752243"
+ "52584907711670556013604839586446706324415722155397"
+ "53697817977846174064955149290862569321978468622482"
+ "83972241375657056057490261407972968652414535100474"
+ "82166370484403199890008895243450658541227588666881"
+ "16427171479924442928230863465674813919123162824586"
+ "17866458359124566529476545682848912883142607690042"
+ "24219022671055626321111109370544217506941658960408"
+ "07198403850962455444362981230987879927244284909188"
+ "84580156166097919133875499200524063689912560717606"
+ "05886116467109405077541002256983155200055935729725"
+ "71636269561882670428252483600823257530420752963450");
int max = -1;
for (int n = 0; n < num.length() - 4; n++) {
greatest = ((num.charAt(n) - '0') * 10000 + (num.charAt(n + 1) - '0') * 1000
+ (num.charAt(n + 2) - '0') * 100 + (num.charAt(n + 3) - '0') * 10 + (num.charAt(n + 4) - '0'));
if (max < greatest) {
max = greatest;
}
}
System.out.print(max);
}
}
I think you'll find it's not grabbing three digits, but rather the sum of the six characters you are pulling out is a 3-digit number.
If you're after the largest five digit number, you need to extract five digits (not six) as you do and assign them a weight. So the first digit must be multiplied by 10,000, the second by 1,000 and so on.
But there's more: you're are getting the character at an index within your string. This is not what you want as it is not the same as the numeric value of that character. For that you need:
num.charAt(n) - '0'
These changes should allow you to correct your algorithm as it stands.
A more efficient approach would be to extract 5-digit sub-strings and convert them to integers. The first one would be:
Integer.parseInt(num.subString(0, 5));
You can iterate to get each one to find the greatest.

Using Recursion given m(i)=1+1/2+1/3+1/4+1/5... + 1/i

This is what I have so far. I can't tell exactly how to change the numbers so it makes sense. Do I need to include the index as part of the equation? Although it seems like n1(the previous number) + (1/n2) should give me a new n2. Any thoughts?
package myrecursivemethod;
public class MyRecursiveMethod {
private static double index = 0;
private static double stoppingPoint=10;
public static void main(String[] args) {
double n1= 0;
double n2= 1;
System.out.println("index: " + index + "->" + n1 );
myRecursiveMethod(n1, n2);
}
public static void myRecursiveMethod(double n1, double n2)
{
System.out.println("index: " + index + " -> " + (n1+(1/n2)));
if (index == stoppingPoint)
return;
index ++;
myRecursiveMethod(n2, n1+(1/n2));
}
}
You need to take a look at your formula little closer and try to find a way to present this formula using similar formula with different arguments. For instance
sum(i) = 1 + 2 + 3 + 4 + ... + (i-1) + i
is same as
sum(i) = (1 + 2 + 3 + 4 + ... + (i-1)) + i
but since
1 + 2 + 3 + 4 + ... + (i-1) = sum(i-1)
we can rewrite entire formula as
sum(i) = sum(i-1) + i
(or actually)
{ sum(i-1) + i if i>0
sum(i) = {
{ 0 if i==0
Formula from your question is very similar to this one and can be presented in similar (recursive) way.

Java Homework MaxSumTest

I am currently attempting to revise a Java Homework program for my Data Structures class that displays the output of the MaxSumTest program in a table.
I've created four arrays, and placed them inside one of the loops that calculates the timing info. I am trying to populate each array with only the timing info for one algorithm. So each array would have 4 elements. However, every time I run my revision of the program, I get an out of bounds error.
Here is the Original
The only method I revised is getTimingInfo. Here is my revision:
public static void getTimingInfo( int n, int alg )
{
int [] test = new int[ n ];
long startTime = System.currentTimeMillis( );;
long totalTime = 0;
//create an array for each Algorithm
long[] alg4;
long[] alg3;
long[] alg2;
long[] alg1;
//allocate memory for 5 long ints
alg4 = new long[5];
alg3 = new long[5];
alg2 = new long[5];
alg1 = new long[5];
int i;
int j;
int index = 0;
for( i = 0; totalTime < 4000; i++ )
{
for( j = 0; j < test.length; j++ )
test[ j ] = rand.nextInt( 100 ) - 50;
index = j;
switch( alg )
{
case 1:
maxSubSum1( test );
break;
case 2:
maxSubSum2( test );
break;
case 3:
maxSubSum3( test );
break;
case 4:
maxSubSum4( test );
break;
}
totalTime = System.currentTimeMillis( ) - startTime;
}
alg1[index] = totalTime * 1000 / i;
alg2[index] = totalTime * 1000 / i;
alg3[index] = totalTime * 1000 / i;
alg4[index] = totalTime * 1000 / i;
//Build first column of table
System.out.println("Size of N Algorithms\t" + "250\t" + "2500\t" + "25000\t" + "250000");
System.out.println("Alg #4\t" + alg4[0] + "\t" + alg4[1] + "\t" + alg4[2] + "\t" + alg4[3] + "\t" + alg4[4]);
System.out.println("Alg #3\t" + alg3[0] + "\t" + alg3[1] + "\t" + alg3[2] + "\t" + alg3[3] + "\t" + alg3[4]);
System.out.println("Alg #2\t" + alg2[0] + "\t" + alg2[1] + "\t" + alg2[2] + "\t" + alg2[3] + "\t" + alg2[4]);
System.out.println("Alg #1\t" + alg1[0] + "\t" + alg1[1] + "\t" + alg1[2] + "\t" + alg1[3] + "\t" + alg1[4]);
/*
System.out.println( "Algorithm #" + alg + "\t"
+ "N = " + test.length
+ "\ttime = " + ( totalTime * 1000 / i ) + " microsec" );
*/
}
Any advice or pointers in the right direction would be appreciated.
Shouldn't the lines that update the array elements be inside the inner loop?
alg1[index] = totalTime * 1000 / i;
alg2[index] = totalTime * 1000 / i;
alg3[index] = totalTime * 1000 / i;
alg4[index] = totalTime * 1000 / i;
You are assigning them outside the loop, where the value of index is 5, so it's out of bounds.
I'm not sure exactly what you are trying to do, but I think that what you really want to do is call getTimingInfo() a number of times and then assign the results to a table, not change the internal workings of getTimingInfo().
This requires that you define the arrays in which you will store the timing data OUTSIDE of getTimingInfo(), and then the only change inside of getTimingInfo() would be storing to that (already defined) array, rather than printing.
long[] alg1;
alg4 = new long[5];
Can be better expressed as
long[] alg1 = new long[5];
maxSubSum3( test );
Is written nicer as
maxSubSum3(test);
int j;
int index = 0;
Actually do the same thing.
Just to pick a few points.
You are setting the alg4, alg3, alg2, and alg1 to size 5, not size 4 as you said you needed.

Categories