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

综合练习——洗牌发牌Easy版

标签:
Java

Card代码,里面是卡牌的属性,重写了equals方法,并且实现了Comparable接口。这里因为要求只是比较大小,所以我在属性里多添加了一条,用多加的这条属性来进行比较,由数字10,20,30,40,50,60,70,80,90,91,92,93,94还有颜色用A-D表示,组合而成的一个字符串,这样去排序看大小的话会非常方便。
因为一般的时候2是最大的牌。2用94,A93,K92,Q91,J90,10用80……3用10。后面跟着如果数字相同则比较花色,如黑红梅方,用DCBA来表示,比如说当都是2时,前两位都是94,方片2是94A,黑桃2就是94D。这样升序会排在对方后面。直接排序然后取最大的值就可以了。再用最大的值和当时的手牌去比较,相同的那位玩家获胜。

游戏运行的类

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by Administrator on 2017/4/4.
 */
public class PokerGameTest {
    public static void main(String[] args) {
        System.out.println("--------欢迎来到扑克牌游戏--------");
        System.out.println("------------创建扑克牌------------");
        System.out.println("----------扑克牌创建成功----------");
        System.out.print("扑克牌为:");
        //创建卡牌集合类,实例化,调用方法创建52张的卡组
        PokerCardsList pcl=new PokerCardsList();
        pcl.pokerCardsListAdd();
        System.out.println("------------开始洗牌-------------");
        System.out.println("------------洗牌结束!------------");
        System.out.println("------------创建玩家--------------");
        PokerPlayerList pm=new PokerPlayerList();
        //新建一个PokerPlayerList集合把这两个玩家存储进来
        System.out.println("请输入第1位玩家的ID和姓名");
        pm.playerIdNameAdd();
        Player player1=pm.playerList.get(0);
        System.out.println("请输入第2位玩家的ID和姓名");
        pm.playerIdNameAdd();
        Player player2=pm.playerList.get(1);
        System.out.println("欢迎玩家:"+player1.playerName);
        System.out.println("欢迎玩家:"+player2.playerName);
        System.out.println("------------开始发牌-------------");
        int[] k=pcl.randomTest();
        Card player1Card1=new Card(pcl.pokerCardsList.get(k[0]).colorNum,
                pcl.pokerCardsList.get(k[0]).colorSum);
        Card player1Card2=new Card(pcl.pokerCardsList.get(k[1]).colorNum,
                pcl.pokerCardsList.get(k[1]).colorSum);
        Card player2Card1=new Card(pcl.pokerCardsList.get(k[2]).colorNum,
                pcl.pokerCardsList.get(k[2]).colorSum);
        Card player2Card2=new Card(pcl.pokerCardsList.get(k[3]).colorNum,
                pcl.pokerCardsList.get(k[3]).colorSum);
        //创建两个玩家的手牌List,实例化,否则会无法调用,空指针异常
        player1.playerCardsList=new ArrayList<Card>();
        player2.playerCardsList=new ArrayList<Card>();
        player1.playerCardsList.add(player1Card1);
        System.out.println("-----玩家:"+player1.playerName+"----拿牌");
        player2.playerCardsList.add(player2Card1);
        System.out.println("-----玩家:"+player2.playerName+"----拿牌");
        player1.playerCardsList.add(player1Card2);
        System.out.println("-----玩家:"+player1.playerName+"----拿牌");
        player2.playerCardsList.add(player2Card2);
        System.out.println("-----玩家:"+player2.playerName+"----拿牌");
        System.out.println("------------发牌结束-------------");
        System.out.println("------------开始游戏-------------");
        Collections.sort(player1.playerCardsList);
        System.out.println("玩家"+player1.playerName+"最大的手牌是:"+
                player1.playerCardsList.get(1).colorNum);
        Collections.sort(player2.playerCardsList);
        System.out.println("玩家"+player2.playerName+"最大的手牌是:"+
                player2.playerCardsList.get(1).colorNum);
        Player nullPlayer=new Player();
        nullPlayer.playerCardsList=new ArrayList<Card>();
        nullPlayer.playerCardsList.add(player1.playerCardsList.get(1));
        nullPlayer.playerCardsList.add(player2.playerCardsList.get(1));
        Collections.sort(nullPlayer.playerCardsList);
        if(nullPlayer.playerCardsList.get(1).equals(player1.playerCardsList.get(1))){
            System.out.println("----------"+"玩家"+player1.playerName+"获胜"+"----------");
        }else{
            System.out.println("----------"+"玩家"+player2.playerName+"获胜"+"-----------");
        }
        System.out.println("玩家各自的手牌为:");
        System.out.println("玩家"+player1.playerName+":"+player1.playerCardsList.get(0).colorNum+
                " "+player1.playerCardsList.get(1).colorNum);
        System.out.println("玩家"+player2.playerName+":"+player2.playerCardsList.get(0).colorNum+
                " "+player2.playerCardsList.get(1).colorNum);
    }
}

