手记

Java入门第三季7-1简易扑克牌游戏(自定义玩家个数,玩家个数和ID异常检测)

玩家类:

package playingCardsGame;

import java.util.ArrayList;
import java.util.List;
/*
 * 玩家类
 */
public class Players {
    int ID;
    String name;
    List<HandCards> handCards;
    Players(int ID, String name){
        this.ID = ID;
        this.name = name;
        this.handCards = new ArrayList<HandCards>();
    }
}

手牌类:

package playingCardsGame;
/*
 * 手牌类
 */
public class HandCards {
    String card;
    HandCards(String card) {
        this.card = card;
    }
}

游戏类:

package playingCardsGame;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/*
 * 游戏类
 */
public class CardsGame {
    public List<String> cardsList;
    public List<String> shuffleList;
    public List<Players> player;
    public Players ps;
    private Scanner console;
    Random random;
    public CardsGame(){
        cardsList = new ArrayList<String>();
        shuffleList = new ArrayList<String>();
        player = new ArrayList<Players>();
        ps = new Players(0, null);
        console = new Scanner(System.in);
        random = new Random();
    }
    String[] color = {"方片","梅花","红桃","黑桃"};
    String[] ch = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
    //创建扑克牌并洗牌
    public void Cards(){
        System.out.println("--------------创建扑克牌...--------------");
        for(int i = 0; i < color.length; i++){
            for(int j = 0; j < ch.length; j++){
                cardsList.add(color[i] + ch[j]);
            }
        }
        System.out.println("------------扑克牌创建成功!-------------");
        System.out.print("为:[");
        for(int k = 0; k < cardsList.size(); k++){
            if(k < cardsList.size()-1){
                System.out.print(cardsList.get(k)+",");
            }else
                System.out.println(cardsList.get(k)+"]");
        }
        System.out.println("--------------开始洗牌...--------------");
        for(int m = 0; m < cardsList.size(); m++){
            int number;
            do{ 
                number = random.nextInt(cardsList.size());
            }while(shuffleList.contains(cardsList.get(number).toString()));
            shuffleList.add(cardsList.get(number).toString());
        }
        System.out.println("--------------洗牌结束!--------------");
    }
    //创建玩家并进行游戏
    public void Play(){
        System.out.println("--------------创建玩家...--------------");
        //自定义玩家个数并进行异常检测
        int numberOfPlayers;
        while(true){
            System.out.println("请输入玩家个数(最少2名玩家,最多26名玩家):");
            Scanner console = new Scanner(System.in);
            try{
                numberOfPlayers = console.nextInt();
                if(numberOfPlayers > 1 && numberOfPlayers < 27){
                    break;
                }else{
                    throw new Exception();
                }
            }catch(InputMismatchException e){
                System.out.println("请输入整数类型的玩家个数!");
            }catch(Exception e){
                System.out.println("玩家个数不在规定范围之内!");
            }
        }
        //获取每个玩家的ID和姓名并对ID进行异常检测
        for(int i = 1; i <= numberOfPlayers; i++){
            System.out.println("请输入第"+ i + "位玩家的ID和姓名:");
            int ID;
            while(true){
                System.out.println("输入ID:");
                Scanner console = new Scanner(System.in);
                try{
                    ID = console.nextInt();
                    break;
                }catch(InputMismatchException e){
                    System.out.println("请输入整数类型的ID!");
                }
            }
            System.out.println("输入姓名:");
            String name = console.next();
            player.add(new Players(ID, name));
        }
        for (Players player : player) {
            System.out.println("-----欢迎玩家:" + player.name);
        }
        System.out.println("--------------开始发牌...--------------");
        //获取所发牌的手牌序列
        for(int i = 0; i < 2; i ++){
            for(int j = 0; j < numberOfPlayers; j ++){
                System.out.println("-----玩家:" + player.get(j).name + "-拿牌");
                if(i == 0)
                    ps.handCards.add(new HandCards(shuffleList.get(j).toString()));
                else
                    ps.handCards.add(new HandCards(shuffleList.get(j + numberOfPlayers).toString()));
            }
        }
        System.out.println("--------------发牌结束!--------------");
        System.out.println("--------------开始游戏...--------------");
        //获取每个玩家的最大手牌序列
        List<String> maxCards = new ArrayList<String>();
        int m = 0, n = 0, p = 0, q = 0;
        String max;
        for(int i = 0;i < numberOfPlayers; i ++){
            String card1 = ps.handCards.get(i).card.toString();
            String card2 = ps.handCards.get(i + numberOfPlayers).card.toString();
            //比较牌面大小
            for(int j = 0; j < ch.length; j ++){
                if(card1.substring(2).equals(ch[j]))
                    m = j;
                if(card2.substring(2).equals(ch[j]))
                    n = j;
            }
            if(m >= n){
                if(m == n){//如果牌面大小相等,再比较花色并最终得到最大值 
                    for(int j = 0; j < color.length; j ++){
                        if(card1.substring(0,2).equals(color[j]))
                            p = j;  
                        if(card2.substring(0,2).equals(color[j]))
                            q = j;
                    }
                    if(p > q){
                        max = card1.toString();

                    }else{
                        max = card2.toString();
                    }   
                }else{
                    max = card1.toString();
                }
            }else{
                max = card2.toString();
            }
            System.out.println("玩家:" + player.get(i).name + "最大的手牌为:" + max);
            maxCards.add(max);
        }
        //比较最大手牌序列,先比较大小再比较花色,最后得到最大牌对应的玩家
        for(int i = 0, j = 1;j < numberOfPlayers; j ++){
            String card3 = maxCards.get(i).toString();
            String card4 = maxCards.get(j).toString();
            for(int k = 0; k < ch.length; k ++){
                if(card3.substring(2).equals(ch[k]))
                    m = k ;
                if(card4.substring(2).equals(ch[k]))
                    n = k;
            }
            if(m <= n){
                if(m == n){
                    for(int r = 0; r < color.length; r ++){
                        if(card3.substring(0,2).equals(color[r]))
                            p = r;  
                        if(card4.substring(0,2).equals(color[r]))
                            q = r;
                    }
                    if(p < q)
                        i = j;
                }else
                    i = j;
            }
            m = i;
        }
        //输出游戏结果
        System.out.println("--------------玩家:" +player.get(m).name + "获胜!--------------");
        System.out.println("玩家各自的手牌为:");
        for (int i = 0; i < numberOfPlayers; i ++)
            System.out.println(player.get(i).name + ":[" + ps.handCards.get(i).card.toString()
                    +"," + ps.handCards.get(i + numberOfPlayers).card.toString() + "]");
    }
    public static void main(String[] args) {
        CardsGame cg = new CardsGame();
        cg.Cards();
        cg.Play();
    }
}
9人推荐
随时随地看视频
慕课网APP

热门评论

你的手牌序列A Q K J怎么比较大小啊,怎么能用equals直接比呢

查看全部评论