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

JAVA入门第三季项目(简易扑克牌游戏)

标签:
Java

Player类:

package com.cards;

public class Player {

    private String id;
    private String name;
    private String[] cardsInHand;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String[] getCardsInHand() {
        return cardsInHand;
    }
    public void setCardsInHand(String[] cardsInHand) {
        this.cardsInHand = cardsInHand;
    }

}

Cards类:

package com.cards;

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

public class Cards {

    static List<String> allCards=new ArrayList<String>();
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("创建扑克牌");
        allCards.add("方片A");
        allCards.add("梅花A");
        allCards.add("红桃A");
        allCards.add("黑桃A");
        for(int i=2;i<11;i++){
            allCards.add("方片"+i);
            allCards.add("梅花"+i);
            allCards.add("红桃"+i);
            allCards.add("黑桃"+i);
        }
        allCards.add("方片J");
        allCards.add("梅花J");
        allCards.add("红桃J");
        allCards.add("黑桃J");
        allCards.add("方片Q");
        allCards.add("梅花Q");
        allCards.add("红桃Q");
        allCards.add("黑桃Q");
        allCards.add("方片K");
        allCards.add("梅花K");
        allCards.add("红桃K");
        allCards.add("黑桃K");

        String str=allCards.toString();
        System.out.println("扑克牌创建成功:"+str);

        System.out.println("开始洗牌");
        List<String> allCardsDisorder=new ArrayList<String>();
        allCardsDisorder.addAll(allCards);
        Collections.shuffle(allCardsDisorder);
        System.out.println("洗牌结束");
        System.out.println("创建玩家");

        Player player1=new Player();
        Player player2=new Player();

        while(true){
            try{
                System.out.println("请输入第一位玩家的ID:");
                Scanner input=new Scanner(System.in);
                int id=input.nextInt();
                player1.setId(id+"");
                System.out.println("请输入第一位玩家的名字:");
                String name=input.next();
                player1.setName(name);
                break;
            }catch(InputMismatchException e){
                System.out.println("应输入数字ID!");
            }

        }
        while(true){
            try{
                System.out.println("请输入第二位玩家的ID:");
                Scanner input=new Scanner(System.in);
                int id=input.nextInt();
                player2.setId(id+"");
                System.out.println("请输入第二位玩家的名字:");
                String name=input.next();
                player2.setName(name);
                break;
            }catch(InputMismatchException e){
                System.out.println("应输入数字ID!");
            }

        }
        System.out.println("欢迎玩家:"+player1.getName()+"和"+player2.getName());
        System.out.println("玩家"+player1.getName()+"拿牌");
        System.out.println("玩家"+player2.getName()+"拿牌");
        System.out.println("玩家"+player1.getName()+"拿牌");
        System.out.println("玩家"+player2.getName()+"拿牌");
        player1.setCardsInHand(new String[] {allCardsDisorder.get(0),allCardsDisorder.get(2)});
        player2.setCardsInHand(new String[] {allCardsDisorder.get(1),allCardsDisorder.get(3)});
        System.out.println("发牌结束");
        System.out.println("开始游戏");
        Cards game=new Cards();
        String p1Card1=player1.getCardsInHand()[0];
        String p1Card2=player1.getCardsInHand()[1];
        String p2Card1=player2.getCardsInHand()[0];
        String p2Card2=player2.getCardsInHand()[1];
        String p1Card=game.cardsCompare(p1Card1, p1Card2);
        String p2Card=game.cardsCompare(p2Card1, p2Card2);
        System.out.println("玩家"+player1.getName()+"的最大手牌为"+p1Card);
        System.out.println("玩家"+player2.getName()+"的最大手牌为"+p2Card);
        String winner="";
        if(game.cardsCompare(p1Card, p2Card).equals(p1Card)){
            winner=player1.getName();
        }else if(game.cardsCompare(p1Card, p2Card).equals(p2Card)){
            winner=player2.getName();
        }
        System.out.println("玩家"+winner+"获胜");
        System.out.println("玩家各自的手牌为");
        System.out.println(player1.getName()+":"+p1Card1+","+p1Card2);
        System.out.println(player2.getName()+":"+p2Card1+","+p2Card2);
    }

    public String cardsCompare(String s1,String s2){
        int index1=0,index2=0;
        for(int i=0;i<allCards.size();i++){
            if(allCards.get(i).equals(s1)){
                index1=i;
                break;
            }
        }
        for(int i=0;i<allCards.size();i++){
            if(allCards.get(i).equals(s2)){
                index2=i;
                break;
            }
        }
        if(index1<index2){
            return s2;
        }else{
            return s1;
        }
    }

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

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消