package com.hui_Card;
/*
* 成员属性:花色 ♠ ♥ ♣ ♦
* 大小,按照都斗地主的规则:A 2 3 4 5 6 7 8 9 10 J Q K
* 构造方法不能是空的必须有值
*/
public class Card {
private String huase;
private String num;
public Card(String cord_huase, String cord_num) {
super();
this.huase = cord_huase;
this.num = cord_num;
}
@Override
public String toString() {
return huase + num;
}
}
执行代码
package com.hui_Card;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
/*
* 第一步 先建立集合 在集合里面 存储卡片信息
*/
public class Play {
public static void main(String[] args) {
//建立存储卡牌的Map集合
HashMap<Integer,Card> card = new HashMap<>();
//建立存储卡牌的方法,存储卡片
set_card(card);
System.out.println(card);
//建立与键值相对应的索引集合
ArrayList<Integer> index = new ArrayList<>();
add_index(index);
System.out.println(index);
//堆积把索引洗一下
Collections.shuffle(index);
System.out.println(index);
//随机个三个人分配牌对应的索引
ArrayList<Integer> P1 =cend_index(index);
System.out.println(P1);
ArrayList<Integer> P2 =cend_index(index);
System.out.println(P2);
ArrayList<Integer> P3=cend_index(index);
System.out.println(P3);
System.out.println(index);
//把玩家的分配索引按顺序排好
Collections.sort(P1);
System.out.println(P1);
Collections.sort(P2);
System.out.println(P2);
Collections.sort(P3);
System.out.println(P3);
Collections.sort(index);
System.out.println(index);
//创建方法,根据索引给每个人发相应的牌;
//不应该用hashMap来集合来接收数据
// HashMap<Integer, Card> player1 = cend_card(P1,card);
// System.out.println(player1);
// HashMap<Integer, Card> player2 = cend_card(P2,card);
// System.out.println(player2);
// HashMap<Integer, Card> player3 = cend_card(P3,card);
// System.out.println(player3);
// HashMap<Integer, Card> 底牌 = cend_card(index,card);
// System.out.println(底牌);
//新的方法接收排好顺序的牌 用ArrayList接收
// ArrayList<Card> player1 = cend_card(P1,card);
// System.out.println("玩家一的牌是:"+player1);
// ArrayList<Card> player2 = cend_card(P2,card);
// System.out.println("玩家一的牌是:"+player2);
// ArrayList<Card> player3 = cend_card(P3,card);
// System.out.println("玩家一的牌是:"+player3);
// ArrayList<Card> 底牌 = cend_card(index,card);
// System.out.println("玩家一的牌是:"+底牌);
//新的改进
System.out.print("玩家一的牌是:");
cend_card(P1,card);
System.out.print("玩家一的牌是:");
cend_card(P2,card);
System.out.print("玩家一的牌是:");
cend_card(P3,card);
System.out.print("底牌是:");
cend_card(index,card);
}
//创建的hashMap来接收数据会打乱已将排好的顺序
// private static HashMap<Integer, Card> cend_card(ArrayList<Integer> p1, HashMap<Integer, Card> card) {
// // TODO Auto-generated method stub
// HashMap<Integer,Card> player1 = new HashMap<>();
// for (Integer index : p1) {
// Card card2 = card.get(index);
// player1.put(index, card2);
// }
// return player1;
// }
//改后是顺序排列
// private static ArrayList<Card> cend_card(ArrayList<Integer> p1, HashMap<Integer, Card> card) {
// // TODO Auto-generated method stub
// ArrayList<Card> player1 = new ArrayList<>();
// for (Integer index : p1) {
// Card card2 = card.get(index);
// player1.add(card2);
// }
// return player1;
// }
private static void cend_card(ArrayList<Integer> p1, HashMap<Integer, Card> card) {
// TODO Auto-generated method stub
ArrayList<Card> player1 = new ArrayList<>();
for (Integer index : p1) {
Card card2 = card.get(index);
player1.add(card2);
}
for (Card card2 : player1) {
System.out.print(card2+" ");
}
System.out.println();
}
private static ArrayList<Integer> cend_index(ArrayList<Integer> index) {
// TODO Auto-generated method stub
ArrayList<Integer> P = new ArrayList<>();
for (int i = 0; i < 17; i++) {
Integer remove = index.remove(0);
P.add(remove);
}
//System.out.println(P);
return P;
}
private static void add_index(ArrayList<Integer> index) {
// TODO Auto-generated method stub
for (int i = 1; i < 55; i++) {
index.add(i);
}
}
private static void set_card(HashMap<Integer, Card> card) {
// TODO Auto-generated method stub
card.put(1, new Card("大", "☺"));
card.put(2, new Card("小", "☺"));
int a = 3;
String [] cord_huase = {"♠","♥","♣","♦"};
String[] cord_num = {"A","2","K","Q","J","10","9","8","7","6","5","4","3"};
for (int i = 0; i < cord_num.length; i++) {
for (int j = 0; j < cord_huase.length; j++) {
card.put(a++, new Card(cord_huase[j],cord_num[i]));
}
}
}
} |
|