So i need to create a deck of 52 cards (a normal deck), and then i have to shuffle the deck. I originally did a swap shuffle code (switching 2 random cards 1000 times), but i need to do a Fisher Yates Shuffle. I need to "place each card in a random location". And i am having issues with how that works.My current shuffle method is something i got offline but im not sure how it works, i was just testing it. Also, no matter how many times i compiled it with the original shuffle, the first card would always be the same (the first card coded in the program), the last card would always be the same(the last card coded in the program), and the 26th card coded (king of spades) would be "empty" and not display suit or point value. Thanks!Sorry if formatting is bad im new to this site.
public static void main(String args[])
{
System.out.println("Lab11bst.JAVA");
System.out.println();
Deck deck = new Deck();
System.out.println(deck);
}
}
class Card
{
private String suit;
private String rank;
private int pointValue;
public Card(String s, String r, int pV)
{
suit = s;
rank = r;
pointValue = pV;
}
public String suit() { return suit; }
public String rank() { return rank; }
public int pointValue() { return pointValue; }
public String toString() { return rank + " of " + suit + " (point value = " + pointValue + ")"; }
}
class Deck
{
private Card[] cards;
private int size;
public Deck()
{
size=52;
cards=new Card[size];
for ( int q = 0 ; q < 13 ; q++ ) {
String rank = "";
int pointValue =0;
if (q+1==1){
rank = "Ace";
pointValue =1;
}
if (q+1==2){
rank = "Two";
pointValue =2;
}
if (q+1==3){
rank = "Three";
pointValue =3;
}
if (q+1==4){
rank = "Four";
pointValue =4;
}
if (q+1==5){
rank = "Five";
pointValue =5;
}
if (q+1==6){
rank = "Six";
pointValue =6;
}
if (q+1==7){
rank = "Seven";
pointValue =7;
}
if (q+1==8){
rank = "Eight";
pointValue =8;
}
if (q+1==9){
rank = "Nine";
pointValue =9;
}
if (q+1==10){
rank = "Ten";
pointValue =10;
}
if (q+1==11){
rank = "Jack";
pointValue = 10;
}
if (q+1==12){
rank = "Queen";
pointValue = 10;
}
if (q+1==13){
rank = "King";
pointValue = 10;
}
Card a = new Card( "Clubs", rank, pointValue );
cards[q] = a;
}
for (int q=13;q < 26; q++) {
String rank = "";
int pointValue = 0;
if (q+1==14){
rank = "Ace";
pointValue=1;
}
if (q+1==15){
rank = "Two";
pointValue=2;
}
if (q+1==16){
rank = "Three";
pointValue =3;
}
if (q+1==17){
rank = "Four";
pointValue=4;
}
if (q+1==18){
rank = "Five";
pointValue =5;
}
if (q+1==19){
rank = "Six";
pointValue=6;
}
if (q+1==20){
rank = "Seven";
pointValue =7;
}
if (q+1==21){
rank = "Eight";
pointValue =8;
}
if (q+1==22){
rank = "Nine";
pointValue =9;
}
if (q+1==23){
rank = "Ten";
pointValue =10;
}
if (q+1==24){
rank = "Jack";
pointValue =10;
}
if (q+1==25){
rank = "Queen";
pointValue = 10;
}
if (q+1==26){
rank = "King";
pointValue = 10;
}
Card a = new Card("Diamonds",rank,pointValue);
cards[q]=a;
}
for (int q=26;q <39; q++) {
String rank = "";
int pointValue = 0;
if (q+1==27){
rank = "Ace";
pointValue=1;
}
if (q+1==28){
rank = "Two";
pointValue=2;
}
if (q+1==29){
rank = "Three";
pointValue =3;
}
if (q+1==30){
rank = "Four";
pointValue=4;
}
if (q+1==31){
rank = "Five";
pointValue =5;
}
if (q+1==32){
rank = "Six";
pointValue=6;
}
if (q+1==33){
rank = "Seven";
pointValue =7;
}
if (q+1==34){
rank = "Eight";
pointValue =8;
}
if (q+1==35){
rank = "Nine";
pointValue =9;
}
if (q+1==36){
rank = "Ten";
pointValue =10;
}
if (q+1==37){
rank = "Jack";
pointValue =10;
}
if (q+1==38){
rank = "Queen";
pointValue = 10;
}
if (q+1==26){
rank="King";
pointValue=10;
}
Card a = new Card("Spades",rank,pointValue);
cards[q]=a;
}
for (int q=39;q <52; q++) {
String rank = "";
int pointValue = 0;
if (q+1==40){
rank = "Ace";
pointValue=1;
}
if (q+1==41){
rank = "Two";
pointValue=2;
}
if (q+1==42){
rank = "Three";
pointValue =3;
}
if (q+1==43){
rank = "Four";
pointValue=4;
}
if (q+1==44){
rank = "Five";
pointValue =5;
}
if (q+1==45){
rank = "Six";
pointValue=6;
}
if (q+1==46){
rank = "Seven";
pointValue =7;
}
if (q+1==47){
rank = "Eight";
pointValue =8;
}
if (q+1==48){
rank = "Nine";
pointValue =9;
}
if (q+1==49){
rank = "Ten";
pointValue =10;
}
if (q+1==50){
rank = "Jack";
pointValue =10;
}
if (q+1==51){
rank = "Queen";
pointValue = 10;
}
if (q+1==52){
rank = "King";
pointValue = 10;
}
Card a=new Card("Hearts",rank,pointValue);
cards[q]=a;
}
shuffle(52);
}
public int getSize( ) {
return size;
}
public boolean isEmpty( ) {
return size==0;
}
public String toString()
{
String display= "";
for (int q=0;q<size;q++)
display=display+cards[q].toString()+"\n";
return display;
}
private void change(Card[] a,int x,int y)
{
Card temp;
temp = a[x];
a[x] = a[y];
a[y] = temp;
}
static void shuffle(int[] shuffle)
{
Random rnd=new Random();
for (int q=deck.length-1;q>0;q--){
int index=rnd.nextInt(q+1);
int a=deck[index];
deck[index]=deck[q];
deck[q]=a;
}
}
/*private void shuffle(int shuffle)
{
for (int q=0;q<shuffle;q++)
{
int random1=random(0,51);
int random2=random(0,51);
change(cards,random1,random2);
}
}*/
public static int random(int maximum,int minimum)
{
int boundaries=(maximum+1) - minimum;
return(int)((Math.random()*boundaries)+minimum);
}
}
Aucun commentaire:
Enregistrer un commentaire