Java Shuffle (Shuffle) simple algorithm (three implementations)

Source: Internet
Author: User
Tags shuffle

Package shuffle;
public class Shuffle {//Portal

public static void Main (string[] args) {
Ppoker a=new Ppoker ();
System.out.println ("Please check the card ************");
A.getpokerpoint ();
System.out.println ();
System.out.println ("Shuffle in");
A.shuffleone ();
A.getpokerpoint ();
System.out.println ();
System.out.println ("Shuffle in");
Ppoker b=new Ppoker ();
B.shuffletwo ();
B.getpokerpoint ();
System.out.println ();
System.out.println ("Shuffle in");
Ppoker c=new Ppoker ();
C.shufffethree ();
C.getpokerpoint ();
}
}

Package shuffle;

public class Poker {//card (data type),

public String Point;
public String color;
Public Poker (String x,string y)
{
Point=x;
Color=y;
}

}

Package shuffle;

public class Ppoker {//Shuffle people, shuffle people to achieve the specific operation of shuffling
Private poker []poker=new poker[54];
Private String []point;
Private string[] color;
Public Ppoker ()
{

Point=new String [] {"A", "2", "3", "4", "5", "6", "7", "8", "9", "Ten", "J", "Q", "K"};
Color=new String []{"♥", "♣", "♦", "♠"};
int k=0;
for (int i=0;i<13;i++)
{
for (int j=0;j<4;j++)
{poker[k]=new Poker (point[i],color[j]); k++;}
}
Poker[52]=new Poker ("JOKER", "small");
Poker[53]=new Poker ("JOKER", "big");
}
Public poker[] Getpokerpoint ()
{
for (int i=0;i<54;i++)
{
System.out.print (Poker[i].color+poker[i].point);
}
return Poker.clone ();
}
public void Shuffleone ()
{
String []temc=new string[54];
String []temp=new string[54];
for (int j=0;j<3;j++)
{
for (int i=0;i<54;i++)
{
Temc[i]=poker[i].color;
Temp[i]=poker[i].point;
}

for (int i=0;i<27;i++)
{
POKER[2*I].COLOR=TEMC[I+27];
POKER[2*I].POINT=TEMP[I+27];
Poker[2*i+1].color=temc[i];
Poker[2*i+1].point=temp[i];
}
}
}
public void Shuffletwo ()
{
for (int i=0;i<53;i++)
{
int t= (int) (Math.random () * (53-i));
Poker tem=poker[t];
Poker[t]=poker[53-i];
Poker[53-i]=tem;
}

}
public void Shufffethree ()
{

for (int i=0;i<54;i++)
{
int r= (int) (Math.random () *54);
if (Poker[i]!=poker[r])
{
Poker temp=poker[i];
POKER[I]=POKER[R];
Poker[r]=temp;
}
}
}
}

Java Shuffle (Shuffle) simple algorithm (three implementations)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.