其實還是有一些錯誤的,很多功能懶得實現了,順子方面我實現不來,好吧主要還是偷懶,僅供參考,java做撲克的視頻b站有,不過具體實現要靠自己完成,上面只有怎麼把符號和點數組合成一張牌
package Game;
public class Card implements Comparable{
//花色
private int suit;
//點數
private int rank;
public Card() {
super();
}
public Card(int suit, int rank) {
super();
this.suit = suit;
this.rank = rank;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String toString() {
return App.SUITS[suit]+""+App.RANKS[rank];
}
public int compareTo(Object otherCard) {
Card other = (Card)otherCard;
return this.rank-other.getRank();
}
}
package Game;
//工具類
public class App {
//存放花色
public static final String[] SUITS= {"♠","♥","♣","♦"};
public static final String[] RANKS= {"2","3","4","5","6","7","8","9","10",
"J","Q","K","A"};
//紅桃
public static final int HEART = 0;
//黑桃
public static final int SPADE = 1;
//梅花
public static final int CLUB = 2;
//方塊
public static final int DIAMOND = 3;
//點數
public static final int TWO = 0;
public static final int THREE = 1;
public static final int FOUR = 2;
public static final int FIVE = 3;
public static final int SIX = 4;
public static final int SEVEN = 5;
public static final int EIGHT = 6;
public static final int NINE = 7;
public static final int TEN = 8;
public static final int JACK = 9;
public static final int QUEEN = 10;
public static final int KING = 11;
public static final int ACE = 12;
}
package Game;
import java.util.List;
import java.util.ArrayList;
/*
* 玩家類
*
*/
public class Player {
//名字
private String name;
//手牌,不清楚數量,用集合
private List<Card> cards = new ArrayList<Card>();
public Player(String name) {
super();
this.name = name;
}
public Player() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Card> getCards() {
return cards;
}
public void setCards(List<Card> cards) {
this.cards = cards;
}
@Override
public String toString() {
return name+":"+cards;
}
}
package Game;
import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class MainTest {
//撲克牌放集合中
static List<Card> poker;
//玩家集合
static List<Player>players;
static int money1=100,money2=100;//籌碼,也可用線程,這裏嫌麻煩沒用
static int max_suit1=1,max_rank1=1,max_suit2=1,max_rank2=1,max_data1=1,max_data2=1;//分別設值,求相同花色的最大值(同一顏色最多有幾張)和相同大小的最多有幾張
static int summary_suit=1,summary_rank=1;//設中間值,和最大值比較並更新
public MainTest()throws Exception{
User u =new User();
u.setVisible(true);
}
class User extends JFrame{
JButton btn1,btn2,btn3;//開始、下注、棄牌
JTextField text1,text2,text_user,text_show;
JLabel lbl1,lbl2,lbl_user,lbl_show,lbl;
int Jackpot;//獎池
public User() {
this.setTitle("胖虎和小夫的德州撲克");
this.setBounds(200,200,600,700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(6,1));
lbl = new JLabel("每次20籌碼,棄牌10籌碼,加註可加一次,10籌碼");
lbl1 = new JLabel("盧本偉的籌碼");
lbl2 = new JLabel("PDD的籌碼");
lbl_user = new JLabel("盧本偉的底牌:");
lbl_show = new JLabel("公牌:");
text1 = new JTextField(""+money1);
text2 = new JTextField(""+money2);
text_user = new JTextField("");
text_show = new JTextField("");
btn1 = new JButton("開始");
btn2 = new JButton("比牌");
btn3 = new JButton("棄牌");
btn1.addActionListener(new ActionListener(){ //添加監聽器,單擊開始按鈕後產生事件,洗牌併發牌,理牌
public void actionPerformed(ActionEvent e){
//清零
text_show.setText("");
text_user.setText("");
//初始化玩家
initPlayer();
//生成撲克牌
poker = createPoker();
//洗牌,shuffle函數打亂集合的順序
Collections.shuffle(poker);
//發牌
deal();
//理牌
sort();
// for(Player p:players) {
// System.out.println(p);
// }
text_show.setText(""+poker.get(0)+poker.get(1)+poker.get(2)+poker.get(3)+poker.get(4));
Player p = players.get(0);
text_user.setText(""+p);
}
});
btn2.addActionListener(new ActionListener(){ //添加監聽器,進行比較大小,並相應加減籌碼
public void actionPerformed(ActionEvent e){
//先把5張底牌加到每個人的牌底下,讓每個人有7張牌,然後排序,最後通過比較確定大小
summary();
sort();
if(compare() == 1) {
money1+=20;
money2-=20;
text1.setText(""+money1);
text2.setText(""+money2);
}
else {
money1-=20;
money2+=20;
text1.setText(""+money1);
text2.setText(""+money2);
}
if(money1 <= 0) {
JOptionPane.showMessageDialog(null, "光屁股了,別玩了, 回家做作業去吧!", null, JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
else if(money2 <= 0) {
JOptionPane.showMessageDialog(null, "你們可能不知道只用20萬贏到578萬是什麼概念\r\n" +
"\r\n" +
"我們一般只會用兩個字來形容這種人:土塊!!", null, JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
});
btn3.addActionListener(new ActionListener(){ //添加監聽器,直接棄牌認輸,少扣籌碼
public void actionPerformed(ActionEvent e){
money1-=10;
money2+=10;
text1.setText(""+money1);
text2.setText(""+money2);
if(money1 == 0) {
JOptionPane.showMessageDialog(null, "光屁股了,別玩了, 回家做作業去吧!", null, JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
});
add(lbl1);
add(text1);
add(lbl2);
add(text2);
add(lbl_show);
add(text_show);
add(lbl_user);
add(text_user);
add(btn1);
add(btn2);
add(btn3);
add(lbl);
}
}
public static void main(String [] args) throws Exception {
//System.out.println(poker);
//展示手牌
//show();
MainTest test=new MainTest();
}
//把底牌也放進人物的牌組中
private static void summary() {
int i;
Player lbw = players.get(0);
Player pdd = players.get(1);
List<Card> cards1 = lbw.getCards();
List<Card> cards2 = pdd.getCards();
for(i=0; i<5; i++) {
cards1.add(poker.get(i));
cards2.add(poker.get(i));
}
}
//進行比較並輸出結果
private int compare() {
Player lbw = players.get(0);
Player pdd = players.get(1);
List<Card> cards1 = lbw.getCards();
List<Card> cards2 = pdd.getCards();
max_suit1=1;
max_rank1=1;//分別設值,求相同花色的最大值(同一顏色最多有幾張)和相同大小的最多有幾張
summary_suit=1;
summary_rank=1;//設中間值,和最大值比較並更新
max_data1=1;max_data2=1;//記錄相同情況下誰更大
//審閱檢查cards1
for(int i=0; i<cards1.size()-1; i++) {
if(cards1.get(i).getSuit() == cards1.get(i+1).getSuit()) {
summary_suit+=1;
}
else {
if(summary_suit > max_suit1) {
max_suit1 = summary_suit;
}
}
if(cards1.get(i).getRank() == cards1.get(i+1).getRank()) {
summary_rank+=1;
}
else {
if(summary_rank > max_rank1) {
max_rank1 = summary_rank;
max_data1 = cards1.get(i+1).getRank();
}
}
}
summary_suit=1;
summary_rank=1;//設中間值值,和最大值比較並更新
//審閱檢查cards2
for(int i=0; i<cards2.size()-1; i++) {
if(cards2.get(i).getSuit() == cards2.get(i+1).getSuit()) {
summary_suit+=1;
}
else {
if(summary_suit > max_suit2) {
max_suit2 = summary_suit;
}
}
if(cards2.get(i).getRank() == cards2.get(i+1).getRank()) {
summary_rank+=1;
}
else {
if(summary_rank >= max_rank2) {
max_rank2 = summary_rank;
max_data2 = cards2.get(i+1).getRank();
}
}
}
//大小規則:皇家同花順>同花順>炸彈>俘虜(3+2)>同花>順子>三條>一對>高牌
if(max_rank1 == 4){
JOptionPane.showMessageDialog(null, "盧本偉牛逼!", null, JOptionPane.PLAIN_MESSAGE);
return 1;
}
else if(max_rank2 == 4) {
JOptionPane.showMessageDialog(null, "幹尼瑪的殺破狼!幹尼瑪超人戰士!日尼瑪威猛先生!", null, JOptionPane.PLAIN_MESSAGE);
return 2;
}
else if(max_suit1>=5) {
JOptionPane.showMessageDialog(null, "從現在開始這裏叫盧本偉廣場!", null, JOptionPane.PLAIN_MESSAGE);
return 1;
}
else if(max_suit2>=5) {
JOptionPane.showMessageDialog(null, "跟我劉某玩操作!!", null, JOptionPane.PLAIN_MESSAGE);
return 2;
}
else if(max_rank1 == 3 && max_rank2<3) {
JOptionPane.showMessageDialog(null, "當年,陳刀仔用20塊贏到3700萬,我盧本偉用20萬贏到500萬不是問題!", null, JOptionPane.PLAIN_MESSAGE);
return 1;
}
else if(max_rank2 == 3 && max_rank1<3) {
JOptionPane.showMessageDialog(null, "皮皮蝦,我們走~!", null, JOptionPane.PLAIN_MESSAGE);
return 2;
}
else if(max_rank1 == 2 && max_rank2<2) {
JOptionPane.showMessageDialog(null, "給阿姨倒一杯卡布奇諾~", null, JOptionPane.PLAIN_MESSAGE);
return 1;
}
else if(max_rank2 == 2 && max_rank1<2) {
JOptionPane.showMessageDialog(null, "好險好險,騷豬我依然發育起來了。", null, JOptionPane.PLAIN_MESSAGE);
return 2;
}
else if(max_rank2 == max_rank1)
{
if(max_data1 >= max_data1) {
JOptionPane.showMessageDialog(null, "2張牌你能秒我?你能秒殺我?2張牌你能秒殺我,我當場,把電腦屏幕喫掉!", null, JOptionPane.PLAIN_MESSAGE);
return 1;
}
else {
JOptionPane.showMessageDialog(null, "芽兒呦,差點裂開了,太瓦了!", null, JOptionPane.PLAIN_MESSAGE);
return 2;
}
}
return 1;
}
private static void sort() {
for(Player p:players) {
List<Card> cards = p.getCards();
Collections.sort(cards);
}
}
private static void show() {
for(Player p:players) {
System.out.println(p);
}
}
//發牌
private static void deal() {
//3張公牌,令i=5就是從第6張牌開始發給玩家和NPC,前五張作爲公牌
int i=5;
for(int j=0; j<2; j++) {
for(int k=0; k<players.size(); k++) {
Player player = players.get(k);
List<Card> cards = player.getCards();
cards.add(poker.get(i));
i++;
}
}
}
//初始化玩家
private static void initPlayer() {
//創建
players = new ArrayList<Player>();
//添加玩家
players.add(new Player("盧本偉"));
players.add(new Player("PDD"));
}
/**生成撲克牌
*public static final String[] SUITS= {"♠","♥","♣","♦"};
*public static final String[] RANKS= {"2","3","4","5","6","7","8","9","10",
"J","Q","K","A"};
* @return
*/
private static List<Card> createPoker(){
List<Card> poker = new ArrayList<Card>();
for(int i=0; i<App.SUITS.length; i++) {
for(int j=0; j<App.RANKS.length;j++) {
//System.out.println(App.SUITS[i]+""+App.RANKS[j]);
//根據花色點數創建撲克牌
Card card = new Card(i,j);
poker.add(card);
}
}
return poker;
}
}