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

java入门第三季简易的扑克牌游戏,欢迎大家指正!!

标签:
Java
package com.Pokergame;
public class Poker implements Comparable<Poker>{
    private String Color;
    private String Point;
    private int ID;

    public Poker(String color,String point,int id){
        this.Color = color;
        this.Point = point;
        this.ID = id;
    }
    public String getColor() {
        return Color;
    }
    public void setColor(String color) {
        Color = color;
    }
    public String getPoint() {
        return Point;
    }
    public void setPoint(String point) {
        Point = point;
    }
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }

    public int compareTo(Poker o) {
        if (this.ID==o.ID){
            if (this.Color.equals("黑桃")){
                    return 1;
            }else if(this.Color.equals("红桃")){
                if(o.Color.equals("黑桃")){
                    return -1;
                }else{
                    return 1;
                }
            }else if(this.Color.equals("梅花")){
                if(o.Color.equals("黑桃")||o.Color.equals("红桃")){
                    return -1;
                }else{
                    return 1;
                }
            }else {
                    return -1;
            }
        }else{
            if (this.ID>o.ID){
                return 1;
            }else{
                return -1;
            }
        }
    }
package com.Pokergame;

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

/**
 * 玩家类
 * */
public class Gameplayer {
    private int id;
    private String name;
    private List <Poker> takecars;
    public Gameplayer(int id,String name){
        this.id = id;
        this.name = name;
        this.takecars = new ArrayList<Poker>();
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Poker> getTakecars() {
        return takecars;
    }

}
package com.Pokergame;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * 测试
 * */
public class Test {
      private   Scanner input;
      //存放扑克牌的集合
      private List<Poker> poker;
      //存放玩家
      private List<Gameplayer> gameplayer;
      public Test(){
        this.input = new Scanner(System.in);
        this.poker = new ArrayList<Poker> ();
        this.gameplayer = new ArrayList<Gameplayer>();

    }

    //创建扑克牌
      private void AddPoker(){
        Poker[] pokers = {
                new Poker("黑桃","A",1),
                new Poker("红桃","A",2),
                new Poker("梅花","A",3),
                new Poker("方片","A",4),
                new Poker("黑桃","2",5),
                new Poker("红桃","2",6),
                new Poker("梅花","2",7),
                new Poker("方片","2",8),
                new Poker("黑桃","3",9),
                new Poker("红桃","3",10),
                new Poker("梅花","3",11),
                new Poker("方片","3",12),
                new Poker("黑桃","4",13),
                new Poker("红桃","4",14),
                new Poker("梅花","4",15),
                new Poker("方片","4",16),
                new Poker("黑桃","5",17),
                new Poker("红桃","5",18),
                new Poker("梅花","5",19),
                new Poker("方片","5",20),
                new Poker("黑桃","6",21),
                new Poker("红桃","6",22),
                new Poker("梅花","6",23),
                new Poker("方片","6",24),
                new Poker("黑桃","7",25),
                new Poker("红桃","7",26),
                new Poker("梅花","7",27),
                new Poker("方片","7",28),
                new Poker("黑桃","8",29),
                new Poker("红桃","8",30),
                new Poker("梅花","8",31),
                new Poker("方片","8",32),
                new Poker("黑桃","9",33),
                new Poker("红桃","9",34),
                new Poker("梅花","9",35),
                new Poker("方片","9",36),
                new Poker("黑桃","10",37),
                new Poker("红桃","10",38),
                new Poker("梅花","10",39),
                new Poker("方片","10",40),
                new Poker("黑桃","J",41),
                new Poker("红桃","J",42),
                new Poker("梅花","J",43),
                new Poker("方片","J",44),
                new Poker("黑桃","Q",45),
                new Poker("红桃","Q",46),
                new Poker("梅花","Q",47),
                new Poker("方片","Q",48),
                new Poker("黑桃","K",49),
                new Poker("红桃","K",50),
                new Poker("梅花","K",51),
                new Poker("方片","K",52)
                }; 

        poker.addAll(Arrays.asList(pokers));
        Collections.shuffle(poker);
    }

     //创建玩家并拿牌
      private void Createplayer(){
          for(int i=1;i<3;i++){
              System.out.println("请输入第"+i+"名玩家ID:");
              int id =0;
              while(true){
                  try {
                        id = Integer.parseInt(input.nextLine().trim());
                        break;
                    } catch (NumberFormatException  e) {
                        System.out.println("您输入id必须由数字组成!");
                        System.out.println("请重新输入:");
                        id = 0;
                        continue;
                    }
              }

                 System.out.println("请输入第"+i+"名玩家的name:");
                 String name =input.nextLine();

                 gameplayer.add(new Gameplayer(id, name));

                }
         //玩家拿牌
         for (int k=0;k<4;k++){
             System.out.println(gameplayer.get(k%2).getName()+"拿牌!");
             gameplayer.get(k%2).getTakecars().add(new Poker(poker.get(k).getColor(), poker.get(k).getPoint(), poker.get(k).getID()));
         }

         for (Poker p : gameplayer.get(0).getTakecars()) {
             System.out.println(gameplayer.get(0).getName()+"拿牌:"+p.getColor()+p.getPoint());

        }
         for (Poker p : gameplayer.get(1).getTakecars()) {
             System.out.println(gameplayer.get(1).getName()+"拿牌:"+p.getColor()+p.getPoint());

        }

     }

      private void compare(){
             Collections.sort(gameplayer.get(0).getTakecars()); 
             Collections.sort(gameplayer.get(1).getTakecars());
             //比较大小:
             if(gameplayer.get(0).getTakecars().get(0).getID()>gameplayer.get(1).getTakecars().get(0).getID()){
                 System.out.println(gameplayer.get(0).getName()+"获胜!");
             }else{
                 System.out.println(gameplayer.get(1).getName()+"获胜!");
            }
      }

      public static void main(String[] args) {
          while(true){
              Test t1 =new Test();
              t1.AddPoker();
              t1.Createplayer();
              t1.compare();
              System.out.println("是否继续游戏?(1/0)");
              int yn =0;
              try {
                 yn =Integer.parseInt(t1.input.nextLine().trim());
            } catch (Exception e) {
                System.out.println("您输入的参数错误!");
            }
              if(yn == 1){
                  continue;
              }else{
                  break;
              }
          }

    }
点击查看更多内容
1人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消