• 文章
  • Powerball 彩票模擬器
釋出
2013 年 12 月 1 日 (最後更新:2013 年 12 月 5 日)

Powerball 彩票模擬器

評分:3.9/5 (194 票)
*****
這是我寫的第一段程式,除了我關於程式設計的筆記之外,沒有任何幫助。我打算在此程式碼的基礎上繼續新增,但我認為你們也許能幫我格式化程式碼,使其易於其他程式設計師閱讀。我決定編寫一個模擬 Powerball 彩票的程式。我成功地讓程式執行起來,並且我不斷地對其進行改進,以便使用者能夠輕鬆理解程式中的內容。我的問題很簡單,專家程式設計師可以回答。首先,請執行我的程式,並告訴我介紹性選單的佈局是否有改進的空間。其次,我決定新增很多註釋,因為我認為我需要養成寫註釋的習慣,這樣當我進入這個行業時,它們可以幫助其他程式設計師理解我的邏輯。如果您閱讀了我程式中的註釋,請告訴我它們是否易於理解正在發生的事情的流程。正如我之前提到的,這個程式執行良好,我只需要關於如何組織我的註釋的建議,或者您是否可以告訴我是否可以使用與程式更相關的使用者定義變數名或函式名。

**更新**
我最初是在 Mac OS X 上使用 Xcode 編寫這個程式的。我嘗試在 Windows 7 MinGW 上執行它,但它未能執行,我認為這是因為我的陣列沒有常量變數。有什麼建議嗎?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//
//  PowerBall.cpp
//
//  Created by Leonardo Rocha on 11/29/13.
//  Copyright (c) 2013 Leonardo Rocha. All rights reserved.
//

#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;

//Function Protoypes
void setSeed();                         //This will set the seed for the random numbers
int randomWhiteNumber();                //This will get a random number 1-59
int randomRedNumber();                  //This will get a random number 1-35
bool checkArray(int [], int, int);      //This will check to see if a random number has already been found
int displayMenu();                      //This will display the intro menu and let the user buy tickets
int won(int, bool);                     //This function determines how much money a ticket won
//global constants
const int SIZE = 6;                     //This is the number of balls per ticket
const int MAX_WHITE = 58;               //This is the range of the white balls
const int MAX_RED = 34;                 //This is the range of the red balls
const int waysToWin = 9;

int main(int argc, const char * argv[])
{
    int tickets = displayMenu();        //Welcome screen lets you buy tickets
    int spending = tickets * 2;         //Charges you for the tickets
    int randomTickets[tickets][SIZE];   //Ticket number holders
    int randomPowerNumber[SIZE];        //Power ball numbers
    int arrayHolder[SIZE];              //Temporary ticket holder
    int ballCounter = 0;                //Keeps track of winning balls in a ticket
    bool redBall;                       //Keeps track if the red ball matches
    int cashWon = 0;                    //Cash won Accumalator
    int lost = 0;
    int winningTickets[waysToWin];
    setSeed();                          //Sets random seed
    
    /*
     The next set of instructions will randomly generates the first 5 white balls and the last ball is the red ball
                                White balls are 1-59 and the red ball are 1-35
     */
    
    cout<<"Powerballs: ";
    for(int i = 0; i<SIZE; i++)
    {
        randomPowerNumber[i] = randomWhiteNumber();                   //Randomly generates a number 1-59
        while(checkArray(randomPowerNumber, i, randomPowerNumber[i])) //If random number is in use generate another random number
        {
            randomPowerNumber[i] = randomWhiteNumber();               //Randomly generates a number 1-59
        }
        if(i == (SIZE - 1))                                           //If the increment is the last one it is the red ball
        {
            randomPowerNumber[i] = randomRedNumber();                 //So it generates a number 1-35
        }
        cout<<"["<<randomPowerNumber[i]<<"] ";                        //This displays the number at current increment
    }
    cout<<endl;
    
    cout<<"Your tickets\n";
    
    
    /*
     The following code is a nested loop the first loop indicates the ticket we are at
     the second loop indicates the number we are at in the current ticket.
     The number of tickets is user defined. The main purpose of the code is to generate random numbers
     for a ticket to simulate quick picks.
     */
    for(int i = 0; i < tickets; i++)
    {
        ballCounter = 0;                //This counter keeps track of all the numbers that match the white power balls
        redBall = false;                //This boolean variable keeps track if you have a redball match
        for(int j = 0; j < SIZE; j++)
        {
            arrayHolder[j] = randomWhiteNumber();               //This will generates a random number 1-59 at current location
            while(checkArray(arrayHolder, j, arrayHolder[j]))   //If number generated is in use enter while loop
            {
                arrayHolder[j] = randomWhiteNumber();           //Generate a random number for current location
            }
            if(j == (SIZE - 1))                                 //If current location is the red ball generate a number 1-35
            {
                arrayHolder[j] = randomRedNumber();
            }else{
                if(checkArray(randomPowerNumber, (SIZE-1), arrayHolder[j]))       //If current number generated is a white ball
                    ballCounter++;                                         //Check to see if the number is a powerball number if
            }                                                              //it matches increment ball counter
            randomTickets[i][j] = arrayHolder[j];                          //Stores number in ticket
            cout<<"["<<randomTickets[i][j]<<"] ";                          //Display number to screen
        }
        
        cout<<"\nWhiteballs match : "<<ballCounter<<" ";     //This displays the number of white balls that match the powerballs
                                                             //White balls
        
        //This checks to see if the tickets redball matches the powerballs redball
        if(arrayHolder[SIZE - 1] == randomPowerNumber[SIZE-1])
        {
            cout<<"The red ball matches too!";
            redBall = true;
        }
        
        cashWon += won(ballCounter, redBall);       //This adds the cash won from the ticket to your cash fund
        
        
        //The following else if statements just keeps track of how many tickets of each level actually won
        if(ballCounter == 5 && redBall)
            winningTickets[0]++;
        else if(ballCounter == 5)
            winningTickets[1]++;
        else if(ballCounter == 4 && redBall)
            winningTickets[2]++;
        else if(ballCounter == 4)
            winningTickets[3]++;
        else if(ballCounter == 3 && redBall)
            winningTickets[4]++;
        else if(ballCounter == 3)
            winningTickets[5]++;
        else if(ballCounter == 2 && redBall)
            winningTickets[6]++;
        else if(ballCounter == 1 && redBall)
            winningTickets[7]++;
        else if(redBall)
            winningTickets[8]++;
        else
            lost++;
        cout<<endl;
    }
    //The folling code outputs to the user the number of tickets that won and display the number of losing tickets
    cout<<"You have "<<winningTickets[0]<<" tickets that is a grand prize winner"<<endl;
    cout<<"You have "<<winningTickets[1]<<" tickets winning $1,000,000"<<endl;
    cout<<"You have "<<winningTickets[2]<<" tickets winning $10,000"<<endl;
    cout<<"You have "<<winningTickets[3] + winningTickets[4]<<" tickets winning $100"<<endl;
    cout<<"You have "<<winningTickets[5] + winningTickets[6]<<" tickets winning $7"<<endl;
    cout<<"You have "<<winningTickets[7] + winningTickets[8]<<" tickets winning $4"<<endl;
    cout<<"You have "<<lost<<" losing tickets"<<endl;
    
    //The following code outputs how much money you spent and how much money you won
    cout<<"You spent $"<<spending<<endl;
    cout<<"You won $"<<cashWon<<endl;
}

