溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用java對(duì)一副撲克牌建模

發(fā)布時(shí)間:2020-08-01 10:26:32 來源:億速云 閱讀:237 作者:小豬 欄目:編程語言

這篇文章主要為大家展示了如何使用java對(duì)一副撲克牌建模,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來看看吧。

第一步:使用枚舉法對(duì)一副撲克牌(52張牌,不包括大小王)進(jìn)行建模:一副撲克牌共有四種花色分別是:黑桃,方塊,梅花,紅心;而每種花色的牌一共有13張;

import java.util.EnumMap;
import java.util.EnumSet;

public class EnumSetTest {
 //枚舉類型Suit
 enum Suit{"HEART ","SPADE ","DIAMOND ","CLUB "};
 enum Rank{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
 }

第二步:創(chuàng)建2個(gè)枚舉類型包含這兩類實(shí)例。

每張撲克牌Card對(duì)象都會(huì)有這兩個(gè)屬性,創(chuàng)建一個(gè)帶有這兩個(gè)枚舉類型屬性的類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;
 }
 
 /*
 * 若花色,牌面大小相同,則認(rèn)為是同一張牌
 * */
 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;
 }
 
 /* 以數(shù)字的方式,返回牌面的大小 */
 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<Card> vector=null;
 
 public CardHeap()
 {
 this.initialize(); 
 }
 
 /*初始化13*4=52張牌*/
 public void initialize()
 {
 vector=new Vector<Card>();
 Card tmp=null;
 for (int i = 0; i < 13; i++)//13 cards in Color Heart
 {
 tmp=new Card(CardSetting.COLOR[CardSetting.INDEX_HEART], CardSetting.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);
 } 
 }

以上就是關(guān)于如何使用java對(duì)一副撲克牌建模的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI