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

JAVA入门之简易扑克牌游戏

标签:
Java
创建CardsPlayer类

用来记录玩家ID、姓名、获得的手牌组合以及手牌面值及其对应的索引值

package PlayingCards;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class CardsPlayer {
    public String id;
    public String name;
    public Set<String> cards;   //用来记录玩家获得的手牌
    public Map values;           //用来记录值和索引键值对
    //public Set labels;          //用来记录获得手牌的索引
    //public Set values;          //用来记录获得手牌的值
    public CardsPlayer(String id, String name){
        this.id = id;
        this.name = name;
        this.cards = new HashSet<String>();
        //this.labels = new HashSet();
        //this.values = new HashSet();
        this.values = new HashMap();
    }
}
简易扑克牌游戏主程序

包括生成扑克牌、创建玩家、洗牌和发牌、输出各玩家获得手牌情况并比较手牌大小四部分

package PlayingCards;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class PlayingCards {
    public String[] value = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    public String[] type = {"黑桃","红桃","梅花","方片"};
    public int amount;        //用来记录给每个玩家发牌的数量
    public int[] values = new int[52];      //用来记录扑克牌的值
    public List<String> cards = new ArrayList<String>();       //用来记录生成的扑克牌
    public Map<String, CardsPlayer> players = new HashMap<String, CardsPlayer>();     //用来记录玩家信息
    public List<String> playersID = new ArrayList<String>();     //用来记录玩家的ID
    /*
     * 生成扑克牌
     * 创建花型和数值数组,遍历数组生成扑克牌
    */
    public void createCard(){
        System.out.println("---------创建扑克牌---------");
        String str;
        for (int i=0; i<type.length; i++){
            for (int j=0; j<value.length; j++){
                str = type[i]+value[j];
                if (value[j].equals("J"))
                    values[i*value.length+j] = 11;
                else if (value[j].equals("Q"))
                    values[i*value.length+j] = 12;
                else if (value[j].equals("K"))
                    values[i*value.length+j] = 13;
                else if (value[j].equals("A"))
                    values[i*value.length+j] = 14;
                else if (value[j].equals("10"))
                    values[i*value.length+j] = 10;
                else if (value[j].equals("9"))
                    values[i*value.length+j] = 9;
                else if (value[j].equals("8"))
                    values[i*value.length+j] = 8;
                else if (value[j].equals("7"))
                    values[i*value.length+j] = 7;
                else if (value[j].equals("6"))
                    values[i*value.length+j] = 6;
                else if (value[j].equals("5"))
                    values[i*value.length+j] = 5;
                else if (value[j].equals("4"))
                    values[i*value.length+j] = 4;
                else if (value[j].equals("3"))
                    values[i*value.length+j] = 3;
                else
                    values[i*value.length+j] = 2;
                cards.add(str);
            }
        }
        System.out.println("---------扑克牌创建成功!---------");
        System.out.print("为:["+cards.get(0));
        for (int i=1; i<cards.size(); i++){
            System.out.print(","+cards.get(i));
        }
        System.out.println("]");
    }

    /*
     * 创建玩家
     * 玩家信息包括ID、姓名、手牌组合等
    */
    public void createPlayer(){
        System.out.println("---------创建玩家---------");
        Scanner in = new Scanner(System.in); 
        String id;
        String name;
        int i=1;
        while(i<3) {
            System.out.println("请输入第"+i+"位玩家的ID和姓名:");
            System.out.println("输入ID:");
            id = in.next();
            if (players.get(id) == null){
                System.out.println("输入姓名:");
                name = in.next();
                CardsPlayer p = new CardsPlayer(id,name);
                players.put(id, p);
                i++;
            }else{
                System.out.println("该ID已被占用!!!");
                continue;
            }
        }
        Set keyList = players.keySet();
        for (Object object : keyList) {
            playersID.add((String)object);
            System.out.println("欢迎玩家:"+players.get(object).name);
        }
    }

    /*洗牌和发牌*/
    public void sendCards(){
        Random r = new Random();
        int[] flags = new int[52];    //用于记录相应位置的牌是否已发出,1为发出,0为未发出
        Scanner in = new Scanner(System.in);
        System.out.println("请输入每人发牌张数:");
        amount = in.nextInt();
        System.out.println("--------洗牌结束--------");
        System.out.println("--------开始发牌--------");
        int label;
        for (int i=1; i<=amount; i++){
            for (String id:playersID){
                do{
                label = r.nextInt(cards.size());
                }while(flags[label]==1);
                players.get(id).cards.add(cards.get(label));     //前一个cards为CardsPlayer类的cards属性,后一个cards为记录生成扑克牌的列表
                //players.get(id).labels.add(label);
                players.get(id).values.put(values[label],label);     //CardsPlayer类的values属性为HashMap,记录手牌的数值和对应cards列表的索引,通过比较数值和索引大小,即可判断出手牌大小
                flags[label] = 1;
            }
        }
        System.out.println("--------发牌结束--------");
    }

    /*输出各玩家获得手牌情况,并比较手牌大小*/
    public void cardsObtained(){
        Set keyList = players.keySet();
        int[][] num = new int[2][amount];     //用来记录玩家获得的手牌的面值,并进行升序排列
        String[] id = new String[2];          //用来记录与手牌面值信息对应的玩家ID
        int i=0;
        for (Object object : keyList) {
            System.out.println("玩家"+players.get((String)object).name+"获得的手牌为:");
            for (String card:players.get((String)object).cards){
                System.out.print(card+"\t");
            }
            System.out.println();
            Set kidKey = players.get((String)object).values.keySet();
            int j=0;
            for (Object object2 : kidKey) {
                num[i][j] = (int)object2;
                j++;
            }
            Arrays.sort(num[i]);      //进行升序排列
            id[i] = (String)object;
            i++;
        }
        for (int label=amount-1; label>=0; label--){
            if (num[0][label] == num[1][label]){
                if ((int)players.get(id[0]).values.get(num[0][label]) < (int)players.get(id[1]).values.get(num[1][label])){
                    System.out.println("玩家"+players.get(id[0]).name+"的牌更大,为"+cards.get((int)players.get(id[0]).values.get(num[0][label])));
                    break;
                }
                if ((int)players.get(id[0]).values.get(num[0][label]) > (int)players.get(id[1]).values.get(num[1][label])){
                    System.out.println("玩家"+players.get(id[1]).name+"的牌更大,为"+cards.get((int)players.get(id[1]).values.get(num[1][label]))); 
                    break;
                }
            }
            if (num[0][label] > num[1][label]){
                System.out.println("玩家"+players.get(id[0]).name+"的牌更大,为"+cards.get((int)players.get(id[0]).values.get(num[0][label])));
                break;
            }
            if (num[0][label] < num[1][label]){
                System.out.println("玩家"+players.get(id[1]).name+"的牌更大,为"+cards.get((int)players.get(id[0]).values.get(num[1][label])));
                break;
            }
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PlayingCards pc = new PlayingCards();
        pc.createCard();
        pc.createPlayer();
        pc.sendCards();
        pc.cardsObtained();
    }

}
进一步优化:

1.CardsPlayer类中可以将扑克牌、扑克牌面值、扑克牌对应的索引记录在一个成员变量里面,定义为HashMap或数组均可
2.主程序中用来记录玩家信息的变量players可定义为CardsPlayer类的数组,这样通过索引即可获得所需信息,简化程序

点击查看更多内容
5人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消