扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
java内的Collections类自带了一个shuffle洗牌算法。
成都创新互联公司是一家专注于成都网站制作、成都网站建设与策划设计,桦南网站建设哪家好?成都创新互联公司做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:桦南等地区。桦南做网站价格咨询:18982081108
static void shuffle(List? list)
使用默认随机源对指定列表进行置换。
static void shuffle(List? list, Random rnd)
使用指定的随机源对指定列表进行置换。
现在你可以把52张牌放进一个List里,调用他的shuffle算法打乱顺序。
1.定义牌类
public class Card{
public int id;//牌大小
public String number;//牌值名称
public String color;//花色
}
2.定义牌组,用ArrayList存放所有牌
ArrayListCard cardGroup =new ArrayListCard();
3.定义花色枚举
public static final String[] COLORS=new String[]{"方块","梅花","红桃","黑桃"};
4.定义牌值大小枚举
public static fina String[] CARDVALUES=new String[]{"A","1","2","3",
"4","5","6","7","8","9","10","J","Q","K","King_Small","King_Big"};
5.初始化牌组,这就是一副新牌,没切过,己经排好序的
public void initCardGroup(){
//加入A~K
for(int i=0;i13;i++){
for (int j=0;j4;j++){
//生成一张牌的对象
Card card=new Card();
card.id=i;
card.color=COLORS[j];
card.munber=CARDVALUES[i];
//将对象加入牌组
cardGroup.add(card);
}
}
//加入大小王
Card card_k1=new Card();
card_k1.id=13;
card_k1.color="King_Small";
card_k1.number="King_Small";
cardGroup.add(card_k1);
Card card_k2=new Card();
card_k2.id=14;
card_k2.color="King_big";
card_k2.number="King_big";
cardGroup.add(card_k2);
}
6. 洗牌,将原牌组打乱,得到新的牌组
public ArrayListCard flushCardsGroup(ArrayListCard src){
ArrayListCard result=new ArrayListCard();
while(src.size0){
int size=src.size();
//以size为限,生成0~size之间的随机数
Random r=new Random();
int index=r.nextInt(size);
//对index做个检查
if(index0){index=0;}
if(indexsize-1){index=size-1;}
//index就是捡出来的牌,加到新的数组中,从原来数组中删除
Card card=src.get(index);
result.add(card);
card.remove(index);
}
return result;
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
/*
* 思路:
* A:创建一个HashMap集合
* B:创建一个ArrayList集合
* C:创建花色数组和点数数组
* D:从0开始往HashMap里面存储编号,并存储对应的牌
* 同时往ArrayList里面存储编号即可。
* E:洗牌(洗的是编号)
* F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
* G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
*/
public class PokerDemo {
public static void main(String[] args) {
// 创建一个HashMap集合
HashMapInteger, String hm = new HashMapInteger, String();
// 创建一个ArrayList集合
ArrayListInteger array = new ArrayListInteger();
// 创建花色数组和点数数组
// 定义一个花色数组
String[] colors = { "♠", "♥", "♣", "♦" };
// 定义一个点数数组
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K", "A", "2", };
// 从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。
int index = 0;
for (String number : numbers) {
for (String color : colors) {
String poker = color.concat(number);
hm.put(index, poker);
array.add(index);
index++;
}
}
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
// 洗牌(洗的是编号)
Collections.shuffle(array);
// 发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
TreeSetInteger fengQingYang = new TreeSetInteger();
TreeSetInteger linQingXia = new TreeSetInteger();
TreeSetInteger liuYi = new TreeSetInteger();
TreeSetInteger diPai = new TreeSetInteger();
for (int x = 0; x array.size(); x++) {
if (x = array.size() - 3) {
diPai.add(array.get(x));
} else if (x % 3 == 0) {
fengQingYang.add(array.get(x));
} else if (x % 3 == 1) {
linQingXia.add(array.get(x));
} else if (x % 3 == 2) {
liuYi.add(array.get(x));
}
}
// 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
lookPoker("风清扬", fengQingYang, hm);
lookPoker("林青霞", linQingXia, hm);
lookPoker("刘意", liuYi, hm);
lookPoker("底牌", diPai, hm);
}
// 写看牌的功能
public static void lookPoker(String name, TreeSetInteger ts,
HashMapInteger, String hm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String value = hm.get(key);
System.out.print(value + " ");
}
System.out.println();
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ListString list = new ArrayListString();
while (scanner.hasNextLine()) {
String string = scanner.nextLine();
list.add(string);
if (list.size() == 5) {
int r = getCard(list);
System.out.println(r);
list.clear();
}
}
}
private static int getCard(ListString list) {
int temp = 7;
int size = list.size();
ListString numList = new ArrayListString();
SetString numSet = new TreeSetString();
ListString colourList = new ArrayListString();
SetString colourSet = new TreeSetString();
for (int i = 0; i list.size(); i++) {
String num = list.get(i).split(" ")[0];
String colour = list.get(i).split(" ")[1];
if (num.equals("J")) {
numList.add("11");
numSet.add("11");
} else if (num.equals("Q")) {
numList.add("12");
numSet.add("12");
} else if (num.equals("K")) {
numList.add("13");
numSet.add("13");
} else if (num.equals("A")) {
numList.add("14");
numSet.add("14");
} else {
numList.add(num);
numSet.add(num);
}
colourList.add(colour);
colourSet.add(colour);
}
// 同花顺,或者同花
if (colourSet.size() == 1) {
Collections.sort(numList);
for (int i = 0; i numList.size() - 1; i++) {
if (Integer.parseInt(numList.get(i + 1)) - Integer.parseInt(numList.get(i)) == 1) {
if (i == numList.size() - 2) {
temp = 1;
}
continue;
} else {
if (temp 4) {
temp = 4;
}
}
}
}
// 四条或者葫芦
if (numSet.size() == 2) {
Collections.sort(numList);
// 四条
if (!numList.get(0).equals(numList.get(1)) || !numList.get(size - 1).equals(numList.get(size - 2))) {
if (temp 2) {
temp = 2;
}
} else {
if (temp 3) {
temp = 3;
}
}
}
// 三条
if (numSet.size() == 3) {
if (temp 6) {
temp = 6;
}
}
// 顺子
if (colourSet.size() 1) {
Collections.sort(numList);
for (int i = 0; i numList.size() - 1; i++) {
if (Integer.parseInt(numList.get(i + 1)) - Integer.parseInt(numList.get(i)) == 1) {
if (i == numList.size() - 2) {
if(temp5){
temp = 5;
}
}
} else {
break;
}
}
}
return temp;
}
}
一:import java.util.Scanner;
public class Play {
/**
* 玩牌
*/
public static void main(String[] args) {
Poker poker = new Poker();
boolean over = false;
Scanner cin=new Scanner(System.in);
while(!over){
System.out.println("打牌输入1,展示剩余牌输入其他字符:");
String c=cin.nextLine();
if("1".equals(c)){
int index = (int) (Math.random()*poker.getSize());
poker.remove(index);
}else{
poker.ShowPages();
}
}
/*for(int i = 0;i54;i++){
int index = (int) (Math.random()*poker.getSize());
poker.remove(index);
poker.ShowPages();
}
*/
}
}二://牌
public class Page { private String huase = "";
private String haoma = "";
public Page(String huase,String haoma){
this.huase = huase;
this.haoma = haoma;
}
public String show(){
return this.huase+this.haoma;
}
}三:import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 一副扑克
*/
public class Poker {
private List pages = new ArrayList();
public Poker(){
String huase[] = new String[]{"黑桃","红桃","草花","方块"};
String haoma[] = new String[]{"A","2","3","4","5","6","7","8","9","10"
,"J","Q","K"};
for(int i = 0;ihaoma.length;i++){
for(int j = 0;jhuase.length;j++){
pages.add(new Page(huase[j],haoma[i]));
}
}
pages.add(new Page("","大王"));
pages.add(new Page("","小王"));
}
public void ShowPages(){
System.out.println("展示剩余牌:");
for(Iterator i = pages.iterator();i.hasNext();){
System.out.println(((Page)i.next()).show());
}
}
//打牌
public void remove(int index){
pages.remove(index);
}
//剩余牌数
public int getSize(){
return pages.size();
}
}
public class Test {
public static void main(String args[]){
int puke[][] = new int[52][2];
for(int i = 0 ; i 52; i++){
puke[i][0] = i+1;//牌面
puke[i][1] = 0;//0表示正面,1表示背面
}
for(int i = 2; i = 52 ;i++){
for(int j = 0 ; j 52; j++){
if(puke[j][0]%i==0){
puke[j][1]=Math.abs(puke[j][1]-1);//1为0,0为1;
}
}
}
for(int i = 0 ; i 52; i++){
if(puke[i][1]==0)//为正面打印
System.out.println(puke[i][0]);
}
}
}
结果为:1,4,9,16,25,36,49
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流