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

写的简单的java第三季的课后作业

标签:
Java

1、card类,继承了comparable接口

package cardGame_simple;

public class card implements Comparable<card>{

    private String card_num;
    private String card_type;
    private double card_indent;

    public card(){}
    /**
     * 创建一般牌*/
    public card(String card_num,String card_type) throws CardsException{
        //生成卡牌的数值
        int tempInt = Integer.parseInt(card_num);
        int tempNum;

        if(tempInt<1  tempInt>13)
            throw new CardsException("输入了不合法的卡片数值,卡片数值应该在1~13之间");
        else {
            switch(tempInt){
            case 11:this.card_num="J";
            break;
            case 12:this.card_num="Q";
            break;
            case 13:this.card_num="K";
            break;
            default:this.card_num=tempInt+"";
            }
        }

        if(card_type.equals("红桃")){
            this.card_type = card_type;
            tempNum = 0;
        } else if(card_type.equals("黑桃")){
            this.card_type = card_type;
            tempNum = 13;
        } else if(card_type.equals("梅花")){
            this.card_type = card_type;
            tempNum = 26;
        } else if(card_type.equals("方块")){
            this.card_type = card_type;
            tempNum = 39;
        }

        else throw new CardsException("不合法的花色,本构造器不允许大王小王");

        this.card_indent = tempInt+tempNum;
    }

    public String getCard_num() {
        return card_num;
    }
    public void setCard_num(String card_num) {
        this.card_num = card_num;
    }
    public String getCard_type() {
        return card_type;
    }
    public void setCard_type(String card_type) {
        this.card_type = card_type;
    }
    public double getCard_indent() {
        return card_indent;
    }
    public void setCard_indent(double card_indent) {
        this.card_indent = card_indent;
    }

    @Override

    public int hashCode() {
        final int prime = 31;
        int result = 1;
        long temp;
        temp = Double.doubleToLongBits(card_indent);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        card other = (card) obj;
        if (Double.doubleToLongBits(card_indent) != Double.doubleToLongBits(other.card_indent))
            return false;
        return true;
    }

    public int compareTo(card card) {
        if(this.card_indent>card.card_indent) return 1;
        else if(this.card_indent<card.card_indent) return -1;
        return 0;
    }

    public String toString() {
        return " "+card_type + card_num+" " ;
    }

}

2、实践一下自定义异常类,并且在上文的card类的构造方法上抛出

package cardGame_simple;

public class CardsException extends Exception{
    public CardsException(){}
    public CardsException(String message){super();}
}

创建的player类,countScore()方法是后面发现要比较大小凑得..

package cardGame_simple;

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

public class player {
    private String name;
    private List<card> deliverdCard = new ArrayList<card>();
    public player(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }
    public List<card> getDeliverdCard() {
        return deliverdCard;
    }

    public void pushCard(card card){
        deliverdCard.add(card);
    }

    public void pushCards(List<card> cards){
        deliverdCard.addAll(cards);
    }

    public double countScore(){
        return deliverdCard.get(deliverdCard.size()-1).getCard_indent()+
                deliverdCard.get(deliverdCard.size()-2).getCard_indent();
    }

    @Override
    public String toString() {
        return name;
    }

}

3、游戏类

package cardGame_simple;

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

public class cardGame{
    public static int ALLNUM = 13*4;
    private List<card> RandomCards;
    private List<card> BasicCardSet;
    private List<player> playerList;

    public cardGame(){
    }

    //主逻辑
    public void newRound(Scanner console) throws CardsException{
        genaratePlayer(console);
        genarateCards();
        while(true){
            deliverCard(console);
            compareCard();
            System.out.println("是否继续:true/false");
            String temp = console.next();
            if(temp.equals("true")) continue;
            else break;
        }

    }
    //生成玩家
    private void genaratePlayer(Scanner console){
        playerList = new ArrayList<player>();
        System.out.println("现在开始加入玩家(建议加入两个以上玩家)");

        while(true){
            System.out.println("请输入玩家姓名:");
            String temp = console.next();
            player tempP = new player(temp);
            playerList.add(tempP);
            System.out.println("是否继续:true/false");
            temp = console.next();
            if(temp.equals("true")) continue;
            else break;
        }

        //加入空缺的玩家,以robot代替
        int j = 1;
            while(playerList.size()<2){
                player tempRob = new player("robot"+j);
                playerList.add(tempRob);
                j++;
            }

        System.out.println("加入玩家完毕!\n");
        printPlayerList();
    }

    //发牌,如果牌池无牌了则停止
    private void deliverCard(Scanner console) throws CardsException{
        if(RandomCards.size()<=playerList.size()*2) isRestartRound(console);
        else{
            for (player temp : playerList) {
                temp.pushCard(RandomCards.get(0));
                temp.pushCard(RandomCards.get(1));
                RandomCards.remove(0);
                RandomCards.remove(1);
            }
        }

    }

    //决定胜负
    private void compareCard(){
        System.out.println("现在公布手牌");
        player MaxOne = playerList.get(1);
        for (player tempP:playerList) {
            if(tempP.countScore()>MaxOne.countScore()) MaxOne = tempP;
            System.out.println(tempP.toString()+":");
            printCardsList(tempP.getDeliverdCard(), ALLNUM);
        }
        System.out.println("赢家是:"+MaxOne.toString());
    }

    //停止
    private void isRestartRound(Scanner console) throws CardsException {
        System.out.println("牌池已经干枯,是否开始新的一局游戏?");
        String tempStr = console.next();
        if(tempStr.equals("true")) this.newRound(console);
        else System.exit(0);
    }

    private void genarateCards() throws CardsException{
        BasicCardSet = new ArrayList<card>();
        RandomCards = new ArrayList<card>();
        for(int i=1 ; i<=13 ;i++){
            BasicCardSet.add(new card(i+"","红桃"));
            BasicCardSet.add(new card(i+"","黑桃"));
            BasicCardSet.add(new card(i+"","梅花"));
            BasicCardSet.add(new card(i+"","方块"));
        }
        BasicCardSet.sort(null);

        System.out.println("现有牌组如下\n");
        printCardsList(BasicCardSet,ALLNUM/4);
        System.out.println("洗牌");

        //生成随机的卡片组合RandomCards
        int randomInt;
        card temp;
        for(int i=1 ;i<=ALLNUM; i++){
            do{
                randomInt = (int)Math.floor(ALLNUM*Math.random());//使用floor来避免数组越界的异常出现
                temp = BasicCardSet.get(randomInt);
            }while(RandomCards.contains(temp));
            RandomCards.add(temp);
        }

    }

    private void printPlayerList(){
        System.out.println("现有以下玩家");
        StringBuilder tempStr = new StringBuilder();
        for (player temp : playerList) tempStr.append(temp.toString()+"\n");
        System.out.println(tempStr.toString());
    }

    private void printCardsList(List<card> cards,int lineWidth){
        StringBuilder tempStr = new StringBuilder();
        //通过i控制每行打印多少卡片
        int i=1;
        for (card card : cards){
            if(i>lineWidth){
                tempStr.append("\n");
                i=1;
            }
            tempStr.append(card.toString());
            i++;
        }  
        System.out.println(tempStr.toString());
    }

//主函数   

    public static void main(String[] args) throws CardsException {
        Scanner console = new Scanner(System.in);
        cardGame new1 = new cardGame();
        new1.newRound(console);
    }

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

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消