继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

扑克牌游戏。比较菜,,所以代码会有点长。

1367936928
关注TA
已关注
手记 4
粉丝 2
获赞 43

Brand.java 如下

package com_brand_project;

import java.util.ArrayList;
import java.util.List;

class newbrand<newbrand>{    //创建一个数据类型
    String color;
    int number;
    public newbrand(String color,int number){
        this.color=color;
        this.number=number;
    }
}

public class Brand {
    //创建一个List集合
    public List<newbrand> list=new ArrayList<newbrand>();
    public void creat(){
        /**
         * 用List集合创建一整套扑克牌
         * 
         */
        String color[]={"黑桃","梅花","方片","红桃"};
        String number[]={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};

        for(int k=0;k<4;k++){
        for(int i=0;i<number.length;i++){
            switch(k){
            case 0:{
                newbrand card=new newbrand("方块"+number[i], i); //前者花色  后者为值
                list.add(card);
            }break;
            case 1:{
                newbrand card=new newbrand("梅花"+number[i], i);
                list.add(card);
            }break;
            case 2:{
                newbrand card=new newbrand("黑桃"+number[i], i);
                list.add(card);
            }break;
            case 3:{
                newbrand card=new newbrand("红桃"+number[i], i);
                list.add(card);
            }break;
            }
        }
        }

//      for (newbrand list : list) {  //遍历 list集合
//          System.out.println(list.color+"\t"+list.number);
//      }

    }
}

Player.java如下

package com_brand_project;

import java.util.Scanner;

public class Player {
    boolean flag=true;
    static int i=0;
    public int player_ID;
    public String player_Name;
    public Scanner sc=new Scanner(System.in);
    public Player(){
        if(i>=2){
            i=0;
        }
        i++;
            System.out.println("----------->创建第" + i + "名玩家···\n");
            while (flag) {
                System.out.println("----------->请输入第" + i + "位玩家的ID:\n");
                try {
                    player_ID = Integer.decode(sc.nextLine());
                    flag = false;
                } catch (Exception e) {
                    System.out.println("你的输入有误!\n\n");
                    //flag=false;
                    //continue;
                }

            }
            System.out.println("----------->请输入第" + i + "位玩家的名字:\n");
            player_Name = sc.next();

    }
}

PlayGame.java 如下

