这是本人最近一段时间写的斗地主的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



												

java——棋牌类游戏斗地主(singleddz3.0)的更多相关文章

  1. android开发之网络棋牌类在线游戏开发心得(服务器端、Java) 好文章值得收藏

    标签: android服务器 2013-10-09 17:28 3618人阅读 评论(0) 收藏 举报 分类: android(11) 转自:http://blog.csdn.net/bromon/a ...

  2. CCF201512-2 消除类游戏 java(100分)

    试题编号: 201512-2 试题名称: 消除类游戏 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进 ...

  3. 网狐6603 cocos2dx 棋牌、捕鱼、休闲类游戏《李逵捕鱼》手机端完整源码分析及分享

    该资源说明: cocos2d 棋牌.捕鱼.休闲类游戏<李逵捕鱼>手机端完整源码,网狐6603配套手机版源码,可以选桌子,适合新手学习参考,小编已亲测试,绝对完整可编译手机端,下载后将文件考 ...

  4. java导出数据EXCEL的工具类(以spring-webmvc-4.0.4jar为基础)

    1.本工具类继承于  spring-webmvc-4.0.4jar文件心中的一个类   AbstractExcelView 2.代码如下 package com.skjd.util; import j ...

  5. C++ MFC棋牌类小游戏day1

    好用没用过C++做一个完整一点的东西了,今天开始希望靠我这点微薄的技术来完成这个小游戏. 我现在的水平应该算是菜鸟中的战斗鸡了,所以又很多东西在设计和技术方面肯定会有很大的缺陷,我做这个小游戏的目的单 ...

  6. javascript开发HTML5游戏--斗地主(单机模式part3)

    最近学习使用了一款HTML5游戏引擎(青瓷引擎),并用它尝试做了一个斗地主的游戏,简单实现了单机对战和网络对战,代码可已放到github上,在此谈谈自己如何通过引擎来开发这款游戏的. 客户端代码 服务 ...

  7. 《Unity3D/2D游戏开发从0到1》正式出版发行

    <Unity3D/2D游戏开发从0到1>正式出版发行 去年个人编写的Unity书籍正式在2015年7月正式发行,现在补充介绍一下个人著作.书籍信息:      书籍的名称: <Uni ...

  8. JAVASCRIPT开发HTML5游戏--斗地主(网络对战PART4)

    继之前用游戏引擎(青瓷引擎)做了斗地主单机版游戏之后,这里分享下使用socket.io来实现网络对战,代码可已放到github上,在此谈谈自己整个的开发思路吧. 客户端代码 服务端代码 (点击图片进入 ...

  9. javascript开发HTML5游戏--斗地主(单机模式part2)

    最近学习使用了一款HTML5游戏引擎(青瓷引擎),并用它尝试做了一个斗地主的游戏,简单实现了单机对战和网络对战,代码可已放到github上,在此谈谈自己如何通过引擎来开发这款游戏的. 客户端代码 服务 ...

  10. 为什么MOBA和吃鸡类游戏不推荐用tcp协议 延迟不利于实时游戏

    http://news.gamedog.cn/a/20171221/2287418.html 我们知道,不同类型的游戏因为玩法.竞技程度不一样,采用的同步算法不一样,对网络延迟的要求也不一样.例如,M ...

随机推荐

  1. 『读书笔记』你不知道的JavaScript(上)

    前言 文章只记录理解以及容易遗忘的知识点. 词法作用域.块作用域 词法作用域 词法作用域:简单的说,词法作用域就是定义在词法阶段的作用域.换句话说,词法作用域就是在你写代码时将变量和块作用域写在哪里来 ...

  2. CSS – 实战 Spacing & Layout

    前言 这篇想整理一下在网页开发中, Spacing (间距) 和 Layout 排版是如果被处理的. Spacing 介绍 东西密密麻麻会给人一种很恐怖的感觉. 只要加上一点空间 (间距), 整体感觉 ...

  3. “RazorTagHelper”任务意外失败。解决方案

    严重性    代码    说明    项目    文件    行    禁止显示状态错误    MSB4018    "RazorTagHelper"任务意外失败.System.I ...

  4. MyBatis——案例——查询-多条件查询-动态条件查询(关键字 if where)

    动态条件查询   SQL语句会随着用户的输入或外部条件的变化而变化,我们称为 动态SQL   MyBatis 对动态SQL有很强大的支撑:   if   choose(when,otherwise) ...

  5. 服务器文件打压缩包下载(java)

    public void download(HttpServletRequest request, HttpServletResponse response){ try { String downloa ...

  6. SpringBoot配置多个数据源-详解

    一直在趟坑,从未被超越. 借鉴文章 个人觉得我算是整理的比较详细的了,有些博客老是缺斤少两的.最恶心的是竟然会有人写到,如需下文请关注什么什么公众号. 结构 pom文件 <dependencie ...

  7. 【USB3.0协议学习】Topic1·USB3.0Hub的一些机制

    一.USB3.0 Hub的单播(非广播)机制 Hub通过解析下行packet header中的Route String字段识别packet要传递的终点,其中4'b0000代表hub本身,4'b0001 ...

  8. ABP VNext 系列:框架启动流程以及依赖注入原理和源码分析

    简单介绍 ABP VNext Github 地址:https://github.com/abpframework/abp 官网文档地址:https://abp.io/docs/latest 官网:ht ...

  9. KubeSphere 社区双周报|2024.09.27-10.10

    KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书.新增的讲师证书以及两周内提交过 commit 的贡献者,并对近期重要的 PR 进行解析,同时还包含了线上/线下活动和布道推广等一系列 ...

  10. 云原生周刊:Cilium v1.16.0 发布|20240729

    开源项目 Cyclops Cyclops 是一个开源的开发工具,通过易于使用的用户界面简化了 Kubernetes,使其更易上手.不再需要使用 YAML 创建和配置 Kubernetes 清单,可以使 ...