0

所以我写了一个模拟骰子游戏的程序,几乎就像掷骰子的游戏。我想把这个普通的控制台应用程序变成一个基于对话的 MFC。所以我会创建一个标记为“滚动”的按钮,窗口会显示两个骰子正在滚动。这是我为 condole 应用程序提供的代码。它工作得很好,但我很困惑如何将代码分配给按钮以及如何将模具面分配给其各自的位图。感谢您的帮助!

//Game of chance "Craps"

#include "stdafx.h"
using namespace std;

void dice(void)
{
    //variables
    string  input;
    int     i,roll,gamePoint,val,val_1,val_2,roll_1,roll_2,sumRoll;
    long    winnings,losses,wager,odds,oddsNumer,oddsDenom;
    bool    A=false;
    //declarations
    roll=0;gamePoint=0;val=0;val_1=0;val_2=0;roll_1=0;roll_2=0;sumRoll=0;
    //user input
    cout<<"Enter your odds ratio:\nNumerator: ";
    cin>>oddsNumer;
    cout<<setw(8)<<"Denominator: ";
    cin>>oddsDenom;
    cout<<"Enter your wager for the game: ";
    cin>>wager;
    odds=abs((oddsNumer/oddsDenom)+1);
    winnings=abs(odds*wager-wager);
    cout<<winnings;
    //dice output
    do
    {
        srand (unsigned(time(NULL)));
        sumRoll=0;
        for(i=0;i<2;i++)
        {
            val_1=rollDice();
            //cout<<"***test1***";
            switch (val_1)
            {//start switch
                case 1:
                    dieFace(val_1);
                    break;
                case 2:
                    dieFace(val_1);
                    break;
                case 3:
                    dieFace(val_1);
                    break;
                case 4:
                    dieFace(val_1);
                    break;
                case 5:
                    dieFace(val_1);
                    break;
                case 6:
                    dieFace(val_1);
                    break;
            }//end switch
            sumRoll=val_1+sumRoll;
        }//end for loop dice roll
        cout<<endl<<endl<<"\tYou rolled a: "<<sumRoll<<endl;
        system("pause");
        if (sumRoll==7 ||sumRoll==11)
        {
            cout<<"Hurray! You win!\n"<<
            "Your total winnings: $"<<winnings<<endl;
            A=true;
            system("pause");
        }//and if winner statement
        else if(sumRoll==2 ||sumRoll==3||sumRoll==12)
        {
            losses=abs(odds*wager);
            cout<<"Sorry, you lose. The house wins. Total lost is $"<<
            losses<<endl;
            A=true;
            system("pause");
        }
        else
        {

            gamePoint=sumRoll;
            roll=0;

            do
            {
                srand (unsigned(time(NULL)));
                sumRoll=0;
                roll++;
                for(i=0;i<2;i++)
                {
                    val_1=rollDice();

                    switch (val_1)
                    {//start switch
                        case 1:
                            dieFace(val_1);
                            break;
                        case 2:
                            dieFace(val_1);
                            break;
                        case 3:
                            dieFace(val_1);
                            break;
                        case 4:
                            dieFace(val_1);
                            break;
                        case 5:
                            dieFace(val_1);
                            break;
                        case 6:
                            dieFace(val_1);
                            break;
                    }//end switch
                    sumRoll=val_1+sumRoll;
                }//end for loop dice roll
                cout<<endl<<endl<<"\tYou rolled a: "<<sumRoll<<endl;
                system("pause");
                if (sumRoll==7)
                {
                    losses=abs(wager*roll*odds);
                    cout<<"Sorry, you lose. The house wins."<<
                    "Total lost is $"<<losses<<endl;
                    A=true;
                    system("pause");
                }
                else if(sumRoll==gamePoint)
                {
                    winnings=abs(wager*roll*odds-wager);
                    cout<<"Hurray! You win!\n"<<
                    "Your total winnings are $"<<winnings<<endl;
                    A=true;
                    system("pause");
                }
            }while(A==false);
        }
    }while(A==false);
    system("pause");
    system("cls");
    return ;
}

int rollDice(void)
{
    //srand (unsigned(time(0)));
    return((rand()%6)+1);
}
void dieFace(int val)
{

    switch (val)
    {//start switch
        case 1:
            cout<<endl<<endl<<endl;
            cout<<"     *************"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     *     O     *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     *************"<<endl<<endl;

            break;
        case 2:
            cout<<endl<<endl<<endl;
            cout<<"     *************"<<endl;
            cout<<"     *         O *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     * O         *"<<endl;
            cout<<"     *************"<<endl<<endl;

            break;
        case 3:
            cout<<endl<<endl<<endl;
            cout<<"     *************"<<endl;
            cout<<"     *         O *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     *     O     *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     * O         *"<<endl;
            cout<<"     *************"<<endl<<endl;

            break;
        case 4:
            cout<<endl<<endl<<endl;
            cout<<"     *************"<<endl;
            cout<<"     * O       O *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     * O       O *"<<endl;
            cout<<"     *************"<<endl<<endl;

            break;  
        case 5:
            cout<<endl<<endl<<endl;
            cout<<"     *************"<<endl;
            cout<<"     * O       O *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     *     O     *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     * O       O *"<<endl;
            cout<<"     *************"<<endl<<endl;

            break;
        case 6:
            cout<<endl<<endl<<endl;
            cout<<"     *************"<<endl;
            cout<<"     * O       O *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     * O       O *"<<endl;
            cout<<"     *           *"<<endl;
            cout<<"     * O       O *"<<endl;
            cout<<"     *************"<<endl<<endl;

            break;
    }
    return  ;
}
4

1 回答 1

1

您需要添加一个事件处理程序来处理按钮控件的鼠标单击。假设您在对话框中添加了一个按钮并为其指定了资源 id IDC_ROLLDICE。要添加事件处理程序,请右键单击您添加的按钮并选择“添加事件处理程序”。将弹出一个窗口,要求您填写一些信息。在“消息类型”下确保BN_CLICKED已选中。这将告诉 MFC 消息调度程序您对该特定事件感兴趣。在标有“函数处理程序名称”的字段中为其命名。添加完所有信息后,单击“添加和编辑”。这将为您的 CDialog 派生类添加一个成员函数,并在消息映射中添加一个条目以确保它被调用。它应该如下所示

void CMyDialog::OnBnClickedRolldice()
{
// TODO: Add your control notification handler code here
}

现在,每当您单击按钮时,MFC 都会调用OnBnClickedRolldice. 只需开始添加代码以将骰子掷入该成员函数并根据需要添加成员变量。

基本上,您将通过成员变量和函数将整个应用程序移动到 CDialog 派生类中。

于 2013-04-23T05:37:28.723 回答