📄 gameclient.java
字号:
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class gameClient extends JFrame implements ActionListener{
JButton b500=new JButton("$$500$$");
JButton b400=new JButton("$$400$$");
JButton b300=new JButton("$$300$$");
JButton b200=new JButton("$$200$$");
JButton b100=new JButton("$$100$$");
JButton b_ready=new JButton("准备");
JButton b_leave=new JButton("离开");
JButton b_reqOpenCard=new JButton("请求开牌");
JButton b_send=new JButton("send");
TextArea ta_info=new TextArea(4,4);//显示所有信息
JTextField tf_say=new JTextField(300);//交谈
JTextField tf_fund=new JTextField(15);//显示资金
JButton b_myCard[]=new JButton[5];
JButton b_hisCard[]=new JButton[5];
JPanel p1=new JPanel();
JPanel p2=new JPanel();
///////
Socket mysock;
ObjectInputStream in;
ObjectOutputStream out;
/////
int myAnte=300;//我的下赌注
int hisAnte=300;//他的下赌注
int myFund=5000;//我的资金
int hisFund=5000;//他的资金
Card mycard[]=new Card[5];
Card hiscard[]=new Card[5];//牌
cardType myType=new cardType();//我的牌的类型
//cardType hisType=new cardType();//他的牌的类型
int whowin=0;
SERVER server;
gameClient(Socket sock){
mysock=sock;
getContentPane().setLayout(null);
getContentPane().add(p1);
getContentPane().add(p2);
p1.setBounds(0, 0, 700, 500);
p2.setBounds(700, 0, 150, 500);
init_p1();
init_p2();
///////
setSize(850,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-850)/2,(screen.height-500)/2);
setTitle("client");
setVisible(true);
setResizable(false);
////////
init_begin();
server=new SERVER();//开线程
server.start();
}
public void init_p1(){
p1.setLayout(null);
JLabel l1=new JLabel("对手的牌");
p1.add(l1);
l1.setBounds(380,10, 100,20 );
JLabel l2=new JLabel("你 的 牌");
p1.add(l2);
l2.setBounds(380,330, 100,20 );
JLabel l3=new JLabel("交谈 ");
p1.add(l3);
l3.setBounds(150,285, 30,20 );
p1.add(ta_info);
p1.add(tf_say);
p1.add(b_send);
for(int i=0;i<5;i++){
b_myCard[i]=new JButton(new ImageIcon("waiting.gif"));
p1.add(b_myCard[i]);
b_myCard[i].setBounds(150+i*110, 380, 80, 80);
}
for(int i=0;i<5;i++){
b_hisCard[i]=new JButton(new ImageIcon("waiting.gif"));
p1.add(b_hisCard[i]);
b_hisCard[i].setBounds(150+i*110, 40, 80, 80);
}
ta_info.setBounds(150, 130, 500, 150);
tf_say.setBounds(180, 285, 380, 30);
b_send.setBounds(570, 285, 80, 30);
ta_info.setEditable(false);
b_send.addActionListener(this);
tf_say.addActionListener(this);
ta_info.setText("加入中...\n");
///
JLabel back2=new JLabel(new ImageIcon("p1.JPG"));
p1.add(back2);
back2.setBounds(0, 0, 900, 500);
}
public void init_p2(){
p2.setLayout(null);
JLabel l_ante=new JLabel("金币:" );
p2.add(b_ready);p2.add(b_leave);p2.add(b_reqOpenCard);p2.add(l_ante);p2.add(tf_fund);
p2.add(b100);p2.add(b200);p2.add(b300);p2.add(b400);p2.add(b500);
JLabel back=new JLabel(new ImageIcon("p2.JPG"));
p2.add(back);
back.setBounds(0, 0, 150, 500);
///////
b_leave.setBounds(20, 0, 100, 40);
b_ready.setBounds(20, 100, 100, 40);
b_reqOpenCard.setBounds(20, 150, 100, 40);
l_ante.setBounds(20, 200, 150, 40);
tf_fund.setBounds(20, 240, 100, 40);
b500.setBounds(20, 300,100, 30);
b400.setBounds(20, 330,100, 30);
b300.setBounds(20, 360,100, 30);
b200.setBounds(20, 390,100, 30);
b100.setBounds(20, 420,100, 30);
////
b_leave.addActionListener(this);
b_ready.addActionListener(this);
b_reqOpenCard.addActionListener(this);
b500.addActionListener(this);
b400.addActionListener(this);
b300.addActionListener(this);
b200.addActionListener(this);
b100.addActionListener(this);
////////
tf_fund.setText("5000");
tf_fund.setEditable(false);
b_reqOpenCard.setEnabled(false);
}
public void showMycard(){//看到自己的牌
for(int i=0;i<5;i++){
b_myCard[i].setIcon((new ImageIcon(mycard[i].getCardPicture())));
}
myType=myType.check(mycard);
if(myType.type==0){ta_info.append("一对都没有...少下点阿老大!\n");}
if(myType.type==1){ta_info.append("有一对哦,还行! \n");}
if(myType.type==2){ta_info.append("竟然有两对,应该不错吧 !\n");}
if(myType.type==3){ta_info.append("有三张!!不得了啊!\n");}
if(myType.type==4){ta_info.append("顺子都捡到,他死定了!\n");}
if(myType.type==5){ta_info.append("同花啊!!赢定了吧!\n");}
if(myType.type==6){ta_info.append("full house!!挂不去了吧这科。。\n");}
if(myType.type==7){ta_info.append("四张,我想我要拿A了!\n");}
if(myType.type==8){ta_info.append("my god!同花顺,我要拿A+!\n");}
ta_info.append("请下注吧...\n");
b100.setEnabled(true);
b200.setEnabled(true);
b300.setEnabled(true);
b400.setEnabled(true);
b500.setEnabled(true);
b_reqOpenCard.setEnabled(true);
}
public void openCard(){//开牌
for(int i=0;i<5;i++){
b_hisCard[i].setIcon((new ImageIcon(hiscard[i].getCardPicture())));
}
if(whowin==1){//I
ta_info.append("恭喜你赢了...\n");
myFund+=myAnte;
hisFund-=hisAnte;
ta_info.append("你赢了"+myAnte+"...\n");
ta_info.append("他输了"+hisAnte+"...他还有"+hisFund+"...\n");
}
if(whowin==0){
ta_info.append("打平...\n");
}
if(whowin==2){
ta_info.append("不好意思,你输了...\n");
myFund-=myAnte;
hisFund+=hisAnte;
ta_info.append("输了了"+myAnte+"...\n");
ta_info.append("他赢了"+hisAnte+"...他还有"+hisFund+"...\n");
}
init_begin();
}
public void init_begin(){//游戏初始化
b_ready.setEnabled(true);
b100.setEnabled(false);
b200.setEnabled(false);
b300.setEnabled(false);
b400.setEnabled(false);
b500.setEnabled(false);
tf_fund.setText(""+myFund);
}
public void stringToCard(Card card[],String s){//将接收到的牌String转回Card类型
for(int i=0;i<5;i++){
card[i]=new Card();
}
int j=0;
for(int i=0;i<5;i++){
card[i].pip=Integer.parseInt(s.substring(j, j+2));
card[i].face=Integer.parseInt(s.substring(j+2,j+4));
j=j+4;
}
}
class SERVER extends Thread {//线程类,通过输入流接收server的信息
SERVER(){
try{
in=new ObjectInputStream(mysock.getInputStream());
out=new ObjectOutputStream(mysock.getOutputStream());
try {
out.writeObject( "5 " );
out.flush();
}
catch ( Exception e2 ) {
ta_info.append( e2.getMessage() );
}
ta_info.append("连接到"+mysock.getInetAddress().toString()+"...请准备!\n");
}catch(IOException e1){ta_info.append(e1.getMessage()+"\n");}
}
public void run(){
String fromServer=new String();
do{
try{
fromServer=(String)in.readObject();
//fromServer.charAt(0)为信息头,根据信息头来判断
if(fromServer.charAt(0)=='1'){
ta_info.append("对手说:"+fromServer.substring(1)+"\n");
}
if(fromServer.charAt(0)=='2'){
stringToCard(hiscard,fromServer.substring(1));
}
if(fromServer.charAt(0)=='3'){
stringToCard(mycard,fromServer.substring(1));
showMycard();
}
if(fromServer.charAt(0)=='4'){
//对方下注
ta_info.append("对手下注为"+fromServer.substring(2)+"\n");
whowin=Integer.parseInt(fromServer.substring(1,2));//第二位是结果
hisAnte=Integer.parseInt(fromServer.substring(2));
openCard();
}
if(fromServer.charAt(0)=='0'){
ta_info.append("对手走了\n"+"你也走吧,玩多无益啊 ~~#\n");
b_ready.setEnabled(false);
b_send.setEnabled(false);
b_reqOpenCard.setEnabled(false);
tf_say.setEditable(false);
b100.setEnabled(false);
b200.setEnabled(false);
b300.setEnabled(false);
b400.setEnabled(false);
b500.setEnabled(false);
return;
}
}
catch(Exception e1){ta_info.append(e1.getMessage()+"\n");return;}
}while(true);
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b_ready){//发送准备信息
try {
out.writeObject( "2准备了,请开始吧 " );
out.flush();
ta_info.setText("准备好了...请等待发牌...\n");
}
catch ( Exception e2 ) {
ta_info.append( e2.getMessage()+"...可能连接丢失了,重启游戏吧\n" );
}
for(int i=0;i<5;i++){
b_myCard[i].setIcon(new ImageIcon("waiting.gif"));
b_hisCard[i].setIcon(new ImageIcon("waiting.gif"));
}
b_ready.setText("再来");
b_ready.setEnabled(false);
}
if(e.getSource()==b_reqOpenCard){//发送请求开牌信息
try {//发送server的下注
out.writeObject( "4"+String.valueOf(myAnte));
out.flush();
ta_info.append("你的下注是:"+myAnte+"...等待开牌...\n");
}
catch ( Exception e2 ) {
ta_info.append( e2.getMessage()+"...可能连接丢失了,重启游戏吧\n" );
}
b_reqOpenCard.setEnabled(false);
b100.setEnabled(false);
b200.setEnabled(false);
b300.setEnabled(false);
b400.setEnabled(false);
b500.setEnabled(false);
}
if(e.getSource()==b_leave){//离开,关闭
try {
out.writeObject( "0zou le!" );
out.flush();
out.close();
in.close();
mysock.close();
}
catch ( Exception e1 ) {
System.exit(0);
}
System.exit(0);
}
if(e.getSource()==b_send||e.getSource()==tf_say){//交谈
String rline=tf_say.getText().trim();
ta_info.append("我说:"+rline+"\n");
tf_say.setSelectionStart(0);
tf_say.setSelectionEnd(1000);
tf_say.requestFocus(true);
///////
try {
out.writeObject( "1"+rline );
out.flush();
}
catch ( Exception e2 ) {
ta_info.append( e2.getMessage() +"...可能连接丢失了,重启游戏吧\n");
}
}
// 下注
if(e.getSource()==b100){
myAnte=100;
ta_info.append("你选择下注100...可以请求开牌...\n");
}
if(e.getSource()==b200){
myAnte=200;
ta_info.append("你选择下注200...可以请求开牌...\n");
}
if(e.getSource()==b300){
myAnte=300;
ta_info.append("你选择下注300...可以请求开牌...\n");
}
if(e.getSource()==b400){
myAnte=400;
ta_info.append("你选择下注400...可以请求开牌...\n");
}
if(e.getSource()==b500){
myAnte=500;
ta_info.append("你选择下注500...可以请求开牌...\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -