这是我写的简易扑克牌游戏,希望能帮到大家,欢迎建议,共同进步

来源:7-1 简易扑克牌游戏

Hu丶0403

2018-07-28 06:13

******player.java**************************************************

package com.PokeGame;

import java.util.ArrayList;

import java.util.List;


public class Player {


    public int id;

    public String name;

    public List<Poke> handpoke;//用来存放玩家手牌


    public Player(int id,String name) {

        this.id = id;

        this.name = name;

        this.handpoke = new ArrayList<Poke>();

    }

}

******poke.java******************************************************

package com.PokeGame;


public class Poke {

    public String pointsort;//用于游戏点数排序比较

    public String flowercolorsort;//用于游戏花色排序

    public String flowercolor;

    public String point;


    public Poke(String flowercolor, String point, String pointsort, String flowercolorsort){

    this.flowercolor = flowercolor;

    this.point = point;

    this.pointsort = pointsort;

    this.flowercolorsort = flowercolorsort;

    }

    @Override

    public String toString() {

    return flowercolor + point ;

    }

}

******Shuffle.java******************************************************

package com.PokeGame;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;


public class Shuffle {

    //用于存放扑克牌的List

    public List<Poke> pokes;

    public  Shuffle(){

    pokes = new ArrayList<>();

    }

    /**

    * 创建扑克牌并添加到扑克牌List

    */

    public void pokeCreate(){

        String[] flowercolor = {"方块","梅花","红桃","黑桃"};

        String[] point = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

        for(int i = 0; i<flowercolor.length; i++){

            for(int j =0; j<point.length; j++){

                Poke poke = new Poke(flowercolor[i],point[j],j+"",i+"");

                this.pokes.add(poke);

            }

        }

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

        System.out.println(this.pokes.toString());

    }

    /**

    * 使用Collections.shuffle()随机打乱List顺序;

    */

    public void pokeShuffle(){

        System.out.println("------------------开始洗牌----------------");

        Collections.shuffle(pokes);

        System.out.println("------------------洗牌结束----------------");

    }

}

******Game.java******************************************************

package com.PokeGame;

import java.util.ArrayList;

import java.util.Collections;

import java.util.InputMismatchException;

import java.util.List;

import java.util.Random;

import java.util.Scanner;


public class Game {

    //定义一个玩家List

    public List<Player> players;

    public Game() {

        //初始化玩家List

        players = new ArrayList<Player>();

    }

    /**

    * 创建两位玩家进行游戏

    * 通过输入玩家ID和姓名进行创建

    */

    public void createplayer(){

        System.out.println("------------------创建玩家..---------------");

        for(int i = 1; i < 3; i++){

            System.out.println("请输入第"+i+"位玩家的ID和姓名:");

            try {

                System.out.println("请输入ID:");

                Scanner console = new Scanner(System.in);

                int playerid = console.nextInt();

                System.out.println("请输入姓名:");

                String name = console.next();

                Player player = new Player(playerid,name);

                players.add(player);

             } catch (InputMismatchException e) {

                System.out.println("请输入整数类型的ID!");

                i--;

                continue;

            }

        }

    }

    /**

    * 通过For Each遍历玩家List

    */

    public void forEachPlayers(){

        for (Player player : players) {

            System.out.println("------欢迎玩家:" + player.name);

        }

    }

    /**

    *传入创建扑克牌并洗牌的Shuffle类的实例s,

    *才能使用创建存放扑克牌名为pokes的List,

    *并随机生成52以内的整数(一共52张牌)作为

    *添加进玩家手牌handpoke中的poke在List中的位置

    */

    public void faPai(Shuffle s){

        System.out.println("------------------开始发牌----------------");

        Random random = new Random();

        for(int i = 0; i < 4; i++){

            if(i==0||i==2){

                int a = random.nextInt(52);

                players.get(0).handpoke.add(s.pokes.get(a));

                //往玩家手牌(handpoke)List中添加完一张牌,将这张牌从(Pokes)List中删除

                s.pokes.remove(a);

                System.out.println("玩家:" + players.get(0).name + "--拿牌");

            }else{

                int b = random.nextInt(51);

                players.get(1).handpoke.add(s.pokes.get(b));

                System.out.println("玩家:" + players.get(1).name + "--拿牌");

            }

        }

        System.out.println("------------------发牌结束----------------");

    }

