⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 deckofcards.java

📁 用Java编写的扑克牌游戏(含代码)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************
 * (C) Copyright 10/06/2005 by Jokeyu  (姓名:麦天柱;学号:2003131014;03计A) *
 * All Rights Reserved.                                                   *
 *************************************************************************/
/**************************************************************************
 1. 输赢的规则为: 烂牌<一对<两对<同三<顺子<同花<Full-house<同四<同花顺。

 2.  发牌时每人每次发一张,这是为了防备洗牌时有些连着的牌没洗到,而造成发牌时连着发给同一个人的情况。

 3.  发完牌后,下注前,可以换自己手上的牌,每张扣100。也可以偷看对手的牌,也是每张扣100。

 4.  获胜几率的计算是依据现在你手上的牌,算出所有比它更小的牌的组合之和占一副牌所有组合的百分率,即为获胜几率。
 *************************************************************************/
package deckofcards;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class DeckOfCards
    extends JFrame {
  private int card_name[][] = new int[5][2]; //显示牌时用的图片名字
  private long player_score;
  private int wager; //赌注
  private int EMULANT = 0, PLAYER = 1;
  private int lightbutton; //发光的赌注按钮
  private boolean GameIsStart = false, BreakConnect = false; //游戏是否进行中,是否取牌完毕
  private Socket socket;
  private DataInputStream input;
  private DataOutputStream output;
  private int UpdateStatus=0,GetCard=1,ChangeCard=2,PeekCard=3,Wager=4;
  Client client;

  JButton button_100, button_200, button_300, button_400, button_500,
      button_begin, emulant_image, player_image,
      card_image[][] = new JButton[5][2], button_client, button_break;
  JLabel emulant_status, player_status, result_label, wager_label, copyright_label,
      warn_label, score_label, level_label, winchance_label, blank_label,
      address_label, emulant_level_label, emulant_score_label;
  JTextField address_textf, port_textf;
  GridBagLayout gbLayout;
  GridBagConstraints gbConstraints;
  JPanel p;
  Icon image;
  Image icon;

  public DeckOfCards() {
    super("扑克牌小游戏");
    player_score = 1000;
    gbLayout = new GridBagLayout();
    Container c = getContentPane();
    JPanel p = new JPanel();
    p.setLayout(gbLayout);
    gbConstraints = new GridBagConstraints();
    JScrollPane sp = new JScrollPane(p);

    icon = Toolkit.getDefaultToolkit().getImage("smile.gif");
    this.setIconImage(icon);

    copyright_label = new JLabel(
        "\u00A9 Copyright 10/06/2005 by Jokeyu @C4-702. All Rights Reserved.");
    warn_label = new JLabel("程序员忠告玩家: 赌博害己害人,切勿轻易尝试!本软件仅供游戏娱乐用途!");
    warn_label.setFont(Font.getFont("新宋体"));
    blank_label = new JLabel("^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^");

    image = new ImageIcon("player.gif");
    player_image = new JButton();
    player_image.setIcon(image);

    image = new ImageIcon("opponent.gif");
    emulant_image = new JButton();
    emulant_image.setIcon(image);
    image = new ImageIcon("waiting.gif");
    for (int i = 0; i < 5; i++) {
      card_image[i][EMULANT] = new JButton();
      card_image[i][EMULANT].setIcon(image);
      card_image[i][PLAYER] = new JButton();
      card_image[i][PLAYER].setIcon(image);
    }

    image = new ImageIcon("smile.gif");
    result_label = new JLabel("尚未连接服务器");
    result_label.setFont(Font.getFont("新宋体"));
    result_label.setIcon(image);
    level_label = new JLabel("你的等级 : 未知");
    level_label.setFont(Font.getFont("新宋体"));
    score_label = new JLabel("你的分数 : 未知");
    score_label.setFont(Font.getFont("新宋体"));
    emulant_level_label = new JLabel("对手等级 : 未知");
    emulant_level_label.setFont(Font.getFont("新宋体"));
    emulant_score_label = new JLabel("对手分数 : 未知");
    emulant_score_label.setFont(Font.getFont("新宋体"));
    winchance_label = new JLabel("获胜几率: 未知");
    winchance_label.setFont(Font.getFont("新宋体"));
    button_begin = new JButton("点这里开始游戏");
    button_begin.setFont(Font.getFont("新宋体"));
    image = new ImageIcon("money.gif");
    wager_label = new JLabel("请 下 注");
    wager_label.setFont(Font.getFont("新宋体"));
    wager_label.setIcon(image);
    button_100 = new JButton("100");
    button_200 = new JButton("200");
    button_300 = new JButton("300");
    button_400 = new JButton("400");
    button_500 = new JButton("500");
    player_status = new JLabel("你的牌 : 请先连接服务器");
    player_status.setFont(Font.getFont("新宋体"));
    emulant_status = new JLabel("对手的牌 : 请先连接服务器");
    emulant_status.setFont(Font.getFont("新宋体"));
    button_break = new JButton("断开连接");
    button_break.setFont(Font.getFont("新宋体"));
    button_break.setBackground(Color.white);
    button_client = new JButton("连服务器");
    button_client.setFont(Font.getFont("新宋体"));
    button_client.setBackground(Color.white);
    address_label = new JLabel("---地址和端口--->");
    address_label.setFont(Font.getFont("新宋体"));
    address_textf = new JTextField("127.0.0.1");
    port_textf = new JTextField("1617");

    gbConstraints.weightx = 1;
    gbConstraints.weighty = 1;
    gbConstraints.fill = GridBagConstraints.BOTH;

    addComponent(copyright_label, gbLayout, gbConstraints, p, 0, 0, 6, 1);
    addComponent(warn_label, gbLayout, gbConstraints, p, 1, 0, 6, 1);
    addComponent(blank_label, gbLayout, gbConstraints, p, 2, 0, 6, 1);
    addComponent(emulant_level_label, gbLayout, gbConstraints, p, 3, 0, 2, 1);
    addComponent(emulant_score_label, gbLayout, gbConstraints, p, 3, 2, 4, 1);
    addComponent(emulant_image, gbLayout, gbConstraints, p, 4, 0, 1, 5);
    for (int i = 1; i <= 5; i++) {
      addComponent(card_image[i - 1][EMULANT], gbLayout, gbConstraints, p, 4,
                   i, 1, 5);
    }
    addComponent(emulant_status, gbLayout, gbConstraints, p, 9, 0, 6, 1);
    addComponent(player_image, gbLayout, gbConstraints, p, 10, 0, 1, 5);
    for (int i = 1; i <= 5; i++) {
      addComponent(card_image[i - 1][PLAYER], gbLayout, gbConstraints, p, 10, i,
                   1, 5);
    }
    addComponent(player_status, gbLayout, gbConstraints, p, 15, 0, 6, 1);
    addComponent(wager_label, gbLayout, gbConstraints, p, 16, 0, 1, 1);
    addComponent(button_100, gbLayout, gbConstraints, p, 16, 5, 1, 1);
    addComponent(button_200, gbLayout, gbConstraints, p, 16, 4, 1, 1);
    addComponent(button_300, gbLayout, gbConstraints, p, 16, 3, 1, 1);
    addComponent(button_400, gbLayout, gbConstraints, p, 16, 2, 1, 1);
    addComponent(button_500, gbLayout, gbConstraints, p, 16, 1, 1, 1);
    addComponent(level_label, gbLayout, gbConstraints, p, 17, 0, 2, 1);
    addComponent(score_label, gbLayout, gbConstraints, p, 17, 2, 2, 1);
    addComponent(winchance_label, gbLayout, gbConstraints, p, 17, 4, 2, 1);
    addComponent(button_begin, gbLayout, gbConstraints, p, 18, 0, 2, 1);
    addComponent(result_label, gbLayout, gbConstraints, p, 18, 2, 4, 1);
    addComponent(button_break, gbLayout, gbConstraints, p, 19, 0, 1, 1);
    addComponent(button_client, gbLayout, gbConstraints, p, 19, 1, 1, 1);
    addComponent(address_label, gbLayout, gbConstraints, p, 19, 2, 1, 1);
    addComponent(address_textf, gbLayout, gbConstraints, p, 19, 3, 2, 1);
    addComponent(port_textf, gbLayout, gbConstraints, p, 19, 5, 1, 1);

    c.add(sp);
    setSize(550, 400);
    this.setLocation(250, 150);

    button_100.setEnabled(false);
    button_100.setBackground(Color.lightGray);
    button_200.setEnabled(false);
    button_200.setBackground(Color.lightGray);
    button_300.setEnabled(false);
    button_300.setBackground(Color.lightGray);
    button_400.setEnabled(false);
    button_400.setBackground(Color.lightGray);
    button_500.setEnabled(false);
    button_500.setBackground(Color.lightGray);
    button_begin.setEnabled(false);
    button_begin.setBackground(Color.lightGray);
    button_break.setEnabled(false);
    button_break.setBackground(Color.lightGray);

    show(); // show the window

    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
//开始游戏按钮被点击的触发事件
    button_begin.addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        switch (lightbutton) {
          case 1:
            button_100.setBackground(Color.lightGray);
            break;
          case 2:
            button_200.setBackground(Color.lightGray);
            break;
          case 3:
            button_300.setBackground(Color.lightGray);
            break;
          case 4:
            button_400.setBackground(Color.lightGray);
            break;
          case 5:
            button_500.setBackground(Color.lightGray);
            break;
        }
        for (int i = 0; i < 5; i++) {
          image = new ImageIcon("waiting.gif");
          card_image[i][EMULANT].setIcon(image);
        }
        try {
          output.writeInt(GetCard);
          button_begin.setText("游戏已经开始");
          button_begin.setEnabled(false);
          button_begin.setBackground(Color.lightGray);
          result_label.setText("正在取牌......");
        }
        catch (IOException E) {}
      }
    }
    );
