首页 > 编程知识 正文

java联机版斗地主源码(java联机版斗地主源码大全)

时间:2023-12-17 12:25:53 阅读:316462 作者:YPFV

本文目录一览:

求java单机斗地主完整源码

一共实现了如下功能

1. 抢地主功能,玩家可以选择自己当地主,还是留给电脑抢地主

2.牌型判断,判断 单牌,对子,3对,顺子,飞机,等等可能情况 为后面出牌跟牌做准备

3. 实现轮流出牌逻辑,并简单测试

4. 实现倒计时功能,如果玩家30秒为出牌自动超时,归为下一家出牌,电脑默认操作时间2秒

public void run() { while(i-1 isRun){ main.time[1].setText("倒计时:"+i--); second(1);//等一秒 } if(i==-1)//正常终结,说明超时 main.time[1].setText("不抢"); //如果自己抢到地主 if(main.time[1].getText().equals("抢地主")){ //得到地主牌 main.playerList[1].addAll(main.lordList); openlord(true); second(2);//等待五秒 Common.order(main.playerList[1]); Common.rePosition(main, main.playerList[1], 1); setlord(1); }else{ //电脑选地主 if(Common.getScore(main.playerList[0])

上面是实现逻辑的一个线程

Time.java文件里面

下面给出拆牌的逻辑

下面是一个枚举,命名不是很规范,但是容易懂,形象化的特征10多种牌型

package com; public enum CardType { c1,//单牌 c2,//对子 c3,//3不带 c4,//炸弹 c31,//3带1 c32,//3带2 c411,//4带2个单,或者一对 c422,//4带2对 c123,//连子 c1122,//连队 c111222,//飞机 c11122234,//飞机带单排 c1112223344,//飞机带对子 c0//不能出牌 }

下面是具体判断方法

//判断牌型 public static CardType jugdeType(List list){ //因为之前排序过所以比较好判断 int len=list.size(); //单牌,对子,3不带,4个一样炸弹 if(len=4) { //如果第一个和最后个相同,说明全部相同 if(Common.getValue(list.get(0))==Common.getValue(list.get(len-1))) { switch (len) { case 1: return CardType.c1; case 2: return CardType.c2; case 3: return CardType.c3; case 4: return CardType.c4; } } //双王,化为对子返回 if(len==2Common.getColor(list.get(1))==5) return CardType.c2; //当第一个和最后个不同时,3带1 if(len==4 Common.getValue(list.get(0))==Common.getValue(list.get(len-2))) return CardType.c31; else { return CardType.c0; } } //当5张以上时,连字,3带2,飞机,2顺,4带2等等 if(len=5) {//现在按相同数字最大出现次数 Card_index card_index=new Card_index(); for(int i=0;i4;i++) card_index.a[i]=new ArrayList(); //求出各种数字出现频率 Common.getMax( card_index,list); //a[0,1,2,3]分别表示重复1,2,3,4次的牌 //3带2 -----必含重复3次的牌 if(card_index.a[2].size()==1 card_index.a[1].size()==1 len==5) return CardType.c32; //4带2(单,双) if(card_index.a[3].size()==1 len==6) return CardType.c411; if(card_index.a[3].size()==1 card_index.a[1].size()==2 len==8) return CardType.c422; //单连,保证不存在王 if((Common.getColor(list.get(0))!=5)(card_index.a[0].size()==len) (Common.getValue(list.get(0))-Common.getValue(list.get(len-1))==len-1)) return CardType.c123; //连队 if(card_index.a[1].size()==len/2 len%2==0 len/2=3 (Common.getValue(list.get(0))-Common.getValue(list.get(len-1))==(len/2-1))) return CardType.c1122; //飞机 if(card_index.a[2].size()==len/3 (len%3==0) (Common.getValue(list.get(0))-Common.getValue(list.get(len-1))==(len/3-1))) return CardType.c111222; //飞机带n单,n/2对 if(card_index.a[2].size()==len/4 ((Integer)(card_index.a[2].get(len/4-1))-(Integer)(card_index.a[2].get(0))==len/4-1)) return CardType.c11122234; //飞机带n双 if(card_index.a[2].size()==len/5 card_index.a[2].size()==len/5 ((Integer)(card_index.a[2].get(len/5-1))-(Integer)(card_index.a[2].get(0))==len/5-1)) return CardType.c1112223344; } return CardType.c0; }

下面是上面函数用到的一个函数

//返回花色 public static int getColor(Card card){ return Integer.parseInt(card.name.substring(0,1)); } //返回值 public static int getValue(Card card){ int i= Integer.parseInt(card.name.substring(2,card.name.length())); if(card.name.substring(2,card.name.length()).equals("2")) i+=13; if(card.name.substring(2,card.name.length()).equals("1")) i+=13; return i; } //得到最大相同数 public static void getMax(Card_index card_index,List list){ int count[]=new int[14];//1-13各算一种,王算第14种 for(int i=0;i14;i++) count[i]=0; for(int i=0,len=list.size();i14;i++) { switch (count[i]) { case 1: card_index.a[0].add(i+1); break; case 2: card_index.a[1].add(i+1); break; case 3: card_index.a[2].add(i+1); break; case 4: card_index.a[3].add(i+1); break; } } } } class Card_index{ List a[]=new ArrayList[4];//单张 }

求用java编写的斗地主程序就,要求可以在局域网内实现两桌以上同时玩。

客户端:

import java.io.*;

import java.net.*;

import java.awt.*;

import java.awt.event.*;

public class ChatClient extends Frame

{

TextArea ta = new TextArea();

TextField tf = new TextField();

public void launchFrame() throws Exception

{

this.add(ta, BorderLayout.CENTER);

this.add(tf, BorderLayout.SOUTH);

tf.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

try {

String sSend = tf.getText();

if(sSend.trim().length() == 0) return;

ChatClient.this.send(sSend);

tf.setText("");

ta.append(sSend + "n");

}

catch (Exception e)

}

}

);

this.addWindowListener(

new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

);

setBounds(300,300,300,400);

setVisible(true);

tf.requestFocus();

}

Socket s = null;

public ChatClient() throws Exception

{

s = new Socket("127.0.0.1", 8888);

launchFrame();

(new Thread(new ReceiveThread())).start();

}

public void send(String str) throws Exception

{

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

dos.writeUTF(str);

}

public void disconnect() throws Exception

{

s.close();

}

public static void main(String[] args) throws Exception

{

BufferedReader br = new BufferedReader (

new InputStreamReader(System.in));

ChatClient cc = new ChatClient();

String str = br.readLine();

while(str != null str.length() != 0)

{

cc.send(str);

str = br.readLine();

}

cc.disconnect();

}

class ReceiveThread implements Runnable

{

public void run()

{

if(s == null) return;

try {

DataInputStream dis = new DataInputStream(s.getInputStream());

String str = dis.readUTF();

while (str != null str.length() != 0)

{

//System.out.println(str);

ChatClient.this.ta.append(str + "n");

str = dis.readUTF();

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

}

服务器:

import java.net.*;

import java.util.*;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

public class ChatServer extends Frame

{

TextArea ta = new TextArea();

public void launchFrame()

{

add(ta, BorderLayout.CENTER);

setBounds(0,0,200,300);

this.addWindowListener(

new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

);

setVisible(true);

}

ServerSocket server = null;

Collection cClient = new ArrayList();

public ChatServer(int port) throws Exception

{

server = new ServerSocket(port);

launchFrame();

}

public void startServer() throws Exception

{

while(true)

{

Socket s = server.accept();

cClient.add( new ClientConn(s) );

ta.append("NEW-CLIENT " + s.getInetAddress() + ":" + s.getPort());

ta.append("n" + "CLIENTS-COUNT: " + cClient.size() + "nn");

}

}

class ClientConn implements Runnable

{

Socket s = null;

public ClientConn(Socket s)

{

this.s = s;

(new Thread(this)).start();

}

public void send(String str) throws IOException

{

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

dos.writeUTF(str);

}

public void dispose()

{

try {

if (s != null) s.close();

cClient.remove(this);

ta.append("A client out! n");

ta.append("CLIENT-COUNT: " + cClient.size() + "nn");

}

catch (Exception e)

{

e.printStackTrace();

}

}

public void run()

{

try {

DataInputStream dis = new DataInputStream(s.getInputStream());

String str = dis.readUTF();

while(str != null str.length() !=0)

{

System.out.println(str);

for(Iterator it = cClient.iterator(); it.hasNext(); )

{

ClientConn cc = (ClientConn)it.next();

if(this != cc)

{

cc.send(str);

}

}

str = dis.readUTF();

//send(str);

}

this.dispose();

}

catch (Exception e)

{

System.out.println("client quit");

this.dispose();

}

}

}

public static void main(String[] args) throws Exception

{

ChatServer cs = new ChatServer(8888);

cs.startServer();

}

}

(来自BJSXT)

求JAVA斗地主源代码

你可以试试哈~我这边正常运行

jdk1.6+eclipse正常运行,编码是gbk

我在网络上下载了一些java游戏源码,扩展名是jar格式,请问怎么打开

你把它直接扔到手机里就行了。

第一步:于手机的数据线连接(读卡器也行)

第二补:把JAVA游戏扔到手机里

第三步:打开手机,安装,

第四步:你可以玩了

Java 斗地主代码 怎样运行

你到eclipse 里点击 file -import -Existing Projects into Workspace

然后选择你的文件夹,导入,就好了

导入好了以后,找到,你要运行的主文件,点run 就可以运行了

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。