使用java对一副扑克牌建模

网友投稿 340 2022-12-08


使用java对一副扑克牌建模

本文实例为大家分享了java对一副扑克牌建模的具体代码,供大家参考,具体内容如下

第一步:使用枚举法对一副扑克牌(52张牌,不包括大小王)进行建模:一副扑克牌共有四种花色分别是:黑桃,方块,梅花,红心;而每种花色的牌一共有13张;

import java.util.EnumMap;

import java.util.EnumSet;

public class EnumSetTest {

//枚举类型Suit

enum Suit{"HEART ","SPADE ","DIAMOND "ogugFxz,"CLUB "};

enum Rank{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

}

第二步:创建2个枚举类型包含这两类实例。

每张扑克牌Card对象都会有这两个属性,创建一个带有这两个枚举类型属性的类Card

`package com.Cards.model;

package com.Cards.model;

/**

* @author Rock Lee

* @version 2012-10-21 16:51:49

* @see CardSetting.java

* @proposal Build A Class Contain All the info for ONE Card in the poker

* @fix 2012-12-12 13:01:04

* */

public class Card

{

private String color = null;

private String num = null;

private boolean visible = false;

private boolean red = false;

public Card(String color, String num)

{

this.color = color;

this.num = num;

// if the color is diamond or heart ,then red is true,else false

if (color.equals(CardSetting.COLOR[CardSetting.INDEX_HEART])

|| color.equals(CardSetting.COLOR[CardSetting.INDEX_DIAMOND]))

this.red = true;

else

this.red = false;

}

/*

* 若花色,牌面大小相同,则认为是同一张牌

* */

public boolean equals(Object obj)

{

Card anotherCard = (Card) obj;

return this.color.equals((anotherCard.color))

&& this.num.equals(anotherCard.num);

}

public void setVisiable(boolean visible)

{

this.visible = visible;

}

public boolean isVisible()

{

return visible;

}

public boolean isRed()

{

return red;

}

public String toString()

{

if (this == null)

return "EMPTY";

else

{

if (this.isVisible())

{

return (color + " " + num + " ");

}

else

return "███ ";

}

}

public String getColor()

{

return color;

}

/* 以数字的方式,返回牌面的大小 */

public int getNum()

{

char ch = num.charAt(0);

switch (ch)

{

case 'A':

return 1;

case 'J':

return 11;

case 'Q':

return 12;

case 'K':

return 13;

default:

return Integer.parseInt(num);

}

}

/* 以String的方式,返回牌面的大小 */

public String getNumInString()

{

return num;

}

}

第三步:初始化扑克牌排列顺序

public class CardHeap

{

private Vector vector=null;

public CardHeap()

{

this.initialize();

}

/*初始化13*4=52张牌*/

public void initialize()

{

vector=new Vector();

Card tmp=null;

for (int i = 0; i < 13; i++)//13 cards in Color Heart

{

tmp=new Card(CardSetting.COLOR[CardSetting.INDEX_HEART], CardSetting.http://NUM[i]);

tmp.setVisiable(false);

vector.add(tmp);

}

for (int i = 0; i < 13; i++)//13 cards in Color SPADE

{

tmp=new Card(CardSetting.COLOR[CardSetting.INDEX_SPADE], CardSetting.NUM[i]);

tmp.setVisiable(false);

vector.add(tmp);

}

for (int i = 0; i < 13; i++)//13 cards in Color DIAMOND

{

tmp=new Card(CardSetting.COLOR[CardSetting.INDEX_DIAMOND], CardSetting.NUM[i]);

tmp.setVisiable(false);

vector.add(tmp);

}

for (int i = 0; i < 13; i++)//13 cards in Color CLUB

{

tmp=new Card(CardSetting.COLOR[CardSetting.INDEX_CLUB], CardSetting.NUM[i]);

tmp.setVisiable(false);

vector.add(tmp);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot结合Redis哨兵模式的实现示例
下一篇:Springboot集成restTemplate过程详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~