//100分赌注按钮被点击的触发事件
    button_100.addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if(GameIsStart==true){
          try {
            output.writeInt(Wager);
            output.writeInt(100);
            lightbutton = 1;
            wager = 100;
            wager();
            button_100.setBackground(Color.yellow);
            result_label.setText("正在等待比较结果......");
          }
          catch (IOException E) {}
        }
        GameIsStart=false;
      }
    }
    );
//200分赌注按钮被点击的触发事件
    button_200.addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if(GameIsStart==true){
          try {
            output.writeInt(Wager);
            output.writeInt(200);
            lightbutton = 2;
            wager = 200;
            wager();
            button_200.setBackground(Color.yellow);
            result_label.setText("正在等待比较结果......");
          }
          catch (IOException E) {}
        }
        GameIsStart=false;
      }
    }
    );
//300分赌注按钮被点击的触发事件
    button_300.addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if(GameIsStart==true){
          try {
            output.writeInt(Wager);
            output.writeInt(300);
            lightbutton = 3;
            wager = 300;
            wager();
            button_300.setBackground(Color.yellow);
            result_label.setText("正在等待比较结果......");
          }
          catch (IOException E) {}
        }
        GameIsStart=false;
      }
    }
    );
//400分赌注按钮被点击的触发事件
    button_400.addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if(GameIsStart==true){
          try {
            output.writeInt(Wager);
            output.writeInt(400);
            lightbutton = 4;
            wager = 400;
            wager();
            button_400.setBackground(Color.yellow);
            result_label.setText("正在等待比较结果......");
          }
          catch (IOException E) {}
        }
        GameIsStart=false;
      }
    }
    );
