2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語でブラックジャックを作った

Last updated at Posted at 2020-05-14

はじめに

以前にC言語でおみくじを作ったので今度はブラックジャックを作りました。
(追記 5/16)今回はC言語の学習のために作ったので、一部ルールを省いています。
     例えばAの判定は本当なら1または11ですが、今回は1で固定しています。

コード

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <time.h>

typedef struct {
	char suit[8];
	int rank;
} Card;

typedef struct {
	Card hands[12];
	int handnum;
	int score;
} Participant;

void shuffle(Card deck[], int);
void drawing(Card deck[], int*, Participant*, int);
void show(Card, Participant, int);

int main(void)
{
	/* Make a deck */
	char suits[][8] = {"spade", "club", "diamond", "heart"};
	int i, j, k = 0;
	Card deck[52];

	for (i = 0; i < 4; i++) {
		for (j = 0; j < 13; j++) {
			strcpy(deck[k].suit, suits[i]);
			deck[k].rank = j + 1;
			k++;
		}
	}
	shuffle(deck, 52);
	/* end */

	/* First draw */
	Participant player;
	Participant dealer;
	int decknum = 0;
	player.handnum = 0;
	dealer.handnum = 0;
	player.score = 0;
	dealer.score = 0;

	drawing(deck, &decknum, &player, 1);
	drawing(deck, &decknum, &player, 1);
	drawing(deck, &decknum, &dealer, 1);
	drawing(deck, &decknum, &dealer, 0);
	/* end */

	/* Hit or Stand */
	char str[8];
	int choose;
	while (1) {
		do {
			printf("Choose Hit or Stand.\n");
			printf("If Hit, enter 2. If Stand, enter 1.\n");
			fgets(str, sizeof(str), stdin);
			printf("\n");
			choose = atoi(str);
		} while (!(choose == 2 || choose == 1));
		if (choose == 1) break;
		drawing(deck, &decknum, &player, 1);
		if (player.score > 21) {
			printf("You lose.\n");
			exit(0);
		}
	}
	/* end */

	/* Dealer's turn */
	show(dealer.hands[1], dealer, 1);
	while (1) {
		if (dealer.score >= 17) break;
		drawing(deck, &decknum, &dealer, 1);
	}
	/* end */

	/* Judge */
	if (dealer.score > 21 || player.score > dealer.score) {
		printf("You win!\n");
	} else if (dealer.score > player.score) {
		printf("You lose\n");
	} else {
		printf("Draw\n");
	}
	/* end */

	return 0;
}

void shuffle(Card deck[], int size)
{
	int i, j;
	Card t;

	srand((unsigned int)time(NULL));
	for (i = 0; i < size; i++) {
		j = rand() % size;
		t = deck[i];
		deck[i] = deck[j];
		deck[j] = t;
	}
}

void drawing(Card deck[], int *decknum, Participant *people, int display)
{
	Card card;
	card = deck[*decknum];
	*decknum += 1;
	people->hands[people->handnum] = card;
	people->handnum += 1;
	if (card.rank >= 11) {
		people->score += 10;
	} else {
		people->score += card.rank;
	}
	if (display) {
		show(card, *people, 0);
	}
}

void show(Card card, Participant people, int shownum)
{
	char str[32];
	if (shownum == 0) {
		strcpy(str, "Drawn card");
	} else {
		strcpy(str, "Dealer's another card");
	}
	switch (card.rank) {
                case 1:
                        printf("%s is A of %s.\n", str, card.suit);
                        break;
                case 10:
                        printf("%s is 10 of %s.\n", str, card.suit);
                        break;
                case 11:
                        printf("%s is J of %s.\n", str, card.suit);
                        break;
                case 12:
                        printf("%s is Q of %s.\n", str, card.suit);
                        break;
                case 13:
                        printf("%s is K of %s.\n", str, card.suit);
                        break;
                default:
                        printf("%s is %d of %s.\n", str, card.rank, card.suit);
                        break;
        }
        printf("The total score is %d.\n\n", people.score);
}

以上です

2
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?