java——棋牌类游戏斗地主(singleddz3.0)

这是本人最近一段时间写的斗地主的java代码,大体框架都实现了,主要缺少,AI的智能算法。
下载地址http://download.csdn.net/detail/novelly/5695445
import javax.swing.JFrame;



/***************************************************************************
 * TODO
 * <br>Created on 2013-6-28 下午7:45:13<br>
 * @author daicy
 ***************************************************************************/
public class GameFrame extends JFrame  {
	
	
	
	public GameFrame(String name){
		
		
		initMain();// 初始化
		
		//this.setlord(1);
		add(new GamePanel(name));
		
	    setVisible(true);
		
		//new Thread(new PaintThread()).start();
	    
	   
	}
	
	// 初始化窗体
	private void initMain() {
		{
			setTitle("斗地主游戏");
			setSize(830,620);
			setResizable(false);
			setLocationRelativeTo(getOwner()); // 屏幕居中
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}

	}
		

	public static void main(String args[]) {
		GameFrame GamePanel = new GameFrame("daicy");
	
	}


	

}

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/***************************************************************************
 * TODO
 * <br>Created on 2013-7-3 下午8:26:59<br>
 * @author daicy
 ***************************************************************************/
public class GamePanel extends JPanel implements ActionListener{
	
	private String[] cardNames ;
	
	public CardLabel deck[] = new CardLabel[54];
	
	private Player players[] = new Player[3];
	
	public List<String> lordList;//地主牌
	
	public List<String> allSendCards = new ArrayList<String>();//地主牌
	
	private Container container = null;// 定义容器
	
	private JButton landlords[]=new JButton[2];//抢地主按钮
	
	public JLabel lordIcon; //地主图标

	
	private List<JComponent> allJComponent = new ArrayList<JComponent>();
	
	private GameThread gameThread ;
	
	private String[][] buttonNames = new String[][]{{"抢地主","不抢"},{"出牌","不出"}};
	
	public JButton startButton=new JButton("开始");//开始按钮
	
	private int[][] locations = {{50,60,0,17},{180,450,23,0},{700,60,0,17},{300,10,80,0}};
	
	private int[][] currentLocations = {{200,60,0,17},{180,300,23,0},{550,60,0,17}};
	
	
	public GamePanel(String name){
		
		
		
		initOther();
		
		reStart();
		
		//this.setlord(1);
		
	    setVisible(true);
		
		//new Thread(new PaintThread()).start();
	    
	   
	}
	
	// 初始化窗体
	private void reStart() {
		
		for(int i=0;i<2;i++){
			landlords[i].setText(buttonNames[0][i]);
		}

		cardNames = Robot.refreshCard(Robot.CardInit());
		
		for (int i = 0; i < cardNames.length; i++) {
   	        deck[i].setName(cardNames[i]);
   	        deck[i].turnRear();
   	        deck[i].setVisible(true);
   	        deck[i].setClicked(false);
	    }
		
		List<String>[] listNames = Robot.sendCard(cardNames);
		
		for (int i = 0; i < listNames.length; i++) {
			Robot.reSetCardLocation(locations[i][0], locations[i][1], locations[i][2], locations[i][3], deck,listNames[i]);
		}
		
		for (int i = 0; i < players.length; i++) {
			players[i] = new Player(i+"",i);
			players[i].setPlayerCards(listNames[i]);
			players[i].userState = 0;
		}
		
		lordList = listNames[3];
		
		Robot.myCardTurn(Robot.myCardLabels(deck, players[1].getPlayerCards()));
		
		
		players[1].userState = -1;
		
		gameThread = new GameThread(this);
		
		gameThread.start();
		
		gameThread.turnIndex = 1;
			
	}	
	
	
	

