王建国66 2022-09-13 20:34 采纳率: 77.8%
浏览 124
已结题

card类与循环建立二十一点扑克游戏,共四问,需要一个模板

利用Card类和循环语句编写一个扑克牌游戏
编写“21点”的扑克游戏
a) 计算机随机地向用户发5张牌,如果牌的总点数小於或等於21点,则用户赢;超过21点则计算机赢。

注意类的没有参数的构造器随机产生一张牌,并且不会重复发牌。类模仿真实牌局的行为,可以用restart

方法从一副牌中重复抽取牌。

你的程序应该:

抽取一张牌;

向用户显示这张牌;

显示到目前为止已发牌的总点数

b) 为使程序更象真实的牌局,用户应该能与计算机互动玩牌。修改刚才的程序,计算机仍然抽取5张牌,不超过21点的是赢家,如果双方都不超过21点,则双方都不算赢,你的程序应该能准确地报告结果。

程序应该:

为用户和计算机抽牌;

向用户显示牌;

分别显示用户和计算机的当前总点数;

为使游戏更加有趣,每抽一张新牌时都要显示牌和总点数

c) 修改程序使用户可以选择是否抽牌,而规定计算机必须抽5张牌或直到牌的总点数大於等於21

d)修改程序使得用户可以一直玩下去,并分别累计用户和计算机赢的牌局数。

package java.lancs ;

/**

  • Represents a playing card
  • @author Roger Garside/John Mariani
  • @version Last Rewritten: 16th Sept 1997
  • /

import java.util.* ;

public class Card
{
// Card Class Variables

private static boolean initialised = false ;
private static Random rand = new Random() ;
private static boolean[][] dealt = new boolean[4][13] ;
private static int noDealt ;

// Card Instance Variables

/*
 * the suit of the card (0 to 3)
 */
private int suit ;
/*
 * the value of the card (0 to 12)
 */
private int value ;

// Card Class Constants

/**
* Constant - Spades
*/
public static final int SPADES = 0 ;
/**
* Constant - Hearts
*/
public static final int HEARTS = 1 ;
/**
* Constant - Clubs
*/
public static final int CLUBS = 2 ;
/**
* Constant - Diamonds
*/
public static final int DIAMONDS = 3 ;

// Card Constructor Methods

/**
 * Creates an instance of the Card class with random values
 * (if all have been dealt, then starts again)
 */
public Card()
{
    if (noDealt == 52)
        {
    System.err.println("all 52 cards dealt") ;
        initialised = false ;
        }
if (!initialised)
    {
    for (int i = 0 ; i < 4 ; i++)
        for (int j = 0 ; j < 13 ; j++)
        dealt[i][j] = false ;
    initialised = true ;
    noDealt = 0 ;
    }
int s, v ;
do
    {
    s = (int) (Math.abs(rand.nextInt()) % 4) ;
    v = (int) (Math.abs(rand.nextInt()) % 13) ;
    }
    while (dealt[s][v]) ;
dealt[s][v]  = true ;
    suit = s ;
value = v ;
    noDealt++ ;
} // end of constructor method

/**
 * Creates an instance of the Card class with specified values
 * (if all have been dealt, then starts again)
 * @param s suit of the card
 * @param v value of the card
 */
public Card(int s, int v)
{
    if (noDealt == 52)
        {
    System.err.println("all 52 cards dealt") ;
        initialised = false ;
        }
if (!initialised)
    {
    for (int i = 0 ; i < 4 ; i++)
        for (int j = 0 ; j < 13 ; j++)
        dealt[i][j] = false ;
    initialised = true ;
    noDealt = 0 ;
    }
    if ((s < 0) || (s > 3))
        {
        System.out.println("invalid suit") ;
        System.exit(1) ;
        }
    if ((v < 0) || (v > 12))
        {
        System.out.println("invalid value") ;
        System.exit(1) ;
        }
    if (dealt[s][v]) ;
        {
        System.out.println("card already used") ;
        System.exit(1) ;
        }
dealt[s][v]  = true ;
    suit = s ;
value = v ;
    noDealt++ ;
} // end of constructor method

// Card Instance Methods - Selectors

/**
 * returns the suit attribute of the card
 * @return the suit attribute of the card
 */
public int getSuit()
{
return suit ;
} // end of method getSuit

/**
 * returns the suit attribute of the card as a string
 * @return the suit attribute of the card as a string
 */
public String getSuitString()
{
switch (suit)
    {
    case SPADES :
    return "Spades" ;
    case HEARTS :
    return "Hearts" ;
    case CLUBS :
    return "Clubs" ;
    case DIAMONDS :
    return "Diamonds" ;
        default :
    return "Unknown" ;
        }
} //end of method getSuitString

/**
 * returns the value attribute of the card
 * @return the value attribute of the card
 */
public int getValue()
{
return value ;
} // end of class getValue

/**
 * returns the value attribute of the card as a string
 * @return the value attribute of the card as a string
 */
public String getValueString()
{
switch (value)
    {
    case 0 :
    return "Ace" ;
    case 1 :
    return "two" ;
    case 2 :
    return "three" ;
    case 3 :
    return "four" ;
    case 4 :
    return "five" ;
    case 5 :
    return "six" ;
    case 6 :
    return "seven" ;
    case 7 :
    return "eight" ;
    case 8 :
    return "nine" ;
    case 9 :
    return "ten" ;
    case 10 :
    return "Jack" ;
    case 11 :
    return "Queen" ;
    case 12 :
    return "King" ;
    default :
    return "Unknown" ;
        }
} //end of method getValueString

// Other Card Methods

/**
 * Resets the deck of cards to all undealt
 */
public void restart()
{
for (int i = 0 ; i < 4 ; i++)
    for (int j = 0 ; j < 13 ; j++)
    dealt[i][j] = false ;
    noDealt = 0 ;
    } // end of method restart

/**
 * return a string representing the card
 * @return details of the card ('value of suit')
 */
public String toString()
    {
    return getValueString() + " of " + getSuitString() ;
    } // end of method toString

/* public void setSuit(int s)
{
// s should be in the range 0 to 3
if ((s < 0) || (s > 3)) suit = 0;
else suit = s;
} // end of method setSuit

public void setValue(int v)
{
// value should be in the range 0 to 12
if ((v < 0) || (v > 12)) value = 0;
else value = v;
} // end of method setValue */

} // end of class card
  • 写回答

4条回答 默认 最新

  • Joanofarc_alter 2022-09-13 21:34
    关注

    问一下计算点数方面是按照标准的游戏规则进行计算还是说只需要在其对应的值上+1即可?如(A为1点 K为13点 )百度百科上对于21点的点数计算规则与此不同

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 9月27日
  • 已采纳回答 9月19日
  • 修改了问题 9月13日
  • 创建了问题 9月13日

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站