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

JAVA入门第三季——最后练习题扑克牌

qq_不在视线里_0
关注TA
已关注
手记 1
粉丝 3
获赞 9

图片描述
图片描述

玩家类:

package playCards;

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

//创建对象玩家,玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合

public class Players {
    public int id;
    public String name;
    public List<Cards> handCards;
    public Players(Integer id,String name) {
        this.id = id;
        this.name = name;
        this.handCards = new ArrayList<Cards>();
    }
}

卡牌类

package playCards;

//创建对象扑克牌,不考虑大小王
public class Cards {
    public String num;
    public String color;
    public Cards(String num, String color) {
        this.num=num;
        this.color=color;
    }
    public Cards() {

    }
    @Override
    public boolean equals(Object arg0) {
        // TODO 自动生成的方法存根
        return super.equals(arg0);
    }

}

游戏类(含main方法)

package playCards;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class PlayGame  {

    public static List<Cards> cardsToSelect;
    public static List<Cards> cardsToSelectNew;
    private Scanner console;
    public Players player1;
    public Players player2;

    public PlayGame() {
        cardsToSelect = new ArrayList<Cards>();
        cardsToSelectNew = new ArrayList<Cards>();
        console = new Scanner(System.in);
    }

//添加扑克牌
    public void addCards() {
        System.out.println("创建扑克牌");//默认花色大小:黑桃》红桃》梅花》方片
        Cards[] cd = {new Cards("2","方片"),new Cards("2","梅花"),new Cards("2","红桃"),new Cards("2","黑桃"),
                new Cards("3","方片"),new Cards("3","梅花"),new Cards("3","红桃"),new Cards("3","黑桃"),
                new Cards("4","方片"),new Cards("4","梅花"),new Cards("4","红桃"),new Cards("4","黑桃"),
                new Cards("5","方片"),new Cards("5","梅花"),new Cards("5","红桃"),new Cards("5","黑桃"),
                new Cards("6","方片"),new Cards("6","梅花"),new Cards("6","红桃"),new Cards("6","黑桃"),
                new Cards("7","方片"),new Cards("7","梅花"),new Cards("7","红桃"),new Cards("7","黑桃"),
                new Cards("8","方片"),new Cards("8","梅花"),new Cards("8","红桃"),new Cards("8","黑桃"),
                new Cards("9","方片"),new Cards("9","梅花"),new Cards("9","红桃"),new Cards("9","黑桃"),
                new Cards("10","方片"),new Cards("10","梅花"),new Cards("10","红桃"),new Cards("10","黑桃"),
                new Cards("J","方片"),new Cards("J","梅花"),new Cards("J","红桃"),new Cards("J","黑桃"),
                new Cards("Q","方片"),new Cards("Q","梅花"),new Cards("Q","红桃"),new Cards("Q","黑桃"),
                new Cards("K","方片"),new Cards("K","梅花"),new Cards("K","红桃"),new Cards("K","黑桃"),
                new Cards("A","方片"),new Cards("A","梅花"),new Cards("A","红桃"),new Cards("A","黑桃")
                ,};
        for (Cards cards : cd) {
            cardsToSelect.add(cards);
        }
        System.out.println("创建扑克牌成功!");
    }

    //遍历所有扑克牌
    public void cardsForEach(List<Cards> cardsToSelect) {
        System.out.print("所有的牌为:");
        for (Cards cards : cardsToSelect) {
            System.out.print(cards.color+cards.num+",");
        }
        System.out.println();
    }

    //洗牌
    public List<Cards> shuffle(List<Cards> cardsToSelect) {
        Cards cdNew = new Cards();
        Random random = new Random();
        List<Cards> cardsToSelectNew = new ArrayList<Cards>();
        for(int i=0;i<cardsToSelect.size();i++) {
            do {
                int position = random.nextInt(cardsToSelect.size());
                cdNew = cardsToSelect.get(position);
            }while(cardsToSelectNew.contains(cdNew));//先执行do再判断while里的条件是否满足,不满足条件则跳出循环。可保证随机产生的数不相同
            cardsToSelectNew.add(cdNew);
        }

        return cardsToSelectNew;

    }

    //添加玩家
    public void addPlayer() {
        System.out.println("请出入第一位玩家ID和姓名:");
        int play1ID; 
        while(true){
            System.out.println("请输入ID:");

            try{

                play1ID = console.nextInt();
                System.out.println("请输入姓名:");
                String play1Name = console.next();
                player1 = new Players(play1ID,play1Name);
                break;
            }catch(InputMismatchException e) {

                System.out.println("请输入整数类型的ID");//输入异常,提示输入整数型ID,进行下次循环
                console.next();//上一次输入的字符还在缓冲流,定义一个字符串读走,防止出现无限循环情况
            }

        }

        System.out.println("请出入第二位玩家ID和姓名:");//因为玩家较少,我直接使用复制粘贴,玩家数量多时,应循环输入所有玩家更好
        int play2ID;
        while(true){
            System.out.println("请输入ID:");

            try{                
                play2ID= console.nextInt();
                System.out.println("请输入姓名:");
                String play2Name = console.next();
                player2= new Players(play2ID,play2Name);
                break;
                }catch(InputMismatchException e) {
                System.out.println("请输入整数类型的ID"); //输入异常,提示输入整数型ID,进行下次循环
                console.next();//上一次输入的字符还在缓冲流,定义一个字符串读走,防止出现无限循环情况
                }

        }
        System.out.println("欢迎玩家:"+player1.name);
        System.out.println("欢迎玩家:"+player2.name);
    }

    //发牌方法
    public void deal() {
        System.out.println("-----开始发牌-----");

        for(int i = 0;i<3;i=i+2) {
            System.out.println("---玩家:"+player1.name+"-拿牌");
            player1.handCards.add(cardsToSelectNew.get(i));
            //cardsToSelect.add(0,cardsToSelect.get(0));
            System.out.println("---玩家:"+player2.name+"-拿牌");
            player2.handCards.add(cardsToSelectNew.get(i+1));
            //cardsToSelect.add(1,cardsToSelect.get(1));
        }

        System.out.println("-----发牌结束-----");
    }

    //开始比大小游戏
    public void gameStart() {
        Cards max1 =new Cards(); 
        max1 = player1.handCards.get(0);
        //Collections.sort(player1.handCards);

        for (Cards cards : player1.handCards) {//试了String1.compareTo(String2)来比大小方法取得玩家的最大牌有点不对,因为cards的数值都是String型
                                            //Sting型的比大小是通过ASSIC码来比较的,与传统意义打牌的大小不一样
            if(cardsToSelect.indexOf(max1)<cardsToSelect.indexOf(cards) ) {//所以需要尝试其他排序方法,从网上学到别人的collection。sort()方法,
                max1 = cards;               //但是comparator我也不会弄,所以就把原始的牌按顺序排序,根据在原始牌的位置来排大小
            }
            /*if(max1.num.compareTo(cards.num)==0)
            {
                switch(max1.color) {
                case "黑桃": break;
                case "红桃": if(cards.num == "黑桃") {
                            max1 = cards;
                            }
                            break;
                case "梅花": if(cards.num == "黑桃"||cards.num == "红桃") {
                            max1 = cards;
                            }
                            break;
                case "方片": max1 = cards;
                            break;
                }
            }*/
        }
        System.out.println(player1.name+" 最大的手牌是:"+max1.color+max1.num);
        Cards max2 = player2.handCards.get(0);
        //Collections.sort(player2.handCards);;
        for (Cards cards : player2.handCards) {
            if(cardsToSelect.indexOf(max2)<cardsToSelect.indexOf(cards) ) {
                max2 = cards;
            }
            /*if(max2.num.compareTo(cards.num)==0)
            {
                switch(max2.color) {
                case "黑桃": break;
                case "红桃": if(cards.num == "黑桃") {
                            max2 = cards;
                            }
                            break;
                case "梅花": if(cards.num == "黑桃"||cards.num == "红桃") {
                            max2 = cards;
                            }
                            break;
                case "方片": max2 = cards;
                            break;
                }
            }*/
        }
        System.out.println(player2.name+" 最大的手牌是:"+max2.color+max2.num);

        if(cardsToSelect.indexOf(max1)>cardsToSelect.indexOf(max2)) {
            System.out.println("-----玩家"+player1.name+"获胜-----");
            System.out.println("玩家各自的手牌为:");
            System.out.println(player1.name+":["+player1.handCards.get(0).color+player1.handCards.get(0).num
                    +player1.handCards.get(1).color+player1.handCards.get(1).num+"]");
            System.out.println(player2.name+":["+player2.handCards.get(0).color+player2.handCards.get(0).num
                    +player2.handCards.get(1).color+player2.handCards.get(1).num+"]");
        }
        else {
            System.out.println("-----玩家"+player1.name+"获胜-----");
            System.out.println("玩家各自的手牌为:");
            System.out.println(player1.name+":["+player1.handCards.get(0).color+player1.handCards.get(0).num
                    +player1.handCards.get(1).color+player1.handCards.get(1).num+"]");
            System.out.println(player2.name+":["+player2.handCards.get(0).color+player2.handCards.get(0).num
                    +player2.handCards.get(1).color+player2.handCards.get(1).num+"]");
        }

    }

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        PlayGame game = new PlayGame();
        game.addCards();
        game.cardsForEach(cardsToSelect);
        cardsToSelectNew=game.shuffle(cardsToSelect);
        System.out.println("洗牌成功!");
        game.cardsForEach(cardsToSelectNew);
        game.addPlayer();
        game.deal();
        game.gameStart();
    }

}

第一次写手记,个人基础也不好,高级的方法用的很少,比大小的时候也是只有用原来按大小创建的卡牌的位置来比大小,希望各位大佬多多指教。

打开App,阅读手记
6人推荐
发表评论
随时随地看视频慕课网APP