Construct a C program that performs the Roulette Wheel, Mutation, and Selection
ID: 3871489 • Letter: C
Question
Construct a C program that performs the Roulette Wheel, Mutation, and Selection operations.The Mutation operation you will use is the insert mutation. That is randomly selecting two positions, swap the position and shift everything accordingly shown as follows. A B C D E F G H (before the insert mutation) A G B C D E F H (after the insert mutation) For the Selection operation, use Roulette Wheel to select the next generation
The pseudo-code of the GA( genetic algorithm) is given as follows
The Initialize Population
while (not stop) // runs 100 iterations Mutation Compute Fitness
Roulette Wheel
Selection
Output the best schedule
Explanation / Answer
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <iomanip>
#include <stdlib.h>
#include <conio.h>
#include <ctime>
#include "Console.h"
namespace con = JadedHoboConsole;
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int PickNum();
int SpinWheel(int choice);
int StraightBet();
int RedBet();
int BlackBet();
int EvenBet();
int OddBet();
int First12();
int Second12();
int Third12();
void Stats();
double HighScore();
void Save();
void Load();
double bank=100;
double amountBet;
int SaveArr[5];
double HighScore1=0;
int NumBets=0;
int NumWon=0;
int NumLost=0;
double PercentWon;
double PercentLost;
int main ()
{
cout<<setiosflags(ios::fixed|ios::showpoint);
cout<<setprecision(2);
int option;
srand(unsigned(time(0)));
do
{
bank=100.00;
do
{
int choice=PickNum();
SpinWheel(choice);
}
while(bank!=0);
system("cls");
if(bank==0.00||bank<0.00)
cout<<" You lose, you don't have any more money! ";
getch();
cout<<" Do you want to play again? (1 for yes, 2 for no): ";
cin>>option;
}
while(option!=2);
return 0;
}
int PickNum()
{
system("cls");
int choice;
cout<<con::fg_yellow<<" What do you want to bet on? ";
cout<<" Straight bet(#00-36)...1"<<endl;
cout<<" Red....................2"<<endl;
cout<<" Black..................3"<<endl;
cout<<" Even...................4"<<endl;
cout<<" Odd....................5"<<" Bank: $"<<bank<<" ";
cout<<" First 12...............6"<<endl;
cout<<" Second 12..............7"<<endl;
cout<<" Third 12...............8"<<endl;
cout<<" Stats..................9"<<endl;
cout<<" Save Game.............10"<<endl;
cout<<" Load Game.............11"<<endl;
cout<<" Choose an option: ";
cin>>choice;
HighScore();
if(choice<=8)
{
cout<<" Amount to bet from bank: ";
cin>>amountBet;
if(amountBet>bank)
{
cout<<" You bet more than the bank! Press any key and try again.";
getch();
PickNum();
}
}
if(amountBet<0)
{
cout<<" You can't bet a negative number! Try again...";
getch();
PickNum();
}
if(choice==10)
{
cout<<" ....Game Saved! Press any key to continue";
getch();
}
if(choice==11)
{
cout<<" ....Game Loaded! Press any key to continue.";
getch();
}
if(choice==123)
{
int cheat=rand()%100+1;
if(cheat==1)
bank=bank+10000;
if(cheat<99&&cheat>75)
bank=bank+1000;
else
bank=bank+100;
}
if(choice>11&&choice!=123)
{
cout<<" Invalid choice, try again.";
getch();
PickNum();
}
return choice;
}
int SpinWheel(int choice)
{
if(choice==1)
StraightBet();
if(choice==2)
RedBet();
if(choice==3)
BlackBet();
if(choice==4)
EvenBet();
if(choice==5)
OddBet();
if(choice==6)
First12();
if(choice==7)
Second12();
if(choice==8)
Third12();
if(choice==9)
Stats();
if(choice==10)
Save();
if(choice==11)
Load();
return 0;
}
int StraightBet()
{
system("cls");
NumBets++;
int WinningNum=rand()%38+1;
//cout<<WinningNum;
int StraightBetChoice;
cout<<" Choose a number from 0-36, or 00: ";
cin>>StraightBetChoice;
if(StraightBetChoice==WinningNum)
{
NumWon++;
cout<<" Winner!";
bank=bank+amountBet*25;
cout<<" You now have $"<<bank;
getch();
}
if(StraightBetChoice!=WinningNum)
{
NumLost++;
cout<<" Loser! The winning number was "<<WinningNum<<". ";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int RedBet()
{
system("cls");
//1 is red and 2 is black
NumBets++;
int WinningNum=rand()%2+1;
if(WinningNum==1)
{
NumWon++;
cout<<" The number rolled ("<<WinningNum<<") was red, you are a winner!";
bank=bank+(amountBet*2);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum==2)
{
NumLost++;
cout<<" You lose, the number rolled ("<<WinningNum<<") was black!";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int BlackBet()
{
system("cls");
//1 is red and 2 is black
NumBets++;
int WinningNum=rand()%2+1;
if(WinningNum==2)
{
NumWon++;
cout<<" The number rolled ("<<WinningNum<<") was black, you are a winner!";
bank=bank+(amountBet*2);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum==1)
{
NumLost++;
cout<<" You lose, the number rolled ("<<WinningNum<<") was red!";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int EvenBet()
{
system("cls");
NumBets++;
int WinningNum=rand()%38+1;
if(WinningNum%2==0)
{
NumWon++;
cout<<" The number rolled was even ("<<WinningNum<<"), you are a winner!";
bank=bank+(amountBet*2);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum%2!=0)
{
NumLost++;
cout<<" You lose, the number rolled was odd! ("<<WinningNum<<").";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int OddBet()
{
system("cls");
int WinningNum=rand()%38+1;
NumBets++;
if(WinningNum%2!=0)
{
NumWon++;
cout<<" The number rolled was odd ("<<WinningNum<<"), you are a winner!";
bank=bank+(amountBet*2);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum%2==0)
{
NumLost++;
cout<<" You lose, the number rolled was even! ("<<WinningNum<<").";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int First12()
{
system("cls");
int WinningNum=rand()%38+1;
NumBets++;
if(WinningNum<=12)
{
NumWon++;
cout<<" The number rolled was in the first 12 ("<<WinningNum<<"), you are a winner!";
bank=bank+(amountBet*3);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum>12)
{
NumLost++;
cout<<" The number rolled was NOT in the first 12 ("<<WinningNum<<"), you lose!";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int Second12()
{
system("cls");
int WinningNum=rand()%38+1;
NumBets++;
if(WinningNum>=13&&WinningNum<=24)
{
NumWon++;
cout<<" The number rolled was in the second 12 ("<<WinningNum<<"), you are a winner!";
bank=bank+(amountBet*3);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum<13||WinningNum>24)
{
NumLost++;
cout<<" The number rolled was NOT in the second 12 ("<<WinningNum<<"), you lose!";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int Third12()
{
system("cls");
int WinningNum=rand()%38+1;
NumBets++;
if(WinningNum>24)
{
NumWon++;
cout<<" The number rolled was in the third 12 ("<<WinningNum<<"), you are a winner!";
bank=bank+(amountBet*3);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum<=24)
{
NumLost++;
cout<<" The number rolled was NOT in the third 12 ("<<WinningNum<<"), you lose!";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
void Save()
{
ofstream outf("save.txt");
outf<<bank<<endl;
outf<<HighScore1<<endl;
outf<<NumBets<<endl;
outf<<NumWon<<endl;
outf<<NumLost<<endl;
outf.close();
}
void Load()
{
ifstream inf("save.txt");
inf>>bank;
inf>>HighScore1;
inf>>NumBets;
inf>>NumWon;
inf>>NumLost;
inf.close();
}
void Stats()
{
system("CLS");
PercentWon=(double(NumWon)/NumBets)*100;
PercentLost=(double(NumLost)/NumBets)*100;
cout<<" Stats ";
cout<<" Highest Amount Ever in Bank: $"<<HighScore();
cout<<" Number of Bets Made: "<<NumBets;
cout<<" Number of bets won: "<<NumWon;
if(NumBets!=0)
cout<<" ("<<PercentWon<<"%)";
cout<<" Number of bets lost: "<<NumLost;
if(NumBets!=0)
cout<<" ("<<PercentLost<<"%)";
getch();
system("CLS");
}
double HighScore()
{
if(bank>HighScore1)
HighScore1=bank;
return HighScore1;
}
//old save/load funtions
/*void Load()
{
int num1;
ifstream inf("save.txt");
inf>>num1;
bank=num1;
inf.close();
}*/
/*void Save(int bank)
{
ofstream outf("save.txt");
outf<<bank;
outf.close();
}*/
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <iomanip>
#include <stdlib.h>
#include <conio.h>
#include <ctime>
#include "Console.h"
namespace con = JadedHoboConsole;
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int PickNum();
int SpinWheel(int choice);
int StraightBet();
int RedBet();
int BlackBet();
int EvenBet();
int OddBet();
int First12();
int Second12();
int Third12();
void Stats();
double HighScore();
void Save();
void Load();
double bank=100;
double amountBet;
int SaveArr[5];
double HighScore1=0;
int NumBets=0;
int NumWon=0;
int NumLost=0;
double PercentWon;
double PercentLost;
int main ()
{
cout<<setiosflags(ios::fixed|ios::showpoint);
cout<<setprecision(2);
int option;
srand(unsigned(time(0)));
do
{
bank=100.00;
do
{
int choice=PickNum();
SpinWheel(choice);
}
while(bank!=0);
system("cls");
if(bank==0.00||bank<0.00)
cout<<" You lose, you don't have any more money! ";
getch();
cout<<" Do you want to play again? (1 for yes, 2 for no): ";
cin>>option;
}
while(option!=2);
return 0;
}
int PickNum()
{
system("cls");
int choice;
cout<<con::fg_yellow<<" What do you want to bet on? ";
cout<<" Straight bet(#00-36)...1"<<endl;
cout<<" Red....................2"<<endl;
cout<<" Black..................3"<<endl;
cout<<" Even...................4"<<endl;
cout<<" Odd....................5"<<" Bank: $"<<bank<<" ";
cout<<" First 12...............6"<<endl;
cout<<" Second 12..............7"<<endl;
cout<<" Third 12...............8"<<endl;
cout<<" Stats..................9"<<endl;
cout<<" Save Game.............10"<<endl;
cout<<" Load Game.............11"<<endl;
cout<<" Choose an option: ";
cin>>choice;
HighScore();
if(choice<=8)
{
cout<<" Amount to bet from bank: ";
cin>>amountBet;
if(amountBet>bank)
{
cout<<" You bet more than the bank! Press any key and try again.";
getch();
PickNum();
}
}
if(amountBet<0)
{
cout<<" You can't bet a negative number! Try again...";
getch();
PickNum();
}
if(choice==10)
{
cout<<" ....Game Saved! Press any key to continue";
getch();
}
if(choice==11)
{
cout<<" ....Game Loaded! Press any key to continue.";
getch();
}
if(choice==123)
{
int cheat=rand()%100+1;
if(cheat==1)
bank=bank+10000;
if(cheat<99&&cheat>75)
bank=bank+1000;
else
bank=bank+100;
}
if(choice>11&&choice!=123)
{
cout<<" Invalid choice, try again.";
getch();
PickNum();
}
return choice;
}
int SpinWheel(int choice)
{
if(choice==1)
StraightBet();
if(choice==2)
RedBet();
if(choice==3)
BlackBet();
if(choice==4)
EvenBet();
if(choice==5)
OddBet();
if(choice==6)
First12();
if(choice==7)
Second12();
if(choice==8)
Third12();
if(choice==9)
Stats();
if(choice==10)
Save();
if(choice==11)
Load();
return 0;
}
int StraightBet()
{
system("cls");
NumBets++;
int WinningNum=rand()%38+1;
//cout<<WinningNum;
int StraightBetChoice;
cout<<" Choose a number from 0-36, or 00: ";
cin>>StraightBetChoice;
if(StraightBetChoice==WinningNum)
{
NumWon++;
cout<<" Winner!";
bank=bank+amountBet*25;
cout<<" You now have $"<<bank;
getch();
}
if(StraightBetChoice!=WinningNum)
{
NumLost++;
cout<<" Loser! The winning number was "<<WinningNum<<". ";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int RedBet()
{
system("cls");
//1 is red and 2 is black
NumBets++;
int WinningNum=rand()%2+1;
if(WinningNum==1)
{
NumWon++;
cout<<" The number rolled ("<<WinningNum<<") was red, you are a winner!";
bank=bank+(amountBet*2);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum==2)
{
NumLost++;
cout<<" You lose, the number rolled ("<<WinningNum<<") was black!";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int BlackBet()
{
system("cls");
//1 is red and 2 is black
NumBets++;
int WinningNum=rand()%2+1;
if(WinningNum==2)
{
NumWon++;
cout<<" The number rolled ("<<WinningNum<<") was black, you are a winner!";
bank=bank+(amountBet*2);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum==1)
{
NumLost++;
cout<<" You lose, the number rolled ("<<WinningNum<<") was red!";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int EvenBet()
{
system("cls");
NumBets++;
int WinningNum=rand()%38+1;
if(WinningNum%2==0)
{
NumWon++;
cout<<" The number rolled was even ("<<WinningNum<<"), you are a winner!";
bank=bank+(amountBet*2);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum%2!=0)
{
NumLost++;
cout<<" You lose, the number rolled was odd! ("<<WinningNum<<").";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int OddBet()
{
system("cls");
int WinningNum=rand()%38+1;
NumBets++;
if(WinningNum%2!=0)
{
NumWon++;
cout<<" The number rolled was odd ("<<WinningNum<<"), you are a winner!";
bank=bank+(amountBet*2);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum%2==0)
{
NumLost++;
cout<<" You lose, the number rolled was even! ("<<WinningNum<<").";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int First12()
{
system("cls");
int WinningNum=rand()%38+1;
NumBets++;
if(WinningNum<=12)
{
NumWon++;
cout<<" The number rolled was in the first 12 ("<<WinningNum<<"), you are a winner!";
bank=bank+(amountBet*3);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum>12)
{
NumLost++;
cout<<" The number rolled was NOT in the first 12 ("<<WinningNum<<"), you lose!";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int Second12()
{
system("cls");
int WinningNum=rand()%38+1;
NumBets++;
if(WinningNum>=13&&WinningNum<=24)
{
NumWon++;
cout<<" The number rolled was in the second 12 ("<<WinningNum<<"), you are a winner!";
bank=bank+(amountBet*3);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum<13||WinningNum>24)
{
NumLost++;
cout<<" The number rolled was NOT in the second 12 ("<<WinningNum<<"), you lose!";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
int Third12()
{
system("cls");
int WinningNum=rand()%38+1;
NumBets++;
if(WinningNum>24)
{
NumWon++;
cout<<" The number rolled was in the third 12 ("<<WinningNum<<"), you are a winner!";
bank=bank+(amountBet*3);
cout<<" You now have $"<<bank;
getch();
}
if(WinningNum<=24)
{
NumLost++;
cout<<" The number rolled was NOT in the third 12 ("<<WinningNum<<"), you lose!";
bank=bank-amountBet;
cout<<" You now have $"<<bank;
getch();
}
return 0;
}
void Save()
{
ofstream outf("save.txt");
outf<<bank<<endl;
outf<<HighScore1<<endl;
outf<<NumBets<<endl;
outf<<NumWon<<endl;
outf<<NumLost<<endl;
outf.close();
}
void Load()
{
ifstream inf("save.txt");
inf>>bank;
inf>>HighScore1;
inf>>NumBets;
inf>>NumWon;
inf>>NumLost;
inf.close();
}
void Stats()
{
system("CLS");
PercentWon=(double(NumWon)/NumBets)*100;
PercentLost=(double(NumLost)/NumBets)*100;
cout<<" Stats ";
cout<<" Highest Amount Ever in Bank: $"<<HighScore();
cout<<" Number of Bets Made: "<<NumBets;
cout<<" Number of bets won: "<<NumWon;
if(NumBets!=0)
cout<<" ("<<PercentWon<<"%)";
cout<<" Number of bets lost: "<<NumLost;
if(NumBets!=0)
cout<<" ("<<PercentLost<<"%)";
getch();
system("CLS");
}
double HighScore()
{
if(bank>HighScore1)
HighScore1=bank;
return HighScore1;
}
//old save/load funtions
/*void Load()
{
int num1;
ifstream inf("save.txt");
inf>>num1;
bank=num1;
inf.close();
}*/
/*void Save(int bank)
{
ofstream outf("save.txt");
outf<<bank;
outf.close();
}*/