手记

JAVA入门第三季--简易扑克牌游戏

/**
*功能:JAVA入门第三季--简易扑克牌游戏
*@奋斗的小龟龟
*小白原创,请多指教~
**/
//主函数部分
package com.easyCard;

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

/**
 * 一、创建一副扑克牌
 * 包括四种花色:黑桃、红桃、梅花、方片
 * 十三种点数:2-10,J、Q、K、A不考虑大小王。
 * 
 * 二、创建两名玩家
 *玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合
 *
 *三、洗牌
 *将之前创建的“一副扑克牌”打乱顺序
 *
 *四、发牌
 *将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,
 *每人发两张
 *
 *五、游戏
 *比较两名玩家手中的扑克牌,规则为:取两人各自手中点数最大的牌进行比较,点数大的赢;
 *若两人各自的点数最大的牌相等,则再按花色比较,即黑红梅方的顺序
 *
 */

public class TestPlay {

    public Scanner input ;

    public List<Cards> cardsToSelect;

    public Map<Integer,Players> players;

    public Map<Integer,Cards>  subcards;

    //public Players player;

    public TestPlay(){

    this.cardsToSelect = new ArrayList<Cards>();

    this.input = new Scanner(System.in);

    this.players = new HashMap<Integer,Players>();

    this.subcards = new HashMap<Integer,Cards>();

    //this.player = new Players();

    }

    /**
     *  一、创建一副扑克牌
     * 包括四种花色:黑桃、红桃、梅花、方片
     * 十三种点数:2-10,J、Q、K、A不考虑大小王。*/
    public void addCards(){
        String[] color ={"黑桃","红桃","梅花","方片"}; 
        String[] num = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        int c =0;
        System.out.println("------------创建扑克牌-------------");
        for(int i = 0; i < 4; i++)
        {            
             StringBuffer strbuf=new StringBuffer();
             strbuf .append(color[i]);
             for(int  j = 0; j < 13; j++){
                strbuf.insert(2, num[j]);
                Cards card = new Cards();
                card.name = strbuf.toString();
                cardsToSelect.add(c, card);
                c++;
                strbuf.delete(2, strbuf.length());
            }
         }
        System.out.println("----------扑克牌创建成功!---------------");

        for (Cards card : cardsToSelect) {
            System.out.print(card.name+",");

        }
            System.out.println();
     }

    /** 二、创建两名玩家
     *玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合
     */
    public void addPlayers(){
            System.out.println("-------------创建玩家-----------------");
            int i = 1;
            while(true){
                System.out.println("请输入第"+i+"位玩家的ID和姓名:");
            try {
                System.out.println("输入ID:");
                int id = input.nextInt();
                Players pl = players.get(id);
                if( pl == null){ 
                    System.out.println("输入姓名:");
                    String name = input.next();
                    Players player = new Players(id,name);
                    players.put(id, player);
                    System.out.println("成功添加玩家:"+players.get(id).name+"\n");
                    i++;

                 }
                else{
                    System.out.println("该ID以已被占用!");
                    continue;
                }
                if(i > 2)
                    break;
            } catch (InputMismatchException e) {
                System.out.println("请输入整数类型的ID!");
                input.next();
                continue;
            }
            }
            System.out.println("---------------玩家创建完成----------------");
            Set<Integer> keySet = players.keySet();
            for (Integer integer : keySet) {
                System.out.println("   ---------欢迎玩家:"+players.get(integer).name+"----------");
            }
            System.out.println();

    }

    /**
     * 三、洗牌(Shuffle)
     * @param args
     */
    public void shuffle(){
        System.out.println("------------开始洗牌...---------------");
        Collections.shuffle(cardsToSelect);
        System.out.println("------------洗牌结束-------------");

        //测试是否洗牌成功,同时给元素分配id
        int i = 0;
        for (Cards cards : cardsToSelect) {
            cards.id = i;
            //System.out.println(cards.id+":"+cards.name);
            i++;
        }
    }

