Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    12
    Rep Power
    0

    Need Help With BlackJack Program


    i have been working on making the game blackjack. the code is nearly completed and most parts of it compile but i am having trouble with the hand program. at line 17 it says that i cannot compare String[] types and int types. I guess my question is there any way i can compare them or if there is any way to temporarily convert the string array into int types.

    The code has 4 parts, Card, Deck, Hand, and BlackJack

    CARD

    import java.util.*;



    public class Card{

    static String[] v = {"", "SPADES", "CLUBES", "DIAMONDS", "HEARTS"};

    static String[] c = {"", "","ACE", "TWO", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

    private int value;

    private int face;



    public static final int ACE = 1;

    public final int KING = 13;

    public final int QUEEN = 12;

    public final int JACK = 11;

    public final int TEN = 10;

    public final int NINE = 9;

    public final int EIGHT = 8;

    public final int SEVEN = 7;

    public final int SIX = 6;

    public final int FIVE = 5;

    public final int FOUR = 4;

    public final int THREE = 3;

    public final int TWO = 2;

    public final int HEARTS = 0;

    public final int DIAMONDS = 0;

    public final int CLUBS = 0;

    public final int SPADES = 0;







    public String[] getValue(){

    int TEN =10; int JACK = 10; int Queen = 10; int KING = 10;

    return c;

    }



    public String toString() {



    String[] v = {"", "SPADES", "CLUBES", "DIAMONDS", "HEARTS"};

    String[] c = {"", "","ACE", "TWO", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    return null;


    }
    public Card(int value, int face){

    }

    public static void main (String args[]){
    System.out.println(v + " " + c);
    }
    }

    DECK

    import java.util.*;



    public class Deck {

    private int numberOfCards = 52;

    private Card[] deck = new Card[numberOfCards];



    Deck() {

    fill();

    shuffle();

    }



    private void fill() {

    for (int i = 0; i < deck.length; ++i) {
    deck[i] = new Card((i / 13) + 1, (i % 13) + 1);
    }

    }



    private void shuffle(){

    Collections.shuffle(Arrays.asList(deck));

    }



    Card draw() {

    if (numberOfCards < 0) {

    return null;
    }

    return deck[--numberOfCards];

    }
    public static void main (String[] args){

    }
    }

    HAND

    import java.util.ArrayList;
    import java.util.*;


    public class Hand{

    public int score = 0;
    private List<Card> cards = new ArrayList<Card>();

    private int AcesHigh = 0;


    void hit(Card c){

    cards.add(c);
    // THIS IS WHERE THE PROBLEM IS
    if(c.getValue() == Card.ACE && (score +11) <=21) {


    score += 11;

    ++ AcesHigh;

    }
    else {
    score += 1;
    }

    }



    Card newCard() {

    if(cards.size() > 0) {
    return cards.get(cards.size() - 1);
    }

    return null;

    }



    int getScore(){

    while (score > 21 && AcesHigh > 0){

    score -= 10;

    -- AcesHigh;

    }

    return score;

    }



    public String toString(){

    String v = "";

    for (Card c : cards) {
    v += c + "\n";
    }

    return v;

    }
    public static void main (String args[]){

    }
    }

    import java.util.Random;
    import java.util.Scanner;

    public class BlackJack{

    Scanner myScanner = new Scanner (System.in);

    static Deck deck = new Deck();
    static Hand player = new Hand();
    static Hand dealer = new Hand();

    static void deal(){

    player.hit(deck.draw());

    dealer.hit(deck.draw());

    player.hit(deck.draw());

    dealer.hit(deck.draw());

    System.out.println("Dealer's up card = " + dealer.newCard() + '\n');

    System.out.println("Your hand is: ");

    System.out.println(player);

    }
    @SuppressWarnings("empty-statement")
    static boolean finalPlayerHand(){


    System.out.println("Do You want to Hit? Press y for yes, n for no:>> ");
    Scanner myScanner = new Scanner (System.in);

    String answer;
    answer = myScanner.next();

    while (answer.equalsIgnoreCase("y"));{

    player.hit(deck.draw());

    System.out.println(player.newCard());

    if (player.getScore() > 21){

    System.out.println("You Lose!");

    }
    }
    return true;
    }
    public static boolean finalDealerHand() {

    System.out.println("\n" + "Dealer's hand is: \n" + dealer);

    while(dealer.getScore() < 17) {

    System.out.print("Dealer draws: ");

    dealer.hit(deck.draw());

    System.out.println(dealer.newCard());

    if (dealer.getScore() > 21){

    System.out.println("You Win!");
    }
    }
    return true;
    }

    static void finalScoring(){

    if (player.getScore() > dealer.getScore()){

    System.out.println("You Win!");

    }

    else if (player.getScore() < dealer.getScore()) {

    System.out.println("Dealer Wins!");

    }

    else if (player.getScore() == dealer.getScore()){

    System.out.println("You Tied!");

    }

    }
    public static void main(String[] args) {

    BlackJack game = new BlackJack();

    deal();

    if (finalPlayerHand()) {
    return;
    }

    if (finalDealerHand()) {
    return;
    }

    finalScoring();

    }

    }

    Any other feedback is appreciated as well
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please copy the full text of the error messages and paste it here.

    also please wrap the code in code tags to preserve formatting.
    Put [code] before the code
    and [/code] after the code.

    (c.getValue() == Card.ACE)
    the compiler does not know how to compare the array returned bu the getValue() method to an int value?

    What is this code supposed to do?
    Last edited by NormR; April 27th, 2013 at 05:51 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    12
    Rep Power
    0

    error message


    my bad this is the error message when i try to run BlackJack

    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incomparable types: java.lang.String[] and int
    at Hand.hit(Hand.java:17)
    at BlackJack.deal(BlackJack.java:14)
    at BlackJack.main(BlackJack.java:98)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    that part of the code is supposed to give ACE value 11 if it does not bring the players hand over 21 and value of 1 if it does
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Code:
    (c.getValue() == Card.ACE)
    the compiler does not know how to compare the array returned bu the getValue() method to an int value?

    What is the code in this statement supposed to do?
    Why would you compare an array of String to an int value?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    12
    Rep Power
    0
    in the code Card in the String [] c, ACE is given an int value
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    That doesn't make sense. Can you explain it differently?
    In the Card class, c is a String array that contains the String "ACE" at index of 1
    The variable: ACE is an int with a value of 1

    In the Hand class's hit() method, c is a Card object. The Card class's getValue() method returns c the String array.
    A String array can NOT be compared with a int value!!!

    Please edit your post and wrap the code with code tags.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    12
    Rep Power
    0
    ok i see. as i explained before i am trying to make ACE a situational value that returns 1 when the players score is higher than 10 and 11 otherwise. How should i go about tackling this problem?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Use an if statement to test the current score and if the score is as desired, change the value of the ACE variable to 11.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    12
    Rep Power
    0
    here is the new if statement but netbeans is telling me that i
    [code]
    void hit(Card c){

    cards.add(c);
    if (score <= 10){
    Card.ACE = 11;

    }
    else{
    Card.ACE = 1;
    }
    }
    [end of code]

    Black Jack is now running but the results are not coming out correctly when i try to run it the output is

    run:
    Dealer's up card = null

    Your hand is:
    null
    null

    Do You want to Hit? Press y for yes, n for no:>>


    Everything comes back null and entering y or n does not do anything. Any idea what i did wrong?
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Everything comes back null
    That means the variable has not been assigned a valid value. Find the variable with the null value and then back track in the code to see why is has not been assigned a value.

    The code tags are not right. The ending tag should be: [/code]
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    12
    Rep Power
    0
    so i routed it back to Card program and i believe the problem was at the end when i made the card method
    Code:
    public Card(int value, int face){
       
    }
    im really not sure what i need to assign these to.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    12
    Rep Power
    0
    so i routed it back to Card program and i believe the problem was at the end when i made the card method
    Code:
    public Card(int value, int face){
       
    }
    im really not sure what i need to assign these to.
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Each card has a count value and an face value.
    For example a jack has face value of 11 and a count value of 10.
    The constructor needs to save those values in class variables.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    12
    Rep Power
    0
    im starting to run into that problem of comparing string values with int values again. i want to write something like
    Code:
    for (c[3]){
    face = 1;
    value = 1;
    }
    this doesnt seem to be working how exactly do i reference a specific element from an array and assign other int values to it?
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    how exactly do i reference a specific element from an array and assign other int values
    The values that are assigned to slots in an array MUST be the same type as the type of the array. int values to int arrays, String values to String arrays.
    You can't assign an int value to a slot in a String array.

    Is your problem using a class object that is in an array's slot?
    Treat the array slot reference: anArray[ix] just the same as any variable:
    If anArray is an array of String, then you can check the length of the String in slot 2 with this:
    if(anArray[2].length() > 3) // check the length of the String in slot 2
    that's the same as this if aString is a String variable
    if(aString.length() > 3)
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo