# blackjack java



## Galaxy_Tab (Nov 9, 2011)

who can help to do it ??? i don't know how to do, i am studying the java.


----------



## DoubleHelix (Dec 10, 2004)

Go to class, and do your own homework.


----------



## sepala (May 20, 2010)

Welcome to TSG

Need to develop a GAME with a superb GUI ? What is your JAVA level?. Anyway, here we correct codes, we do not give the complete code


----------



## Galaxy_Tab (Nov 9, 2011)

@sepala Just a basic version of this game.I'm a beginner in programming language 
So how do i store these ???

suit int Card 
Possible values: 0 to 3
Represents "Spade", "Diamond", "Club", "Heart".

suit int Card
Possible values: 0 to 12
Represents "Ace", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten", "Jack",
"Queen", "King".


----------



## sepala (May 20, 2010)

Hi,

Actually I have no idea about this game, because of that it could be very hard for me to find what you are trying to do. My apologies for that.

Anyway the answer for HOW TO STORE VALUES is, use Variables. In your case, you will need the help of "IF ELSE" condition too.

For an example, have a look below.


```
public class BlackJack
{
    public static void main(String[]args)
    {
        String spade= "SPADE";
        String diamond = "DIAMOND";
        String club = "CLUB"
        int i=0;

       if(i==0)
       {
          System.out.println(spade);
        }
       else if(i==1)
       {
           System.out.println(diamond);
       }
       else if(i==2)
       {
          System.out.println(club);
        }
}
}
```
This is just a small example to show how to use Variables, and how to address the MAIN part of your issue. You have to change these according to your need. Apart from that, you might want to get the user input to check what the CARD THEY PLAY is. For that you need to use STREAMS. I didn't use any of those here, because I wanted to keep this simple as possible.

Ask any question you have


----------



## Galaxy_Tab (Nov 9, 2011)

Thanks for the tips but what should i do if i want to return the value instead of printing?


----------



## sepala (May 20, 2010)

2 Ways to do

1. Assign the value to a separate variable
2. Use methods with NON VOID return functions


----------



## Galaxy_Tab (Nov 9, 2011)

i dont get what you mean . I'm a total noob at this


----------



## sepala (May 20, 2010)

Galaxy_Tab said:


> i dont get what you mean . I'm a total noob at this


I got your PM with the code. I like to assist you in the thread rather than using PM .. May I post your code here and correct it?


----------



## sepala (May 20, 2010)

Anyway, you have coded it better than I thought. Good Job ..

At a glance, there are no errors in the code, and no possible runtime errors too. But I can't exactly say that because you haven't provide the full code. However, there is something that you MUST learn. Learn where to use "this" keyword and where not to use. You have used that in several places where it is not needed.


----------



## Galaxy_Tab (Nov 9, 2011)

sepala said:


> Anyway, you have coded it better than I thought. Good Job ..
> 
> At a glance, there are no errors in the code, and no possible runtime errors too. But I can't exactly say that because you haven't provide the full code. However, there is something that you MUST learn. Learn where to use "this" keyword and where not to use. You have used that in several places where it is not needed.


I'm still not very sure how "this" works, feel free to edit the codes


----------



## sepala (May 20, 2010)

"this" keyword is a keyword used to access "class cope variables". This is mainly used INSIDE A METHOD if the method has parameters WHICH ARE SIMILAR TO CLASS COPE VARIABLES. I will edit your code, no problem, but it will be posted tomorrow because now I am in a middle of a project


----------



## sepala (May 20, 2010)

You code is sent to you via PM. 

Here what you have to learn...

1. Avoid unnecessary keywords in unnecessary places.
2. Arrange the code well. Use proper alignments. Then only it will be programmer friendly.
3. Don't use method names like "toString()". They are already defined methods in JAVA, in your case it didn't make any issues, but other people might get confused on that. (toString() is a powerful built in method in JAVA. It is defined inside OBJECT, the ROOT of the java classes)
4. When you give your code to someone to check, give the full code, or at least enough parts to RUN the code.
5. When you are using names for variables, avoid giving similar names such as "Number","NUMBER","number" etc.


----------



## Galaxy_Tab (Nov 9, 2011)

I'm stuck with the Deck class this is the code i have done so far.I'm not sure why is there an error in Card dispenseCard()

import java.util.Random;

