problems evaluating 5 playing cards

jonneymendoza

Supreme [H]ardness
Joined
Sep 11, 2004
Messages
6,395
Hi i am trying to create a video poker game and the problem i am having is that when i try to evaluate a players cards it evaluates it incorrectly telling me that i have got a pair of jacks or better.

my code is simple, i store images of cards into an array and also store the cards filename on its seperate array of strings. i can generate 5 random cards to the player by simply generating 5 random values between 0-51 and assigning them has the index value of the card image.

the 5 random cards appear fine but when evaluating them it keeps saying that i win even though i diddnt. anyways here is the code for the part that evaluates the 5 cards...

Code:
char [] rank = new char[5];
 
    //king qeen, jack, and ace
        char [] typeRank = {'A', 'K', 'Q', 'J'};
        
        
        //get all the ranks of each card
        for(int i = 0;i<5;i++)
        {
            rank[i] = suit[i].charAt(1); //get the rank of each card ie the 2nd char value of the image name
        }
        
        for(int x = 0; x < 5;x++)
        {
               for(int z = 0; z < 5; z++)
               {
                    if((x != z) && (rank[x] == rank[z]))
                    {
                            //if card has same suit evaluate to see if its a jack or better
                            int j = 0;
                            while(j < 4 || !jackorBetter)
                            {
                                if(rank[x] == typeRank[j])
                                {
                                    jackorBetter = true;
                                }
                                j++;
                            }
                    }
               }
         }
        
        
        return jackorBetter;



and here is the code that calls this method:
Code:
public void evaluate()
{
boolean result;
//filename is an array that stores the image names of the 5 generated cards
result = Cards.jackBetter(filename);
}

hope someone can help, thanks
 
It may help to post the complete code. The snippets you have posted don't help much. Have you tried debugging your program? My guess is the error will become abundantly clear once you load your program up in a debugger and step through it a couple of times.
 
Looks like you forgot to reset jackorBetter between your inner or outer loop. Btu I'm not too sure of that diagnosis -- as generelz points out, you don't show us all of your code and I feel like I'm lacking a little context.
 
The easiest way to solve this would be to use a debugger and step through the code and verify the characters in the arrays are what you're expecting.

But two things caught my eye. I didn't see jackorbetter initialized anywhere. The other thing is your while loop.
Code:
while(j < 4 || !jackorBetter)
If jackorbetter is initialized as false, then negating it will make it true. Your while loop would run infinitely as even when j = 5 (or more) the while loop would look like "while (false or true)"

Since your program is not getting stuck in an infinite loop jackorbetter is probably being initialized as true which is why you're always winning.


Edit: 3 minutes too slow...
 
The ! operator doesn't change its operand.

Sorry, poor wording on my part. I meant that the while loop will evaluate as true, I wasn't trying to imply that it changes the value of jackorBetter.

What I should have said is "If jackorbetter is initialized as false, then !jackorbetter would be true. causing the while loop to true"
 
at the top of my code the jackorbetetr is created using boolean jackorbetter;

by default its set to fault but only changes when a player wins as shown above in my code. i dont have teh full code on me where i am but tomorrow i will post teh full code of the card class in which that evaluation code is located at plus the necesary codes from the main game class.

one more question, how do i debug? im using netbeans 5.5
 
ok guys here is the card class with most of the relivent methods:

Code:
import java.io.*;
import javax.microedition.lcdui.*;
import java.util.Random;
import javax.*;

