📄 gameserver.java
字号:
b_myCard[i].setIcon(new ImageIcon("waiting.gif"));
b_hisCard[i].setIcon(new ImageIcon("waiting.gif"));
}
b_deal.setEnabled(false);
b_openCard.setEnabled(false);
b_ready.setText("准备");
b_ready.setEnabled(false);
b100.setEnabled(false);
b200.setEnabled(false);
b300.setEnabled(false);
b400.setEnabled(false);
b500.setEnabled(false);
tf_fund.setText(""+myFund);
ta_info.setText("等待对方准备...\n");
}
public void showMycard(){//显示我的牌
for(int i=0;i<5;i++){
b_hisCard[i].setIcon(new ImageIcon("waiting.gif"));
b_myCard[i].setIcon((new ImageIcon(mycard[i].getCardPicture())));
}
myType=myType.check(mycard);
if(myType.type==0){ta_info.append("一对都没有...少下点阿老大!");}
if(myType.type==1){ta_info.append("有一对哦,还行! ");}
if(myType.type==2){ta_info.append("竟然有两对,应该不错吧 !");}
if(myType.type==3){ta_info.append("有三张!!不得了啊!");}
if(myType.type==4){ta_info.append("顺子都捡到,他死定了!");}
if(myType.type==5){ta_info.append("同花啊!!赢定了吧!");}
if(myType.type==6){ta_info.append("full house!!挂不去了吧这科。。");}
if(myType.type==7){ta_info.append("四张,我想我要拿A了!");}
if(myType.type==8){ta_info.append("my god!同花顺,我要拿A+!");}
ta_info.append("下注吧。。。\n");
b100.setEnabled(true);
b200.setEnabled(true);
b300.setEnabled(true);
b400.setEnabled(true);
b500.setEnabled(true);
}
class Client extends Thread {//与客户端通讯类,线程
public Client(){
try{
ta_info.setText("正在开启端口...");
PORT=Integer.parseInt(tf_port.getText().trim());
myServer=new ServerSocket(PORT);
ta_info.append("ok...\n");
ta_info.append("本机IP地址是"
+InetAddress.getLocalHost().getHostAddress()+" :"+PORT
+"\n等待对手加入。。。\n");
ta_info.append("若要关闭请通过 Ctrl+Alt+Del, 嘻嘻,没办法 ~~!\n");
mysock=myServer.accept();
ta_info.append("对手来自"+mysock.getInetAddress().toString()+"\n");
ta_info.append("有人加入游戏了,请等待对手启动游戏。。。\n");
out = new ObjectOutputStream( mysock.getOutputStream() );
in = new ObjectInputStream( mysock.getInputStream() );
}
catch(Exception e1){
ta_info.setText(e1.getMessage()+"....建立失败了\n");
b_openPort.setEnabled(true);
}
}
public void run() {
String tem="";
String fromClient="";
do{
try{
fromClient=(String)in.readObject();
tem=fromClient;
if(fromClient.charAt(0)=='9'){//对方加入游戏信息
ta_info.append("ok");
}
if(fromClient.charAt(0)=='1'){//对方说话信息
ta_info.append("对手说:"+tem.substring(1)+"\n");
}
if(fromClient.charAt(0)=='0'){//对方离开信息,关掉socket连接,可以重新开启端口
ta_info.append("对手走了\n"+"你也走吧,玩多无益啊 ...\n");
out.close();
in.close();
mysock.close();
myServer.close();
b_deal.setEnabled(false);
tf_say.setEditable(false);
b_send.setEnabled(false);
b_openPort.setEnabled(true);
this.stop();
}
if(fromClient.charAt(0)=='4'){//接收到请求开牌信息
//对方下注
ta_info.append("对手下注为"+tem.substring(1)+"。。。可以开牌了!\n");
hisAnte=Integer.parseInt(tem.substring(1));
b_openCard.setEnabled(true);
}
if(fromClient.charAt(0)=='2'){//接收到请求开始信息
ta_info.setText("对方准备好了,请发牌吧。。。\n");
b_ready.setEnabled(false);
b_deal.setEnabled(true);
}
if(fromClient.charAt(0)=='5'){//连接成功
ta_info.append("ok,请准备或开始游戏吧...\n");
b_leave.setEnabled(true);
b_ready.setEnabled(true);
b_send.setEnabled(true);
tf_say.setEditable(true);
}
}catch(Exception e){
ta_info.append(e.getMessage()+"\n");
this.stop();
return;
}
}while(true);
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b_leave){//离开
try{
out.writeObject("0");
out.flush();
myServer.close();
out.close();
in.close();
mysock.close();
}catch(Exception ee){ System.exit(0);}
System.exit(0);
}
if(e.getSource()==b_deal){//发牌
deal();//洗牌并分配好,并发送
showMycard();//显示我的牌
//下注键有效
b100.setEnabled(true);b200.setEnabled(true);b300.setEnabled(true);
b400.setEnabled(true);b500.setEnabled(true);
b_deal.setEnabled(false);
b_ready.setEnabled(false);
}
if(e.getSource()==b_ready){//通知对方准备
try {
ta_info.append("准备好了,请等待对手准备...\n");
out.writeObject( "1请准备吧老兄,等很久了啊。。。" );
out.flush();
}
catch ( Exception e1) {
ta_info.append(e1.getMessage() );
}
init_begin();
}
if(e.getSource()==b_openCard){//开牌
String myanteString=String.valueOf(myAnte);
whowin=this.compareTheCardType();
try {//发送server的下注
out.writeObject( "4"+whowin+myanteString );
out.flush();
}
catch ( Exception e1 ) {
ta_info.append(e1.getMessage() );
}
// 开牌
for(int i=0;i<5;i++){//显示他的牌
b_hisCard[i].setIcon((new ImageIcon(hiscard[i].getCardPicture())));
}
if(whowin==2){//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==1){
ta_info.append("不好意思,你输了...\n");
myFund-=myAnte;
hisFund+=hisAnte;
ta_info.append("你输了"+myAnte+"...\n");
ta_info.append("他赢了"+hisAnte+"...他还有"+hisFund+"...\n");
}
b_ready.setEnabled(true);
b_ready.setText("再来");
b_openCard.setEnabled(false);
b100.setEnabled(false);
b200.setEnabled(false);
b300.setEnabled(false);
b400.setEnabled(false);
b500.setEnabled(false);
}
//下注
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");
}
if(e.getSource()==b_openPort){//开端口,创建线程
b_leave.setEnabled(true);
b_openPort.setEnabled(false);
client=new Client();
client.start();
}
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 e1 ) {
ta_info.append(e1.getMessage() );
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -