手记

20170710简易扑克牌游戏

一、先创建扑克类

package PuKeGame;

public class PuKe implements Comparable<PuKe> {
    public int PuKeID; //扑克牌的序号,比大小时使用
    public String PuKeName; //扑克牌的名称
    public int FlowerID; //花色序号,比大小时使用
    public String FlowerName; //花色名称

    public PuKe(){

    }

    public PuKe(int PuKeID, String PuKeName, int FlowerID, String FlowerName){
        this.PuKeID = PuKeID;
        this.PuKeName = PuKeName;
        this.FlowerID = FlowerID;
        this.FlowerName = FlowerName;
    }

    public int compareTo(PuKe puke){
        if(this.PuKeID > puke.PuKeID){
            return 1;
        }else if (this.PuKeID < puke.PuKeID){
            return -1;
        }else{
            if(this.FlowerID > puke.FlowerID){
                return 1;
            }else if (this.FlowerID < puke.FlowerID){
                return -1;
            }else{
                return 0;
            }       
        }
    }

    @Override
    public String toString() {
        return "[" + FlowerName + PuKeName + "]";
    }

}

二、创建花色类

package PuKeGame;

public class Flower {
    public int FlowerID; //花色序号
    public String FlowerName; //花色名称

    public Flower(){

    }

    public Flower(int FlowerID, String FlowerName){
        this.FlowerID = FlowerID;
        this.FlowerName = FlowerName;
    }
}

三、创建玩家类

package PuKeGame;

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

public class Person {
    public int PersonID;
    public String PersonName;
    public PuKe MaxPuKe = new PuKe();
    List<PuKe> HavePuKe = new ArrayList<PuKe>(); //拥有的扑克牌集合

    public Person(){

    }

    public Person(int PersonID, String PersonName){
        this.PersonID = PersonID;
        this.PersonName = PersonName;
    }

    //添加扑克牌的方法
    public void AddPuKe(PuKe puke){
        this.HavePuKe.add(puke);
    }
}

四、创建扑克游戏

package PuKeGame;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class PuKeGame {

    public static void main(String[] args) {
        Scanner sn = new Scanner(System.in);
        int PersonCards = 2; //玩家牌的数量
        int PersonNum = 2; //玩家数量

        //创建花色的集合
        String[] FlowerNames = {"黑桃", "红桃", "草花", "方块"};
        List<Flower> Flowers = new ArrayList<Flower>();
        for(int i = 0; i < FlowerNames.length; i++){
            Flowers.add(new Flower(i, FlowerNames[i]));
        }

        //创建扑克类
        String[] PuKeNames = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        List<PuKe> PuKes = new ArrayList<PuKe>();
        for(int i = 0; i < PuKeNames.length; i++){
            for(int j = 0; j < Flowers.size(); j++){
                PuKes.add(new PuKe(i, PuKeNames[i], Flowers.get(j).FlowerID, Flowers.get(j).FlowerName));
            }
        }

        System.out.println("---------------扑克牌创建成功-------------");

        /*查询未洗牌前的扑克牌
        for (PuKe puke: PuKes){
            System.out.println(puke.FlowerID + ":" + puke.FlowerName + puke.PuKeID + ":" + puke.PuKeName);
        }
        */

        //创建两名玩家
        System.out.println("需要有两名玩家,请按要求输入两名玩家信息");
        List<Person> persons = new ArrayList<Person>();
        for(int i = 0; i < PersonNum; i++){
            System.out.println("请输入第" + (i + 1) + "名员工的姓名");
            String Name = sn.next();
            persons.add(new Person(i, Name));
        }
        System.out.println("----------------玩家创建成功-------------------");
        System.out.println("玩家的信息如下:");
        for(Person person: persons){
            System.out.println("玩家编号:" + person.PersonID + ",玩家姓名:" + person.PersonName);
        }

        int iRefresh;
        //判断是否开始游戏
        System.out.println("是否开始游戏:0是,1否");
        do{
            iRefresh = sn.nextInt(); //获取员工的选择
            if((iRefresh == 0) || (iRefresh == 1)){
                break;
            }else{
                System.out.println("输入的 " + iRefresh + " 不合法,请您输入 0 或 1 ");
                continue;
            }
        }while(true);

        //开始洗牌和发牌
        if(iRefresh == 0){
            Random rd = new Random();
            for(int i =  0; i < PuKes.size(); i++){
                int iSwapOne = rd.nextInt(PuKes.size());
                int iSwapTwo = rd.nextInt(PuKes.size());
                Collections.swap(PuKes, iSwapOne, iSwapTwo); //随机交换牌堆的两张牌
            }

            /*查看洗牌后的牌
            for (PuKe puke: PuKes){
                System.out.println(puke.FlowerID + ":" + puke.FlowerName + puke.PuKeID + ":" + puke.PuKeName);
            }
            */

            //发牌
            int Order = 0;
            while(Order < (PersonNum * PersonCards)){
                for(int i = 0; i < PersonNum; i++){
                    persons.get(i).AddPuKe(PuKes.get(Order));
                    System.out.println("玩家:" + persons.get(i).PersonName + "拿牌");
                    Order++;
                }
            }

            System.out.println("-----------发牌结束----------------");
            System.out.println("------------开始比较大小-------------");
            //获取每一个玩家最大的手牌
            for(int i = 0; i < PersonNum; i++){ //遍历玩家
                persons.get(i).MaxPuKe = persons.get(i).HavePuKe.get(0); //先给玩家最大的扑克牌属性赋值
                for(int j = 1; j < PersonCards; j++){ //遍历每一个玩家的手牌
                    //当玩家第一张最大的手牌小于第二张手牌时,就进行互换,给最大的手牌重新赋值
                    if (persons.get(i).MaxPuKe.compareTo(persons.get(i).HavePuKe.get(j)) < 0){
                        persons.get(i).MaxPuKe = persons.get(i).HavePuKe.get(j);
                    }
                }
                System.out.println("玩家:" + persons.get(i).PersonName + "最大的手牌为" 
                        + persons.get(i).MaxPuKe.toString());
            }

            //比较每一名玩家,得到获胜者
            Person WinPerson = persons.get(0); //先定位到第一名玩家上
            for(int i = 1; i < PersonNum; i++){
                if (WinPerson.MaxPuKe.compareTo(persons.get(i).MaxPuKe) < 0){
                    WinPerson = persons.get(i);
                }
            }
            System.out.println("获胜玩家是:" + WinPerson.PersonName + "最大的手牌为:"
                    + WinPerson.MaxPuKe.toString());

            //打印各个玩家的手牌
            StringBuilder sb = new StringBuilder();
            System.out.println("玩家各自的手牌如下:");
            for(int i = 0; i < PersonNum; i++){
                for(int j = 0; j < PersonCards; j++){
                    sb.append(persons.get(i).HavePuKe.get(j).toString());
                }
                System.out.println("玩家:" + persons.get(i).PersonName + "的手牌为:" + sb.toString());
                sb.delete(0, sb.length());
            }

            System.out.println("----------游戏结束------------");

        }else{
            System.out.println("--------------欢迎下次再来玩---------------");
        }
        sn.close();
    }
}

五、附上打印的效果

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

热门评论

是不是少发了一张牌啊

查看全部评论