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

Java入门第三季 7-1 简易扑克牌游戏

脱脂棉纱布毛毯棉布团
关注TA
已关注
手记 3
粉丝 1
获赞 13
  • 扑克牌
package collection;

public class Cards implements Comparable<Cards>{

    String name;
    String number;
    public Cards(String name,String number){
        this.name=name;
        this.number=number;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((number == null) ? 0 : number.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Cards))
            return false;
        Cards other = (Cards) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (number == null) {
            if (other.number != null)
                return false;
        } else if (!number.equals(other.number))
            return false;
        return true;
    }
    @Override
    public int compareTo(Cards o) {
        // TODO Auto-generated method stub
        return this.number.compareTo(o.number);
    }

}
package collection;

import java.util.Comparator;

public class CardsComparator implements Comparator<Cards> {

    @Override
    public int compare(Cards o1, Cards o2) {
        // TODO Auto-generated method stub
        return o1.name.compareTo(o2.name);
    }

}
  • 玩家
package collection;

public class Player {
    public String id;
    public String name;

    public Player(String id,String name){
        this.id=id;
        this.name=name;
    }
}
  • 比较大小用的winCard
package collection;

public class Winner implements Comparable<Winner>{

    String name;
    String number;
    String nameWinner;

    public Winner(String nameWinner,String name,String number){
        this.name=name;
        this.nameWinner=nameWinner;
        this.number=number;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((number == null) ? 0 : number.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Winner))
            return false;
        Winner other = (Winner) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (number == null) {
            if (other.number != null)
                return false;
        } else if (!number.equals(other.number))
            return false;
        return true;
    }

    @Override
    public int compareTo(Winner o) {
        // TODO Auto-generated method stub
        return this.number.compareTo(o.number);
    }
}
package collection;

import java.util.Comparator;

public class WinnerComparator implements Comparator<Winner> {

    @Override
    public int compare(Winner o1, Winner o2) {
        // TODO Auto-generated method stub
        return o1.name.compareTo(o2.name);
    }

}
  • 主函数
package collection;

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

public class Card {

    public List<Player>players;
    public List<Cards>cards;
    public List<Cards>cards1;
    public List<Cards>pcards1;
    public List<Cards>pcards2;
    public List<Winner>pwin;
    public Scanner scanner;

    public Card(){
        players=new ArrayList<Player>();
        cards=new ArrayList<Cards>();
        cards1=new ArrayList<Cards>();
        pcards1=new ArrayList<Cards>();
        pcards2=new ArrayList<Cards>();
        pwin=new ArrayList<Winner>();
        scanner=new Scanner(System.in);
    }

    public void createCard(){
        System.out.println("------------创建扑克牌--------------");
        Random random=new Random();
        System.out.println("-----------扑克牌创建成功------------");
        String names[]={"方块","梅花","红桃","黑桃"};
        Cards cds1=new Cards("", "");
        String numbers[]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        for(int i=0;i<4;i++){
            for(int j=0;j<13;j++){
                Cards cds=new Cards(names[i],numbers[j]);
                cards.add(cds);
            }
        }
        System.out.print("为:");
        for(Cards cd:cards){
            System.out.print(cd.name+cd.number+" ");
        }
        System.out.println();
        System.out.println("-------------开始洗牌--------------");
        for(int k=0;k<52;k++){
        do{
            cds1=new Cards(names[random.nextInt(4)],numbers[random.nextInt(13)]);
        }while(cards1.contains(cds1));
        cards1.add(cds1);
        }
        System.out.print("为:");
        for(Cards cd1:cards1){
            System.out.print(cd1.name+cd1.number+" ");
        }
        System.out.println();
        System.out.println("-------------洗牌结束--------------");
    }

    public void createPlayer(){
        System.out.println("-------------创建玩家--------------");
        for(int i=0;i<2;i++){
            System.out.println("请输入第"+(i+1)+"位玩家ID和姓名:");
            System.out.println("输入ID:");
            String a=scanner.next();
            System.out.println("输入姓名:");
            String b=scanner.next();
            Player player=new Player(a, b);
            players.add(player);
        }
        for(Player plname:players){
            System.out.println("---欢迎玩家:"+plname.name);
        }
    }

    public void getCards(){
        System.out.println("-------------开始发牌--------------");
        for(int i=0;i<3;i++){
            System.out.println("---玩家:"+players.get(0).name+"-拿牌");
            pcards1.add(new Cards(cards1.get(i).name, cards1.get(i).number));
            System.out.println("---玩家:"+players.get(1).name+"-拿牌");
            pcards2.add(new Cards(cards1.get(i+1).name, cards1.get(i+1).number));
            i++;
        }
        System.out.println("-------------发牌结束--------------");
        System.out.println("-------------开始游戏--------------");
        System.out.println("-----------玩家各自的手牌为--------------");
        System.out.print(players.get(0).name+":");
        System.out.print("[");
        for(Cards pcds1:pcards1){
            System.out.print(pcds1.name+pcds1.number+" ");
        }
        System.out.print("]");
        System.out.println();
        System.out.print(players.get(1).name+":");
        System.out.print("[");
        for(Cards pcds2:pcards2){
            System.out.print(pcds2.name+pcds2.number+" ");
        }
        System.out.print("]");
        System.out.println();
        String a=pcards1.get(0).number;
        String b=pcards1.get(1).number;
        if(!a.equals(b)){
            Collections.sort(pcards1);
            String bigName=pcards1.get(1).name;
            String bigNumber=pcards1.get(1).number;
            System.out.println("玩家:"+players.get(0).name+"最大的手牌为:"+bigName+bigNumber);
            pwin.add(new Winner(players.get(0).name,bigName, bigNumber));
        }else{
            Collections.sort(pcards1,new CardsComparator());
            String bigName=pcards1.get(1).name;
            String bigNumber=pcards1.get(1).number;
            System.out.println("玩家:"+players.get(0).name+"最大的手牌为:"+bigName+bigNumber);
            pwin.add(new Winner(players.get(0).name,bigName, bigNumber));
        }
        String c=pcards2.get(0).number;
        String d=pcards2.get(1).number;
        if(!c.equals(d)){
            Collections.sort(pcards2);
            String bigName=pcards2.get(1).name;
            String bigNumber=pcards2.get(1).number;
            System.out.println("玩家:"+players.get(1).name+"最大的手牌为:"+bigName+bigNumber);
            pwin.add(new Winner(players.get(1).name, bigName, bigNumber));
        }else{
            Collections.sort(pcards2,new CardsComparator());
            String bigName=pcards2.get(1).name;
            String bigNumber=pcards2.get(1).number;
            System.out.println("玩家:"+players.get(1).name+"最大的手牌为:"+bigName+bigNumber);
            pwin.add(new Winner(players.get(1).name, bigName, bigNumber));
        }
        String e=pwin.get(0).number;
        String f=pwin.get(1).number;
        if(!e.equals(f)){
            Collections.sort(pwin);
            System.out.println("获胜者为:"+pwin.get(1).nameWinner);
        }else{
            Collections.sort(pwin,new WinnerComparator());
            System.out.println("获胜者为:"+pwin.get(1).nameWinner);
        }
    }

      public static void main(String[] args) {
          Card cd=new Card();
          cd.createCard();
          cd.createPlayer();
          cd.getCards();
      }
}

图片描述
图片描述
报错功能没有添加,因为我输入的都是string类型。跟老师的格式有所出入,如果有不理解的地方,可以一起交流。

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