//500分赌注按钮被点击的触发事件
    button_500.addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if(GameIsStart==true){
          try {
            output.writeInt(Wager);
            output.writeInt(500);
            lightbutton = 5;
            wager = 500;
            wager();
            button_500.setBackground(Color.yellow);
            result_label.setText("正在等待比较结果......");
          }
          catch (IOException E) {}
        }
        GameIsStart=false;
      }
    }
    );
//玩家的第一张牌被点击触发的换牌事件
    card_image[0][PLAYER].addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (GameIsStart == true) {
          if (player_score >= 200) {
            try {
              output.writeInt(ChangeCard);
              output.writeInt(0);
              result_label.setText("正在换牌......");
            }
            catch (IOException E) {}
          }
          else {
            result_label.setText("你的分数不够!不能再换牌了!");
          }
        }
      }
    }
    );
//玩家的第二张牌被点击触发的换牌事件
    card_image[1][PLAYER].addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (GameIsStart == true) {
          if (player_score >= 200) {
            try {
              output.writeInt(ChangeCard);
              output.writeInt(1);
              result_label.setText("正在换牌......");
            }
            catch (IOException E) {}
          }
          else {
            result_label.setText("你的分数不够!不能再换牌了!");
          }
        }
      }
    }
    );
//玩家的第三张牌被点击触发的换牌事件
    card_image[2][PLAYER].addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (GameIsStart == true) {
          if (player_score >= 200) {
            try {
              output.writeInt(ChangeCard);
              output.writeInt(2);
              result_label.setText("正在换牌......");
            }
            catch (IOException E) {}
          }
          else {
            result_label.setText("你的分数不够!不能再换牌了!");
          }
        }
      }
    }
    );
//玩家的第四张牌被点击触发的换牌事件
    card_image[3][PLAYER].addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (GameIsStart == true) {
          if (player_score >= 200) {
            try {
              output.writeInt(ChangeCard);
              output.writeInt(3);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -