手记

简单的扑克牌游戏

总共创建了三个类:玩家Players、扑克牌Cards、主类Initial
实现功能是三个玩家,一次抓取3张牌,比较谁最大

package com.test.cardgame;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * 用集合框架的思想创建扑克牌游戏,炸金花
 * 创建一副牌
 * 2/3/4/5/6/7/8/9/10、J、Q、K、A 
 * 以及各花色:黑、红、梅、方
 * 洗牌
 * 创建若干个玩家,需要有ID和姓名
 * 三个玩家分别抓取3张牌比大小
 * @author 蒋森
 * Date:2018.5.8
 */
public class Initial {
    //创建一个扑克牌集合
    List<Cards> cardList = new ArrayList<Cards>();
    //创建玩家List集合
    List<Players> playerList = new ArrayList<Players>();
    //创建扑克牌花色数组
    public String[] type = {"方块","梅花","红桃","黑桃"};
    //创建扑克牌数字数组
    public String[] num = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    //输入类实例化
    Scanner in = new Scanner(System.in);
    /*
     * 创建扑克牌方法
     */
    public void setCards(){
        System.out.println("<======这里是一副扑克牌!!!========>");
        //将扑克牌放入到集合中去
        for(int i=0;i<type.length;i++){
            for(int j=0;j<num.length;j++){
                cardList.add(new Cards(type[i],i,num[j],j));//将扑克牌用add的方法添加到扑克牌集合中去
                Cards car = cardList.get(i+j);
                System.out.printf("%s\t",car.toString());
            }
            System.out.println();
        }
    }
    /*
     * 创建洗牌方法
     */
    public void sortCards(){
        System.out.println("<==========开始洗牌!!!=============>");
        Collections.shuffle(cardList);      //用Collections.shuffle();方法洗牌
        //用foreach方法打印出新List集合中的扑克牌
        int o=1;                            //辅助打印参数,13换行
        for(Cards p:cardList){
            System.out.printf("%s\t",p.toString());
            if(o%13==0){
                System.out.println();
            }
            o++;
        }
        System.out.println("<=====洗牌结束!!!============>");
    }
    /*
     * 用List集合创建玩家
     */
    public void setPlayer(){
        int i = 1;
        while(i<4){
            System.out.println("请输入第"+i+"个玩家的ID:");
            int ID = in.nextInt();
            System.out.println("请输入第"+i+"个玩家的name:");
            String name = in.next();
            Players player = new Players(ID,name);
            playerList.add(i-1,player);
            i++;
        }
        //输出玩家信息
        for(Players o:playerList){
            System.out.printf("%s\t\n",o.toString());
        }
    }
    /**
     * 创建发牌方法
     */
    public void cardToPlayer(int p){
        System.out.println("<======开始发牌!!!==========>");
        System.out.println("每个人几张牌:");
        p = in.nextInt();
        int j = 0;//初始牌下标
        for(int i=0;p-i>0;i++){
            for(Players o:playerList){
                o.playerCard.add(cardList.get(j));
                j++;
            }
        }
        for(Players o:playerList){
            System.out.printf("玩家%s的牌是:%s\t\n",o.getName(),o.playerCard.toString());
        }
        System.out.println("<======发牌结束!!!===========>");
    }
    /*
     * 创建比较大小方法
     */
    //展示各自最大手牌
    public void compare(){
        System.out.println("<=====玩家各自最大手牌分别是:========>");
        for(Players o:playerList){
            Collections.sort(o.playerCard);
            System.out.println("玩家"+o.getName()+"最大的手牌是:"+o.playerCard.get(0));
        }
    }
    //决出获胜方
    public void winner(){
        System.out.println("<======获得本次炸金花比赛最终胜利的是:=====>");
        //创建所有玩家最大牌List
        List<Cards> allBig = new ArrayList<Cards>();
        for(Players o:playerList){
            allBig.add(o.playerCard.get(0));
        }
        Collections.sort(allBig);
        for(Players o:playerList){
            if(o.playerCard.contains(allBig.get(0))){
                System.out.printf("最后获胜的玩家是:%s他的手牌是:%s,%s,%s",o.getName(),
                        o.playerCard.get(0),o.playerCard.get(1),o.playerCard.get(2));
            }
        }
    }

    public static void main(String[] args) {
        //创建实例对象
        Initial test = new Initial();
        test.setCards();//创建扑克牌
        test.sortCards();//洗牌
        test.setPlayer();//创建玩家
        test.cardToPlayer(3);//发牌
        test.compare();//亮牌
        test.winner();//决出胜负

    }

}
package com.test.cardgame;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建玩家,包括ID和名字
 * @author 蒋森
 * Date:2018.5.8
 */
public class Players {
    private int ID;     //创建ID
    private String name;//创建name
    public List<Cards> playerCard;
    public Players(int Id,String name){
        this.ID = Id;
        this.name = name;
        this.playerCard = new ArrayList<Cards>();
    }
    @Override
    public String toString() {
        return ID + ":" + name;
    }
    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;
    }
}

package com.test.cardgame;

/**
 * 创建扑克牌,包括花色和数字
 * @author 蒋森
 * Date:2018.5.8
 */
public class Cards implements Comparable<Cards>{
    //创建花色
    private String type;
    //创建花色权重
    private Integer typeVal;
    //创建字符
    private String num;
    //创建字符权重
    private Integer numVal;
    //构造器
    public Cards(String type,int typeVal,String num,int numVal){
        this.num = num;
        this.type = type;
        this.numVal = numVal;
        this.typeVal = typeVal;
    }
    @Override
    public String toString() {
        return type+num;
    }
    //重写方法,权重大的靠前显示
    @Override
    public int compareTo(Cards o) {
        // 重写比较方法
        if(numVal!=o.numVal){
            return o.numVal.compareTo(numVal);
        }
        else{
            return o.typeVal.compareTo(typeVal);
        }
    }
    public Integer getTypeVal() {
        return typeVal;
    }
    public void setTypeVal(Integer typeVal) {
        this.typeVal = typeVal;
    }
    public Integer getNumVal() {
        return numVal;
    }
    public void setNumVal(Integer numVal) {
        this.numVal = numVal;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getNum() {
        return num;
    }
    public void setNum(String num) {
        this.num = num;
    }

}```
7人推荐
随时随地看视频
慕课网APP

热门评论

稍微改一下就更完美了。

Cards car = cardList.get(i * num.length + j);


查看全部评论