-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCant_stop_simulation.cpp
595 lines (525 loc) · 24.6 KB
/
Cant_stop_simulation.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
#include <iostream>
#include <string>
#include <random>
#include <thread>
#include <chrono>
#include <ctime>
#include <cstdlib>
#include <bits/stdc++.h>
using namespace std;
int roll_die_1(){
return rand()%6 + 1;
}
int roll_die_2(){
return rand()%6 + 1;
}
int roll_die_3(){
return rand()%6 + 1;
}
int roll_die_4(){
return rand()%6 + 1;
}
void print_4_dice_result(int *roll_die_1, int *roll_die_2, int *roll_die_3, int *roll_die_4){
cout << "Dice 1 rolls: "<<*roll_die_1<<"\n";
cout << "Dice 2 rolls: "<<*roll_die_2<<"\n";
cout << "Dice 3 rolls: "<<*roll_die_3<<"\n";
cout << "Dice 4 rolls: "<<*roll_die_4<<"\n";
}
void Clear_Terminal(){
cout << "\n\n\n\n\n\n\n\n\n";
}
void End_formater(){
cout << "\n\n\n";
}
void Dice_Randomiser(bool *run_game, int *dice1, int *dice2, int *dice3, int *dice4){
srand(time(0));
while (*run_game == true){
this_thread::sleep_for(chrono::milliseconds(1));
*dice1 = roll_die_1();
*dice2 = roll_die_2();
*dice3 = roll_die_3();
*dice4 = roll_die_4();
}
}
bool check_for_busting(){
return true;
}
void Player_1_turn(){
}
int getNearest(int x, int y, int target) {
if (target - x >= y - target)
return y;
else
return x;
}
int getNearestElement(int arr[], int n, int target) {
if (target <= arr[0])
return arr[0];
if (target >= arr[n - 1])
return arr[n - 1];
int left = 0, right = n, mid = 0;
while (left < right) {
mid = (left + right) / 2;
if (arr[mid] == target)
return arr[mid];
if (target < arr[mid]) {
if (mid > 0 && target > arr[mid - 1])
return getNearest(arr[mid - 1], arr[mid], target);
right = mid;
} else {
if (mid < n - 1 && target < arr[mid + 1])
return getNearest(arr[mid], arr[mid + 1], target);
left = mid + 1;
}
}
return arr[mid];
}
void print_board_progress(int board_col_2[], int board_col_3[], int board_col_4[], int board_col_5[], int board_col_6[], int board_col_7[], int board_col_8[], int board_col_9[], int board_col_10[], int board_col_11[], int board_col_12[]){
cout << "\n\n Board progress:\n\n";
for (int f = 0; f < 2; f++){
cout << " "<<board_col_2[f];
}
cout << "\n";
for (int f = 0; f < 4; f++){
cout << " "<<board_col_3[f];
}
cout << "\n";
for (int f = 0; f < 6; f++){
cout << " "<<board_col_4[f];
}
cout << "\n";
for (int f = 0; f < 8; f++){
cout << " "<<board_col_5[f];
}
cout << "\n";
for (int f = 0; f < 10; f++){
cout << " "<<board_col_6[f];
}
cout << "\n";
for (int f = 0; f < 11; f++){
cout << " "<<board_col_7[f];
}
cout << "\n";
for (int f = 0; f < 10; f++){
cout << " "<<board_col_8[f];
}
cout << "\n";
for (int f = 0; f < 8; f++){
cout << " "<<board_col_9[f];
}
cout << "\n";
for (int f = 0; f < 6; f++){
cout << " "<<board_col_10[f];
}
cout << "\n";
for (int f = 0; f < 4; f++){
cout << " "<<board_col_11[f];
}
cout << "\n";
for (int f = 0; f < 2; f++){
cout << " "<<board_col_12[f];
}
cout << "\n\n\n";
}
void reset_board(int board_col_2[], int board_col_3[], int board_col_4[], int board_col_5[], int board_col_6[], int board_col_7[], int board_col_8[], int board_col_9[], int board_col_10[], int board_col_11[], int board_col_12[]){
for (int f = 0; f < 2; f++){
board_col_2[f] = 0;
}
for (int f = 0; f < 4; f++){
board_col_3[f] = 0;
}
for (int f = 0; f < 6; f++){
board_col_4[f] = 0;
}
for (int f = 0; f < 8; f++){
board_col_5[f] = 0;
}
for (int f = 0; f < 10; f++){
board_col_6[f] = 0;
}
for (int f = 0; f < 11; f++){
board_col_7[f] = 0;
}
for (int f = 0; f < 10; f++){
board_col_8[f] = 0;
}
for (int f = 0; f < 8; f++){
board_col_9[f] = 0;
}
for (int f = 0; f < 6; f++){
board_col_10[f] = 0;
}
for (int f = 0; f < 4; f++){
board_col_11[f] = 0;
}
for (int f = 0; f < 2; f++){
board_col_12[f] = 0;
}
}
void switch_player_1_selection(int column_selection, int board_col_2[], int board_col_3[], int board_col_4[], int board_col_5[], int board_col_6[], int board_col_7[], int board_col_8[], int board_col_9[], int board_col_10[], int board_col_11[], int board_col_12[], int *two_column_progress, int *three_column_progress, int *four_column_progress, int *five_column_progress, int *six_column_progress, int *seven_column_progress, int *eight_column_progress, int *nine_column_progress, int *ten_column_progress, int *eleven_column_progress, int *twelve_column_progress){
switch (column_selection){
case 2:
if (board_col_2[1] == 0){
board_col_2[(*two_column_progress)] = 1;
(*two_column_progress)++;
}
break;
case 3:
if (board_col_3[3] == 0){
board_col_3[(*three_column_progress)] = 1;
(*three_column_progress)++;
}
break;
case 4:
if (board_col_4[5] == 0){
board_col_4[(*four_column_progress)] = 1;
(*four_column_progress)++;
}
break;
case 5:
if (board_col_5[7] == 0){
board_col_5[(*five_column_progress)] = 1;
(*five_column_progress)++;
}
break;
case 6:
if (board_col_6[9] == 0){
board_col_6[(*six_column_progress)] = 1;
(*six_column_progress)++;
}
break;
case 7:
if (board_col_7[10] == 0){
board_col_7[(*seven_column_progress)] = 1;
(*seven_column_progress)++;
}
break;
case 8:
if (board_col_8[9] == 0){
board_col_8[(*eight_column_progress)] = 1;
(*eight_column_progress)++;
}
break;
case 9:
if (board_col_9[7] == 0){
board_col_9[(*nine_column_progress)] = 1;
(*nine_column_progress)++;
}
break;
case 10:
if (board_col_10[5] == 0){
board_col_10[(*ten_column_progress)] = 1;
(*ten_column_progress)++;
}
break;
case 11:
if (board_col_11[3] == 0){
board_col_11[(*eleven_column_progress)] = 1;
(*eleven_column_progress)++;
}
break;
case 12:
if (board_col_12[1] == 0){
board_col_12[(*twelve_column_progress)] = 1;
(*twelve_column_progress)++;
}
break;
}
}
void simulation(){
dice_1_p1 = dice_1_next;
dice_2_p1 = dice_2_next;
dice_3_p1 = dice_3_next;
dice_4_p1 = dice_4_next;
if (first_sum == first_column){
switch_player_1_selection(first_column, board_col_2, board_col_3, board_col_4, board_col_5, board_col_6, board_col_7, board_col_8, board_col_9, board_col_10, board_col_11, board_col_12, &two_column_progress, &three_column_progress, &four_column_progress, &five_column_progress, &six_column_progress, &seven_column_progress, &eight_column_progress, &nine_column_progress, &ten_column_progress, &eleven_column_progress, &twelve_column_progress);
}
if (second_sum == second_column){
switch_player_1_selection(second_column, board_col_2, board_col_3, board_col_4, board_col_5, board_col_6, board_col_7, board_col_8, board_col_9, board_col_10, board_col_11, board_col_12, &two_column_progress, &three_column_progress, &four_column_progress, &five_column_progress, &six_column_progress, &seven_column_progress, &eight_column_progress, &nine_column_progress, &ten_column_progress, &eleven_column_progress, &twelve_column_progress);
}
if (third_sum == third_column){
switch_player_1_selection(third_column, board_col_2, board_col_3, board_col_4, board_col_5, board_col_6, board_col_7, board_col_8, board_col_9, board_col_10, board_col_11, board_col_12, &two_column_progress, &three_column_progress, &four_column_progress, &five_column_progress, &six_column_progress, &seven_column_progress, &eight_column_progress, &nine_column_progress, &ten_column_progress, &eleven_column_progress, &twelve_column_progress);
}
if ((first_sum != ) && (second_sum != second_column) && (third_sum != third_column)){
busting = true;
{(dice_1_p1+dice_2_p1), (dice_3_p1+dice_4_p1), (dice_2_p1+dice_3_p1)};
}
}
int main(){
bool run_game = true;
bool run_main = true;
bool busting = false;
int player_1_ID = 1;
int player_2_ID = 2;
int Player_1_SET = 3;
int Player_2_SET = 4;
// Empty game board at the start
// The randomised future dices, these dices are rolled on another thread non_stop
int dice_1_next = 0;
int dice_2_next = 0;
int dice_3_next = 0;
int dice_4_next = 0;
// After player 1 makes the decision, these dices are then selected from the future dices from the other thread
int dice_1_p1 = 0;
int dice_2_p1 = 0;
int dice_3_p1 = 0;
int dice_4_p1 = 0;
// After player 1 makes the decision, these dices are then selected from the future dices from the other thread
int dice_1_p2 = 0;
int dice_2_p2 = 0;
int dice_3_p2 = 0;
int dice_4_p2 = 0;
// running thread
thread Run____Dice_Randomiser___THREAD(Dice_Randomiser,&run_game, &dice_1_next, &dice_2_next, &dice_3_next, &dice_4_next);
Run____Dice_Randomiser___THREAD.detach();
string player_1_decision = "";
string player_2_decision = "";
int player_1_pair_selection_first = 0;
int player_1_pair_selection_second = 0;
int player_1_pair_selection_third = 0;
int first_column = 0;
int second_column = 0;
int third_column = 0;
int Busting_Failure[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
int two_column_progress = 0;
int three_column_progress = 0;
int four_column_progress = 0;
int five_column_progress = 0;
int six_column_progress = 0;
int seven_column_progress = 0;
int eight_column_progress = 0;
int nine_column_progress = 0;
int ten_column_progress = 0;
int eleven_column_progress = 0;
int twelve_column_progress = 0;
bool first_roll = true;
// cout << "\n\n\n";
int board_col_2[2] = {0, 0};
int board_col_3[4] = {0, 0, 0, 0};
int board_col_4[6] = {0, 0, 0, 0, 0, 0};
int board_col_5[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int board_col_6[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int board_col_7[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int board_col_8[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int board_col_9[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int board_col_10[6] = {0, 0, 0, 0, 0, 0};
int board_col_11[4] = {0, 0, 0, 0};
int board_col_12[2] = {0, 0};
// int number_of_rolls = 0;
for (int k = 1; k < 101; k++){
reset_board(board_col_2, board_col_3, board_col_4, board_col_5, board_col_6, board_col_7, board_col_8, board_col_9, board_col_10, board_col_11, board_col_12);
busting = false;
this_thread::sleep_for(chrono::milliseconds(5));
int number_of_rolls = 0;
dice_1_p1 = dice_1_next;
dice_2_p1 = dice_2_next;
dice_3_p1 = dice_3_next;
dice_4_p1 = dice_4_next;
// print_4_dice_result(&dice_1_p1, &dice_2_p1, &dice_3_p1, &dice_4_p1);
// cout << "\n\n";
// cout << "\n\n There are 3 dice pair combinations: \n";
// cout << " 1 " << dice_1_p1 <<" and "<< dice_2_p1 << " ("<<dice_1_p1+dice_2_p1<< ") \n";
// cout << " 2 " << dice_3_p1 <<" and "<< dice_4_p1 << " ("<<dice_3_p1+dice_4_p1<< ") \n";
// cout << " 3 " << dice_2_p1 <<" and "<< dice_3_p1 << " ("<<dice_2_p1+dice_3_p1<< ") \n";
// cout << "\n\n";
int closet_to_7[3] = {(dice_1_p1+dice_2_p1), (dice_3_p1+dice_4_p1), (dice_2_p1+dice_3_p1)};
bool swapped = false;
for (int i = 0; i < 3-1; i++){
swapped = false;
for (int j = 0; j < 3-i-1; j++){
if (closet_to_7[j] > closet_to_7[j+1]){
swap(closet_to_7[j], closet_to_7[j+1]);
swapped = true;
}
}
}
int closet_first = getNearestElement(closet_to_7,3,7);
for (int j = 0; j < 3; j++){
if (closet_first == closet_to_7[j]){
closet_to_7[j] = -99;
}
}
bool swapped_second = false;
for (int i = 0; i < 3-1; i++){
swapped_second = false;
for (int j = 0; j < 3-i-1; j++){
if (closet_to_7[j] > closet_to_7[j+1]){
swap(closet_to_7[j], closet_to_7[j+1]);
swapped_second = true;
}
}
}
int closet_second = getNearestElement(closet_to_7,3,7);
for (int j = 0; j < 3; j++){
if (closet_second == closet_to_7[j]){
closet_to_7[j] = -109;
}
}
bool swapped_third = false;
for (int i = 0; i < 3-1; i++){
swapped_second = false;
for (int j = 0; j < 3-i-1; j++){
if (closet_to_7[j] > closet_to_7[j+1]){
swap(closet_to_7[j], closet_to_7[j+1]);
swapped_second = true;
}
}
}
int closet_third = getNearestElement(closet_to_7,3,7);
if (closet_first >=2 && closet_first <= 12){
player_1_pair_selection_first = closet_first;
}
if (closet_second >=2 && closet_second <= 12){
player_1_pair_selection_second = closet_second;
}
if (closet_third >= 2 && closet_third <= 12){
player_1_pair_selection_third = closet_third;
}
if (first_roll == true){
first_column = player_1_pair_selection_first;
switch_player_1_selection(first_column, board_col_2, board_col_3, board_col_4, board_col_5, board_col_6, board_col_7, board_col_8, board_col_9, board_col_10, board_col_11, board_col_12, &two_column_progress, &three_column_progress, &four_column_progress, &five_column_progress, &six_column_progress, &seven_column_progress, &eight_column_progress, &nine_column_progress, &ten_column_progress, &eleven_column_progress, &twelve_column_progress);
second_column = player_1_pair_selection_second;
switch_player_1_selection(second_column, board_col_2, board_col_3, board_col_4, board_col_5, board_col_6, board_col_7, board_col_8, board_col_9, board_col_10, board_col_11, board_col_12, &two_column_progress, &three_column_progress, &four_column_progress, &five_column_progress, &six_column_progress, &seven_column_progress, &eight_column_progress, &nine_column_progress, &ten_column_progress, &eleven_column_progress, &twelve_column_progress);
third_column = player_1_pair_selection_third;
switch_player_1_selection(third_column, board_col_2, board_col_3, board_col_4, board_col_5, board_col_6, board_col_7, board_col_8, board_col_9, board_col_10, board_col_11, board_col_12, &two_column_progress, &three_column_progress, &four_column_progress, &five_column_progress, &six_column_progress, &seven_column_progress, &eight_column_progress, &nine_column_progress, &ten_column_progress, &eleven_column_progress, &twelve_column_progress);
first_roll = false;
}
// cout << "\n\n\n Chosen columns are: \n";
// cout << " "<<first_column<<"\n";
// cout << " "<<second_column<<"\n";
// cout << " "<<third_column<<"\n";
while (busting == false){
this_thread::sleep_for(chrono::milliseconds(5));
number_of_rolls++;
dice_1_p1 = dice_1_next;
dice_2_p1 = dice_2_next;
dice_3_p1 = dice_3_next;
dice_4_p1 = dice_4_next;
// print_4_dice_result(&dice_1_p1, &dice_2_p1, &dice_3_p1, &dice_4_p1);
// cout << "\n\n";
// cout << "\n\n There are 3 dice pair combinations: \n";
// cout << " 1 " << dice_1_p1 <<" and "<< dice_2_p1 << " ("<<dice_1_p1+dice_2_p1<< ") \n";
// cout << " 2 " << dice_3_p1 <<" and "<< dice_4_p1 << " ("<<dice_3_p1+dice_4_p1<< ") \n";
// cout << " 3 " << dice_2_p1 <<" and "<< dice_3_p1 << " ("<<dice_2_p1+dice_3_p1<< ") \n";
// cout << "\n\n";
closet_to_7[0] = (dice_1_p1+dice_2_p1);
closet_to_7[1] = (dice_3_p1+dice_4_p1);
closet_to_7[2] = (dice_2_p1+dice_3_p1);
bool swapped = false;
for (int i = 0; i < 3-1; i++){
swapped = false;
for (int j = 0; j < 3-i-1; j++){
if (closet_to_7[j] > closet_to_7[j+1]){
swap(closet_to_7[j], closet_to_7[j+1]);
swapped = true;
}
}
}
int closet_first = getNearestElement(closet_to_7,3,7);
for (int j = 0; j < 3; j++){
if (closet_first == closet_to_7[j]){
closet_to_7[j] = -99;
}
}
bool swapped_second = false;
for (int i = 0; i < 3-1; i++){
swapped_second = false;
for (int j = 0; j < 3-i-1; j++){
if (closet_to_7[j] > closet_to_7[j+1]){
swap(closet_to_7[j], closet_to_7[j+1]);
swapped_second = true;
}
}
}
int closet_second = getNearestElement(closet_to_7,3,7);
for (int j = 0; j < 3; j++){
if (closet_second == closet_to_7[j]){
closet_to_7[j] = -109;
}
}
bool swapped_third = false;
for (int i = 0; i < 3-1; i++){
swapped_second = false;
for (int j = 0; j < 3-i-1; j++){
if (closet_to_7[j] > closet_to_7[j+1]){
swap(closet_to_7[j], closet_to_7[j+1]);
swapped_second = true;
}
}
}
int closet_third = getNearestElement(closet_to_7,3,7);
int first_sum = 0;
int second_sum = 0;
int third_sum = 0;
if (closet_first >=2 && closet_first <= 12){
first_sum = closet_first;
}
if (closet_second >=2 && closet_second <= 12){
second_sum = closet_second;
}
if (closet_third >= 2 && closet_third <= 12){
third_sum = closet_third;
}
if (first_roll == false){
if (first_sum == first_column){
switch_player_1_selection(first_column, board_col_2, board_col_3, board_col_4, board_col_5, board_col_6, board_col_7, board_col_8, board_col_9, board_col_10, board_col_11, board_col_12, &two_column_progress, &three_column_progress, &four_column_progress, &five_column_progress, &six_column_progress, &seven_column_progress, &eight_column_progress, &nine_column_progress, &ten_column_progress, &eleven_column_progress, &twelve_column_progress);
}
if (second_sum == second_column){
switch_player_1_selection(second_column, board_col_2, board_col_3, board_col_4, board_col_5, board_col_6, board_col_7, board_col_8, board_col_9, board_col_10, board_col_11, board_col_12, &two_column_progress, &three_column_progress, &four_column_progress, &five_column_progress, &six_column_progress, &seven_column_progress, &eight_column_progress, &nine_column_progress, &ten_column_progress, &eleven_column_progress, &twelve_column_progress);
}
if (third_sum == third_column){
switch_player_1_selection(third_column, board_col_2, board_col_3, board_col_4, board_col_5, board_col_6, board_col_7, board_col_8, board_col_9, board_col_10, board_col_11, board_col_12, &two_column_progress, &three_column_progress, &four_column_progress, &five_column_progress, &six_column_progress, &seven_column_progress, &eight_column_progress, &nine_column_progress, &ten_column_progress, &eleven_column_progress, &twelve_column_progress);
}
}
if ((first_sum != first_column) && (second_sum != second_column) && (third_sum != third_column)){
busting = true;
// cout << "\n\n\n BUSTED!!!!! after "<< number_of_rolls<< " rolls \n\n";
switch (number_of_rolls){
case 1:
Busting_Failure[0] = Busting_Failure[0] +1;
break;
case 2:
Busting_Failure[1] = Busting_Failure[1] +1;
break;
case 3:
Busting_Failure[2] = Busting_Failure[2] +1;
break;
case 4:
Busting_Failure[3] = Busting_Failure[3] +1;
break;
case 5:
Busting_Failure[4] = Busting_Failure[4] +1;
break;
case 6:
Busting_Failure[5] = Busting_Failure[5] +1;
break;
case 7:
Busting_Failure[6] = Busting_Failure[6] +1;
break;
case 8:
Busting_Failure[7] = Busting_Failure[7] +1;
break;
case 9:
Busting_Failure[8] = Busting_Failure[8] +1;
break;
case 10:
Busting_Failure[9] = Busting_Failure[9] +1;
break;
case 11:
Busting_Failure[10] = Busting_Failure[10] +1;
break;
case 12:
Busting_Failure[11] = Busting_Failure[11] +1;
break;
}
}
//print_board_progress(board_col_2, board_col_3, board_col_4, board_col_5, board_col_6, board_col_7, board_col_8, board_col_9, board_col_10, board_col_11, board_col_12);
}
}
End_formater();
for (int j = 0; j < 12; j++){
cout << " " << Busting_Failure[j]<<"\n";
}
End_formater();
return 0;
}