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

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

Francis_Lee
关注TA
已关注
手记 3
粉丝 3
获赞 64

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;
        }
    }

}
打开App,阅读手记
19人推荐
发表评论
随时随地看视频慕课网APP

热门评论

if(game.cardsCompare(p1Card, p2Card).equals(p1Card)){if(game.cardsCompare(p1Card,p3Card).equals(p1Card){winner=player1.getName();}else{winner=player3.getName();}}else if(game.cardsCompare(p1Card, p2Card).equals(p2Card)){if(game.cardsCompare(p2Card,p3Card).equals(p2Card){winner=player2.getName();}else{winner=player3.getName();}}


额,为什么直接放到eclipse上有错误啊

比较那块处理的很巧妙

查看全部评论