/****************************************************************************
 *                          displayMenu()                                   *
 *    The displayMenu function outputs intro menu to screen and it allows   *
 *    The user to buy 1 to 250000 tickets                                   *
 ***************************************************************************/
int displayMenu()
{
    int tickets;
    cout<<"Powerball Simulator\n";
    cout<<"5 White Balls will be random generated and the last ball is red\n";
    cout<<"Each ticket cost $2\nEnter how many ticket will you like to buy: ";
    cin>>tickets;
    
    while((tickets <= 0 || tickets > 250000))
    {
        cout<<"Please enter a number between 1-1000: ";
        cin>>tickets;
    }
    return tickets;
}

/****************************************************************************
 *                          setSeed()                                       *
 *    The set Seed function sets the seed for the rand function             *
 ***************************************************************************/

void setSeed()
{
    long seed;
    seed = time(0);
    unsigned int sseed = static_cast<unsigned int>(seed);
    srand(sseed);
}

/****************************************************************************
 *                          randomWhiteNumber()                             *
 *    The function will generate a number 1-59 and return the number        *
 ***************************************************************************/

int randomWhiteNumber()
{
    int randomVar = (rand() % MAX_WHITE)+1;
    return randomVar;
}

/****************************************************************************
 *                          randomRedNumber()                               *
 *    The function will generate a number 1-35 and return the number        *
 ***************************************************************************/
int randomRedNumber()
{
    int randomVar = (rand() % MAX_RED)+1;
    return randomVar;
}

/****************************************************************************
 *                              checkArray()                                *
 *    This function will search through array[] for a number if  the number *
 *    is found then return true if it is not found return false             *
 ***************************************************************************/
bool checkArray(int array[], int size, int number)
{
    bool has = false;
    for(int i = 0; i< size; i++)
    {
        if(array[i] == number)
        {
            has = true;
        }
    }
    return has;
}
/****************************************************************************
 *                              won()                                       *
 *    This functions compare the number of balls that match in a ticket     *
 *    and returns the amount of cash the ticket is worth                    *
 ***************************************************************************/
int won(int ballCount, bool redBall)
{
    int cash = 0;
    switch(ballCount)
    {
        case 5:
            if(redBall)
                cash = 70000000;
            else
                cash = 1000000;
            break;
        case 4:
            if(redBall)
                cash = 10000;
            else
                cash = 100;
            break;
        case 3:
            if(redBall)
                cash = 100;
            else
                cash = 7;
            break;
        case 2:
            if(redBall)
                cash = 7;
            else
                cash = 0;
            break;
        case 1:
            if(redBall)
                cash = 4;
            else
                cash = 0;
            break;
        default:
            if(redBall)
                cash = 4;
            else
                cash =0;
            break;
    }
    return cash;
}