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

扑克牌小游戏,请大家多多指教

package com.pukerGame;

import java.util.*;

public class PuKer {

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


    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 PuKer(String color,String point){
        this.color = color;
        this.point = point;
    }
    public PuKer(){

    }

    /**
     * 获得String数组对应元素的下标
     * @param str
     * @return
     */

    public int getIndex(String str){
        //String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        int i = 0;
        while (i<points.length){
            if(points[i].equals(str)){
                return i;
            }
            i++;
        }
        return i;
    }

    public PuKer compare(PuKer puKer) {
        //通过比较在数组中的下标来比较扑克牌的点数大小
        int a = getIndex(this.getPoint());
        int b = getIndex(puKer.getPoint());
        if(a>b) {
            return this;
        }
        if(a<b){
            return puKer;
        }else {
            if (this.color.equals("黑桃")){
                return this;
            }else if(puKer.color.equals("黑桃")){
                return puKer;
            }else if(this.color.equals("红桃")){
                return this;
            }else if(puKer.color.equals("红桃")) {
                return puKer;
            }else if(this.color.equals("梅花")){
                return this;
            }else if(puKer.color.equals("梅花")) {
                return puKer;
            }else
                return this;
            }

        }



    public List<PuKer> creatADeck(){
        System.out.println("-------------创建扑克牌---------------------");
        List<PuKer> puKerList = new ArrayList<>();

        for (int i = 0 ; i < points.length; i++){
            puKerList.add(new PuKer("黑桃",points[i]+""));
        }
        for (int i = 0 ; i < points.length; i++){
            puKerList.add(new PuKer("红桃",points[i]+""));
        }
        for (int i = 0 ; i < points.length; i++){
            puKerList.add(new PuKer("梅花",points[i]+""));
        }
        for (int i = 0 ; i < points.length; i++){
            puKerList.add(new PuKer("方片",points[i]+""));
        }
        System.out.println("------------扑克牌创建成功----------------");
        System.out.println(puKerList.toString());
        System.out.println("--------------开始洗牌------------------");
        Collections.shuffle(puKerList);
        System.out.println(puKerList.toString());
        return puKerList;
    }

    @Override
    public String toString() {
        return  color +
                 point ;
    }
}
package com.pukerGame;

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

public class Player {

    private int ID;
    private String name;
    public List<PuKer> puKerList;
    public Scanner input;

    public Player() {
        input = new Scanner(System.in);
        this.puKerList = new ArrayList<>();


    }


    public Player(int id, String name) {
        this.ID = id;
        this.name = name;
        this.puKerList = new ArrayList<>();
    }

    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;
    }

    /**
     * ID 输入异常处理
     * @return
     */
    public int inputID() {
        for (; ; ) {
            try {
                Scanner in = new Scanner(System.in);
                System.out.println("输入ID:");
                int id = in.nextInt();
                return id;
            } catch (InputMismatchException e) {
                System.out.println("请输入整数类型的ID!");
                continue;
            }
        }
    }

    public void playGame() {
        //创建两位玩家
        Scanner input = new Scanner(System.in);
        System.out.println("请输入第一位玩家的ID和姓名:");
        Player player1 = new Player();
        player1.setID(inputID());
        System.out.println("请输入姓名:");
        player1.setName(input.next());
        System.out.println("请输入第二位玩家的ID和姓名:");
        Player player2 = new Player();
        player2.setID(inputID());
        System.out.println("请输入姓名:");
        player2.setName(input.next());
        System.out.println(player1.getID() + "." + player1.getName());
        System.out.println(player2.getID() + "." + player2.getName());
        //创建一副扑克牌
        PuKer puKer = new PuKer();
        List<PuKer> puKers = puKer.creatADeck();
        System.out.println("-------------开始发牌--------------");
        System.out.println(player1.getName() + "-拿牌");
        player1.puKerList.add(puKers.get(0));
        System.out.println(player2.getName() + "-拿牌");
        player2.puKerList.add(puKers.get(1));
        System.out.println(player1.getName() + "-拿牌");
        player1.puKerList.add(puKers.get(2));
        System.out.println(player2.getName() + "-拿牌");
        player2.puKerList.add(puKers.get(3));
        System.out.println("-------------发牌结束--------------");

        System.out.println("-------------开始游戏--------------");
        PuKer puKer1 = puKers.get(0).compare(puKers.get(2));
        System.out.println("玩家"+player1.getName()+"最大的手牌为:" +puKer1.toString());
        PuKer puKer2 = player2.puKerList.get(0).compare(player2.puKerList.get(1));
        System.out.println("玩家"+player2.getName()+"最大的手牌为:" +puKer2.toString());
        PuKer puKer3 = puKer1.compare(puKer2);
        if(puKer3 ==puKer1){
            System.out.println("------玩家:"+player1.getName()+"获胜--------");
        }else {
            System.out.println("------玩家:"+player2.getName()+"获胜--------");
        }
        System.out.println("玩家各自的手牌为:");
        System.out.println(player1.puKerList.toString());
        System.out.println(player2.puKerList.toString());

    }
}
package com.pukerGame;



public class Game {
    public static void main(String[] args) {

        Player player = new Player();
        player.playGame();

    }
}


正在回答

举报

0/150
提交
取消

扑克牌小游戏,请大家多多指教

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信