手记

Java第三季简易扑克牌比较程序


由于点数是字符串类型,所以这个程序在10这个点数,会先比较1,就造成了10和6比较,6最大。另外当点数一样,比较花色功能也没实现,不说了,再去看一遍教程。
Cards.java 扑克牌类

/**
 * 扑克牌,里面有花色和点数
 * @author Administrator
 *
 */

public class Cards implements Comparable<Cards>{

    private String pattern;         //花色
    private String figure;          //点数
    public Cards(String pattern, String figure) {
        super();
        this.pattern = pattern;
        this.figure = figure;
    }
    public String getPattern() {
        return pattern;
    }
    public void setPattern(String pattern) {
        this.pattern = pattern;
    }
    public String getFigure() {
        return figure;
    }
    public void setFigure(String figure) {
        this.figure = figure;
    }
    @Override
    public String toString() {
        return pattern+figure;
    }
    @Override
    public int compareTo(Cards o) {
        return this.getFigure().compareTo(o.getFigure());
    }
}

Player.java 玩家类

import java.util.ArrayList;

/**
 * 这是一个玩家类,里面有玩家ID,姓名,手牌
 * @author Administrator
 *
 */

public class Player {

    private String ID;
    private String name;
    ArrayList<Cards> hand = new ArrayList<Cards>();
    public String getID() {
        return ID;
    }
    public void setID(String iD) {
        this.ID = iD;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public ArrayList<Cards> getHand() {
        return hand;
    }

    public void setHand(ArrayList<Cards> hand) {
        this.hand = hand;
    }
    public Player(String iD, String name) {
        super();
        this.ID = iD;
        this.name = name;
    }

}

Game.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Game {

    public static void main(String[] args) {

        ArrayList<Cards> cards = new ArrayList<Cards>();        //创建扑克牌
        ArrayList<Player> player = new ArrayList<Player>();     //创建玩家动态数组

        System.out.println("-----------创建扑克牌-----------");
        String[] pattern = {"黑桃", "红桃", "梅花", "方片"}; 
        String[] figure = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        for(int i=0;i<4;i++){
            for(int j=0;j<figure.length;j++){
                cards.add(new Cards(pattern[i], figure[j]));
            }
        }
        System.out.println("-----------扑克牌创建成功-----------");
        for (Cards card : cards) {
            System.out.print("为"+card+",");
        }
        System.out.println("----------开始洗牌-----------");
        Collections.shuffle(cards);         //打乱扑克牌顺序
        System.out.println("------------洗牌结束----------");
        System.out.println("--------创建玩家---------");
        Scanner input = new Scanner(System.in);
        //创建两个玩家
        for(int i=1;i<3;i++){
            System.out.println("请输入第"+i+"位玩家的姓名");
            System.out.println("输入ID");
            String id = input.next();
            System.out.println("输入姓名");
            String name = input.next();
            player.add(new Player(id, name));
        }
        for (Player player2 : player) {
            System.out.println("--------欢迎玩家"+player2.getName());
        }
        System.out.println("-----------开始发牌-----------");
        int num = 0;    //获取cards中的0~3的4张牌
        //每人拿两张牌
        for(int i=0;i<2;i++){
            System.out.println("------------玩家"+player.get(0).getName()+"拿牌");
            player.get(0).getHand().add(cards.get(num++));
            System.out.println("-----------玩家"+player.get(1).getName()+"拿牌");
            player.get(1).getHand().add(cards.get(num++));
        }
        System.out.println("----------开始游戏-----------");
        //玩家1中的手牌比较
        Collections.sort(player.get(0).getHand());
        System.out.println("玩家:"+player.get(0).getName()+"最大手牌为:"+player.get(0).getHand().get(1));
        //玩家2中的手牌比较
        Collections.sort(player.get(1).getHand());
        System.out.println("玩家:"+player.get(1).getName()+"最大手牌为:"+player.get(1).getHand().get(1));
        //compareTo返回值大于0表示前一个数据比后一个数据大, 0表示相等,小于0表示第一个数据小于第二个数据
        int i = player.get(0).getHand().get(1).compareTo(player.get(1).getHand().get(1));
        if(i>0){
            System.out.println("-----------玩家"+player.get(0).getName()+"获胜------------");
        }else if(i<0){
            System.out.println("----------玩家"+player.get(1).getName()+"获胜-------------");
        }else{
            System.out.println("------------双方平手------------");
        }
        System.out.println("玩家各自的手牌为");
        for (Player player2 : player) {
            System.out.println(player2.getName()+":"+player2.getHand());
        }
    }

}
0人推荐
随时随地看视频
慕课网APP