ITPub博客

首页 > IT职业 > IT职场 > Java入门第三季—7.1扑克牌小游戏

Java入门第三季—7.1扑克牌小游戏

IT职场 作者:freefly 时间:2021-09-09 08:57:13 0 删除 编辑
PokerCard.java
package 扑克牌游戏;

public class PokerCard {

    private String color;
    private String point;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getPoint() {
        return point;
    }

    public void setPoint(String point) {
        this.point = point;
    }

    public PokerCard(){
        super();
    }

    public PokerCard(String color, String point) {
        super();
        this.color = color;
        this.point = point;
    }
}
Player.java
package 扑克牌游戏;

import java.util.HashSet;
import java.util.Set;

public class Player {

    private int id;
    private String name;
    private Set pokerCard;

    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 Set getPokerCard() {
        return pokerCard;
    }

    public void setPokerCard(Set pokerCard) {
        this.pokerCard = pokerCard;
    }

    public Player(int id, String name) {
        super();
        this.id = id;
        this.name = name;
        this.pokerCard = new HashSet();
    }

}
PokerGame.java
package 扑克牌游戏;

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

public class PokerGame {

    private static Scanner scanner;
    // 每个玩家的拿牌数
    private static final int CARDSNUM = 2;
    // 玩家数量
    private static final int PLAYERNUM = 2;

    // 花色数组
    private static final String[] COLORS = new String[] { "方片", "梅花", "红桃", "黑桃" };
    // 花色Map,以便于查找index,并通过index比较大小
    private static Map colorMap = new HashMap();
    // 点数数组
    private static final String[] POINTS = new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",
            "A" };
    // 点数Map,以便于查找index,并通过index比较大小
    private static Map pointMap = new HashMap();

    // 玩家数组
    private static final Player[] PLAYERS = new Player[PLAYERNUM];
    // 扑克牌List
    private static List pokerCards;

    // 玩家1的卡牌组
    private static PokerCard[] pokerCards0 = new PokerCard[CARDSNUM];
    // 玩家2的卡牌组
    private static PokerCard[] pokerCards1 = new PokerCard[CARDSNUM];

    public static void main(String[] args) {
        pokerCards = new ArrayList();
        scanner = new Scanner(System.in);
        // 开始游戏
        initGame();
        // 洗牌
        shuffle();
        // 创建玩家
        initPlayers();
        // 开始发牌
        dealCards();
        // 开始游戏
        playGame();
        // 显示玩家各自的手牌
        showPlayersCards();
        scanner.close();
    }

    private static void showPlayersCards() {
        System.out.println("----------玩家各自的手牌为----------");
        // 玩家1
        System.out.print("玩家" + PLAYERS[0].getName() + ":[");
        System.out.print(pokerCards0[0].getColor() + pokerCards0[0].getPoint()+", "+pokerCards0[1].getColor() + pokerCards0[1].getPoint());
        System.out.print("]n");
        // 玩家2
        System.out.print("玩家" + PLAYERS[1].getName() + ":[");
        System.out.print(pokerCards1[0].getColor() + pokerCards1[0].getPoint()+", "+pokerCards1[1].getColor() + pokerCards1[1].getPoint());
        System.out.print("]");

    }

    /**
     * 开始游戏
     */
    private static void playGame() {
        System.out.println("----------开始游戏----------");
        PokerCard pokerCard0;
        PokerCard pokerCard1;
        // 找到玩家1最大的牌
        Iterator iterator = PLAYERS[0].getPokerCard().iterator();
        for (int j = 0; j  iterator1 = PLAYERS[1].getPokerCard().iterator();
        for (int j = 0; j  colorMap.get(p2.getColor()) ? p1 : p2;
            }
        } else {
            return pointMap.get(p1.getPoint()) > pointMap.get(p2.getPoint()) ? p1 : p2;
        }
    }

    /**
     * 发牌
     */
    private static void dealCards() {
        System.out.println("----------开始发牌----------");
        for (int i = 0; i  0 && (i + 1) % 13 == 0) {
                System.out.println();
            }
        }
        System.out.print(pokerCards.get(51).getColor() + pokerCards.get(51).getPoint());
        System.out.print("]");
    }

    /**
     * 输入判断--如果输入为非数字,则报错并重新输入
     * 
     * @return
     */
    private static int inputInt() {
        int result = 0;
        while (true) {
            try {
                scanner = new Scanner(System.in);
                // 这里如果输入非int类型则会出现异常
                result = scanner.nextInt();
                break;
            } catch (Exception e) {
                // 捕获异常并进行提示
                System.out.println("请输入整型数字!");
                continue;
            }
        }
        return result;
    }
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1064/viewspace-2798108/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    73
  • 访问量
    662