    /**
    *四、发牌
     *将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,
     *每人发两张
    */
    public void deal(){
            System.out.println("----------------开始发牌...----------------");
            Set<Integer> keySet = players.keySet();
            int i = 0;
            while(i < 3){
                for (Integer integer : keySet) {
                    Players player = players.get(integer);
                    Cards card = new Cards(i,null);
                    card.name = cardsToSelect.get(i).name ;
                    //测试每次发牌顺序
                    //System.out.println(i+subcards.get(i).name);
                    player.cards.add(card);
                    //测试每人拿牌环节是否正常
                    //System.out.println("-----------玩家:"+player.name+" 拿牌"+card.name+"-------------");
                    System.out.println("-----------玩家:"+player.name+" 拿牌-------------");
                    i++;
                }
            }
            System.out.println("---------------发牌结束---------------");
            System.out.println();
    }

     /**五、游戏
     *比较两名玩家手中的扑克牌,规则为:取两人各自手中点数最大的牌进行比较,点数大的赢;
     *若两人各自的点数最大的牌相等,则再按花色比较,即黑红梅方的顺序
     **/
    public void testGame(){
        System.out.println("--------------开始游戏----------------");
        Set<Integer> keySet = players.keySet();
        List<Cards> cpcards = new ArrayList<Cards>();
        for (Integer integer : keySet) {
            Players player = players.get(integer);
            List<Cards> card = player.cards;
            Collections.sort(card, new CardsComparator());
            System.out.println("----玩家"+players.get(integer).name+"最大的手牌为:"+card.get(1).name);
            card.get(1).id = player.id;
            cpcards.add(card.get(1));
        }
        Collections.sort(cpcards, new CardsComparator());
        for (Integer integer : keySet) {
            //比较两个玩家手中最大的牌
            if(cpcards.get(1).id == players.get(integer).id)
                System.out.println("-----------------玩家:"+players.get(integer).name+"获胜 !------------------");
            else
                continue;
        }
        System.out.println("玩家各自的手牌为:");
        for (Integer integer : keySet) {
            Players player = players.get(integer);
            System.out.print(players.get(integer).name+":");
            for (Cards cards : player.cards) {
                System.out.print(cards.name+",");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
           TestPlay tp = new TestPlay();
           tp.addCards();
           tp.shuffle();
           tp.addPlayers();
           tp.deal();
           tp.testGame();
        }
}
//创建牌类
package com.easyCard;

public class Cards{

    public int id;

    public String name;

    public Cards(){

    }

    public Cards(int id,String name){

        this.id = id;

        this.name = name;

    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Cards))
            return false;
        Cards other = (Cards) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
}
//创建玩家类
package com.easyCard;

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

public class Players  {

    public int id;

    public String name;

    public List<Cards> cards;

    public Players(){

    }

    public Players(int id,String name){

        this.id = id;

        this.name = name;

        this.cards = new ArrayList<Cards>();

    }
//创建牌的临时比较规则
package com.easyCard;

import java.util.Comparator;

public class CardsComparator implements Comparator<Cards>{

    public static void main(String[] args) {

    }
 /**
  * 临时比较规则:按黑、红、梅、方的顺序比较大小(黑桃最大,方片最小)
  */
    @Override
    public int compare(Cards o1, Cards o2) {
        String o1name = o1.name.substring(0, 2);
        String o2name = o2.name.substring(0, 2);
        String o1num = o1.name.substring(2);
        String o2num = o2.name.substring(2);

        if(o1name.equals(o2name))
            return o1num.compareTo(o2num);

        if( o1name.equals("黑桃") )
            return 1;

        if(o1name.equals("红桃"))
        {
                if(o2name.equals("黑桃"))
                    return -1;
                else 
                    return 1;
            }

        if(o1name.equals("梅花"))
            {
            if(o2name.equals("方片"))
                return 1;
            else
                return -1;
            }
        return 0;
    }

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

热门评论

您好 请问TestPlay 里面中的int c是什么意思

查看全部评论