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

扑克牌游戏,经验不足,请各位大神多多指教!

标签:
Java

具体实现的要求如下:
1、创建一副扑克牌
2、洗牌打乱顺序
3、创建两名玩家,并且手动输入ID和姓名,ID 不能为非整型
4、发牌,按打乱的扑克牌,从第一张开始每家发两张牌
5、制定比赛规则,比较第一张牌大小,若相等,则比较花色,决出胜负


package playingcards;

import java.util.List;
import java.util.Map;

/**
 * 玩家信息
 * @author Administrator
 *
 */
public class Player {
    private int ID;
    private String name;
    private List<Cards> ownCards;
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Cards> getCards() {
        return ownCards;
    }
    public void setCards(List<Cards> list) {
        this.ownCards = list;
    }
    public Player() {
    }
}

package playingcards;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
 * 扑克牌信息
 * @author Administrator
 *
 */
public class Cards {
    private String color;
    private String cards;
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getCards() {
        return cards;
    }
    public void setCards(String cards) {
        this.cards = cards;
    }

}

package playingcards;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
 * 扑克牌基本设置
 * @author Administrator
 *
 */
public class CardsSet implements Comparator<Cards> {
    private Set<Cards> cardsSet= new HashSet<Cards>();
    private List<Cards> player1;
    private List<Cards> player2;
    public CardsSet(){
        System.out.println("[扑克牌开始准备]");
        String[] color = new String[]{"黑桃","红桃","梅花","方块"};
        String[] cards = new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        Cards card = null;
        for (String str1 : color) {
            for (String str2 : cards) {
                card = new Cards();
                card.setColor(str1);
                card.setCards(str2);
                cardsSet.add(card);//将牌添加到set中即洗牌过程
                System.out.print("["+card.getColor()+card.getCards()+"]");
                card = null;//每添加一张牌需要清空一次card的属性
            }
            System.out.println();
        }
        System.out.println("[洗牌完毕]");
    }

    /**
     * 发牌
     */
    public void dealing(){
        player1 = new ArrayList<Cards>();
        player2 = new ArrayList<Cards>();
        int i=0;
        for (Cards cards : cardsSet) {
            if(i % 2 == 0)
                player1.add(cards);
            else if (i % 2 != 0)
                player2.add(cards);
            i++;
            if(i==4)
                break;
        }

        System.out.println("[发牌完毕]");
    }

    public List<Cards> getPlayer1() {
        return player1;
    }

    public List<Cards> getPlayer2() {
        return player2;
    }

    @Override
    public int compare(Cards o1, Cards o2) {
        // TODO Auto-generated method stub
        String[] colors = new String[]{"黑桃","红桃","梅花","方块"};
        String[] cards = new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        String card1 = o1.getCards();
        String card2 = o2.getCards();
        int i = 0,index1 = 0,index2 = 0;
        for (String string : cards) {
            i++;
            if(string == card1)
                index1 = i;
            if(string == card2)
                index2 = i;
        }
        if(index1 < index2)
            return -1;
        if(index1 > index2)
            return 1;
        else
        {
            String color1 = o1.getColor();
            String color2 = o2.getColor();
            for (String string : colors) {
                i++;
                if(color1.equals(string))
                    index1 = i;
                if(color2.equals(string))
                    index2 = i;
            }
            if(index1 < index2)
                return -1;
            if(index1 > index2)
                return 1;
            else
                return 0;
        }
    }

}

package playingcards;

import java.util.InputMismatchException;
import java.util.Scanner;
/**
 * 主类,调用其他各类的方法
 * @author Administrator
 *
 */
public class Playing {
public static void main(String[] args) {
    //准备扑克牌,洗牌
    CardsSet cards = new CardsSet();
    Scanner scanner = new Scanner(System.in);
    //输入参赛者信息
    Player player1 = new Player();
    Player player2 = new Player();
    int id = 0;
    String name = "";
    int flag = 0;
    do{
        System.out.println("请输入第一名参赛者的信息:");
        System.out.println("请输入参赛者ID:");
        try{
            flag = 0;
            id =  scanner.nextInt();
            }catch(InputMismatchException e){
                flag = 1;
                scanner = new Scanner(System.in);;
                System.out.println("输入的数据非整型,请重新输入!");  
            }
    }while(flag == 1);
    System.out.print("请输入参赛者姓名:");
    name = scanner.next();
    player1.setID(id);
    player1.setName(name);
    do{
        System.out.println("请输入第二名参赛者的信息:");
        System.out.println("请输入参赛者ID:");
        try{
            flag = 0;
            id =  scanner.nextInt();
            }catch(InputMismatchException e){
                flag = 1;
                scanner = new Scanner(System.in);;
                System.out.println("输入的数据非整型,请重新输入!");  
            }
    }while(flag == 1);
    System.out.print("请输入参赛者姓名:");
    name = scanner.next();
    player2.setID(id);
    player2.setName(name);
    System.out.println("[参赛者准备完毕,开始发牌]");
    cards.dealing();//发牌
    player1.setCards(cards.getPlayer1());
    player2.setCards(cards.getPlayer2());
    int result = cards.compare(player1.getCards().get(0), player2.getCards().get(0));
    if(result == 1)
        System.out.println("ID为"+player1.getID()+"的参赛者:"+player1.getName()+"获胜");
    if(result == -1)
        System.out.println("ID为"+player2.getID()+"的参赛者:"+player2.getName()+"获胜");
    if(result == 0)
        System.out.println("本场比赛为平局");
    System.out.println("[比赛结束]");
    System.out.println("[摊牌]");
    System.out.print("参赛者:"+player1.getName()+"手牌如下:");
    for (Cards card : player1.getCards()) {
        System.out.print("["+card.getColor()+card.getCards()+"]");
    }
    System.out.print("\n参赛者:"+player2.getName()+"手牌如下:");
    for (Cards card : player2.getCards()) {
        System.out.print("["+card.getColor()+card.getCards()+"]");
    }
}
}
点击查看更多内容
10人点赞

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

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
0
获赞与收藏
15

关注作者,订阅最新文章

阅读免费教程

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消