首页 > 用户发贴区 > 编程问题提问区 > 求助…..扑克牌21点游戏代码Playing the Ace-High Gam
2007
01-20

求助…..扑克牌21点游戏代码Playing the Ace-High Gam

问题基本要求: 1:Ace表示11点,头像都表示10点,别的都不变. 2:开始发两张牌,如果超过21点就输. 3:电脑的前两张牌如果少于17点就会再要一张牌,直到超过17点.  4:如果点数一样那各再发一张牌,大的赢.如果还平局那再发一张,直到分出胜负.5:还有一些要求看下面吧,程序运行要求跟下面相似就行.


谢了,比较急


Problem


You have been tasked to develop a computer program that can play Ace-High, a card game similar to, but simpler than, blackjack. The rules and procedures are as follows.



  1. Cards have their standard values (a numerical card has its numerical value in the range of two to ten and face cards all have a value of ten), except that aces always have a value of eleven. To make the game simpler, the drawing of a card will be simulated by a function that will return an integer value in the range of 2 through 11, inclusive.

  2. The program begins by printing a suitable welcome message (see example).

  3. The player always draws first and always draws two cards initially. If the player “busts,” that is, if the combined value of the player’s two cards exceeds 21, then the player loses, the dealer does not draw, and the game is over.

  4. The dealer draws second. The dealer draws two cards initially. If the total of the two cards is less than 17, the dealer then “hits” (draws another card) until the total of her or his cards is greater than or equal to 17. If the dealer busts at any time (from the first two cards or as a result of drawing more cards), then the player wins and the game is over

  5. If at this point the dealer and player are tied, this forces a playoff in which both the dealer and player each draw a single card. If the dealer’s card is higher, the dealer wins; if the player’s card is higher, the player wins. If the dealer’s and player’s cards are equal, then the playoff is repeated (i.e., each draws another card) until either the dealer or player wins.

  6. The program displays as much of this information as is pertinent to the hand:


    • The value of each of the player’s initial two cards and their total value;

    • The value of each of the dealer’s initial two cards and their total value;

    • The value of the player’s card(s) during a playoff; 

    • The value of the dealer’s card(s) during a playoff;

    • A statement declaring whether the player or dealer is the winner.

  7. After the winner has been determined, the program asks if another game should be played. An answer of “y” or “Y” starts another game; any other answer terminates the game.

Example run

WELCOME TO THE ACE-HIGH GAME
Player draws 11 and 11, total 22.
Dealer wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 8 and 10, total 18.
Dealer draws 9 and 11, total 20.
Dealer wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 7 and 4, total 11.
Dealer draws 5 and 9, total 16.
Dealer draws 2, total 18.
Dealer wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 10 and 10, total 20.
Dealer draws 5 and 6, total 11.
Dealer draws 5 and 9, total 25.
Player wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 8 and 9, total 17.
Dealer draws 10 and 7, total 17.
Player draws 9.
Dealer draws 7.
Player wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 9 and 10, total 19.
Dealer draws 11 and 2, total 13.
Dealer draws 2, total 15.
Dealer draws 4, total 19.
Player draws 5.
Dealer draws 10.
Dealer wins.
Play again (y or n)? x
Game ended at player’s request.

Notes


In the program shell, there is a function named get_card() that will return a value in the range of 2 to 11 each time it is called. To use this function, store its value into an integer value like this:

int my_card;
//  my_card will be assigned a value in the range 2 to 11
my_card = get_card();

The program shell shows you where to place your programming with respect to the get_card() function.


Submissions


When your program is finished, name the source PD1(Name&No).cpp


