-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.cpp
1128 lines (893 loc) · 24.4 KB
/
function.cpp
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Here is a function I typed up for Dwarf Therapist . It is the labor optimizer functionality that is in the latest version of Dwarf Therapist. Coded in C++. The function can be found in mainwindow.cpp
The 3 files I worked on are dfinstance.cpp, dwarf.cpp, and mainwindow.cpp. The majority of the function is in initializeSuperStruct()
void MainWindow::initializeSuperStruct()
{
using namespace std;
QList<Dwarf*> m_selected_dwarfs = m_view_manager->get_selected_dwarfs();
int d_count = m_selected_dwarfs.size();
//get labors per dwarf
bool ok;
QString labors_p = QInputDialog::getText(0, tr(“Labors per dwarf”),
tr(“How many dwarves do you want to assign per labor?”), QLineEdit::Normal,””, &ok);
if (!ok)
return;
int labors_per = labors_p.toInt();
//used to derive %’s for dwarfs
float totalCoverage = 0;
//Miner, Hunter, Woodcutter total Coverage;
float MHWCoverage = 0;
float modifiedTotalCoverage = 0;
//get csv filename
ok = 0;
QString file_name = QInputDialog::getText(0, tr(“CSV input file”),
tr(“Name of input file for assigning labors?”), QLineEdit::Normal,””, &ok);
if (!ok)
return;
//int file_name = csv_n.toInt();
ok = 0;
QString percent_jobs = QInputDialog::getText(0, tr(“Percent of Population/Jobs to assign”),
tr(“What percent do you wish to assign? (include decimal, i.e. .7)”), QLineEdit::Normal,””, &ok);
if (!ok)
return;
float pct_jobs = percent_jobs.toFloat();
//dwarf count, to be pulled from selection
//labor count, to be pulled from init .csv
int role_count = 0;
int total_jobs = m_selected_dwarfs.size() * labors_per;
//rounding is done later
float jobs_to_assign = total_jobs * pct_jobs;
bool MHWExceed = 0;
string st;
//SUPERSTRUCT!
QVector <superStruct> sorter;
QVector <roles> ListOfRolesV;
//ifstream f( filename.c_str() );
//ifstream f(“role_labor_list.csv”);
string f_holder = file_name.toStdString();
ifstream f(f_holder.c_str());
//ifstream f (file_name.c_str());
//loops through each line in the file
while (getline (f, st))
{
int pos = 1;
//placeholder
roles p_holder;
//gets the line for processing
istringstream iss( st );
//loops through each , in one line
while (getline (iss, st, ‘,’))
{
//role_name, labor_id, priority_weight, num_to_assign
if (pos == 1)
{
p_holder.role_name = QString::fromStdString(st);
}
else if (pos == 2)
{
int fieldValue = 0;
istringstream (st) >> fieldValue;
p_holder.labor_id = fieldValue;
}
else if (pos == 3)
{
float fieldValue = 0.0f;
istringstream (st) >> fieldValue;
p_holder.priority = fieldValue;
}
else if (pos == 4)
{
float fieldValue = 0;
istringstream (st) >> fieldValue;
p_holder.coverage = fieldValue;
}
//initialize numberAssigned to 0;
p_holder.numberAssigned = 0;
pos++;
}
ListOfRolesV.push_back(p_holder);
pos = 1;
}
f.close();
role_count = ListOfRolesV.size();
//calculate total Coverage
for (int x = 0; x < role_count; x++)
{
int labor = ListOfRolesV[x].labor_id;
if ((labor == 0) || (labor == 10) || (labor == 44))
{
MHWCoverage = MHWCoverage + ListOfRolesV[x].coverage;
}
totalCoverage += ListOfRolesV[x].coverage;
}
//Check if ((MHWCoverage / totalCoverage ) * total_jobs )> m_selected_dwarfs.size();
if (((MHWCoverage / totalCoverage) * jobs_to_assign ) > m_selected_dwarfs.size())
{
if (DT->user_settings()->value(“options/labor_exclusions”,true).toBool())
{
//only set flag if option is checked in options
MHWExceed = 1;
}
modifiedTotalCoverage = totalCoverage – MHWCoverage;
}
else
{
modifiedTotalCoverage = totalCoverage;
}
//time to set numberToAssign
for (int x = 0; x < role_count; x++)
{
int labor = ListOfRolesV[x].labor_id;
float pre_number_to_assign = 0;
if ( (MHWExceed) && ((labor == 0) || (labor == 10) || (labor == 44)))
{
//truncate miner, hunter, woodcutter to population size
pre_number_to_assign = ListOfRolesV[x].coverage / MHWCoverage * m_selected_dwarfs.size();
}
else if (MHWExceed)
{
//since miner, hunter, woodcutter are being truncated down to population size, we need to remove that from the jobs to assign
pre_number_to_assign = ListOfRolesV[x].coverage / modifiedTotalCoverage * (jobs_to_assign – m_selected_dwarfs.size());
}
else
{
//normal
pre_number_to_assign = (ListOfRolesV[x].coverage / modifiedTotalCoverage) * jobs_to_assign;
}
//old, works without conflicting labors
//float pre_number_to_assign = (ListOfRolesV[x].coverage / totalCoverage) * jobs_to_assign;
//rounding magic
if (( pre_number_to_assign + 0.5) >= (int(pre_number_to_assign ) + 1) )
{
ListOfRolesV[x].numberToAssign = int(pre_number_to_assign)+1;
}
else
{
ListOfRolesV[x].numberToAssign = int(pre_number_to_assign);
}
}
//now time to initialize the superStruct
//count # dwarf’s
//foreach(Dwarf *d, m_view_manager->get_selected_dwarfs()) {d_count++;}
//resize based on # dwarfs * # of roles
sorter.resize(d_count*role_count);
QString *temp;
int sstruct_pos = 0;
//for(int sstruct_pos = 0; sstruct_pos < sorter.size(); sstruct_pos++)
{
//cycle through each Dwarf,
foreach (Dwarf *d, m_view_manager->get_selected_dwarfs())
{
int dwarf_count = 0;
//cycle through each Role in ListOfRolesV
for (int role_entry = 0; role_entry < ListOfRolesV.size(); role_entry++)
{
//problem here… need a role per dwarf which I have, but Dwarf doesn’t have anywhere to save a w_percent…
//Could just load into superStruct, but it won’t be a pointer…
sorter[sstruct_pos].d_id = d->id();
sorter[sstruct_pos].labor_id = ListOfRolesV[role_entry].p_l_id();
//QString role_name = ListOfRolesV[role_entry].p_r_name();
//not sure if this is what I want
sorter[sstruct_pos].role_name = ListOfRolesV[role_entry].p_r_name();
//can’t use pointers with this
sorter[sstruct_pos].r_percent = d->get_raw_role_rating(ListOfRolesV[role_entry].role_name);
sorter[sstruct_pos].w_percent = ListOfRolesV[role_entry].priority * d->get_raw_role_rating(ListOfRolesV[role_entry].role_name);
sstruct_pos++;
}
//probably not needed
dwarf_count ++;
}
}
//used for debug pause
int test = 0;
//sort superstruct by w_percent
//runs through list
for (int i = 0; i < sorter.size(); i++)
{
//checks against lower element
//cout << *s->at(i).pPercent << endl;
for (int j = 1; j < (sorter.size()-i); j++)
{
//using pointer dereferences messed this up, but, not using them doesn’t sort right
if (sorter[j-1].w_percent < sorter[j].w_percent)
{
swap(sorter[j-1], sorter[j]);
}
}
}
//now to assign labors!
//vs running through each labor, and assigning up to max # dwarf’s…
//1. need to start from top of allRoles
//2. see what labor is, see if goal is met.
//DONE (via csv input) for DEPLOY APP, need to change this to see if labor is asked for
//3. Y – Skip (use while statement)
//4. N –
//a. Check Dwarf to see if he is available (again, while Statement),
//not available if has conflicting labor
// Y – Assign
// N – Skip
//start from top of list.
for (int sPos = 0; sPos < sorter.size(); sPos++)
{
//dwarf position (used for searching of name)
//I would like to set these to pointers, but I was getting out of bound issues…
int dPos = 0;
//role position
int rPos = 0;
//cycle through each dwarf? I should, but, I can use the *s->at(x).name to find the Dwarf #
//no, need to compare roleName to dwarfName
//search for Dwarf (y) for that role
for (int d = 0; d < d_count; d++)
{
//dPos is never getting updated.
//int temp = d->id();
if (sorter[sPos].d_id == m_selected_dwarfs.at(d)->id())
{
dPos = d;
//break;
};
//dPos++;
}
//match superStruct roleName to myRoles name position (to check if filled up)
for (int r = 0; r < ListOfRolesV.size(); r++)
{
if (sorter[sPos].role_name == ListOfRolesV[r].role_name)
{
rPos = r;
//break;
};
}
//search if role @ rPos is filled up
//role check started…
int number_to_assign = *ListOfRolesV[rPos].n_assign();
if (ListOfRolesV[rPos].numberAssigned < number_to_assign)
{
int labor = ListOfRolesV[rPos].labor_id;
bool incompatible = 0;
//Miner is 0
//Woodcutter is 10
//Hunter is 44
//only set incompatible flag if option is set.
if (DT->user_settings()->value(“options/labor_exclusions”,true).toBool())
{
if ((labor == 0) || (labor == 10) || (labor == 44))
{
if ((m_selected_dwarfs.at(dPos)->labor_enabled(0)) || (m_selected_dwarfs.at(dPos)->labor_enabled(10)) || (m_selected_dwarfs.at(dPos)->labor_enabled(44)))
{
//dwarf has an incompatible labor, set flag
incompatible = 1;
}
}
}
//need to compare to total_assigned_labors,
//this will be used for Custom Professions
//I’m going to have to have a dwarf labors_assigned variable to count for
//CUSTOM PROFESSIONS
int d_currentAssigned = m_selected_dwarfs.at(dPos)->get_dirty_labors().size();
//m_selected_dwarfs.at(dPos)->total_assigned_labors();
//is Dwarf (d) laborsAssigned full? Used in proto-type
if (d_currentAssigned < labors_per)
{
//if Dwarf doesn’t have an incompatible labor enabled, proceed.
if (incompatible == 0)
{
//another check to check for conflicting laborid’s,
//AND
//to see if labor_id flag is checked in Options.
//m_selected_dwarfs.at(dPos)->toggle_labor(ListOfRolesV[rPos].labor_id);
m_selected_dwarfs.at(dPos)->toggle_labor(labor);
//assign labor (z) to Dwarf @ dPos
//add labor (z?) to Dwarf’s (d) labor vector
//m_selected_dwarfs.at(dPos).laborsAssigned.push_back(r->at(rPos).name);
//increase labors assigned to roles vector
//role check completed
ListOfRolesV[rPos].numberAssigned++;
}
}
incompatible = 0;
}
}
m_model->calculate_pending();
};
BlackJack Project
/* ========================================================================== */
/* */
/* BlackJack.cpp */
/* (c) 2008 Joshua Laferriere */
/* */
/* Description */
/* This code compiles with Dev-C++ and it produces a neato chart of hi-lo */
/* values. It also creates a bjchart.csv file useable to create charts. */
/* ========================================================================== */
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
ofstream bjCharts; //used to print cards
//used for creating a deck of cards.
struct cards
{
int index;
string faceValue;
string suite;
//static variable
//static int shoePosition;
//static void incrementCardCounter()
//{
// cardCounter++;
//}
//default constructor
cards::cards()
{
index = 0;
//int cards::cardCounter=0;
};
//int cards::cardCounter=0;
};
//used for individual cards of player/dealer’s hand.
//duh, this should extend the above class.
struct card
{
string faceValue;
string suite;
int value;
//index of card used for hand, this value will be set from 1-4 within the hands struct.
int index;
void getValue()
{
if(faceValue==”A”)
{
value=11;
}
if(faceValue==”2″){value=2;}
if(faceValue==”3″){value=3;}
if(faceValue==”4″){value=4;}
if(faceValue==”5″){value=5;}
if(faceValue==”6″){value=6;}
if(faceValue==”7″){value=7;}
if(faceValue==”8″){value=8;}
if(faceValue==”9″){value=9;}
if(faceValue==”10″){value=10;}
if(faceValue==”J”){value=10;}
if(faceValue==”Q”){value=10;}
if(faceValue==”K”){value=10;}
}
//default constructor
card()
{
index = 0;
faceValue = “”;
suite = “”;
value = 0;
};
};
//going to be used for later implementation.
struct shoe
{
cards cArds[52];
int shoePosition;
};
//used for creating players hand
struct hands
{
card cArd[5]; //5 cards (0-4)
int index; //# of hand.
int sumTotal;
int numCards;
//sets a single ace to 1
void setAceTo1 ()
{
for(int setAceTo1Counter=0; setAceTo1Counter<=numCards; setAceTo1Counter++)
{
if(cArd[setAceTo1Counter].value==11)
{
cArd[setAceTo1Counter].value=1;
break;
}
}
}
//return true if ace (11) is found
bool aceW11()
{
for(int aceW11Counter=0; aceW11Counter<=numCards; aceW11Counter++)
{
if(cArd[aceW11Counter].value==11)
{return true;}
}
return false;
}
//returns number of Aces, used in aceCheck
//not used yet
int getSum()
{
//I included this and it didn’t work
//…
//aceCheck();
sumTotal=0;
for (int z=0; z<=numCards; z++)
{
sumTotal = (cArd[z].value + sumTotal);
}
return sumTotal;
}
//default constructor for hands
hands::hands()
{
index = 0; //# of hand.
sumTotal = 0;
numCards = 0;
}
};
void initShoe(cards shoe[], int numDecks)
{
int i=0; // index of shoe
string suite;
for(int b = 0; b<numDecks; b++) // 0 to numdecks
{
for(int s=0;s<4;s++) // 0 to 3 suites
{
if(s==0){suite=”Clubs”;}
if(s==1){suite=”Hearts”;}
if(s==2){suite=”Spades”;}
if(s==3){suite=”Diamonds”;}
for(int a=0;a<13;a++) // 0-12 number of cards per suite
{
shoe[i].index=i;
//cout << “/n” << “(s)suite: ” << suite;
shoe[i].suite=suite;
//cout << “\n” << shoe[i].suite;
if(a==0){shoe[i].faceValue=”A”;}
if(a==1){shoe[i].faceValue=”2″;}
if(a==2){shoe[i].faceValue=”3″;}
if(a==3){shoe[i].faceValue=”4″;}
if(a==4){shoe[i].faceValue=”5″;}
if(a==5){shoe[i].faceValue=”6″;}
if(a==6){shoe[i].faceValue=”7″;}
if(a==7){shoe[i].faceValue=”8″;}
if(a==8){shoe[i].faceValue=”9″;}
if(a==9){shoe[i].faceValue=”10″;}
if(a==10){shoe[i].faceValue=”J”;}
if(a==11){shoe[i].faceValue=”Q”;}
if(a==12){shoe[i].faceValue=”K”;}
//cout << “\n” << “(b)Deck: ” << b << ” ” << “(s)Suite: ” << suite << ” ” << “(a)Card: ” << a;
//cout << “\n” << shoe[i].faceValue;
//cout << “\n” << shoe[i].suite << ” “;
//cout << “\n”;
i++;
}
}
}
}
void printShoe(const cards shoe[], int sizeOfShoe) //I was thinking of passing this by reference, but in C++, array’s are passed by reference automatically. There is no local copy made.
{
int value = 0;
int addedValue = 0;
int lowestValue=0;
int highestValue=0;
bjCharts.open(“bjChart.csv”, fstream::app); //append
//cout << “\nSize of Shoe: ” << sizeOfShoe;
//this below code is to output a chart for viewing purposes
for(int i=0; i<sizeOfShoe; i++)
{
if( (shoe[i].faceValue==”A”) || (shoe[i].faceValue==”10″) || (shoe[i].faceValue==”J”) || (shoe[i].faceValue==”Q”) || (shoe[i].faceValue==”K”) )
{
addedValue = (-1);
}
if( (shoe[i].faceValue==”2″) || (shoe[i].faceValue==”3″) || (shoe[i].faceValue==”4″) || (shoe[i].faceValue==”5″) || (shoe[i].faceValue==”6″) )
{
addedValue = 1;
}
if( (shoe[i].faceValue==”7″) || (shoe[i].faceValue==”8″) || (shoe[i].faceValue==”9″) )
{
addedValue = 0;
}
//card count value
value = value + addedValue;
//cout << “\nCard Count: ” << value << “/n”;
bjCharts << i << “,” << value << “/n”;
if(value<0)
{
for(int x = 0; x < ( ( ( ( (sizeOfShoe/52) *4) +6) ) + value); x++) //precede with blanks
{
cout << ” “;
}
for(int x=0; x<(value*(-1));x++) //fill with x
{
cout << “x”;
}
cout << “0”;
cout << “\n”;
}
else
if(value>0) // if value is greater than 0
{
for(int x=0; x<( ( (sizeOfShoe/52) *4) +6); x++)
{
cout << ” “;
}
cout << “0”;
for(int x=0; x<value; x++)
{
cout << “x”;
}
cout << “\n”;
}
else
if(value==0)
{
for(int x=0; x<( ( (sizeOfShoe/52) *4) +6); x++)
{
cout << ” “;
}
cout << “0”;
cout << “\n”;
}
//keep track of lowest and highest values
/*
if(value<lowestValue)
{
system(“pause”);
cout << “\nLowest Value:” << value << “\n”;
lowestValue=value;
}
if(value>highestValue)
{
system(“pause”);
cout << “\nHighest Value:” << value << “\n”;
highestValue=value;
}
*/
}
bjCharts.close();
}
void shuffleShoe(cards shoe[], int sizeOfShoe)
{
//— Shuffle elements by randomly exchanging each with one other.
for (int i=0; i<(sizeOfShoe-1); i++) //i is each card incrementally
{
int r = i + (rand() % (sizeOfShoe-i)); // Random remaining position.
cards temp = shoe[i]; //temp card is value of i
shoe[i] = shoe[r]; //move i to random position
shoe[r] = temp; //old random position is temp value
//it does this every card. In affect, the deck is being shuffled twice
}
}
//this function should return a value that is the player’s hand.
//this way this value can be stored.
//need to keep track of shoe[] place.
//unless I do dealer’s hand within dealCards
//a similar function should be used to return the dealer’s hand.
//this way the value can be stored.
//then the values will be compared.
//maybe dealcards can call two other functions
//one is dealer
//one is player
//have to pass a, that’s it. A is my counter.
//returns dealer’s value
int dealer(cards shoe[], int sizeOfShoe, int card_Counter)
{
hands dealer[4];
//used to keep track of cards.
int x = 0;
//keep hitting until 17 is met.
for (int y = card_Counter+1; y<sizeOfShoe; y++)
{
//ensure dealer hasn’t busted.
while( (dealer[0].getSum() > 21) && (dealer[0].aceW11()) )
{
dealer[0].setAceTo1();
}
if (dealer[0].getSum() > 16){break;}
dealer[0].cArd[x].faceValue=shoe[y].faceValue;
dealer[0].cArd[x].index=x;
dealer[0].cArd[x].suite=shoe[y].suite;
dealer[0].numCards=x;
dealer[0].cArd[x].getValue();
cout << “\nCard: ” << dealer[0].cArd[x].faceValue;
cout << “\nCard value: ” << dealer[0].cArd[x].value;
cout << “\nDealer Sum: ” << dealer[0].getSum() << “\n”;
x++;
//system(“pause”);
}
cout << “\nDealer Sum: ” << dealer[0].getSum() << “\n”;
return dealer[0].getSum();
}
void dealCards(cards shoe[], int sizeOfShoe)
{
//note: can’t use hand[0], confuses it with the hand[4] that’s defined within hands struct
//having problem using vectors. Doesn’t find members of hands struct within vector
//vector ha[1];
//used for hand, up to four hands (0-3)
hands player[4];
char hit;
//x is a counter for the card in the hand.
int x=0;
int shoePosition = 0;
//sumtotal is a static var
//ha[0].sumTotal=0;
/*
deal cards until sizeOfshoe has been met
*/
//go through deck
for (int a=0; a<sizeOfShoe; a++)
{
// verify Sum isn’t over 21
if (player[0].getSum()>21){cout << “\n” << “You Busted!” << “\n”; break;}
if (player[0].numCards == 4){cout << “5 cards dealt, you win.” << “\n”; break;}
player[0].cArd[x].faceValue=shoe[a].faceValue;
player[0].cArd[x].index=x;
player[0].cArd[x].suite=shoe[a].suite;
player[0].numCards=x;
player[0].cArd[x].getValue();
cout << “\nCard: ” << player[0].cArd[x].faceValue;
//ha[0].sumTotal = ha[0].sumTotal + ha[0].cArd[x].value;
cout << “\nCard value: ” << player[0].cArd[x].value;
//should this be inside the getSum() after the 21 check?
while( (player[0].getSum() > 21) && (player[0].aceW11()) )
{
player[0].setAceTo1();
}
cout << “\nTotal: ” << player[0].getSum();
x++;
shoePosition = a;
//cout << “\nshoe p: ” << shoePosition << ” ” << a << “\n”;
cout << “\n(s)tand\n”;
cin >> hit;
if (hit==’s’) break;
}
//return player[0].getSum();
int dealerValue = dealer(shoe, sizeOfShoe, shoePosition);
if ( (player[0].getSum()>21) && dealerValue>21) {cout << “\nYou both busted!\n”;}
else if (player[0].getSum()>21) {cout << “\nYou busted! Dealer wins!\n”;}
else if (dealerValue>21) {cout << “\nDealer busted! You win!\n”;}
else if (dealerValue>=player[0].getSum()) {cout << “\nDealer wins!\n”;}
else {cout << “\nYou win!\n”;}
//if (dealerValue>=player[0].getSum()){cout << “\nDealer wins.”;}
//else {cout << “\nPlayer wins!”;};
//system(“pause”);
}
//for dealer
/*
{
hands dealer[1];
int d =0;
while (dealer[0].getSum() >16)
{
dealer[0].cArd[x].faceValue=shoe[a].faceValue;
dealer[0].cArd[x].index=x;
dealer[0].cArd[x].suite=shoe[a].suite;
dealer[0].numCards=x;
dealer[0].cArd[x].getValue();
cout << “\nCard: ” << dealer[0].cArd[x].faceValue;
//ha[0].sumTotal = ha[0].sumTotal + ha[0].cArd[x].value;
cout << “\nCard value: ” << player[0].cArd[x].value;
while( (dealer[0].getSum() > 16) && (dealer[0].aceW11()) )
{
dealer[0].setAceTo1();
}
}
}
*/
int main()
{
int player;
int dealer;
int *cardCounter;
cardCounter=new int;
*cardCounter=0;
system(“del bjChart.csv”);
/* initialize random seed: */
srand ( time(NULL) );
int numDecks;
cout << “# of decks? \n”;
cin >> numDecks;
cout << “\n”;
int sizeOfShoe = numDecks*52;
//initialize shoe
cards shoe[numDecks*52];
//static int shoePosition=0;
//set values
initShoe(shoe, numDecks);
//print before shuffle
//printShoe(shoe, sizeOfShoe);
//print shoe
int numRuns = 1;
cout << “\nNumber of runs?”;
cin >> numRuns;
for(int r=0;r<numRuns;r++)
{
shuffleShoe(shoe, sizeOfShoe);
//print after shuffle
printShoe(shoe, sizeOfShoe);
}
/*dealer = */
dealCards(shoe, sizeOfShoe);
system(“pause”);
main();
}
MultiThreaded Coin Toss App
#include
#include
#include #include
#include
#include
#include
#include
//#include //doesn't work for comma's
//#include <sys/resource.h>
//stack linker setting
//-Wl,–stack,167772160
//future goal, implement a rng x rng new number.
#define PHI 0x9e3779b9
static uint32_t Q[4096], c = 362436;
using namespace std;
const int range = 4000000;
const int halfRange = range / 2;
//max array size is 4 million
//const long arraySize = 4000000;
const long arraySize = 10000000;
const int numThreads = 10;
std::uniform_int_distribution<> two(1,2);
std::uniform_int_distribution<> three(-1,1);
std::uniform_int_distribution<> onehk(1,range);
void main2(int);
class cmwc
{
public:
void init_rand(uint32_t x)
{
int i;
Q[0] = x;
Q[1] = x + PHI;
Q[2] = x + PHI + PHI;
for (i = 3; i < 4096; i++)
Q[i] = Q[i – 3] ^ Q[i – 2] ^ PHI ^ i;
}
uint32_t rand_cmwc(void)
{
uint64_t t, a = 18782LL;
static uint32_t i = 4095;
uint32_t x, r = 0xfffffffe;
i = (i + 1) & 4095;
t = a * Q[i] + c;
c = (t >> 32);
x = t + c;
if (x < c) {
x++;
c++;
}
return (Q[i] = r – x);
}
};
struct myThreadStructure
{
int *value;
//int array[arraySize];
long runningTotal = 0;
typedef std::mt19937 MyRNG;
MyRNG rng;
cmwc rng_1;
int returnValue()
{
return *value;
}
void initSeed(int number)
{
//standard
rng.seed(((unsigned int)time(NULL))+number);
//cmwc
rng_1.init_rand(time(NULL)+number);
}
void initArray()
{
//checks value for engine type
if (*value == 0)
{
for (int x = 0; x < arraySize; x++)
{
//I moved the if statement outside…
//array[x] = (*value == 0) ? (array[x] = ((rng_1.rand_cmwc()%range + 1) <= halfRange ? 1: -1)) : (array[x] = ((onehk(rng)) <= halfRange ? 1: -1));
//I get rid of the array here.
//array[x] = ((rng_1.rand_cmwc()%range + 1) <= halfRange ? 1: -1);
//runningTotal += array[x];
runningTotal += ((rng_1.rand_cmwc()%range + 1) <= halfRange ? 1: -1);
}
}
else if (*value == 1)
{