public class cards 
{
    private Image [] cards = new Image[53]; //create an array of images.
    private String [] card_name = new String[53];
    private int [] ran = new int [5];
    private String [] rtrn_name = new String[5];
    private Image [] rtrn_card = new Image[5];
    Random r = new Random();
    private int j = 2;
    private int i = 0;
    private boolean jackorBetter;

    
    String fname;
    /** Creates a new instance of cards */
    public cards() 
    {
        
    }
    
//loads all cards into an array of images
    public void loadArray()
    {
          try
            {
                //load the club cards into the array
                 while(i < 9)
                 {
                    fname = "C" + j + ".gif"; //store filename
                    card_name[i] = fname;
                    cards[i] = Image.createImage("/"+fname);
                    i++;
                    j++;
                 }
                 
                 cards[i] = Image.createImage("/CA.gif");
                 card_name[i] = "CA.gif";
                 cards[i+1] = Image.createImage("/CQ.gif");
                 card_name[i+1] = "CQ.gif";
                 cards[i+2] = Image.createImage("/CK.gif");
                 card_name[i+2] = "CK.gif";
                 cards[i+3] = Image.createImage("/CJ.gif");
                 card_name[i+3] = "CJ.gif";
                 i = i+4;
                 j = 2;
                 ///////////////////LOAD DIAMONDS/////////////////////////////
                 
                 while(i < 22)
                 {
                    fname = "D" + j + ".gif"; //store filename
                    card_name[i] = fname;
                    cards[i] = Image.createImage("/"+fname);
                    i++;
                    j++;
                 }
                 
                 cards[i] = Image.createImage("/DA.gif");
                 card_name[i] = "DA.gif";
                 cards[i+1] = Image.createImage("/DQ.gif");
                 card_name[i+1] = "DQ.gif";
                 cards[i+2] = Image.createImage("/DK.gif");
                 card_name[i+2] = "DK.gif";
                 cards[i+3] = Image.createImage("/DJ.gif");
                 card_name[i+3] = "DJ.gif";
                 i = i+4;
                 j = 2;
                 
                 ////////////////////////LOAD HEARTS////////////////////////
                
                 while(i < 35)
                 {
                    fname =  "H" + j + ".gif"; //store filename
                    card_name[i] = fname;
                    cards[i] = Image.createImage("/"+fname);
                    i++;
                    j++;
                 }
                 
                 cards[i] = Image.createImage("/HA.gif");
                 card_name[i] = "HA.gif";
                 cards[i+1] = Image.createImage("/HQ.gif");
                 card_name[i+1] = "HQ.gif";
                 cards[i+2] = Image.createImage("/HK.gif");
                 card_name[i+2] = "HK.gif";
                 cards[i+3] = Image.createImage("/HJ.gif");
                 card_name[i+3] = "HJ.gif";
                 i = i+4;
                 j = 2;
                 
                 /////////////////////////LOAD SPADES////////////////////////
                 
                 while(i < 48)
                 {
                    fname =  "S" + j + ".gif"; //store filename
                    card_name[i] = fname;
                    cards[i] = Image.createImage("/"+fname);
                    i++;
                    j++;
                 }
                 
                 cards[i] = Image.createImage("/SA.gif");
                 card_name[i] = "SA.gif";
                 cards[i+1] = Image.createImage("/SQ.gif");
                 card_name[i+1] = "SQ.gif";
                 cards[i+2] = Image.createImage("/SK.gif");
                 card_name[i+2] = "SK.gif";
                 cards[i+3] = Image.createImage("/SJ.gif");
                 card_name[i+3] = "SJ.gif";
                 
                 i = 0;
                 j = 2;
          } //end of try
          catch(Exception e) 
          {
                e.printStackTrace();
          }
          
    }
    
    public void random()
    {
          
            
          try
          {        
                 
                 ////////////////////GENERATE 5 RANDOM CARDS//////////////////
                 
                 //generate random index numbers used for pulling out 5 random images.
                for(int q=0; q < 5; q++)
                {
                    //ran[q] = (int)(java.lang.Math.random()*52);
                    ran[q] = r.nextInt(52);
                }
        
                for(int x = 0; x < 5;x++)
                {
                    for(int z = 0; z < 5; z++)
                    {
                        if(x != z && ran[x] == ran[z])
                        {
                            ran[z] = r.nextInt(52);
                        }
                    }
                }
                 for(int w = 0; w < 5;w++)
                 {
                     rtrn_card[w] = cards[ran[w]];
                     rtrn_name[w] = card_name[ran[w]];
                 }
                
            }
            
            
            catch(Exception e) 
            {
                e.printStackTrace();
            }
    }
    
// here is the method for jack or better
    public boolean jackBetter(String[] suit)
    {
        
       
        char [] rank = new char[5];
        char [] typeRank = {'A', 'K', 'Q', 'J'};
        
        
        //get all the ranks of each card
        for(int i = 0;i<5;i++)
        {
            rank[i] = suit[i].charAt(1);
        }
        
        for(int x = 0; x < 5;x++)
        {
               for(int z = 0; z < 5; z++)
               {
                    if((x != z) && (rank[x] == rank[z]))
                    {
                            //if card has same suit evaluate to see if its a jack or better
                            int j = 0;
                            while(j < 4 || !jackorBetter)
                            {
                                if(rank[x] == typeRank[j])
                                {
                                    jackorBetter = true;
                                }
                                j++;
                            }
                    }
               }
         }
        
        
        return jackorBetter;
    }
    
    
    
    public Image[] get5cards()
    {
        return rtrn_card;
        
    }
    
    public Image getNewCard(int i)
    {
        return cards[i];
    }
    
    public String getNewName(int i)
    {
        return card_name[i];
    }
    
    public String[] get5Name()
    {
        return rtrn_name;
    }
    public int get()
    {
        return i;
    }
    
    public int[] getRan()
    {
        return ran;
    }
    
    
    
}