求助…..扑克牌21点游戏代码Playing the Ace-High Gam》有 3 条评论

  1. 风之树叶 说:

    昏迷,这个还不简单.

    先定义一个 int card[54];

    一共54张牌,

    然后

    for(int i=0;i<54;i++)

        card[i]=i+1;

    然后将 card[54]  随机排序打乱.

    其中 card[54]的值为 1~54

    分别表示   方块 A~K 1~13

      梅花 A~K 14~26

    红桃 A~K 27~39

    黑桃 A~K 为 40~ 52

    小王为 53,大王为 54.

    int get_card()

    {

    随机生成一个 0~53 的 int a.

    if(card[a]==0) 重新生成一个a.

    然后 if(card[a]>52)

    card[a]=0; //表示这张牌被拿掉了

    return 10;

    else

    {

    int b=card[a]%13;

    card[a]=0;

    if(b==0|| b>10)

       return 10;

    if(b==1)

      return 11;

    else

      return b;

    }

    这个是主要的代码,其他的就是循环一下

    再有不懂的联系我好了, qq:330543128

     

     

    }

  2. kobe08 说:

    自己编了,就是运行不出来,有一个错误,哪位高手帮我改下,先谢了!!!

    //程序:ace.hpp
    //功能:21点游戏类头文件

    //类名:ACE_HIGH_GAME
    //功能:实现一个21点扑克牌游戏
    class ACE_HIGH_GAME{
    public:
     int get_card();
     
    private:
     int get_card;
    };

     

    //程序:ace.cpp
    //功能:21点游戏类实现文件。

    #include”ace.hpp”
    #include<cstdlib>

    //随机取得一张扑克牌,并得到其点数。
    int ACE_HIGH_GAME::get_card()
    {
     get_card=rand(2,11);
     return get_card;
    }

     

    #include”ace.hpp”
    #include <iostream.h>

    void main()
    {
      int p_card, p_card1, p_card2, p_card3;
      int d_card, d_card1, d_card2, d_card3, d_card4;
      char choice;
     
     do {
       cout<<”WELCOME TO THE ACE-HIGH GAME!\n”;

       p_card1=get_card();
       p_card2=get_card();
       p_card=p_card1+p_card2;
       cout<<”Player draws “<<p_card1<<” and “<<p_card2<<”, total “<<p_card<<endl;
         
          if(p_card>21){
        cout<<”Dealer wins.\n”;
       }else{
           d_card1=get_card();
           d_card2=get_card();
           d_card=d_card1+d_card2;
           cout<<”Dealer draws “<<d_card1<<” and “<<d_card2<<”, total “<<d_card<<endl;

              while(d_card<17){
            d_card3=get_card();
            d_card=d_card+d_card3;
                  cout<<”Dealer draws “<<d_card3<<”, total “<<d_card<<endl;
        }
          
           if(d_card>21){
            cout<<”Player wins.\n”;
        }else{
               while(d_card=p_card){
                p_card=get_card();
                d_card=get_card();
                cout<<”Player draws “<<p_card<<endl;
                cout<<”Dealer draws “<<d_card<<endl;
         }

               if(d_card<p_card){
                cout<<”Player wins.\n”;
         }else{
                cout<<”Dealer wins.\n”;
         }
              }

       }

           cout<<”Do you want to play again (y or n)?\n”;
           cin>>choice;

     
      } while(choice=’y'||’Y');

       cout<<”Game over! It is player’s request.\n”;
    }

     

    错误如下:

    ——————–Configuration: ace – Win32 Debug——————–
    Compiling…
    ace001.cpp
    e:\作业\c++程序课程设计题目\新建文件夹\ace.hpp(11) : error C2373: ‘get_card’ : redefinition; different type modifiers
            e:\作业\c++程序课程设计题目\新建文件夹\ace.hpp(8) : see declaration of ‘get_card’
    e:\作业\c++程序课程设计题目\新建文件夹\ace001.cpp(16) : error C2065: ‘get_card’ : undeclared identifier
    执行 cl.exe 时出错.

    ace001.obj – 1 error(s), 0 warning(s)

  3. xstar 说:

    ace.hpp中,get_card重复定义了,(变量和函数名都是同一个了,这个是不允许的)

    ace001.cpp中,get_card没定义,(类以及类里面的方法的使用方法自己看看)

留下一个回复