-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard_game.cpp
More file actions
172 lines (156 loc) · 5.29 KB
/
Copy pathcard_game.cpp
File metadata and controls
172 lines (156 loc) · 5.29 KB
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
#include <iostream>
#include <vector>
#include <time.h>
#include <stdlib.h>
#include <map>
#include <stack>
#include <cmath>
using namespace std;
vector<int> deck;
void init() {
for (int i = 1; i <= 13; ++i) {
for (int j = 0; j < 4; ++j) {
deck.push_back(i);
}
}
}
vector<int> newDeck() {
vector<int> unshuffled(deck);
vector<int> shuffled;
for (int i = 52; i >= 1; --i) {
int r = rand() % i;
shuffled.push_back(unshuffled[r]);
unshuffled.erase(unshuffled.begin() + r);
}
return shuffled;
}
bool run(vector<int> d) {
// cout << "the deck: ";
//for (int i = 0; i < d.size(); ++i) {
//cout << d[i] << " ";
//}
//cout << "\n";
// map of card value to it's position in the winning stack
map<int,int> val;
// map of position to it's card value in the winning stack
vector<int> pos(10, 0);
// stack for losing cards
stack<int> loseStack;
for (int i = 0; i < 52; ++i) {
int card = d[i];
//cout << "* CARD = " << card << "\n";
// is it in the win stack?
if (val.count(card)) {
//cout << "card already in there. continuing\n";
continue;
}
// can we place it?
// find available slots for the card
int lowPos = -1, highPos = 10;
int low = 0, high = 14;
// if val.size() is 0, then all positions are open
if (val.size()) {
// find the highest card lower than it
// run from 0 to the card value - 1, because we will never place
// the card in a position the same or higher than its value
for (int j = 0; j < card; ++j) {
if (pos[j] && pos[j] < card && pos[j] > low) {
lowPos = j;
low = pos[j];
}
}
// find the lowest card higher than it
for (int j = 9; j >= 0 && j >= card - 4; --j) {
if (pos[j] && pos[j] > card && pos[j] < high) {
highPos = j;
high = pos[j];
}
}
}
lowPos++;
highPos--;
//cout << "lowPos = " << lowPos << "\n";
//cout << "highPos = " << highPos << "\n";
if (lowPos > highPos) {
// cannot place card
loseStack.push(card);
//cout << "--could not place\n";
} else if (highPos == lowPos) {
// only one spot to place, so place the card
pos[lowPos] = card;
val[card] = lowPos;
//cout << "--placed in " << lowPos << "\n";
} else {
// more than one spot to place it, choose where
// for each position, find the difference of the # of positions
// divided by the number of cards that can be placed and the
// same thing on the right side
int bestPos = lowPos;
// why 3? 10/4 = 2.5 is the highest possible difference
double smallestDifference = 3;
// # of cards that can be placed on the left
int leftCards = (card - (low + 1));
//cout << "leftCards = " << leftCards << "\n";
// # of cards that can be placed on the right
int rightCards = ((high) - (card + 1));
//cout << "rightCards = " << rightCards << "\n";
for (int j = lowPos; j <= highPos; ++j) {
// use j as a pivot point
//cout << "position = " << j << "\n";
// # of positions open on the left
double leftFactor;
double leftPositionsOpen = j - lowPos;
if (leftCards > 0) {
leftFactor = leftPositionsOpen / leftCards;
} else {
leftFactor = 3;
}
//cout << "leftFactor = " << leftFactor << "\n";
// # of positions open on the right
double rightFactor;
double rightPositionsOpen = highPos - j;
if (rightCards > 0) {
rightFactor = rightPositionsOpen / rightCards;
} else {
rightFactor = 3;
}
//cout << "rightFactor = " << rightFactor << "\n";
double difference = abs(leftFactor - rightFactor);
//cout << "difference = " << difference << "\n";
if (difference < smallestDifference) {
smallestDifference = difference;
bestPos = j;
}
}
pos[bestPos] = card;
val[card] = bestPos;
//cout << "best position = " << bestPos << "\n";
//cout << "--placed in " << bestPos << "\n";
}
// have we won?
if (val.size() >= 10) {
return true;
}
// have we lost?
if (loseStack.size() >= 6) {
return false;
}
}
return false;
}
int main(int argc, char const* argv[])
{
int runs = atoi(argv[1]);
int wins = 0;
init();
srand(time(NULL));
for (int i = 0; i < runs; ++i) {
if (run(newDeck())) {
wins++;
}
}
cout << runs << " runs\n";
cout << wins << " wins\n";
cout << (double) wins / runs * 100 << "% win rate\n";
return 0;
}