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

<JAVA>简易扑克牌游戏

标签:
Java

参考了一些慕友的代码,感觉写的有些繁琐,希望大家指正。

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

public class Player {
    private String name;
    private int id;
    public List<Cards> handcards=new ArrayList<Cards>();

    public Player(String name,int id){
        this.name=name;
        this.id=id;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "玩家 : " + name ;
    }

}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Cards implements Comparable{
    private String color;
    private String value;
    private static final List<String> colors= Arrays.asList("方片","梅花","黑桃","红桃");
    private static final List<String> values=Arrays.asList("2","3","4","5","6","7","8","9","10","J","Q","K","A");

    public Cards(String color,String value){
        this.color=color;
        this.value=value;
    }

    public Cards(){
        this.color="";
        this.value="";
    }

    public String getColor() {
        return color;
    }

    public String getValue() {
        return value;
    }

    public List<Cards> generateCards(){
        List<Cards> poker=new ArrayList<Cards>();
        for(int i=0;i<3;i++){
            String color=colors.get(i);
            for(int j=0;j<13;j++){
                String value=values.get(j);
                poker.add(new Cards(color,value));
            }

        }
        return poker;

    }

    @Override
    public String toString() {
        return color +  value ;
    }

    public int compareTo(Cards c) {
        // TODO Auto-generated method stub
        if(values.indexOf(this.value)<values.indexOf(c.value))
            return -1;
        else if(values.indexOf(this.value)>values.indexOf(c.value))
            return 1;
        else{
            if(colors.indexOf(this.color)<colors.indexOf(c.color))
                return -1;
            else if(values.indexOf(this.value)>values.indexOf(c.value))
                return 1;
            else
                return 0;
        }
    }

    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        return 0;
    }

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

public class CardsGame {
    private List<Player> player;
    private List<Cards> cards;
    Scanner input=new Scanner(System.in);

    public CardsGame(){
        System.out.println("-----------创建扑克牌-----------");
        cards=new Cards().generateCards();
        System.out.println("-----------扑克牌创建成功-----------");
        showCards(cards);
        System.out.println("-----------开始洗牌...-----------");
        Collections.shuffle(cards);
        System.out.println("-----------洗牌结束!-----------");
        System.out.println("-----------创建玩家...-----------");
        player=createPlayer();

        for(int i=0;i<2;i++){
            System.out.println("-------欢迎"+player.get(i)+"--------");
        }

        System.out.println("-----------开始发牌-----------");
        sendCards(player,cards);
        System.out.println("-----------发牌结束!-----------");
        System.out.println("-----------开始游戏...-----------");
        checkBiggest(player);
        System.out.println("玩家各自的手牌为:");    
        for(int i=0;i<2;i++){
        System.out.println(player.get(0)+":");
        showCards(player.get(0).handcards);
        }
    }

    private void checkBiggest(List<Player> player) {
        // TODO Auto-generated method stub
        List<Cards> biggestCard=new ArrayList<Cards>();
        for(int i=0;i<2;i++){
            if(player.get(i).handcards.get(0).compareTo(player.get(i).handcards.get(1))>0){
                biggestCard.add(player.get(i).handcards.get(0));
                System.out.println(player.get(i)+"最大的手牌是:"+biggestCard.get(i));
            }   
            else{
                biggestCard.add(player.get(i).handcards.get(1));
                System.out.println(player.get(i)+"最大的手牌是:"+biggestCard.get(i));
            }       
        }

            if(biggestCard.get(0).compareTo(biggestCard.get(1))>0){
                System.out.println("-----------"+player.get(0)+"获胜!--------------");
            }
            else if(biggestCard.get(0).compareTo(biggestCard.get(1))<0){
                System.out.println("-----------"+player.get(1)+"获胜!--------------");
            }
            else
                System.out.println("-----------平局--------------");
    }

    private void sendCards(List<Player> player, List<Cards> cards) {
        // TODO Auto-generated method stub
        for(int i=0;i<4;i++){
            System.out.println("--------"+player.get(i%2)+"-拿牌");
            player.get(i%2).handcards.add(cards.get(i));    
        }
    }

    private List<Player> createPlayer() {
        // TODO Auto-generated method stub
        List<Player> player=new ArrayList<Player>();
        int id;
        String name;

        for(int i=1;i<3;){
            System.out.println("请输入第"+i+"玩家的ID和姓名");
            System.out.println("输入ID:");
            try{
                id=input.nextInt();
            }catch(Exception e){
                System.out.println("请输入正整数:");
                input.next();
                continue;
            }

            System.out.println("输入姓名:");
            name=input.next();

            player.add(new Player(name,id));

            i++;
        }

        return player;
    }

    private void showCards(List<Cards> cards) {
        // TODO Auto-generated method stub
        System.out.println(cards);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
      CardsGame cg=new CardsGame();
    }

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

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消