card類(lèi)
public class card{
private int value;
private String color;
public String toString() {
String str = "";
if (value == 11) {
str = "J";
} else if (value == 12) {
str = "Q";
} else if (value == 13) {
str = "K";
} else if (value == 1) {
str = "A";
} else {
str = value + " ";
}
return color + str;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Poker類(lèi)
public class poker2 {
private List<card2> cards = new ArrayList<card2>();
private int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,};
private String[] colors = {"紅桃", "梅花", "黑桃", "方片"};
public poker2() {
for (int i = 0; i < 52; i++) {
card2 card2s = new card2();
card2s.setColor(colors[i / 13]);
card2s.setValue(values[i % 13]);
cards.add(card2s);
}
}
public void show() {
System.out.println(cards + "\t");
}
public void shuffer() {
Collections.shuffle(cards);
}
public List<card2> takecard() {
int n = 0;
List<card2> card2List = new ArrayList<card2>();
for (card2 c : cards) {
card2List.add(c);
n++;
if (n == 5) {
break;
}
}
return card2List;
}
public String texttype(List<card2> card2List) {
boolean isfoluwer = false;
boolean isshunzi = false;
String str = "h";
Set<String> colorset = new HashSet<String>();
for (card2 cc : card2List) {
colorset.add(cc.getColor());
}
Set<Integer> valueset = new HashSet<Integer>();
List<Integer> valuelist = new ArrayList<Integer>();
for (card2 cc : card2List) {
valueset.add(cc.getValue());
valuelist.add(cc.getValue());
}
Collections.sort(valuelist);
if (colorset.size() == 1) {
isfoluwer = true;
}
if (valueset.size() == 5 && valuelist.get(4) - valuelist.get(0) == 4) {
isshunzi = true;
}
if (isfoluwer == true && isshunzi == true) {
// System.out.println("同花順");
str = "同花順";
} else if (isfoluwer == true) {
// System.out.println("同花");
str = "同花";
} else if (isshunzi == true) {
//System.out.println("順子");
str = "順子";
} else if (valueset.size() == 5) {
// System.out.println("雜牌");
str = "雜牌";
} else if (valueset.size() == 4) {
// System.out.println("一對(duì)");
str = "一對(duì)";
} else if (valueset.size() == 3) {//兩隊(duì)或三條
Map<Integer, Integer> mapCardValue2Times = new HashMap<>();
for (card2 card2 : card2List) {
if (mapCardValue2Times.containsKey(card2.getValue())) {
int c = mapCardValue2Times.get(card2.getValue());
c++;
mapCardValue2Times.put(card2.getValue(), c);
} else {
mapCardValue2Times.put(card2.getValue(), 1);
}
}
for (Integer integer : mapCardValue2Times.keySet()) {//(8,2)(5,2) (4,1) (5,3) (3,1)(4,1)
if (mapCardValue2Times.get(integer) == 2) {
//System.out.println("兩對(duì)");
str = "兩對(duì)";
break;
} else if (mapCardValue2Times.get(integer) == 3) {
// System.out.println("三條");
str = "三條";
break;
}
}
} else if (valueset.size() == 2) {//四代一或三代二
Map<Integer, Integer> mapCardValue2Times = new HashMap<>();
for (card2 card2 : card2List) {
if (mapCardValue2Times.containsKey(card2.getValue())) {
int c = mapCardValue2Times.get(card2.getValue());
c++;
mapCardValue2Times.put(card2.getValue(), c);
} else {
mapCardValue2Times.put(card2.getValue(), 1);
}
}
for (Integer integer : mapCardValue2Times.keySet()) {//(8,4)(5,1) (5,3) (3,2)
if (mapCardValue2Times.get(integer) == 4) {
// System.out.println("四帶一");
str = "四帶一";
break;
} else if (mapCardValue2Times.get(integer) == 3) {
//System.out.println("三帶一對(duì)");
str = "三帶一對(duì)";
break;
}
}
}
return str;
}
}
主方法
public class pokermain {
public static void main(String[] args) {
poker2 p = new poker2();
p.show();
p.shuffer();
System.out.println();
p.show();
List<card2> cl = p.takecard();
System.out.println(cl);
String string = p.texttype(cl);
System.out.println(string);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。