本文目錄一覽:
- 1、求java單機鬥地主完整源碼
- 2、求用java編寫的鬥地主程序就,要求可以在局域網內實現兩桌以上同時玩。
- 3、求JAVA鬥地主源代碼
- 4、我在網絡上下載了一些java遊戲源碼,擴展名是jar格式,請問怎麼打開
- 5、Java 鬥地主代碼 怎樣運行
求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() + “\n\n”);
}
}
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() + “\n\n”);
}
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 就可以運行了
原創文章,作者:IVRIU,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/129323.html