为了账号安全,请及时绑定邮箱和手机立即绑定

java第三季:扑克牌游戏实现

标签:
Java
写的人都要崩溃了,终于完成了,等后面有更好的实现再来优化吧!

创建一个扑克牌的类:

package learn.java2;
import java.util.LinkedList;
//写一个关于扑克牌的类,该类具有name(用于表示牌的信息)和num(比较牌的大小),利用num实现Comparable接口来比较大小
public class Card implements Comparable<Card>{
    int num;
    String name;
    //使用LinkedList是因为据说在多插入,删除操作中其效率会比ArrayList要高
    LinkedList<Card> cards=new LinkedList<Card>();
    public Card(){
    }
    //重载card类的构造方法
    public Card(int num ,String name){
        this.num=num;
        this.name=name;
    }
    //实现Comparable节口的compareTo()方法,注意其返回值类型
    public int compareTo(Card card){
        return (this.num-card.num);
    }
    //创建函数getNewCard()来创建一副新牌,这样写感觉很蠢,一点也不优雅,后面有更好的实现再改吧
    public LinkedList<Card> getNewCards(){
        cards.add(new Card(52,"红桃A"));cards.add(new Card(51,"方片A"));
        cards.add(new Card(50,"黑桃A"));cards.add(new Card(49,"梅花A"));
        cards.add(new Card(48,"红桃K"));cards.add(new Card(47,"方片K"));
        cards.add(new Card(46,"黑桃K"));cards.add(new Card(45,"梅花K"));
        cards.add(new Card(44,"红桃Q"));cards.add(new Card(43,"方片Q"));
        cards.add(new Card(42,"黑桃Q"));cards.add(new Card(41,"梅花Q"));
        cards.add(new Card(40,"红桃J"));cards.add(new Card(39,"方片J"));
        cards.add(new Card(38,"黑桃J"));cards.add(new Card(37,"梅花J"));
        cards.add(new Card(36,"红桃10"));cards.add(new Card(35,"方片10"));
        cards.add(new Card(34,"黑桃10"));cards.add(new Card(33,"梅花10"));
        cards.add(new Card(32,"红桃9"));cards.add(new Card(31,"方片9"));
        cards.add(new Card(30,"黑桃9"));cards.add(new Card(29,"梅花9"));
        cards.add(new Card(28,"红桃8"));cards.add(new Card(27,"方片8"));
        cards.add(new Card(26,"黑桃8"));cards.add(new Card(25,"梅花8"));
        cards.add(new Card(24,"红桃7"));cards.add(new Card(23,"方片7"));
        cards.add(new Card(22,"黑桃7"));cards.add(new Card(21,"梅花7"));
        cards.add(new Card(20,"红桃6"));cards.add(new Card(19,"方片6"));
        cards.add(new Card(18,"黑桃6"));cards.add(new Card(17,"梅花6"));
        cards.add(new Card(16,"红桃5"));cards.add(new Card(15,"方片5"));
        cards.add(new Card(14,"黑桃5"));cards.add(new Card(13,"梅花5"));
        cards.add(new Card(12,"红桃4"));cards.add(new Card(11,"方片4"));
        cards.add(new Card(10,"黑桃4"));cards.add(new Card(9,"梅花4"));
        cards.add(new Card(8,"红桃3"));cards.add(new Card(7,"方片3"));
        cards.add(new Card(6,"黑桃3"));cards.add(new Card(5,"梅花3"));
        cards.add(new Card(4,"红桃2"));cards.add(new Card(3,"方片2"));
        cards.add(new Card(2,"黑桃2"));cards.add(new Card(1,"梅花2"));
        return cards;
    }
}

创建一个玩家的类:

package learn.java2;
import java.util.ArrayList;
//创建一个玩家类,有id和姓名,手牌,三个属性
public class Player {
    int id;
    String name;
    ArrayList<Card> cardsInhand=new ArrayList();
}

主函数所在类,实现玩游戏的功能:

package learn.java2;
//想省事点可以直接导入util.*,这样写是为了更清楚系统的一些api的位置
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
public class PlayGame {
    public static void main(String[] args){
        PlayGame pg=new PlayGame();
        //1. 调用newCards() 方法创建一副牌,得到的牌命名为cards
        LinkedList<Card> cards=pg.newCards();
        //2. 调用washCards() 方法洗牌,传入1中得到的牌,洗后的牌命名为cards1
        LinkedList<Card> cards1=pg.washCards(cards);
        //3. 调用creatPlayer() 方法,创建两名玩家
        System.out.println("---------创建玩家-------------");
        Player player1=new Player();
        Player player2=new Player();
        pg.creatPlayers(player1,player2);
        //4. 调用dealCard()方法来发牌,将洗后的牌和两名玩家作为参数传入
        pg.dealCard(cards1,player1,player2);
        //5. 调用beginGame() 方法比大小,并打印输入
        pg.beginGame(player1, player2);
    }
    //调用Card类中的getNewCards()获得一副新牌,打印牌的信息,返回
    private LinkedList<Card> newCards(){
        System.out.println("------创建扑克牌---------");
        Card myCard=new Card();
        System.out.println("-------扑克牌创建成功----------------");
        LinkedList<Card> cardList=myCard.getNewCards();
        for(Card each:cardList){
            System.out.print("  "+each.name);
        }       
        System.out.println();
        return cardList;
    }
    //实现洗牌操作
    private LinkedList<Card> washCards(LinkedList<Card> cards){
        //创建一个新列表
        LinkedList<Card> card1=new LinkedList<Card>();
        Random random=new Random();
        System.out.println("------------开始洗牌--------");
        //生成一个随即数,从原牌堆cards中循环抽取一张牌到新列表card1中,注意生成随机数的参数的变化,会随着原牌堆的减少而变小
        for(int i=0;i<52;i++){
            int k=random.nextInt(52-i);
            card1.add(cards.remove(k));
        }
        System.out.println("----------洗牌结束---------------");
        //下面代码是为了测试下洗牌成功了没有,实际可以注释掉或删除
        System.out.println("测试下是否洗牌成功,洗后牌总张数:"+card1.size());
        for(Card each:card1){
            System.out.print(" "+each.name);
        }
        System.out.println();
        //返回洗后的牌
        return card1;
    }
    //实现创建玩家的方法
    private void creatPlayers(Player player1,Player player2){
        //写一个死循环,来捕捉不合法的输入
        while(true){
            try{
                System.out.println("请输入第一位玩家id和姓名:");
                System.out.println("输入id:");
                Scanner input=new Scanner(System.in);
                player1.id=input.nextInt();
                System.out.println("输入姓名:");
                player1.name=input.next();
                System.out.println("请输入第二位玩家id和姓名:");
                System.out.println("输入id:");
                player2.id=input.nextInt();
                System.out.println("输入姓名:");
                player2.name=input.next();
                System.out.println("欢迎玩家"+player1.name);
                System.out.println("欢迎玩家"+player2.name);
                break;
            }catch(InputMismatchException e){
                System.out.println("输入类型错误,请检查输入是否符合要求");               
            }
        }
    }
    //实现进行发牌操作的方法,从牌堆抽一张牌到手牌列表中,同时原牌堆也没有了这张牌
    private void dealCard(LinkedList<Card> cards1,Player player1,Player player2){
        System.out.println("------------开始发牌---------------");      
        System.out.println(player1.name+"拿牌");
        //remove()在删除元素的同时也是有返回值得
        player1.cardsInhand.add(cards1.remove());
        System.out.println(player2.name+"拿牌");
        player2.cardsInhand.add(cards1.remove());
        System.out.println(player1.name+"拿牌");
        player1.cardsInhand.add(cards1.remove());
        System.out.println(player2.name+"拿牌");
        player2.cardsInhand.add(cards1.remove());
        System.out.println("-------发牌结束-------------");
    }
    //开始游戏,对手牌排序,第二张就是较大的牌,比较两人最大牌的num属性值的大小,其实 大小王也可以写进去的。
    private void beginGame(Player player1,Player player2){
        System.out.println("----------开始游戏-----------------");
        //对手牌进行排序,获得各自较大的牌
        Collections.sort(player1.cardsInhand);
        Collections.sort(player2.cardsInhand);      
        Card max_py1=player1.cardsInhand.get(1);
        Card max_py2=player2.cardsInhand.get(1);
        System.out.println("玩家"+player1.name+"最大的手牌为:"+max_py1.name);
        System.out.println("玩家"+player2.name+"最大的手牌为:"+max_py2.name);
        //比较二者的大小,打印 胜者信息
        if(max_py1.num>max_py2.num){
            System.out.println("------玩家"+player1.name+"获胜-------");
        }else{
            System.out.println("------玩家"+player2.name+"获胜-------");
        }
        //打印二者的手牌信息
        System.out.println("玩家各自的手牌为:");
        System.out.println(player1.name+":  "+player1.cardsInhand.get(0).name+"  "+player1.cardsInhand.get(1).name);
        System.out.println(player2.name+":  "+player2.cardsInhand.get(0).name+"  "+player2.cardsInhand.get(1).name);
    }
}

虽然完整的实现了老师要求的功能,但感觉和老师的实现方法还是不太一样的。肯定还可以写的更简单,更优雅。以后再来吧!
有什么看法和意见,欢迎共同探讨。

点击查看更多内容
19人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消