手记

简易扑克游戏

入口:
Play.java
Model类
Card.java
Player.java
工具方法:
CompareHandCards.java(比较手牌,返回最终胜利玩家)
CreateCardList.java(初始创建扑克牌集合)
CreatePlayer.java(创建玩家)
Shuffle.java(洗牌)


Play.java

package com.poker.Launcher;

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

import com.poker.model.Card;
import com.poker.model.Player;
import com.poker.util.CompareHandCards;
import com.poker.util.CreateCardList;
import com.poker.util.CreatePlayer;
import com.poker.util.Shuffle;

public class Play {
    public static void main(String[] args) {
        System.out.println("**************创建扑克牌**************");
        //创建扑克牌集合
        CreateCardList createCardList = new CreateCardList();
        List<Card> cardList = new ArrayList<Card>();
        cardList = createCardList.createCardList();
        //输出扑克牌集合
        System.out.println("扑克牌创建成功:");
        System.out.print("[ ");
        for(Card cl:cardList) {
            System.out.print(cl.getSuit()+cl.getFigure()+" ");
        }
        System.out.print("]\n");
        System.out.println("***************开始洗牌***************");
        //开始洗牌
        Shuffle shuffleCard = new Shuffle();
        List<Card> newCardList = new ArrayList<Card>();
        newCardList = shuffleCard.shufferCardList(cardList);
        //测试打印洗牌后的顺序
        System.out.print("[ ");
        for(Card ncl:newCardList) {
            System.out.print(ncl.getSuit()+ncl.getFigure()+" ");
        }
        System.out.print("]\n");
        System.out.println("***************洗牌结束***************");
        System.out.println("***************创建玩家***************");
        //创建玩家开始
        CreatePlayer createPlayer = new CreatePlayer();
        List<Player> players = new ArrayList<Player>();
        players = createPlayer.createPlayer();
        //创建玩家完成,输出玩家姓名
        for(Player player:players) {
            System.out.println("***************欢迎玩家"+player.getName()+"***************");
        }
        int cardListIndex = 0;
        for(int i=0; i<2; i++) {
            /*普通遍历
                for(int j=0;j<2;j++) {

                    System.out.println("***************玩家"+players.get(j).getName()+"取牌***************");
                    players.get(j).getHandCards().add(newCardList.get(cardListIndex));
                    System.out.println("取牌"+newCardList.get(cardListIndex).getSuit()+newCardList.get(cardListIndex).getFigure());
                    cardListIndex++;
                }
            */
            //foreach遍历
            for(Player player:players) {
                System.out.println("***************玩家"+player.getName()+"取牌***************");
                player.getHandCards().add(newCardList.get(cardListIndex));
                System.out.println("取牌"+newCardList.get(cardListIndex).getSuit()+newCardList.get(cardListIndex).getFigure());
                cardListIndex++;
            }
        }   
        System.out.println("***************发牌结束***************");
        System.out.println("***************开始游戏 ***************");
        //开始对比手牌
        CompareHandCards chc = new CompareHandCards();
        Player winner = chc.compareHandCards(players);
        System.out.println(winner.getName()+"获胜");
        //输出手牌结果
        System.out.println("玩家各自手牌时为:");
        for(int i=0; i<2; i++) {
            System.out.print(players.get(i).getName()+":[");
            for(int j=0; j<players.get(i).getHandCards().size(); j++) {
                System.out.print(players.get(i).getHandCards().get(j).getSuit()+players.get(i).getHandCards().get(j).getFigure()+" ");
            }
            System.out.print("]\n");
        }

    }
}

Card.java

package com.poker.model;

public class Card implements Comparable<Card>{
    private String Suit;
    private String Figure;
    public String getSuit() {
        return Suit;
    }
    public void setSuit(String suit) {
        Suit = suit;
    }
    public String getFigure() {
        return Figure;
    }
    public void setFigure(String figure) {
        Figure = figure;
    }

    public Card() {

    }

    public Card(String Suit,String Figure) {
        this.setSuit(Suit); 
        this.setFigure(Figure);
    }

    @Override
    /**
     * 通过对比字符串下标数值来判断大小
     * 重写compareTo方法
     * 分别建立花色字符串数组suitList 和 点数字符串数组 figureList,顺序由小到大建立。
     * 对比当前对象和参数对象的点数和花色得出return
     */
    public int compareTo(Card c) {
        String[] suitList = new String[] {"方块","梅花","红桃","黑桃"};
        String[] figureList = new String[] {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        // TODO Auto-generated method stub
        Card cm = new Card();
        //获得当前对象和参数对象的点数下标
        int comp1 = cm.indexOfArray(this.getFigure(),figureList);
        int comp2 = cm.indexOfArray(c.getFigure(),figureList);
        //获得当前对象和参数对象的花色下标
        int comp3 = cm.indexOfArray(this.getSuit(), suitList);
        int comp4 = cm.indexOfArray(c.getSuit(), suitList);
        //判断当点数下标一样时对比花色下标
        if(comp1 == comp2) {
            if(comp3 > comp4) {
                return 1;
            }else {
                return -1;
            }
        }else if(comp1 > comp2) {
            return 1;
        }else if(comp1 < comp2) {
            return -1;
        }
        return 0;
    }

    //建立获取下标的方法
    public int indexOfArray(String str,String[] strArray) {
        for(int i=0;i<strArray.length;i++) {
            if(strArray[i].equals(str)) {
                return i;
            }
        }
        return -1;
    }
}

Play.java

package com.poker.model;

import java.util.List;

public class Player {
    private int Id;
    private String Name;
    private List<Card> handCards;
    public int getId() {
        return Id;
    }
    public void setId(int id) {
        Id = id;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public List<Card> getHandCards() {
        return handCards;
    }
    public void setHandCards(List<Card> handCards) {
        this.handCards = handCards;
    }

    public Player() {

    }
    public Player(int id,String name,List<Card> handCards) {
        this.setId(id);
        this.setName(name);
        this.setHandCards(handCards);
    }
}

CompareHandCards.java

package com.poker.util;

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

import com.poker.model.Card;
import com.poker.model.Player;

public class CompareHandCards {
    /** 
     * 调用Card重写的compareTo方法重新排列手牌集合
     * 首先将各自手牌进行排序;
     * 得出最后一张卡牌为当前手牌最大卡牌 player.getHandCards().get(size-1);
     * 将各个玩家各自最大的卡牌重新组合一个新的集合compareList;
     * 再次将新得出的集合排序获得最后一个元素为所有手牌中最大的卡牌;
     * 查看哪个玩家手上含有最大手牌;
     */

