ConsoleApplication2.cpp
1 |
// ConsoleApplication2.cpp : ?R???\?[?? ?A?v???P?[?V??????G???g?? ?|?C???g???`??????B
|
---|---|
2 |
//
|
3 |
|
4 |
#include "stdafx.h" |
5 |
|
6 |
struct node {
|
7 |
int card[11]; //A??1?@JQK??10 |
8 |
int myhand , myace;
|
9 |
int dlhand , dlace;
|
10 |
int cardnum;
|
11 |
int life;
|
12 |
}; |
13 |
#define LIFE 21 |
14 |
struct node default_node = {
|
15 |
{ 0,8,8,8,8,8,8,8,8,8,32 }, |
16 |
0,0,0,0, 0, LIFE |
17 |
}; |
18 |
|
19 |
void node_init(struct node * node) { |
20 |
*node = default_node; |
21 |
} |
22 |
FILE *fp_out2; |
23 |
|
24 |
|
25 |
double calc_win_dealer(struct node node) { |
26 |
int i;
|
27 |
double ret = 0.0; |
28 |
struct node node2;
|
29 |
|
30 |
//?f?B?[???[??o?[?X?g????
|
31 |
while (node.dlhand > 21 && node.dlace > 0) { |
32 |
node.dlace--; node.dlhand -= 10;
|
33 |
} |
34 |
if (node.dlhand > 21) { |
35 |
return 1.0; |
36 |
} |
37 |
|
38 |
//?f?B?[???[??Stand?????
|
39 |
if (node.dlhand > 16) { |
40 |
if (node.myhand > node.dlhand) {
|
41 |
return 1.0; |
42 |
} |
43 |
else if (node.myhand == node.dlhand) { |
44 |
return 0.0; |
45 |
} |
46 |
else{
|
47 |
return 0.0; |
48 |
} |
49 |
} |
50 |
|
51 |
//?f?B?[???[??Hit?????
|
52 |
for (i = 1; i < 11; i++) { |
53 |
if (node.card[i] == 0) { |
54 |
continue;
|
55 |
} |
56 |
|
57 |
//?J?[?h???h???[????O????????
|
58 |
node2 = node; |
59 |
//?J?[?h???h???[
|
60 |
node2.card[i]--; node2.cardnum--; |
61 |
switch (i) {
|
62 |
case 1: |
63 |
node2.dlhand += 11; node2.dlace += 1; |
64 |
break;
|
65 |
default:
|
66 |
node2.dlhand += i; |
67 |
break;
|
68 |
} |
69 |
ret += calc_win_dealer(node2) * (double)(node.card[i]) / (double)node.cardnum ; |
70 |
} |
71 |
return ret;
|
72 |
} |
73 |
|
74 |
double calc_win_player(struct node node, int nohit) { |
75 |
int i,j;
|
76 |
double ret = 0.0; |
77 |
struct node node2;
|
78 |
double stand = 0.0; |
79 |
|
80 |
//?v???C???[??o?[?X?g????
|
81 |
while (node.myhand > 21 && node.myace > 0) { |
82 |
node.myace--; node.myhand -= 10;
|
83 |
} |
84 |
if (node.myhand > 21) { |
85 |
for (j = 0; j < LIFE - node.life; j++)fputc(' ', fp_out2); |
86 |
fprintf(fp_out2,"Burst\n");
|
87 |
return 0.0; |
88 |
} |
89 |
|
90 |
//Stand???????m??
|
91 |
if (nohit == 0) { |
92 |
stand = calc_win_dealer(node); |
93 |
for (j = 0; j < LIFE - node.life; j++)fputc(' ', fp_out2); |
94 |
fprintf(fp_out2, "Now %d, ace %d\n", node.myhand, node.myace);
|
95 |
} |
96 |
|
97 |
//??????????m????????o???????Stand??m??????
|
98 |
if (node.life > 0 && node.myhand < 19) { |
99 |
node.life--; |
100 |
//Hit?????
|
101 |
for (i = 1; i < 11; i++) { |
102 |
if (node.card[i] == 0) { |
103 |
continue;
|
104 |
} |
105 |
//?J?[?h???h???[????O????????
|
106 |
node2 = node; |
107 |
//?J?[?h???h???[
|
108 |
node2.card[i]--; node2.cardnum--; |
109 |
switch (i) {
|
110 |
case 1: |
111 |
node2.myhand += 11; node2.myace += 1; |
112 |
break;
|
113 |
default:
|
114 |
node2.myhand += i; |
115 |
break;
|
116 |
} |
117 |
for (j = 0; j < LIFE - node.life; j++)fputc(' ', fp_out2); |
118 |
fprintf(fp_out2, "Hit %d (%d/%d) now %d, ace %d\n", i, node.card[i], node.cardnum, node.myhand, node.myace);
|
119 |
ret += calc_win_player(node2, 0) * (double)(node.card[i]) / (double)node.cardnum; |
120 |
} |
121 |
} |
122 |
for (j = 0; j < LIFE - node.life; j++)fputc(' ', fp_out2); |
123 |
fprintf(fp_out2,"stand %10.7f, Hit %10.7f\n", stand, ret);
|
124 |
if (ret < stand) return stand; |
125 |
return ret;
|
126 |
} |
127 |
|
128 |
|
129 |
int main()
|
130 |
{ |
131 |
struct node node;
|
132 |
int i;
|
133 |
|
134 |
fopen_s(&fp_out2, "d:\\bj.debug.txt", "w"); |
135 |
|
136 |
//?????m?[?h??
|
137 |
node_init(&node); |
138 |
//???????D2???
|
139 |
node.card[1]--;
|
140 |
node.card[2]--;
|
141 |
node.myhand = 13;
|
142 |
node.myace = 1;
|
143 |
//?f?B?[?????D1???
|
144 |
node.card[3]--;
|
145 |
node.dlhand = 3;
|
146 |
node.dlace = 0;
|
147 |
|
148 |
//???????J?E???g
|
149 |
for (i = 1; i < 11; i++) { |
150 |
node.cardnum += node.card[i]; |
151 |
} |
152 |
|
153 |
//Stand?????m??
|
154 |
fprintf(fp_out2,"stand %10.7f\n",calc_win_dealer(node));
|
155 |
fprintf(fp_out2,"hit %10.7f\n", calc_win_player(node,1)); |
156 |
//getchar();
|
157 |
|
158 |
return 0; |
159 |
} |
160 |
|