手记

Java入门 第三季 综合实战 简易扑克牌游戏

没有加异常处理,其余均满足。

//Player.java

package com.imooc;

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

public class Player implements Comparable<Player> {
    private int playerId;
    private String playerName;
    private List<Poker> playerPokers;

    public Player(){
        playerPokers=new ArrayList<Poker>();
    }

    public Player(int id,String name){
        playerId=id;
        playerName=name;
        playerPokers=new ArrayList<Poker>();
    }

    public void getPlayerPoker(Poker p){
        playerPokers.add(p);
    }

    public void initPlayerPokers(){
        playerPokers=new ArrayList<Poker>();
    }

    @Override
    public int compareTo(Player o) {
        // TODO Auto-generated method stub
        if(this.playerPokers.isEmpty() || o.playerPokers.isEmpty() || this.playerPokers.size()!=o.playerPokers.size()){
            System.out.println("----牌组有误----");
            System.out.println("两位玩家的牌组各有:"+this.playerPokers.size()+","+o.playerPokers.size()+"个");
            return 0;
        }

        List<Poker> pp1=new ArrayList<Poker>(playerPokers);
        List<Poker> pp2=new ArrayList<Poker>(o.playerPokers);
        Collections.sort(pp1);
        Collections.sort(pp2);

        for(int i=pp1.size()-1;i>=0;i--){
            int t=pp1.get(i).compareTo(pp2.get(i));
            if(t!=0)
                return t;
        }

        return 0;
    }

    public String getPlayerName() {
        return playerName;
    }

    public Poker getMaxPoker(){
        return Collections.max(playerPokers);
    }

    public List<Poker> getPlayerPokers() {
        return playerPokers;
    }

}

//Poker.java

package com.imooc;

public class Poker implements Comparable<Poker>{
    private static String[] pokerSuits={"方片","梅花","红桃","黑桃"};
    private static String[] pokerNumbers={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

    private int pokerSuitId;
    private int pokerNumberId; 

    public Poker(){
        pokerSuitId=0;
        pokerNumberId=0;
    }

    public Poker(int suitId,int numberId){
        pokerSuitId=suitId;
        pokerNumberId=numberId;
    }

    public int getPokerSuitId() {
        return pokerSuitId;
    }

    public int getPokerNumberId() {
        return pokerNumberId;
    }

    @Override
    public String toString() {
        return pokerSuits[pokerSuitId]+pokerNumbers[pokerNumberId];
    }

    @Override
    public int compareTo(Poker o) {
        // TODO Auto-generated method stub
        if(this.pokerNumberId>o.pokerNumberId)
            return 1;
        else if(this.pokerNumberId<o.pokerNumberId)
            return -1;
        else if(this.pokerSuitId>o.pokerSuitId)
            return 1;
        else if(this.pokerSuitId<o.pokerSuitId)
            return -1;
        else
            return 0;
    }

}

//PokerGameCommand.java

package com.imooc;

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

public class PokerGameCommand {

    static private List<Poker> pokers;
    static private List<Player> players;
    static private Scanner console=new Scanner(System.in);
    static private int pokersFinger;

    //初始化产生一副扑克牌
    public void initPokers(){
        pokers=new ArrayList();
        for(int i=0;i<4;i++){
            for(int j=0;j<13;j++){
                pokers.add(new Poker(i,j));
            }
        }
        pokersFinger=0;     
    }

    //洗牌
    public void shufflePokers(){
        if(pokers.isEmpty()){
            this.initPokers();
        }
        Collections.shuffle(pokers);
    }

    //取一张牌
    private Poker getPoker(){
        if(pokers.isEmpty()){
            this.initPokers();
            this.shufflePokers();
        }
        if(pokersFinger==pokers.size()){
            this.shufflePokers();
            pokersFinger=0;
        }
        Poker p=pokers.get(pokersFinger);
        pokersFinger++;
        return p;
    }

    //创建玩家
    public void initPlayers(int number){
        players=new ArrayList<Player>();
        for(int i=0;i<number;i++){
            Player p=new Player();
            do{
                System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名");
                System.out.println("输入ID:");
                int id=console.nextInt();
                System.out.println("输入姓名:");
                String name=console.next();
                p=new Player(id,name);
            }while(players.contains(p));
            players.add(p);
        }
    }

    //发牌
    public void dealPoker(int number){
        for(int i=0;i<number;i++){
            for(Player p:players){
                System.out.println("玩家:"+p.getPlayerName()+"-拿牌");
                p.getPlayerPoker(this.getPoker());
            }
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PokerGameCommand pgc=new PokerGameCommand();
        System.out.println("-----------创建扑克牌-----------------");
        pgc.initPokers();
        System.out.println("-----------扑克牌创建成功!--------------");
        System.out.println("为:"+pokers);
        System.out.println("-----------开始洗牌-------------------");
        pgc.shufflePokers();
        System.out.println("为:"+pokers);
        System.out.println("-----------洗牌结束!------------------");
        System.out.println("-----------创建玩家-------------------");
        pgc.initPlayers(2);
        for(int i=0;i<players.size();i++){
            System.out.println("----欢迎玩家:"+players.get(i).getPlayerName());
        }
        System.out.println("-----------开始发牌-------------------");
        pgc.dealPoker(2);
        System.out.println("-----------发牌结束-------------------");
        System.out.println("-----------开始游戏-------------------");
        Player winnerPlayer=players.get(0);
        for(Player p:players){
            System.out.println("玩家:"+p.getPlayerName()+"最大的手牌为:"+p.getMaxPoker());
            if(p.compareTo(winnerPlayer)>0)
                winnerPlayer=p;
        }
        System.out.println("-----------玩家:"+winnerPlayer.getPlayerName()+" 获胜----------------");
        for(Player p:players){
            System.out.println(p.getPlayerName()+":"+p.getPlayerPokers().toString());
        }

    }

}
0人推荐
随时随地看视频
慕课网APP