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

简易扑克牌游戏打乱洗牌顺序发牌

慕粉3365094
关注TA
已关注
手记 1
粉丝 0
获赞 29

Card

import java.util.Arrays;
import java.util.List;

public class Card implements Comparable<Card> {
    public String cColor;
    public String point;
    public static final List<String> cColors = Arrays.asList("方片", "梅花", "红桃", "黑桃");

    public Card(String cColor,String point){
        this.cColor=cColor;
        this.point=point;
    }

    @Override
    public String toString() {
        return cColor + point;
    }

    @Override
    public int compareTo(Card o) {
        // TODO 自动生成的方法存根
        if(o.point.equals(this.point))
            return Integer.valueOf(cColors.indexOf(cColor)).compareTo(Integer.valueOf(cColors.indexOf(o.cColor)));
        else
            return this.point.compareTo(o.point);
    }

}

Player

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

public class Player {
    public int id;
    public String name;
    public List<Card> twoCards;

    public Player(int id,String name){
        this.id = id;
        this.name = name;
        twoCards = new ArrayList<Card>();
    }

}

Game

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

public class Game {

    private final String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    private final String[] cColors = { "黑桃", "红桃", "梅花", "方片" };
    private Scanner console;
    private final int num = 2; //玩家的数量
    private final int cardNum = 2;//每人2张牌
    private List<Card> cardList;
    private List<Player> playerList;

    private Game(){
        cardList = new ArrayList<Card>();
        playerList = new ArrayList<Player>();
    }
    /**
     * 初始化cardList并且打乱
     */
    private void cards(){
        Card newCard;
        System.out.println("---------------创建扑克牌-----------------");
        for(int i = 0;i < cColors.length;i++){
            for(int j = 0;j < points.length;j++){
                newCard = new Card(cColors[i],points[j]);
//              System.out.println(newCard.toString());
                cardList.add(newCard);
            }
        }
        System.out.println("---------------扑克牌创建成功-----------------");
            System.out.println(cardList.toString());    
        System.out.println("---------------开始洗牌-----------------");
        Collections.shuffle(cardList);
        System.out.println("---------------洗牌结束-----------------");
//      for(Card card:cardList){
//          System.out.println(card.toString());    
//      }
    }

    /**
     * 创建玩家
     */
    public void twoPlays(){
        for(int i = 0;i < num;i++){
            System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名:");
            console = new Scanner(System.in);
            Integer newID;
            while(true){
                System.out.println("输入ID:");
                try{
                    newID = console.nextInt();
                    break;
                }catch(Exception e){
                    console.next();
                    System.out.println("输入错误,请输入正整数!!");
                    continue;
                }
            }
            System.out.println("输入姓名:");
            String newName = console.next();
            Player newPlayer = new Player(newID,newName);
            playerList.add(newPlayer);
        }
        for(Player player:playerList){
            System.out.println("----欢迎玩家:"+player.name);
        }
    }
    /**
     * 开始发放
     */
    public void playerHasCards(){
        System.out.println("---------------开始发牌-----------------");
        for(int i = 0;i < cardNum*2;i++){
            if(i%2 == 0){
                playerList.get(0).twoCards.add(cardList.get(i));
                System.out.println("----玩家:"+playerList.get(0).name+"-拿牌");
                //System.out.println(playerList.get(0).twoCards);
            }else{
                playerList.get(1).twoCards.add(cardList.get(i));
                System.out.println("----玩家:"+playerList.get(1).name+"-拿牌");
                //System.out.println(playerList.get(1).twoCards);
            }
        }
        System.out.println("---------------发牌结束!-----------------");
    }

    public void startGame(){
        System.out.println("---------------开始游戏!-----------------");    
        Card[] cards = new Card[cardNum];
        for(int i = 0;i < num;i++){
            if(playerList.get(i).twoCards.get(0).compareTo(playerList.get(i).twoCards.get(1)) > 0){
                System.out.println("玩家:"+playerList.get(i).name+"最大的手牌为:"+playerList.get(i).twoCards.get(0));
                cards[i] = playerList.get(i).twoCards.get(0);
            }else{
                System.out.println("玩家:"+playerList.get(i).name+"最大的手牌为:"+playerList.get(i).twoCards.get(1));
                cards[i] = playerList.get(i).twoCards.get(1);
            }
        }
        if(cards[0].compareTo(cards[1]) > 0){
            System.out.println("----------玩家:"+playerList.get(0).name+"获胜!----------");
        }else{
            System.out.println("----------玩家:"+playerList.get(1).name+"获胜!----------");
        }
        System.out.println("玩家各自的手牌为:");
        System.out.println(playerList.get(0).name+":"+playerList.get(0).twoCards.toString());
        System.out.println(playerList.get(1).name+":"+playerList.get(1).twoCards.toString());
    }

    public static void main(String[] args){
        Game game = new Game();
        game.cards();
        game.twoPlays();
        game.playerHasCards();
        game.startGame();
    }
}
打开App,阅读手记
10人推荐
发表评论
随时随地看视频慕课网APP