    public Player compareHandCards(List<Player> players) {
        //先自行对比输出各自最大手牌
        List<Card> compareList = new ArrayList<Card>();
        for (Player player:players) {
            //调用Card重写的compare方法重新排列手牌集合
            Collections.sort(player.getHandCards());
            int size = player.getHandCards().size();
            System.out.println(player.getName()+"最大的手牌为"+player.getHandCards().get(size-1).getSuit()+player.getHandCards().get(size-1).getFigure());
            //将当前玩家的最大手牌放入新的集合中
            compareList.add(player.getHandCards().get(size-1));
        };
        //进行第二次排序,得出所有手牌中最大的卡牌
        Collections.sort(compareList);
        int maxIndex = compareList.size() - 1;
        Card maxCard = compareList.get(maxIndex);
        System.out.println(maxCard.getSuit()+maxCard.getFigure());
        //查看哪个玩家手牌上包含这个最大卡牌,输出最终获胜玩家
        if(players.get(0).getHandCards().contains(maxCard)) {
            return players.get(0);
        }else {
            return players.get(1);
        }
    }
}

CreateCardList.java

package com.poker.util;

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

import com.poker.model.Card;

public class CreateCardList {
    String[] SuitList = new String[] {"黑桃","红桃","梅花","方块"};
    String[] FigureList = new String[] {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    /**
     * 循环两个字符串数组内的元素,创建新的扑克集合
     * @return
     */
    public List<Card> createCardList(){
        List<Card> cardList = new ArrayList<Card>();
        for(int i=0;i<SuitList.length;i++) {
            for(int j=0;j<FigureList.length;j++) {
                cardList.add(new Card(SuitList[i],FigureList[j]));
            }
        }
        return cardList;
    }
}

CreatePlayer.java

package com.poker.util;

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

import com.poker.model.Card;
import com.poker.model.Player;

public class CreatePlayer {
    Scanner scanner;
    public CreatePlayer() {
        scanner = new Scanner(System.in);
    }

    public List<Player> createPlayer() {
        List<Player> players = new ArrayList<Player>();
        String Name;
        int Id;
        for(int i=0; i<2; i++) {
            List<Card> handCards = new ArrayList<Card>();
            System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名:");
            System.out.println("输入ID:");
            while(true) {
                Id = 0;
                try {
                    Id = scanner.nextInt();
                    break;
                } catch (InputMismatchException e) {
                    // TODO Auto-generated catch block
                    //参考某例子,吞掉输入流的错误数据,否则会无限循环
                    scanner.next();
                    System.out.println("请输入整形ID:");
                    continue;
                }
            }
            System.out.println("输入姓名:");
            Name = scanner.next();
            players.add(new Player(Id,Name,handCards));
            System.out.println("创建的玩家ID为"+players.get(i).getId()+"姓名为"+players.get(i).getName());
        }
        return players;
    }
}

Shuffle.java

package com.poker.util;

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

import com.poker.model.Card;
/**
 * 输入集合carList
 * 通过更改下标打乱顺序后输出新的数列
 * @author Administrator
 *
 */
public class Shuffle {
    public List<Card> shufferCardList(List<Card> cardList) {
        //获取cardList 扑克牌的数量
        int cardListLen = cardList.size();
        //创建一个整数包装类集合
        List<Integer> randomList = new ArrayList<Integer>();
        //存放随机数
        Integer randomNum;
        //创建循环为集合randomList赋值
        for(int i=0;i<cardListLen;i++) {
            //生成一个范围在0-cardList-1 以内的不重复随机数
            do {
                Random random = new Random();
                randomNum = random.nextInt(cardListLen);
            }while(randomList.contains(randomNum));
            randomList.add(randomNum);
        }
        //创建更改完顺序新集合返回
        List<Card> newCardList = new ArrayList<Card>();
        //根据生成的随机数集合内的数字改变原扑克牌集合的下标,从而打乱顺序
        for(int k=0;k<cardListLen;k++) {
            newCardList.add(k,cardList.get(randomList.get(k).intValue()));
        }
        //返回
        return newCardList;
    }
}
1人推荐
随时随地看视频
慕课网APP