继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java入门第三季 简易扑克牌游戏 源代码

Hsinwong
关注TA
已关注
手记 4
粉丝 27
获赞 133
示例

图片描述

源码

Poker.java

package com.immoc.poker;

public class Poker implements Comparable<Poker> {
    private int no;
    private String name;

    public Poker(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public int compareTo(Poker o) {
        return Integer.compare(no, o.no);
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Poker{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}

PokerGenerator.java

package com.immoc.poker;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class PokerGenerator {
    public static final String[] SUITS = {"方片", "梅花", "红桃", "黑桃"};
    public static final String[] KINDS = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};

    public static List<Poker> ascOrder() {
        List<Poker> pokers = new LinkedList<>();

        for (int i = 0; i < SUITS.length; i++) {
            for (int j = 0; j < KINDS.length; j++) {
                pokers.add(new Poker(i + j * SUITS.length, SUITS[i] + KINDS[j]));
            }
        }

        return pokers;
    }

    public static List<Poker> randomOrder() {
        List<Poker> pokers = ascOrder();

        Collections.shuffle(pokers);

        return pokers;
    }
}

Player.java

package com.immoc.poker;

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

public class Player {
    private int id;
    private String name;
    private List<Poker> pokers = new ArrayList<>();

    public Player(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Poker> getPokers() {
        return pokers;
    }

    @Override
    public String toString() {
        return "Player{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pokers=" + pokers +
                '}';
    }
}

Main.java

package com.immoc.poker;

import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("开始洗牌……");
        List<Poker> pokers = PokerGenerator.randomOrder();
        int index = 0;
        System.out.println("洗牌结束!");

        System.out.println("请输入第1位玩家的ID和姓名");
        Player player1 = new Player(getPlayerNo(input), getPlayerName(input));
        System.out.println("请输入第2位玩家的ID和姓名");
        Player player2 = new Player(getPlayerNo(input), getPlayerName(input));

        System.out.println("开始发牌……");
        for (int i = 0; i < 2; i++) {
            player1.getPokers().add(pokers.get(index++));
            player2.getPokers().add(pokers.get(index++));
        }
        System.out.println("发牌结束!");

        System.out.println("开始游戏……");
        Collections.sort(player1.getPokers());
        Poker player1MaxPoker = player1.getPokers().get(player1.getPokers().size() - 1);
        System.out.printf("玩家【%s】最大的手牌为:%s%n", player1.getName(), player1MaxPoker.getName());
        Collections.sort(player2.getPokers());
        Poker player2MaxPoker = player2.getPokers().get(player2.getPokers().size() - 1);
        System.out.printf("玩家【%s】最大的手牌为:%s%n", player2.getName(), player2MaxPoker.getName());
        Player winner = player1MaxPoker.getNo() > player2MaxPoker.getNo() ? player1 : player2;
        System.out.printf("游戏结束!玩家【%s】获胜!%n%n", winner.getName());

        System.out.println("手牌公布……");
        System.out.printf("玩家【%s】:%s%n", player1.getName(), player1.getPokers());
        System.out.printf("玩家【%s】:%s%n", player2.getName(), player2.getPokers());
        System.out.println("牌堆公布……");
        System.out.println(pokers);

        input.close();
    }

    private static int getPlayerNo(Scanner input) {
        int playerNo;

        while (true) {
            System.out.print("ID:");
            try {
                playerNo = input.nextInt();
                break;
            } catch (InputMismatchException e) {
                System.out.println("请输入整数类型的ID!");
            }
        }

        return playerNo;
    }

    private static String getPlayerName(Scanner input) {
        System.out.print("姓名:");
        return input.next();
    }
}
打开App,阅读手记
10人推荐
发表评论
随时随地看视频慕课网APP

热门评论

Main.java文件 的private static int getPlayerNo(Scanner input)方法有问题,“return playerNo;”应该在上面的大括号的内部,“return playerNo;”下面的大括号应该包含private static String getPlayerName(Scanner input)方法。

另外,PokerGenerator.java文件的public static List<Poker> ascOrder( )方法for循环实现的Listy元素排列有问题(逻辑问题,非代码问题):实现的是先比较花色然后比较大小的元素序列,导致 例如:方片K 小于 黑桃2


查看全部评论