	// 初始化窗体
	private void initOther() {
		{
			container = this;
			container.setBackground(new Color(0, 112, 26)); // 背景为绿色
			container.setLayout(null);
		}

		{
			int[][] buttonLocations = {{320,400,100,0}};
			
			int[][] buttonSize = {{75,20},{40,40}};
			
			for(int i=0;i<2;i++)
			{
				landlords[i]=new JButton(buttonNames[0][i]);
				landlords[i].setBounds(buttonLocations[0][0]+i*buttonLocations[0][2],buttonLocations[0][1]+i*buttonLocations[0][3]
						,buttonSize[0][0],buttonSize[0][1]);
				landlords[i].addActionListener(this);
				landlords[i].setVisible(false);
				allJComponent.add(landlords[i]);
			}
			
			lordIcon=new JLabel(new ImageIcon("images/dizhu.gif"));
			lordIcon.setVisible(false);
			lordIcon.setSize(buttonSize[1][0],buttonSize[1][1]);
			allJComponent.add(lordIcon);
			
			startButton.setBounds(buttonLocations[0][0],buttonLocations[0][1],buttonSize[0][0],buttonSize[0][1]);
			startButton.addActionListener(this);
			//landlords[i].setVisible(false);
			allJComponent.add(startButton);
		}
		
		{
			
			for (int i = 0; i < deck.length; i++) {
		   	     deck[i] = new CardLabel("",this);
		   	     deck[i].setLocation(350, 50);
		   	     allJComponent.add(deck[i]);
		    }
			
			for (int i = 0; i < allJComponent.size(); i++) {
				container.add(allJComponent.get(i));
				container.setComponentZOrder(allJComponent.get(i), 0);
			}
		}

	}
	
	// 重设按钮
	public void rePlayCardLocation(int i) {
		Robot.reSetCardLocation(locations[i][0], locations[i][1], locations[i][2], locations[i][3], deck,getPlayers()[i].getPlayerCards());
	}
	
	// 重设按钮
	public void reCurrentCardLocation(int i) {
		
		Robot.reSetCardLocation(currentLocations[i][0], currentLocations[i][1], currentLocations[i][2], currentLocations[i][3], 
				deck,getPlayers()[i].getSendCards());
	}
	
	// 重设按钮
	public void reButtonName() {
		for(int i=0;i<2;i++){
			if(landlords[i].getText().equals(buttonNames[0][i])){
				landlords[i].setText(buttonNames[1][i]);
			}else{
				landlords[i].setText(buttonNames[0][i]);
			}
		}
	}
	
	// 重设按钮可视性
	public void reButtonVisible(boolean visible) {
		for(int i=0;i<2;i++){
			landlords[i].setVisible(visible);
		}
	}

	public int getGameState(){
		return gameThread.gameState;
	}
	
	// index 所出的牌
	public void sendCards(int index,List<String> sendCards) {
		if(null!=getPlayers()[index].getSendCards()){
			Robot.myCardLabelsVisible(deck, getPlayers()[index].getSendCards(), false);
		}
		allSendCards.addAll(sendCards);
		getPlayers()[index].setSendCards(sendCards);
		getPlayers()[index].getPlayerCards().removeAll(sendCards);
		reCurrentCardLocation(index);
		if(index!=1){
			Robot.myCardTurn(Robot.myCardLabels(deck, sendCards));
		}
		if(getPlayers()[index].getPlayerCards().size()==0){
			this.gameThread.gameState = -1;
			for (int i = 0; i < getPlayers().length; i++) {
				Robot.myCardTurn(Robot.myCardLabels(deck, players[i].getPlayerCards()));
			}
			String s = "恭喜电脑" + index + ",赢了! 你的智商有待提高哦";
			JOptionPane.showMessageDialog(this, s);
			reStart();
			this.startButton.setVisible(true);
		}
		gameThread.turnIndex = (gameThread.turnIndex+1)%3;
	}



