扑克牌的面向对象建模

扑克牌的面向对象建模问题:
1,创建两个枚举类型:suit(花色),Rank(等级)
2,创建两个类:Card(牌),CardsSet(五张牌的集合)
3,要求CardsSet实现Comparable接口,按照德州扑克规则比较不同牌型的大小

创建两个枚举类型:suit(花色),Rank(等级)

public enum Light {
    suit(012345);
    SPADE (0);
    HEART (1);
    CLUB(2);
    DIAMOND( 3);
    BLACK (4);
    RED (5);

    Rank(100);
    FIRST(1);
    SECOND(2);
    THIRD(3);
    FOUR(4);
    FIVE(5);
    SIX(6);
    SEVEN(7);

    private int nCode;
    private Light(int _nCode) {
        this .nCode = _nCode;
    }
    @Override
    public
    String toString() {
        return String.valueOf(this .nCode);
    }
}

另一定义:

package com.syz.card;
 import java.io.Serializable; 
  public class Card implements Serializable{ 
             private static final long serialVersionUID = -4279016186402068401L;    
             private int suit; 
              private int rank;   
             public static final int SPADE = 0;     
             public static final int HEART = 1;     
              public static final int CLUB = 2;     
               public static final int DIAMOND = 3;      
               public static final int BLACK = 4;      
               public static final int RED = 5;
               private static final String[] suits = new String[] { "黑桃", "红桃", "梅花", "方块",            "小", "大" };     
               public static final int ACE = 1000;     
               public static final int TWO = 1001;     
               public static final int THREE = 1002;     
               public static final int FOUR = 1003;     
               public static final int FIVE = 1004;     
               public static final int SIX = 1005;     
               public static final int SEVEN = 1006;     
               public static final int EIGHT = 1007;     
               public static final int NINE = 1008;    
                public static final int TEN = 1009;     
                public static final int JACK = 1010;     
                public static final int QUEEN = 1011;     
                public static final int KING = 1012;     
                public static final int JOKER = 1013;
                private static final String[] ranks = new String[] { "A", "2", "3", "4",            "5", "6", "7", "8", "9", "10", "J", "Q", "K", "王" };    
                

创建两个类:Card(牌),CardsSet(五张牌的集合)

public Card(int suit, int rank) {      
  if (suit > -1 && suit < 6) {      
        if (suit < 4) {             
            if (rank < 1000 || rank > 1012) {     
                         throw new IllegalArgumentException("花色或点数参数错误!");            
                                 }    
                                                      }            
             else {  
                 if (rank != 1013) {    
                   throw new IllegalArgumentException("花色或点数参数错误!");                                                      
} 
else{
hrow new IllegalArgumentException("花色参数错误!");                 
this.suit = suit;  
his.rank = rank;     
public int getSuit() {  
return suit;
}                                                 
public int getRank()  
{                                     
return rank; 
} 
 @Override                     
public String toString() {       
return suits[suit] + ranks[rank -1000];                                                                                         
} 

写完之后,要new一张扑克牌,需要两个参数,suit和rank,而且suit和rank是有关系的黑红梅方可以对应A2345678910JQK,然后黑色小王,红色大王。这个在构造器里做了校验,如果不符合,则抛出参数错误异常。下面是测试类

CardClientpackage com.syz.card; 
import java.util.ArrayList;
import java.util.List; 
public class CardClient { 
   public static void main(String[] args) {        
   test2();         
    }           
     private static void test1() {                 
     Card c = new Card(Card.BLACK, Card.JOKER);                        
     System.out.println(c);                        
      } 
      private static void test2() {        
      int[] suits = new int[] {   
      Card.SPADE, Card.HEART, Card.CLUB,                Card.DIAMOND };       
           int[] ranks = new int[] {       
           Card.ACE, Card.TWO, Card.THREE, Card.FOUR,                Card.FIVE, Card.SIX, Card.SEVEN, Card.EIGHT, Card.NINE,                Card.TEN, Card.JACK, Card.QUEEN, Card.KING       };              
            List<Card> cards = new ArrayList<Card>();                
             for (int i = 0; i < suits.length; i++) {                        
             for (int j = 0; j < ranks.length; j++) {                                    
             cards.add(new Card(suits[i], ranks[j]));                                           
             }                                                
             }                                                   
              cards.add(new Card(Card.BLACK, Card.JOKER));                                                        cards.add(new Card(Card.RED, Card.JOKER));                                                           System.out.println(cards);                                                          
              }                                                       
               } 

测试结果如下:
[黑桃A, 黑桃2, 黑桃3, 黑桃4, 黑桃5, 黑桃6, 黑桃7, 黑桃8, 黑桃9, 黑桃10, 黑桃J, 黑桃Q, 黑桃K, 红桃A, 红桃2, 红桃3, 红桃4, 红桃5, 红桃6, 红桃7, 红桃8, 红桃9, 红桃10, 红桃J, 红桃Q, 红桃K, 梅花A, 梅花2, 梅花3, 梅花4, 梅花5, 梅花6, 梅花7, 梅花8, 梅花9, 梅花10, 梅花J, 梅花Q, 梅花K, 方块A, 方块2, 方块3, 方块4, 方块5, 方块6, 方块7, 方块8, 方块9, 方块10, 方块J,方块Q, 方块K, 小王, 大王]
这样一副扑克就创建成功了。

在这里插入图片描述

本次实验中存在很多不懂得地方,而且也有些功能并未被实现,不过,经过这次实验,也让我学会了很多,之后我会继续学习的,争取把不明白的弄得明白。

参考文章:https://blog.csdn.net/yunsyz/article/details/51886352

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值