    /**

    * 通过调用Collectoins的sort()方法对玩家手牌List(handpoke)进行排序,

    * 因为每个玩家只有两张手牌,排序之后输入位置为1的元素就是最大手牌

    * 同过实现Comparator接口,用手牌中的pointsort和flowercolorsort,

    * 分别是点数排序和花色排序

    */

    public void handPokeSort(){

        Collections.sort(players.get(0).handpoke, new PokeComparator());

        System.out.println("玩家:" + players.get(0).name + "最大的手牌为:"+

                players.get(0).handpoke.get(1).toString());

        Collections.sort(players.get(1).handpoke, new PokeComparator());

        System.out.println("玩家:" + players.get(1).name + "最大的手牌为:"+

                players.get(1).handpoke.get(1).toString());

        //定义一个新的List用于存放两名玩家最大的手牌

        List<Poke> compare = new ArrayList<Poke>();

        compare.add(players.get(0).handpoke.get(1));

        compare.add(players.get(1).handpoke.get(1));

        Collections.sort(compare, new PokeComparator());

        //通过输出的w值来判断哪名玩家获胜

        int w;

        if(compare.get(1).equals(players.get(0).handpoke.get(1))){

            w = 0;

        }else{

            w = 1;

        }

        System.out.println("------------------玩家"+players.get(w).name+"获胜!----------------");

        System.out.println("玩家各自的手牌为:");

        System.out.println(players.get(0).name+":"+players.get(0).handpoke.toString());

        System.out.println(players.get(1).name+":"+players.get(1).handpoke.toString());

    }

    public static void main(String[] args) {

        Shuffle s = new Shuffle();

        s.pokeCreate();

        s.pokeShuffle();

        Game g = new Game();

        g.createplayer();

        g.forEachPlayers();

        g.faPai(s);

        g.handPokeSort();

    }

}

******PokeComparator.java******************************************************

package com.PokeGame;

import java.util.Comparator;


public class PokeComparator implements Comparator<Poke> {

    @Override

    public int compare(Poke o1, Poke o2) {

        if(o1.pointsort.equals(o2.pointsort)){

            return o1.flowercolorsort.compareTo(o2.flowercolorsort);

        }else{

            return o1.pointsort.compareTo(o2.pointsort);

        }

    }

}


写回答 关注

5回答

  • Aokijic
    2018-08-13 12:50:28

    直接用compareTo方法好像比较J,Q,K,A大小好像有问题啊


    Aokiji...

    哦,懂了

    2018-08-13 12:54:24

    共 1 条回复 >

  • Hu丶0403
    2018-08-02 20:15:46

    我突然发现自己没有考虑到创建玩家ID相同的情况,大家做的时候记得考虑

  • SHFa
    2018-07-31 09:15:32

     players.get(0).handpoke.add(s.pokes.get(a));    这是什么意思啊??


    SHFa 回复Hu丶040...

    好的谢谢

    2018-08-04 08:48:35

    共 4 条回复 >

  • 慕码人4213627
    2018-07-30 18:36:07

    你好,我不太明白你的创建扑克牌的代码里i+"",j+""这部分,这样就是String类型了吗?而且我试了一下你的创建方法不知道为什么打印出来的扑克牌是地址

    http://img.mukewang.com/5b5ee93d00019b4e14400952.jpg

    Hu丶040...

    是的,相加之后就会变成String类型的了,你之所以打印出来的是地址应该是没有重写扑克牌类poke的toString方法

    2018-08-02 17:44:44

    共 1 条回复 >

  • Hu丶0403
    2018-07-28 06:37:24

    这是最终的运行结果

    https://img.mukewang.com/5b5b9ea1000185b910500815.jpg

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题