qq_37055149 2017-05-24 14:57 采纳率: 0%
浏览 1637
已结题

java21点与自制的GUI界面

 package a;
import java.util.Scanner;

import java.util.Arrays;

public class Play21 {

     int[] cards,computer,human;//定义一副54张牌,电脑5张,玩家5张

     Scanner sr=new Scanner(System.in);

     public Play21(){//构造方法

      cards=new int[54];//定义54个大小的数组表示扑克牌

      Arrays.fill(cards,0);//54张牌全赋值零,一旦某一张牌被用了就赋1

      computer=new int[5];//电脑5张牌

    Arrays.fill(computer,0);

      human=new int[5];//人5张牌

      Arrays.fill(human,0);

     }

     public void clear(){//使数组内所有数据赋值0没有用过,用过的赋值1

      Arrays.fill(cards,0);

      Arrays.fill(computer,0);

      Arrays.fill(human,0);
    }

     public void start(){//开始游戏

      System.out.println("-------------------------------------");

      System.out.println("开始游戏!");

      int n=1;



      for(int i=0;i<1;i++){//电脑和玩家先各抽1张

       computer[i]=nextOne();
       human[i]=nextOne();

     }

      while(n<5){//牌数小于5时询问是否再要牌

      show(human,n,1);//显示现在玩家的牌面

      if(ask(human,n)){

       computer[n]=nextOne();

       human[n]=nextOne();

       n++;

      }
    else break;

      }

      show(human,n,1);

      show(computer,n,0);

      if(judge(human,computer,n)==1)

       System.out.println("\n你赢了!");

    else if(judge(human,computer,n)==0)

        System.out.println("\n平局!");

        else if(judge(human,computer,n)==-1)

          System.out.println("\n你输了!");

      System.out.println("------------------------------------");

     }

     void show(int[] a,int num,int c){//显示牌面,如果c是1显示的是玩家的牌面,c是0显示的是电脑的牌面,num就是第几轮

      if(c==1)System.out.println("\n"+(num-2)+":你现在的牌是:");

      else System.out.println("\n"+(num-2)+":电脑现在的牌是:");

        for(int i=0;i<num;i++){//以此把牌都显示出来,如果是1就是A、11就是J、12就是Q、13就是K、14就是小鬼、15就是大鬼

         if(a[i]==1){System.out.print("A ");continue;}

         if(a[i]==11){System.out.print("J ");continue;}

         if(a[i]==12){System.out.print("Q ");continue;}

         if(a[i]==13){System.out.print("K ");continue;}

         if(a[i]==14){System.out.print("小鬼 ");continue;}

         if(a[i]==15){System.out.print("大鬼 ");continue;}

         System.out.print(a[i]+" ");//以空格为分割以次打印

      }

    }




    boolean ask(int[] a,int num){

      System.out.println("\n还抽一张?Y/N");

      String ch=sr.nextLine();

      if(!ch.equals("n")&&!ch.equals("N"))

       return true;

      else return false;

     }

     int nextOne(){//用递归确保返回的牌没有重复

      int n=(int)(Math.random()*54);//通过随机的方法产生数

      if(cards[n]==0){//如果产生的那个数字曾经没有用过那么就用,否则重新产生

       cards[n]=1;//用过的赋值为1

       if(n==52)

        return 14;

       else if(n==53)

         return 15;

         else return n%13+1;//这个数对13取模然后加1就是产生的牌

      }

    else return nextOne();//重新随机产生

     }

     int judge(int[] a,int[] b,int num){//判断电脑和玩家谁的点数更接近21,如果有鬼牌就另行判断

      int sum_a=0,sum_b=0;//设置两个变量为了计数

      boolean joker_a=false,joker_b=false;//分别代表人和电脑是否有鬼牌,true代表有

      for(int i=0;i<num;i++){//这里把电脑和人的牌总和加起来,如果有鬼牌就把joker_a或joker_b设为true,再进一步判断

       if(a[i]==14||a[i]==15)joker_a=true;//14为小鬼、15为大鬼

       else sum_a+=a[i];//取和

       if(b[i]==14||b[i]==15)joker_b=true;

       else sum_b+=b[i];

      }

      if(joker_a)if(sum_a<8)sum_a+=13;//鬼牌可以任意1-13,如果鬼牌存在,并且其余牌总和小于8,那么现在加上13就是总和

          else if(sum_a<21)sum_a=21;//如果除了鬼牌其余牌大于8小于21,那么总和就是21

      if(joker_b)if(sum_b<8)sum_b+=13;//电脑和人判断方法一样

          else if(sum_b<21)sum_b=21;

      if(sum_a>21&&sum_b<=21)return -1;//最后判断大小,如果人的大于21并且电脑小于等于21那么电脑赢

      if(sum_a<=21&&sum_b>21)return 1;//否则人赢

      if(Math.abs(sum_a-21)<Math.abs(sum_b-21))

    return 0;//一样的话平局

        else //if(Math.abs(sum_a-21)>Math.abs(sum_b-21))

         return -1;//否则电脑赢

     }
        public static void main (String[] args) {

              Play21 p=new Play21();//创建一个对象

              Scanner sr=new Scanner(System.in);//再控制台输入

              String ch="y";//定义一个字符

              while(true){//循环

               p.start();//调用start方法

               System.out.println("再来一局?Y/N");

               ch=sr.nextLine();//再控制台输入y或者n

               if(!ch.equals("n")&&!ch.equals("N"))//判断如果不是n或者N的话就重新玩,否则游戏结束

                p.clear();//调用clear方法,把所有牌都标记为0就是没用过

               else break;

              }

        }


    }





 package a;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TOpoint extends Play21{
    public static void main(String[] args){
        JFrame f = new JFrame("21点");
            f.setSize(600,450);    
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);             
            f.setResizable(false);  
            f.setLocationRelativeTo(null) ;     
        Play21 p = new Play21();

        JLabel lblNewLabel = new JLabel("你已经拥有的牌:");



        JLabel lblNewLabel_1 = new JLabel("合计:");

        JLabel lblNewLabel_2 = new JLabel("背面");
        lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);

        JButton button = new JButton("要牌");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });

        JLabel lblNewLabel_3 = new JLabel("玩家");
        lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);

        JLabel label = new JLabel("电脑");
        label.setHorizontalAlignment(SwingConstants.CENTER);

        JLabel label_1 = new JLabel("你已经拥有的牌:");

        JLabel label_2 = new JLabel("合计:");

        JLabel label_3 = new JLabel("背面");
        label_3.setHorizontalAlignment(SwingConstants.CENTER);

        JButton button_1 = new JButton("要牌");
        GroupLayout groupLayout = new GroupLayout(f.getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(30)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 192, GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
                        .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
                            .addComponent(lblNewLabel_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(button, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)))
                    .addGap(162)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label_1, GroupLayout.PREFERRED_SIZE, 192, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE))
                    .addGap(26))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(11)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                    .addGap(18)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblNewLabel)
                        .addComponent(label_1))
                    .addGap(41)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblNewLabel_1)
                        .addComponent(label_2))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE))
                    .addGap(56)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)))
        );
        f.getContentPane().setLayout(groupLayout);

    }
}
  • 写回答

3条回答

  • qq_37055149 2017-05-24 14:58
    关注

    第一个是21点的代码,第二个是自己编写的GUI界面,如何将第一个实现到GUI界面上

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名