Jump to content

Featured Replies

Posted

Im writing a simple blackjack app in java (runs in terminal, no flashy graphics, no betting) for my java hw. (gonna convert to applet with betting later on)

 

Anyway im having the hardest time tryign to figure out how to creat a standard 52 card deck and a dealer method to deal some cards. I have looked at some sample code on google and I do not understand what is going on.

 

Can someone help me by writing a simple 52 card deack with a dealer method or give me tips on how to go about doing this? Thanks.

import java.util.Random;
import java.util.Arrays;

public class Cards
{
boolean[] deck = new boolean[52]; //boolean array of length 52 (used to track which cardshave been dealt and which havn't
int[] hand = new int[10]; //assume the player wont want more than ten cards, they should go bust after this many anyway

Random generator = new Random();

public Cards()
{
	Arrays.fill(deck, true);
	while (!bust || !stay)
	{
		
	//insert blackjack code etc here...
	
	}	
}

public int deal()
{
	int i = generator.nextInt(52) + 1;  // generates a number between 1 and 52
	if (deck[i] = true) {deck[i] = false; return i;} //if it hasnt already been dealt it is returned
	else {return deal();} //otherwise the method calls itself to try again 
}


public static void main(String[] args)
{
	Cards example = new Cards();
}
}

 

here cards are dealt and saved as integers, so to deal a card you put somewhere in the code, like hand = deal()

 

please note this has only been done for one deck, casino blackjack uses four decks, but it should give you an idea of what you need to do

thank you greeneyes

 

you are very welcome, if you need any other help just gimmie a shout.

 

Ew, Java.

agreed

 

oh noes not an easy to use, object orientated, secure, platform independant language!

i made a casino in c# once, the way i did it was i just created a card object and then put that in an array of object,

 

public class cards
{
	private String suit;
	private String type;
	private int number;
	private String details;
	private String cardPicture;

	public cards(String inType, String inSuit ,int inNumber, String inCardPicture) 
	{
		suit = inSuit;
		type = inType;
		number = inNumber;
		cardPicture = inCardPicture;
	}

	public String getType()
	{
		return type;
	}
	public String getSuit()
	{
		return suit;
	}
	public String getCardPicture()
	{
		return cardPicture;
	}
	public int getNumber()
	{
		return number;
	}
	public String toString()
	{
		//details = name + number;
		details = type + " OF " + suit;
		return details;
	}

}
}

 

//SHUFFLE DECK OF CARDS ARRAY
	public static void shuffle()
	{
		int switchLocation;
		Random randomInteger = new Random();
		switchLocation = randomInteger.Next(52);
		cards cardSwitch1;		
		cards cardSwitch2;

		for (int i = 0; i < cardsArray.Length; i++)
		{
			switchLocation = randomInteger.Next(52);
			cardSwitch1 = cardsArray[i];
			cardSwitch2 = cardsArray[switchLocation];

			cardsArray[i] = cardSwitch2;
			cardsArray[switchLocation] = cardSwitch1;
		}
	}

 

//CREATE A DECK OF CARDS ARRAY
	public static void createDeck()
	{
		//HEARTS
		cards createCard;
		int cardCount = 0;
		createCard = new cards("ACE","HEARTS",1,"ah.gif");
		cardsArray[cardCount] = createCard;
		cardCount++;

 

 

theres some bits of code from it, i had 4 arrays, deck array, the players hand array , the dealers hand array and cards in use array, so u cant have the same card twice, and i move them around the arrays

oh noes not an easy to use, object orientated, secure, platform independant language!

 

Of course it's also slow and resource hogging. Even C# is better.

Of course it's also slow and resource hogging. Even C# is better.

 

if platform independance is sacrificed and native code is complied it can be just as fast C/C++, and as it is easier to optimise (no messing with pointers) can even be made to run faster.

 

but tbh the only reason i study java is because i have to for my course, so ya whatever

if platform independance is sacrificed and native code is complied it can be just as fast C/C++, and as it is easier to optimise (no messing with pointers) can even be made to run faster.

 

but tbh the only reason i study java is because i have to for my course, so ya whatever

 

i like java, it is slower because it gets compiled on a virtual machine into bitcode, which makes it portable because thats the whole idea of it. C isn't object oriented, and c++ dont have serializable which i love, java can do anything that c++ can do, it all comes down to personal preference in the end, so next time someone posts a thread asking for help with ANY language only post if you have help, dont post that language sucks, because quite frankly no one gives a fuck about your opinion, they wanted help in one language for a reason and dont need alot of that language sucks posts.

  • Author

I am going to get started on this project tomorrow, thanks for code examples r00t

 

btw, i was just wondering, has anybody here tried putting an applet you wrote on a cell phone?

r00t']i like java' date=' it is slower because it gets compiled on a virtual machine into bitcode, which makes it portable because thats the whole idea of it. C isn't object oriented, and c++ dont have serializable which i love, java can do anything that c++ can do, it all comes down to personal preference in the end, so next time someone posts a thread asking for help with ANY language only post if you have help, dont post that language sucks, because quite frankly no one gives a fuck about your opinion, they wanted help in one language for a reason and dont need alot of that language sucks posts.[/quote']

 

i was helping! or maybe you were only quoting my post because it was the most recent one

i was helping! or maybe you were only quoting my post because it was the most recent one

 

yes i know, i should have maybe put somthing like "i agree" first

 

and sIttin_siDewAys if you want to make programs for mobile phones u need J2ME (micro editon) then using somthing like netbeans with the mobility pack to emulate the phone, and an applet it only a web based program winthin a web brouser with limited functions because security issues

Guest
This topic is now closed to further replies.