public class Deck 
{

// Fields of Deck class
public Deck[] Card = new Deck[52];
public int cardsDispensed;

// Method of Deck class

public Deck()
{
Deck d = new Deck();

// Prints the deck in order with 0 card used starting from Spade Ace to Heart King
System.out.println("An ordered deck (Card Used: " + d.dispenseCard() + ")");
System.out.println(d);
System.out.println();

d.shuffleDeck();
}

public Card dispenseCard()
{

}

public void createDeck()
{

}

public void shuffleDeck()
{

}

}


----------



## Galaxy_Tab (Nov 9, 2011)

Galaxy_Tab said:


> I'm stuck with the Deck class this is the code i have done so far.I'm not sure why is there an error in Card dispenseCard()
> 
> import java.util.Random;
> 
> ...


This is the class diagram

*BlackJackGame consists of *

dealer : Player
players : Player[]
deck : Deck 
numPlayer : int

main(String []) : void
start() : void 
registerPlayers() : void
ask PlayersPlaceChips() : void
dealCardsToDealer() : void
dealCardsToDealer2() : void
dealCardsToPlayers() : void
determineWinLoseOrTie() : void
isGameOver() : boolean

*Deck class consists of*

deck : Card[52]
cardsDispensed : int

Deck()
dispenseCard() : Card
createDeck() : void
shuffleDeck() : void

*Card class consists of *

suit : int
number : int

Card(int, int)
getScore() : int
getTitle() : String
display() : void

*Player class consists of*

id : char
name : String
chips : int
stake : int
hand : Card[5]

Player(Char, String)
leftGame() : boolean
placeStake(int) : void
win() : void
lose() : void
hit(Card) : void
valueOfHand() : int
clearHand() : void
displayStatus() : void


----------



## sepala (May 20, 2010)

Line Number 24 is the problem. You are trying to get a reference to the Card class, but the way you did is invalid. You are creating a method which has no return type, thinking you are calling to the constructor of the Card class. Thats the issue.

What you can do is, create an instance of Card class and use that to access its resources.
*ex: Card c = new Card(parameter1, parameter2);
c.getPublicMethods();*

Apart from that what are you doing in the line number 18? How can you print a WHOLE CLASS??

Pay attention on line number 17 and 21. Have a look in here
_*
d.shuffleDeck(); d.dispenseCard()*_

You don't have a reference to the class because you are calling to the methods inside the same class. Just use dispenseCard() and that is enough


----------



## sepala (May 20, 2010)

???


----------



## Galaxy_Tab (Nov 9, 2011)

Thanks for the help so far. I have send u a pm with my codes ,can you take a look and see if i could improve further?


----------



## sepala (May 20, 2010)

I will...


----------



## sepala (May 20, 2010)

Just to let you know, sorry for the delay of checking your code. I won't be able to check it untill monday (today is saturday) because I am having classes. If you can paste your code here, others will help you for sure


----------



## Galaxy_Tab (Nov 9, 2011)

ok thanks anyway im done with my deck class  but im stuck with player =(
What should i put in hit,valueOfHand and clearHand

// Player class
public class Player {
// attributes
public char id;
public String name;
public int points;
public int pointsInUse;
public Card[] hand = new Card[5];

public final char[] ID = { 'A','B','C' };
public final String[] NAME = { "1","2","3" };
public final int[] POINTS = { 100, 100, 100 };
public final int[] POINTSINUSE = new int[3];
public final Card[] HAND = new Card[5];

// set and get methods
public Player(char id, String name) {
this.id = id;
this.name=name;
}

public boolean leftGame(int i) {
if (POINTS_ == 0) { // If player have 0 chip,the player will leave the game
return true;
} 
else {
return false;
}
}

// Points
public void usePoints(int pointsInUse) {
points = points - pointsInUse;
}

// Add Points
public void addPoints() {
points = 100 + pointsInUse;
}

// Minus Points
public void minusPoints() {
points = 100 - pointsInUse;
}

// Hit
public void hit(int Card) {

}

// Value of Hand
public int valueOfHand() {
return 0;
}

// Clear Hand
public void clearHand() {
System.out.print("Clear Hand");
}

// Display
public void displayStatus() {
System.out.println("PLAYER " + String.valueOf(ID) + " :" + NAME);
}
}_


----------