and here is some of the code from the game class:

Code:
import java.lang.Math.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.Random;

import com.nokia.mid.ui.FullCanvas;
 /*
 * @author  Administrator
 * @version
 */
public class PFFCanvas extends FullCanvas implements CommandListener, Runnable 
{
private cards Cards = new cards(); //creates a new card class object
private boolean drawed;
 private Image [] cards = new Image[5]; //create an array of images.
    private String [] filename = new String[5]; //array of names of images

private int [] ran = new int [5]; 
private Random r = new Random();
public boolean left,right,up,down, enter,result ,evaluate;
private boolean ch1,ch2,ch3,ch4,ch5,
 private int [] ran = new int [5];
  private int [] ran2 = new int [5];

 ////////////////////////evaluate////////////////////////////
    public void evaluate()
    {
        result = Cards.jackBetter(filename);
    }


/////////////replace the cards not held with new ones
public void replaceHold()
    {
        if(!ch1)
        {

            
            ran[0] = r.nextInt(52);
            for(int x = 0;x <5; x++)
            {
                for(int y = 0;y <5;y++)
                {
                    if(x != y && ran[x] == ran[y])
                    {
                        ran[0] = r.nextInt(52);
                    }
                }
            }
            
            cards[0] = Cards.getNewCard(ran[0]);
            
            filename[0] = Cards.getNewName(ran[0]); //get card name
            //test = filename[0].charAt(1);
            //filename[0] = String.valueOf(test);
            
        }
        
        if(!ch2)
        {
            ran[1] = r.nextInt(52);
            for(int x = 0;x <5; x++)
            {
                for(int y = 0;y <5;y++)
                {
                    if(x != y && ran[x] == ran[y])
                    {
                        ran[1] = r.nextInt(52);
                    }
                }
            }
            
            cards[1] = Cards.getNewCard(ran[1]);
            filename[1] = Cards.getNewName(ran[1]); //get card name
        }
        
        if(!ch3)
        {
            ran[2] = r.nextInt(52);
            for(int x = 0;x <5; x++)
            {
                for(int y = 0;y <5;y++)
                {
                    if(x != y && ran[x] == ran[y])
                    {
                        ran[2] = r.nextInt(52);
                    }
                }
            }
            
            cards[2] = Cards.getNewCard(ran[2]);
            filename[2] = Cards.getNewName(ran[2]); //get card name
        }
        
        if(!ch4)
        {
            ran[3] = r.nextInt(52);
            for(int x = 0;x <5; x++)
            {
                for(int y = 0;y <5;y++)
                {
                    if(x != y && ran[x] == ran[y])
                    {
                        ran[3] = r.nextInt(52);
                    }
                }
            }
            
            cards[3] = Cards.getNewCard(ran[3]);
            filename[3] = Cards.getNewName(ran[3]); //get card name
        }
        
        if(!ch5)
        {
            ran[4] = r.nextInt(52);
            for(int x = 0;x <5; x++)
            {
                for(int y = 0;y <5;y++)
                {
                    if(x != y && ran[x] == ran[y])
                    {
                        ran[4] = r.nextInt(52);
                    }
                }
            }
            
            cards[4] = Cards.getNewCard(ran[4]);
            filename[4] = Cards.getNewName(ran[4]); //get card name
        }
                
    }

///if the draw button is highlighted and the user has drawn new cards, do this
if(enter && (cy == 2) && (cx ==4) && drawed)
                {
                    replaceHold();
                    evaluate();
                    
                    if(result)
                    {
                        Alert error = new Alert ("confirm");
                        error.setString("You have got a jack or better " );
                        error.setTimeout (3000);
                        Display.getDisplay(root).setCurrent(error);
                    }
                    else
                    {
                        Alert error = new Alert ("confirm");
                        error.setString("You did not win " );
                        error.setTimeout (3000);
                        Display.getDisplay(root).setCurrent(error);
                    }
                    enter = false;
                    
                    drawed = false;
                    
                    
                    
                }


}

edit: is debugging a program ment to be slow? because when i launch my progam in debug mode its running alot slower than normal
 
What are you still having problems with? Have you tried debugging your program?
 
Why are you bumping? You've already got an answer in post #3. Did that not work? You haven't given me the courtesy of a response.
 
My guess is that
Code:
while(j < 4 || !jackorBetter)

should be
Code:
while(j < 4 && !jackorBetter)
I'd also replace
Code:
for(int z  = 0; z < 5; z++)
{
   if((x != z) && (rank[x] == rank[z])
with
Code:
for(int z  = x+1; z < 5; z++)
{
   if(rank[x] == rank[z])
 
Back
Top