主题:[讨论]求局域网内发牌程序(高手请进)编程
本人想做一个局域网内斗地主游戏,苦于对线程不精,在做发牌时遇到困难,又不想放弃,在此小弟希望哪位高人不吝赐教,本人不胜感激!
本人所作的部分代码如下,希望对高人更好的指点有所帮助:
import java.awt.*;
import javax.swing.*;
public class card extends JLabel
{
private String name; //牌名称
public card(String name)
{
super();
this.name = name;
this.setIcon(new ImageIcon("images/rear.gif"));
this.setSize(71, 96);
setVisible(true);
}
public void turnFront()
{
this.setIcon(new ImageIcon("images/" + name + ".gif"));
}
public int getCardValue() //返回牌的值
{
return Integer.parseInt(name.substring(2));
}
public String getName()
{
return name;
}
}
public class initCard
{
private card [] allCards, lastCards;
private card playerCards[][]=new card[3][20];
private int[] cardsNo={17, 17, 17};
public void initCards()//初始化牌数
{
allCards=new card [54];
for(int i=1;i<=4;i++)
{
for(int j=3;j<=15;j++)
{
allCards[(i-1)*13+j-1]=new card(i+"-"+j);
}
}
allCards[52] = new card("g-16");//设置大小王
allCards[53] = new card("g-17");
randomCards();
}
public void randomCards()//打乱原来初始化的牌!
{
card temp;
for(int i=0;i<54;i++)
{
int x = (int) (Math.random() * 54);
int y = (int) (Math.random() * 54);
temp = allCards[x];
allCards[x] = allCards[y];
allCards[y] = temp;
}
}
public void dealCards()//给每位玩家发牌,前三张牌预留,后面要调用线程客户端发牌
{
randomCards();
lastCards = new card[3];
for(int i = 0; i < 3; i++)
lastCards[i] = allCards[i];
for(int i = 0; i < 3; i ++)
{
for(int j = 0; j < 17; j ++)
{
playerCards[i][j] = allCards[i * 17 + j + 3];
}
}
}
}
本人考虑使用对象流,程序分为两个客户端,一个服务器。服务器既要参与打牌,又要负责发牌。不知这样设计是否合理?而且其中涉及到的线程很多,不知该如何设计?
本人所作的部分代码如下,希望对高人更好的指点有所帮助:
import java.awt.*;
import javax.swing.*;
public class card extends JLabel
{
private String name; //牌名称
public card(String name)
{
super();
this.name = name;
this.setIcon(new ImageIcon("images/rear.gif"));
this.setSize(71, 96);
setVisible(true);
}
public void turnFront()
{
this.setIcon(new ImageIcon("images/" + name + ".gif"));
}
public int getCardValue() //返回牌的值
{
return Integer.parseInt(name.substring(2));
}
public String getName()
{
return name;
}
}
public class initCard
{
private card [] allCards, lastCards;
private card playerCards[][]=new card[3][20];
private int[] cardsNo={17, 17, 17};
public void initCards()//初始化牌数
{
allCards=new card [54];
for(int i=1;i<=4;i++)
{
for(int j=3;j<=15;j++)
{
allCards[(i-1)*13+j-1]=new card(i+"-"+j);
}
}
allCards[52] = new card("g-16");//设置大小王
allCards[53] = new card("g-17");
randomCards();
}
public void randomCards()//打乱原来初始化的牌!
{
card temp;
for(int i=0;i<54;i++)
{
int x = (int) (Math.random() * 54);
int y = (int) (Math.random() * 54);
temp = allCards[x];
allCards[x] = allCards[y];
allCards[y] = temp;
}
}
public void dealCards()//给每位玩家发牌,前三张牌预留,后面要调用线程客户端发牌
{
randomCards();
lastCards = new card[3];
for(int i = 0; i < 3; i++)
lastCards[i] = allCards[i];
for(int i = 0; i < 3; i ++)
{
for(int j = 0; j < 17; j ++)
{
playerCards[i][j] = allCards[i * 17 + j + 3];
}
}
}
}
本人考虑使用对象流,程序分为两个客户端,一个服务器。服务器既要参与打牌,又要负责发牌。不知这样设计是否合理?而且其中涉及到的线程很多,不知该如何设计?