プロジェクト

全般

プロフィール

CTF Writeup TMCTF2015 » ConsoleApplication2.cpp

Programming 500 - kanata, 2025/04/13 14:55

 
// ConsoleApplication2.cpp : ?R???\?[?? ?A?v???P?[?V?????̃G???g?? ?|?C???g???`???܂??B
//

#include "stdafx.h"

struct node {
int card[11]; //A??1?@JQK??10
int myhand , myace;
int dlhand , dlace;
int cardnum;
int life;
};
#define LIFE 21
struct node default_node = {
{ 0,8,8,8,8,8,8,8,8,8,32 },
0,0,0,0, 0, LIFE
};

void node_init(struct node * node) {
*node = default_node;
}
FILE *fp_out2;


double calc_win_dealer(struct node node) {
int i;
double ret = 0.0;
struct node node2;

//?f?B?[???[?̃o?[?X?g????
while (node.dlhand > 21 && node.dlace > 0) {
node.dlace--; node.dlhand -= 10;
}
if (node.dlhand > 21) {
return 1.0;
}

//?f?B?[???[??Stand????ꍇ
if (node.dlhand > 16) {
if (node.myhand > node.dlhand) {
return 1.0;
}
else if (node.myhand == node.dlhand) {
return 0.0;
}
else{
return 0.0;
}
}

//?f?B?[???[??Hit????ꍇ
for (i = 1; i < 11; i++) {
if (node.card[i] == 0) {
continue;
}

//?J?[?h???h???[????O?̏?Ԃɂ???
node2 = node;
//?J?[?h???h???[
node2.card[i]--; node2.cardnum--;
switch (i) {
case 1:
node2.dlhand += 11; node2.dlace += 1;
break;
default:
node2.dlhand += i;
break;
}
ret += calc_win_dealer(node2) * (double)(node.card[i]) / (double)node.cardnum ;
}
return ret;
}

double calc_win_player(struct node node, int nohit) {
int i,j;
double ret = 0.0;
struct node node2;
double stand = 0.0;

//?v???C???[?̃o?[?X?g????
while (node.myhand > 21 && node.myace > 0) {
node.myace--; node.myhand -= 10;
}
if (node.myhand > 21) {
for (j = 0; j < LIFE - node.life; j++)fputc(' ', fp_out2);
fprintf(fp_out2,"Burst\n");
return 0.0;
}

//Stand????ꍇ?̊m??
if (nohit == 0) {
stand = calc_win_dealer(node);
for (j = 0; j < LIFE - node.life; j++)fputc(' ', fp_out2);
fprintf(fp_out2, "Now %d, ace %d\n", node.myhand, node.myace);
}

//???܂?ɂ??Ⴂ?m???ł????o?Ȃ??ꍇ??Stand?̊m????Ԃ?
if (node.life > 0 && node.myhand < 19) {
node.life--;
//Hit????ꍇ
for (i = 1; i < 11; i++) {
if (node.card[i] == 0) {
continue;
}
//?J?[?h???h???[????O?̏?Ԃɂ???
node2 = node;
//?J?[?h???h???[
node2.card[i]--; node2.cardnum--;
switch (i) {
case 1:
node2.myhand += 11; node2.myace += 1;
break;
default:
node2.myhand += i;
break;
}
for (j = 0; j < LIFE - node.life; j++)fputc(' ', fp_out2);
fprintf(fp_out2, "Hit %d (%d/%d) now %d, ace %d\n", i, node.card[i], node.cardnum, node.myhand, node.myace);
ret += calc_win_player(node2, 0) * (double)(node.card[i]) / (double)node.cardnum;
}
}
for (j = 0; j < LIFE - node.life; j++)fputc(' ', fp_out2);
fprintf(fp_out2,"stand %10.7f, Hit %10.7f\n", stand, ret);
if (ret < stand) return stand;
return ret;
}


int main()
{
struct node node;
int i;

fopen_s(&fp_out2, "d:\\bj.debug.txt", "w");

//?????m?[?h?쐬
node_init(&node);
//?????̎?D2?܂?
node.card[1]--;
node.card[2]--;
node.myhand = 13;
node.myace = 1;
//?f?B?[???̎?D1?܂?
node.card[3]--;
node.dlhand = 3;
node.dlace = 0;
//???????J?E???g
for (i = 1; i < 11; i++) {
node.cardnum += node.card[i];
}

//Stand?̏ꍇ?̊m??
fprintf(fp_out2,"stand %10.7f\n",calc_win_dealer(node));
fprintf(fp_out2,"hit %10.7f\n", calc_win_player(node,1));
//getchar();

return 0;
}

(1-1/2)