package com_brand_project;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class PlayGame {
    static int i=0;
    static boolean flag =true;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        // TODO Auto-generated method stub
        Brand b=new Brand();

        List<newbrand> p1Game =new ArrayList<newbrand>();
                 //创建1p的手牌容器
        List<newbrand> p2Game =new ArrayList<newbrand>();   
                //创建2p的手牌容器

        System.out.println("*******************************************\n");
        System.out.println("**************《一起来打扑克》**************\n");
        System.out.println("----------->开始创建牌组:\n");
        b.creat();
        System.out.println("----------->牌组创建完毕\n");
        System.out.println("----------->开始洗牌:\n");
        System.out.println("----------->第一次洗牌··\n");
        Collections.shuffle(b.list);
        System.out.println("----------->第二次洗牌····\n");
        Collections.shuffle(b.list);
        System.out.println("----------->第三次洗牌·······\n");
        Collections.shuffle(b.list);
        System.out.println("----------->洗牌完毕\n");
        System.out.println("----------->开始创建玩家:\n");
        Player p1=new Player();
        Player p2=new Player();
        System.out.println("----------->玩家创建完毕\n");
        do{
            flag=true;
            Collections.shuffle(b.list);
            i=0;
            p1Game.clear();
            p2Game.clear();

        System.out.println("----------->游戏开始:\n");

        System.out.println("----------->抽牌环节:\n");
        System.out.println("玩家【"+p1.player_Name+"】正在进行抽牌\n");
        newbrand p1_card=new newbrand(b.list.get(i).color,b.list.get(i).number);
        p1Game.add(p1_card);
        System.out.println("玩家【"+p1.player_Name+"】抽牌完毕\n");
        System.out.println("玩家【"+p1.player_Name+"】明牌:【"+p1Game.get(0).color+"】\n");
        //System.out.println(p1Game.get(0).color+"\t"+p1Game.get(0).number+"\n");
        i++;
        //------------------------------------------------------------------------
        System.out.println("玩家【"+p2.player_Name+"】正在进行抽牌\n");
        newbrand p2_card=new newbrand(b.list.get(i).color,b.list.get(i).number);
        p2Game.add(p2_card);
        System.out.println("玩家【"+p2.player_Name+"】抽牌完毕\n");
        System.out.println("玩家【"+p2.player_Name+"】明牌:【"+p2Game.get(0).color+"】\n");
        //System.out.println(p2Game.get(0).color+"\t"+p2Game.get(0).number+"\n");
        i++;
        //------------------------------------------------------------------------
        System.out.println("----------->第二轮抽牌:\n");
        //------------------------------------------------------------------------
        System.out.println("玩家【"+p1.player_Name+"】正在进行抽牌\n");
        newbrand p1_card_1=new newbrand(b.list.get(i).color,b.list.get(i).number);
        p1Game.add(p1_card_1);
        System.out.println("玩家【"+p1.player_Name+"】抽牌完毕\n");
        //System.out.println(p1Game.get(1).color+"\t"+p1Game.get(1).number+"\n");
        i++;
        //------------------------------------------------------------------------
        System.out.println("玩家【"+p2.player_Name+"】正在进行抽牌\n");
        newbrand p2_card_1=new newbrand(b.list.get(i).color,b.list.get(i).number);
        p2Game.add(p2_card_1);
        System.out.println("玩家【"+p2.player_Name+"】抽牌完毕\n");
        //System.out.println(p2Game.get(1).color+"\t"+p2Game.get(1).number+"\n");
        i++;

        System.out.println("----------->开始拼点:\n");
        Win w=new Win(p1Game.get(0).color,p1Game.get(0).number,p2Game.get(0).color,p2Game.get(0).number,
                p1Game.get(1).color,p1Game.get(1).number,p2Game.get(1).color,p2Game.get(1).number);
        System.out.println("玩家【"+p1.player_Name+"】的牌是:\n");
        System.out.println("【"+p1Game.get(0).color+"】"+"\t"+"【"+p1Game.get(1).color+"】\n");

        System.out.println("玩家【"+p2.player_Name+"】的牌是:\n");
        System.out.println("【"+p2Game.get(0).color+"】"+"\t"+"【"+p2Game.get(1).color+"】\n");
        int win=w.win(-1);
        System.out.println("----------->拼点结束!答案揭晓:\n");
        if(win==1){
            System.out.println("玩家【"+p1.player_Name+"】\t获胜!\n");

        }else if(win==2){
            System.out.println("玩家【"+p2.player_Name+"】\t获胜!\n");
        }else{
            System.out.println("这局是平局!\n");
        }
        System.out.println("*******************************************\n");
        System.out.println("输入0退出!输入其他数继续游戏");
        int op1=sc.nextInt();
        if(op1==0){
            flag=false;
        }else{
            System.out.println("继续游戏:");
        }
        }while(flag);
    }

}

Win.java 如下

package com_brand_project;

public class Win {

    //第一张牌
    private String p1color;
    private int a;

    private String p2color;
    private int b;

    //第二张牌
    private String p1color2;
    private int a2;
    private String p2color2;
    private int b2;
    PlayGame play=new PlayGame();
    public Win(String p1color,int p1number,String p2color,int p2number,String p1color2,int p1number2,String p2color2,int p2number2){

        this.p1color=p1color;
        this.a=p1number;
        this.p2color=p2color;
        this.b=p2number;

        this.p1color=p1color2;
        this.a2=p1number2;
        this.p2color=p2color2;
        this.b2=p2number2;
    }

    public int win(int win){
        win =-1;
        if(a==a2 && b==b2){  //判断玩家1和玩家2是否都是对子
            if(a>b){
                win=1;   //玩家1胜
                return win;
            }else if(a<b){
                win=2;  //玩家2胜
                return win;
            }else{
                if(p1color.equals(p1color2) && !p2color.equals(p2color2)){//如果都是对子  则判断其花色   同花胜
                    win=1;
                    return win;  //玩家1胜
                }else if(p2color.equals(p2color2) && !p1color.equals(p1color2)){
                    win=2;      //玩家2胜
                    return win;
                }else{           //花色和大小都相同  则平
                    win=3;
                    return win;
                }
            }
        }else{
            if((a>b && a>b2) || (a2>b && a2>b2)){   //判断玩家1是否一张牌比玩家2两张牌大 
                win=1;
                return win; //玩家1胜
            }else if((b>a && b>a2) || (b2>a && b2>a2)){ //同上
                win=2;
                return win;  //玩家2胜
            }else{
                if((a==b || a==b2) && (a2==b || a2==b2)){ //牌都相同则平   不是对子没有同花
                    win=3;
                    return win;
                }
            }
        }
        return win;
    }
}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP