Java练习-扑克牌小游戏-不断升级中~~~
立即下载
资源介绍:
Java练习-扑克牌小游戏-不断升级中~~~
package day0717;
import java.util.*;
public class Poker {
Card[] cards = new Card[20];
String[] suits = {"黑桃","红心","方片","草花"};
public Poker(){
int index = 0;
for (int i = 0;i < suits.length;i ++){
for (int j = 10;j <= 14;j ++){
Card card = new Card(suits[i],j);
cards[index] = card;
index ++;
}
}
}
public void out(){
int index = 0;
for (int i = 0;i < suits.length;i ++) {
for (int j = 10; j <= 14; j++) {
System.out.print(cards[index]+" ");
index ++;
}
System.out.println();
}
}
public void shuffle(){
Random random = new Random();
for (int i = 0; i < 20;i ++){
int index = random.nextInt(20);
Card temp = cards[i];
cards[i] = cards[index];
cards[index] = temp;
}
}
public List getOneCards(){
this.shuffle();
ArrayList list = new ArrayList<>();
for (int i = 0;i < 5;i ++){
list.add(cards[i]);
}
return list;
}
public String getType(List card){
List rankList = new ArrayList<>();
Set suitSet = new HashSet<>();
HashSet rankSet = new HashSet<>();
Map map = new HashMap<>();
for (Card card1 : card){
suitSet.add(card1.suit);
rankSet.add(card1.rank);
rankList.add(card1.rank);
if (map.containsKey(card1.rank)){
int count = map.get(card1.rank);
count ++;
map.put(card1.rank,count);
}else {
map.put(card1.rank,1);
}
}
Collections.sort(rankList);
boolean isSameColor = false;
boolean isShunzi = false;
if (rankList.get(4)-rankList.get(0) == 4 && rankSet.size() == 5){
isShunzi = true;
}
if (suitSet.size()==1){
isSameColor = true;
}
if(isShunzi && isSameColor){
return "同花顺";
}
if (isShunzi){
return "顺子";
}
if (isSameColor){
return "同花";
}
if (rankSet.size() == 2){
if (map.containsValue(4)){
return "四条";
}else {
return "满堂红";
}
}
if (rankSet.size() == 3){
if (map.containsValue(3)){
return "311";
}else {
return "221";
}
}
if (rankSet.size() == 4){
return "一对";
}
return "不知道";
}
public Card getCard(int index){
return cards[index];
}
public static void main(String[] args) {
Poker poker = new Poker();
//poker.out();
//System.out.println("-------------");
//poker.shuffle();
//poker.out();
//System.out.println("-------------");
List card = poker.getOneCards();
poker.out();
System.out.println("-------------");
for(Card card1 : card){
System.out.print(card1+" ");
}
System.out.println("\n\n判断牌型");
String type = poker.getType(card);
System.out.println(type);
}
}