?? 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 + -