	public static void main(String args[]) {
		GamePanel GamePanel = new GamePanel("daicy");
	
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() instanceof JButton){
			JButton button = (JButton) e.getSource();
			if(startButton.equals(e.getSource())){
				getPlayers()[1].userState = 0;
			}else if(button.getText().equals(buttonNames[0][0])){
				reButtonName();
				gameThread.lordIndex = 1;
			}else if(button.getText().equals(buttonNames[0][1])){
				gameThread.turnIndex++;
				reButtonName();
			}else if(button.getText().equals(buttonNames[1][0])){
				List<String> sendCards= Robot.mySendCardLabels(deck, this.getPlayers()[1].getPlayerCards());
				sendCards(1,sendCards);
				this.reButtonVisible(false);
			}else if(button.getText().equals(buttonNames[1][1])){
				gameThread.turnIndex++;
				this.reButtonVisible(false);
			}
			
		}
	}

	public Player[] getPlayers() {
		return players;
	}

	public void setPlayers(Player[] players) {
		this.players = players;
	}
	

}

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;


public class CardLabel extends JLabel implements MouseListener, Comparable<CardLabel>{
	
	private GamePanel GamePanel;

	private boolean up=false;//是否正反面
	
	private boolean clicked=false;//是否点击过
	
	private String name;//图片url名字

	
	public CardLabel(String name,GamePanel GamePanel){
		this.GamePanel = GamePanel;
		turnRear();
		this.name = name;
		this.setSize(71, 96);
		this.setVisible(true);
		this.addMouseListener(this);
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	//反面
	public void turn() {
		if(up){
			turnRear();
		}else{
			turnFront();
		}
	}
		
	//正面
	public void turnFront() {
		this.setIcon(new ImageIcon("images/" + getName() + ".gif"));
		this.up = true;
	}
	//反面
	public void turnRear() {
		this.setIcon(new ImageIcon("images/rear.gif"));
		this.up = false;
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
	}
	public void mouseEntered(MouseEvent arg0) {}
	public void mouseExited(MouseEvent arg0) {}
	public void mousePressed(MouseEvent e) {
		if(up && GamePanel.getGameState()==2)
		{
			Point from=this.getLocation();
			int step; //移动的距离
			if(clicked)
				step=-20;
			else {
				step=20;
			}
			clicked=!clicked; //反向
			//当被选中的时候,向前移动一步/后退一步
			move(new Point(from.x,from.y-step));
		}
	}
	public void mouseReleased(MouseEvent arg0) {
		
	}
	
	public boolean isUp() {
		return up;
	}
	public void setUp(boolean up) {
		this.up = up;
	}
	public boolean isClicked() {
		return clicked;
	}
	public void setClicked(boolean clicked) {
		this.clicked = clicked;
	}
	
	// 移动效果的函数,用于发牌
	public  void move( Point to) {
//		Point from = this.getLocation();
//		if (to.x != from.x) {
//			double k = (1.0) * (to.y - from.y) / (to.x - from.x);
//			double b = to.y - to.x * k;
//			int flag = 0;// 判断向左还是向右移动步幅
//			if (from.x < to.x)
//				flag = 20;
//			else {
//				flag = -20;
//			}
//			for (int i = from.x; Math.abs(i - to.x) > 20; i += flag) {
//				double y = k * i + b;// 这里主要用的数学中的线性函数
//
//				setLocation(i, (int) y);
//				try {
//					Thread.sleep(5); // 延迟,可自己设置
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}
		// 位置校准
		setLocation(to);
	}
	
	
	
	
	// 返回花色
	public int getColor() {
		return Integer.parseInt(getName().substring(0, 1));
	}
	
	// 返回数值
	public int getNum() {
		return Integer.parseInt(getName().substring(2, getName().length()));
	}
	
	// 返回花色
	public boolean isJoker() {
		return getColor()==5;
	}
	
	
	public int compareValue(CardLabel o2) {
		return getValue()-o2.getValue();
	}
	
	public boolean equalsValue(CardLabel o2) {
		return getValue()-o2.getValue()==0?true:false;
	}
	
	@Override
	public int compareTo(CardLabel o2) {
		// TODO Auto-generated method stub
		int a1 = getColor();// 花色
		int a2 = o2.getColor();
		int b1 = getNum();// 数值
		int b2 = o2.getNum();
		int flag = 0;
		// 如果是王的话
		if (a1 == 5)
			b1 += 100;
		if (a1 == 5 && b1 == 1)
			b1 += 50;
		if (a2 == 5)
			b2 += 100;
		if (a2 == 5 && b2 == 1)
			b2 += 50;
		// 如果是A或者2
		if (b1 == 1)
			b1 += 20;
		if (b2 == 1)
			b2 += 20;
		if (b1 == 2)
			b1 += 30;
		if (b2 == 2)
			b2 += 30;
		flag = b2 - b1;
		if (flag == 0)
			return a2 - a1;
		else {
			return flag;
		}
	}
	
	// 返回值
	public int getValue() {
		int i =getNum();
		if (getNum()==2)
			i += 13;
		if (getNum()==1)
			i += 13;
		if (getColor() == 5)
			i += 2;// 是王
		return i;
	}

}

import java.util.ArrayList;import java.util.List;public class Player {//记录玩家手牌的IDprivate List<String> playerCards = new ArrayList<String>();//记录玩家所出手牌的IDprivate List<String> sendCards = new ArrayList<String>();private String name ;private Integer id ;public int userState = -1; // 0 准备 1 抢地主 2 出牌;public Player(String name, Integer id) {super();this.name = name;this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public List<String> getPlayerCards() {return playerCards;}public void setPlayerCards(List<String> playerCards) {this.playerCards = playerCards;}public List<String> getSendCards() {return sendCards;}public void setSendCards(List<String> sendCards) {this.sendCards = sendCards;}}

import java.util.List;

public class GameThread extends Thread {
	private GamePanel mainPanel ;
	
	public int gameState;  //0准备 1 抢地主2出牌; -1 结束
	
	
	public int lordIndex = -1;
	
	public int turnIndex = -1;
	

	public GameThread(GamePanel mainPanel) {
		this.mainPanel = mainPanel;
	}


	@Override
	public void run() {
        while (true) {
        	if(gameState==-1){
        		break;
        	}
			switch (gameState) {
				case 0:
					for (int i = 0; i < mainPanel.getPlayers().length; ) {
						if(mainPanel.getPlayers()[i].userState==0)
							i++;
					}
					//全准备好了
					gameState = 1;
					mainPanel.startButton.setVisible(false);
					mainPanel.reButtonVisible(true);
				    break;
				case 1:
					if(turnIndex==1){
					}else if(turnIndex>=0){
						//随机产生谁是地主
						Integer num = (int) (Math.random() * 2);
						int[] indexs = {0,2};
						lordIndex = indexs[num];
					}
					
					if(lordIndex>0){
						setlord(lordIndex);
					}
					break;
				case 2:
					if(turnIndex==1){
						mainPanel.reButtonVisible(true);
						
					}else if(turnIndex>=0){
						Robot.second(1);
						int[] indexs = {2,0};
						for (int i = 0; i < indexs.length; i++) {
							turnIndex = indexs[i];
							//随机产生出不出牌
							Integer num = (int) (Math.random() * 2);
							boolean[] sendFlag = {true,false};
							if(sendFlag[num]){
								List<String> sendCards = Robot.sendCards(mainPanel.getPlayers()[indexs[i]].getPlayerCards());
								mainPanel.sendCards(indexs[i],sendCards);
							}
						}
					}
					break;
			}
		}
	}
	

	// 设定地主
	public void setlord(int i) {
		lordIndex = i;
		turnIndex = i;
	    gameState = 2;
		
		{
			int[][] iconLocations = {{80,20},{80,430},{700,20}};
			mainPanel.lordIcon.setLocation(iconLocations[i][0], iconLocations[i][1]);
			mainPanel.lordIcon.setVisible(true);
			Robot.myCardTurn(Robot.myCardLabels(mainPanel.deck, mainPanel.lordList));
			Robot.second(3);
			mainPanel.getPlayers()[i].getPlayerCards().addAll(mainPanel.lordList);
			mainPanel.rePlayCardLocation(i);
			if(i!=1){
				Robot.myCardTurn(Robot.myCardLabels(mainPanel.deck, mainPanel.lordList));
			}
			
		}
	}
	

}

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Robot {
	//初始化牌
	public static String[] CardInit() {
		String[] cards = new String[54];
		int count = 0;
		//初始化牌
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= 13; j++) {
				if ((i == 5) && (j > 2))
					break;
				else {
					cards[count] = i + "-" + j;
					count++;
				}
			}
		}
		return cards;
	}
	// 发牌洗牌
	public static String[] refreshCard(String[] cards) {
		
		//打乱顺序
		for(int i=0;i<100;i++){
			Random random=new Random();
			int a=random.nextInt(54);
			int b=random.nextInt(54);
			String k=cards[a];
			cards[a]=cards[b];
			cards[b]=k;
		}
		return cards;
	}
	
	//初始化牌
	// 发牌洗牌
	public static List<String>[] sendCard(String[] cards) {
		List<String>[] listCards = new ArrayList[4];
		
		//开始发牌
		for(int i=0;i<listCards.length;i++)
			listCards[i]=new ArrayList<String>(); //玩家牌
		
		int t=0;
		for(int i=0;i<54;i++)
		{
			if(i>=51)//地主牌
			{
				listCards[3].add(cards[i]);
				continue;
			}
			listCards[(t++)%3].add(cards[i]);
		}
		return listCards;
	}
		
	
	public static void reSetCardLocation(int x,int y,int xSize,int ySize,CardLabel[] allCards,List<String> playerCards) {
		for (int i = 0; i < allCards.length; i++) {
			for (int j = 0; j < playerCards.size(); j++) {
				if(playerCards.get(j).equals(allCards[i].getName())){
					allCards[i].setLocation(x+j*xSize, y+j*ySize);
				}
			}
		}
	}
	
	public static List<CardLabel> myCardLabels(CardLabel[] allCards,List<String> playerCards) {
		return Robot.myCardLabels(allCards, playerCards, false);
	}
	
	public static  void myCardLabelsVisible(CardLabel[] allCards,List<String> playerCards,boolean visible) {
		List<CardLabel> cards =  Robot.myCardLabels(allCards, playerCards, false);
		for (int i = 0; i < cards.size(); i++) {
			cards.get(i).setVisible(visible);
		}
	}
	
	public static List<String> mySendCardLabels(CardLabel[] allCards,List<String> playerCards) {
		return Robot.getCardLabelsName(Robot.myCardLabels(allCards, playerCards, true));
	}
	
	public static List<CardLabel> myCardLabels(CardLabel[] allCards,List<String> playerCards,boolean clicked) {
		List<CardLabel> myCardLabels = new ArrayList<CardLabel>();
		for (int i = 0; i < allCards.length; i++) {
			for (int j = 0; j < playerCards.size(); j++) {
				if(playerCards.get(j).equals(allCards[i].getName())){
					if(clicked){
						if(allCards[i].isClicked())
							 myCardLabels.add(allCards[i]);
					}else{
						 myCardLabels.add(allCards[i]);
					}
				}
			}
		}
		return myCardLabels;
	}
	
	public static void myCardTurn(List<CardLabel> myCardLabels) {
		for (int j = 0; j < myCardLabels.size(); j++) {
			  myCardLabels.get(j).turn();
	     }
	}
	
	public static List<String> sendCards(List<String> playerCards) {
		//随机产生出那张牌
		int length = playerCards.size();
		Integer num = (int) (Math.random() * length);
		List<String> sendCards = new ArrayList<String>();
		sendCards.add(playerCards.get(num));
		return sendCards;
	}
	
	public static List<String> getCardLabelsName(List<CardLabel> playerCards) {
		List<String> myCardLabels = new ArrayList<String>();
		for (int j = 0; j < playerCards.size(); j++) {
			myCardLabels.add(playerCards.get(j).getName());
		}
		return myCardLabels;
	}
	
	

	// 等待i秒
	public static void second(int i) {
		try {
			Thread.sleep(i * 1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	
	// 检查牌的是否能出
	public static int checkCards(List<String> preCards, List<String>[] currentCards) {
		return 1;
	}

	
}
参考:http://blog.csdn.net/cq361106306/article/details/7853078




                                    
原文地址:https://www.cnblogs.com/daichangya/p/12959569.html