为了账号安全,请及时绑定邮箱和手机立即绑定

简易扑克牌游戏 定义了Constants、Main、Player、Poker四个类

标签:
Java

这是我自己写的一个简易扑克牌游戏程序,纯手打,实现了视频中的功能,
如果各位有看不懂的地方可以提出来我会加注释后重新上传。

Constants.java

常数类,用来存放卡牌的花色以及数字信息

package com.imooc;

public class Constants {
    public static final String[] pokerNumber = {
            "A", "2", "3", "4", "5", "6", "7",
            "8", "9", "10", "J", "Q", "K"};
    public static final String[] pokerSuit = {
            "黑桃", "红桃", "梅花", "方片", 
    };
}
Main.java

程序的主函数

package com.imooc;

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 Main {

    private List<Poker> pokers;

    private Player player1;

    private Player player2;

    private Scanner input;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Main main = new Main();
        main.play();
    }

    private void play() {
        System.out.println("-------------创建扑克牌-------------");
        init();

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

        System.out.println("-------------开始洗牌--------------");
        pokerShuffle();
        System.out.println("-------------洗牌结束!--------------");
        System.out.println(pokers.toString());

        System.out.println("-------------创建玩家--------------");
        System.out.println("请输入第一位玩家的ID和姓名");

        System.out.println("输入ID:");
        while(true){
            try{
                //重新为input赋值,否则将重复输出报错语句
                input = new Scanner(System.in);
                player1.setId(input.nextInt());
                break;
            }catch(InputMismatchException e) {
                System.out.println("请输入整数类型的ID!");
            }
        }

        System.out.println("输入姓名");
        player1.setName(input.next());

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

        System.out.println("输入ID:");
        while(true){
            try{
                //重新为input赋值,否则将重复输出报错语句
                input = new Scanner(System.in);
                player2.setId(input.nextInt());
                break;
            }catch(InputMismatchException e) {
                System.out.println("请输入整数类型的ID!");
            }
        } 

        System.out.println("输入姓名");
        player2.setName(input.next());

        System.out.println("-----欢迎玩家:" + player1.getName());
        System.out.println("-----欢迎玩家:" + player2.getName());

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

        System.out.println("-----玩家:" + player1.getName() + "-拿牌");
        System.out.println("-----玩家:" + player2.getName() + "-拿牌");
        System.out.println("-----玩家:" + player1.getName() + "-拿牌");
        System.out.println("-----玩家:" + player2.getName() + "-拿牌");

        System.out.println("-------------发牌结束!--------------");
        System.out.println("-------------开始游戏--------------");

        System.out.println("-----玩家:" + player1.getName() + "最大的手牌为:" 
                + player1.getMaxPoker().getPokerSuit()
                + player1.getMaxPoker().getPokerNumber());

        System.out.println("-----玩家:" + player2.getName() + "最大的手牌为:"
                + player2.getMaxPoker().getPokerSuit()
                + player2.getMaxPoker().getPokerNumber());

        Player winner = getWinner();

        if(winner != null)
            System.out.println("-------------玩家:" + winner.getName() + "获胜!--------------");
        else
            System.err.println("比较出错!");

        System.out.println("玩家各自的手牌为:");
        System.out.println(player1.getName() + "手牌为:" + player1.pokersOwnedByPlayer.toString());
        System.out.println(player2.getName() + "手牌为:" + player2.pokersOwnedByPlayer.toString());
    }

    private void init() {
        input = new Scanner(System.in);

        player1 = new Player();

        player2 = new Player();

        pokers = new ArrayList<Poker>(){
            /**
             * 在实例化pokers数组的过程中重写ArrayList的toString方法,
             * 按照指定格式输出所有卡牌。
             */
            @Override
            public String toString() {
                // TODO Auto-generated method stub
                StringBuilder builder = new StringBuilder();

                builder.append("为:[");

                String separator = "";

                for(Poker p : pokers){
                    builder.append(separator);
                    builder.append(p.getPokerSuit());
                    builder.append(p.getPokerNumber());
                    separator = ", ";
                }

                builder.append("]");

                return builder.toString();
            }   
        };

        for(int i = 0; i < 52; i++){
            pokers.add(new Poker(i / 13, i % 13));
        }
    }

    public void pokerShuffle() {
        Random random = new Random();

        for(int i = 0; i < pokers.size(); i++) {
            Collections.swap(pokers, i, random.nextInt(pokers.size()));
        } 
    }

    public void pokerDeal(int j) {
        int index = 0;

        for(int i = 0; i < j; i++){
            player1.pokersOwnedByPlayer.add(pokers.get(index++));
            player2.pokersOwnedByPlayer.add(pokers.get(index++));
        }
    }

    public Player getWinner() {
        switch(player1.getMaxPoker().compareTo(player2.getMaxPoker())) {
            case 1:
                return player1;
            case -1:
                return player2;
            default:
                return null;
        }
    }
}
Player.java

用来存储玩家信息的类

package com.imooc;

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

public class Player {

    private int mId;

    private String mName;

    private Poker maxPoker;

    public List<Poker> pokersOwnedByPlayer;

    public Player() {
        pokersOwnedByPlayer = new ArrayList<Poker>() {
            /**
             * 在实例化pokers数组的过程中重写ArrayList的toString方法,
             * 按照指定格式输出所有卡牌。
             */
            @Override
            public String toString() {
                // TODO Auto-generated method stub
                StringBuilder builder = new StringBuilder();

                builder.append("[");

                String separator = "";

                for(Poker p : pokersOwnedByPlayer){
                    builder.append(separator);
                    builder.append(p.getPokerSuit());
                    builder.append(p.getPokerNumber());
                    separator = ", ";
                }

                builder.append("]");

                return builder.toString();
            }   
        };

        maxPoker = new Poker();
    }

    public Player(int id, String name) {
        mId = id;
        mName = name;
        pokersOwnedByPlayer = new ArrayList<Poker>();
        maxPoker = new Poker();
    }

    public void setId(int id) {
        mId = id;
    }

    public void setName(String name) {
        mName = name;
    }

    public int getId() {
        return mId;
    }

    public String getName() {
        return mName;
    }

    public Poker getMaxPoker() {
        Collections.sort(pokersOwnedByPlayer);
        return pokersOwnedByPlayer.get(0);
    }

}
Poker.java

用来存储卡牌信息的类

package com.imooc;

public class Poker implements Comparable<Poker>{

    private int mPokerSuitNumber;

    private int mPokerNumber;

    public Poker(){

    }

    public Poker(int pokerSuitNumber, int pokerNumber){
        mPokerSuitNumber = pokerSuitNumber;
        mPokerNumber = pokerNumber;
    }

    public String getPokerSuit() {
        return Constants.pokerSuit[mPokerSuitNumber];
    }

    public String getPokerNumber() {
        return Constants.pokerNumber[mPokerNumber];
    }

    @Override
    public int compareTo(Poker p) {
        // TODO Auto-generated method stub
        if(this.mPokerNumber == p.mPokerNumber) {
            if(this.mPokerSuitNumber == p.mPokerSuitNumber)
                return 0;
            else if(this.mPokerSuitNumber < p.mPokerSuitNumber)
                return -1;
            else
                return 1;
        }else{
            if(this.mPokerNumber > p.mPokerNumber) {
                return -1;
            }
            else{
                return 1;
            }
        }
    }
}
点击查看更多内容
7人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消