Card类

package pokergame;

/**
 * Created by Administrator on 2017/4/5.
 * 定义一个卡牌类
 */
public class Card implements Comparable<Card>{
    public String colorNum;
    public String colorSum;
    public Card(String colorNum,String colorSum) {
        this.colorSum = colorSum;
        this.colorNum=colorNum;
        }

    @Override
    public int compareTo(Card o) {
        return this.colorSum.compareTo(o.colorSum);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Card)) return false;

        Card card = (Card) o;

        return colorNum.equals(card.colorNum);
    }
}

游戏玩家类

import java.util.*;

/**
 * Created by Administrator on 2017/4/4.
 */
public class Player{
    public Integer playerId;
    public String playerName;
    public List<Card> playerCardsList;
    public Player(Integer playerId,String playerName){
        this.playerId=playerId;
        this.playerName=playerName;
        List<Card> playerCardsList=new ArrayList<Card>();
    }
    public Player(){
    }
}

创建了整副牌的List类

import java.util.*;

/**
 * Created by Administrator on 2017/4/5.
 * 创建一整副卡牌的集合,由于要打乱卡牌的顺序
 * 可以创建四个不重复的随机数,分别取牌发给玩家
 */
public class PokerCardsList {
    public List<Card> pokerCardsList;
    public PokerCardsList() {
        this.pokerCardsList = new ArrayList<Card>();
    }
    /**
     * 往pokerCardsList中添加卡牌的方法
     */
    public void pokerCardsListAdd() {
        final String[] Color = {"黑桃", "红桃", "梅花", "方片"};
        final String[] Num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        final String[] colorTwo={"D","C","A","B"};
        final int[] sum={93,94,10,20,30,40,50,60,70,80,90,91,92};
        for(int i = 0;i<4;i++){
            for(int j=0;j<13;j++){
                Card car=new Card(Color[i]+Num[j],sum[j]+colorTwo[i]);
                pokerCardsList.add(car);
            }
        }
        for(Card car:pokerCardsList){
            System.out.print(car.colorNum+" ");
        }
    }
    /**
     * 抽牌的方法
     * 输出四个随机数
     */
    public int[] randomTest(){
        Random num = new Random();
        Set testSet=new HashSet();
        for (int i=0,j=51;i<4&&j>47;j--){
            testSet.add(num.nextInt(j));
            if(testSet.size()>i){
                i++;
            }else{
                continue;
            }
        }
        //定义一个数组接收这四个数
        int[] k=new int[4];
        int i=0;
        for(Object x:testSet) {
            int y=(int)x;
            k[i]=y;
            i++;
        }
        return k;
    }
}

玩家信息保存的类

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

/**
 * Created by Administrator on 2017/4/5.
 */
public class PokerPlayerList {
    //创建一个List集合存放两名玩家的信息
    public List<Player> playerList;
    public PokerPlayerList(){
        this.playerList=new ArrayList<Player>();
    }
    /**
     * 存放玩家信息
     */
    public void playerIdNameAdd() {
        while(true) {
            try {
                Scanner sc=new Scanner(System.in);
                System.out.println("输入ID:");
                Integer playerid = sc.nextInt();
                System.out.println("输入姓名:");
                String playername = sc.next();
                Player p = new Player(playerid, playername);
                this.playerList.add(p);
                break;
            } catch (InputMismatchException e) {
                System.out.println("您输入的ID非数字");
                continue;
            }
        }
    }
}
点击查看更多内容
8人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消