Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Black Jack for C-Programming, a program that two people can use to play a game o

ID: 3717531 • Letter: B

Question

Black Jack for C-Programming, a program that two people can use to play a game of BlackJack

NOTE: There should be THREE players. Two regular players AND the dealer, as the dealer also gets dealt a hand in Black-Jack.

Explanation / Answer

/* *----------------------------------------------------------------------------- * Adaptive Blackjack player * * A trivial C code that plays blackjack. Actually, this is the code * that *learns* how to play blackjack, by playing it with itself. The * code implements a bona fide neural network with * back-propagation. The network is made of a single neuron, * possessing a single byte of intelligence. The neuron has "weights" * and a "threshold"; the neuron "fires" when the value of an * activation function computed over the current input exceeds the * current threshold. In game terms, using the current score and the * number of aces on hand the neuron decides if to draw another card * or to stay. The threshold (the neuron's memory) is adjusted as the * game proceeds, using back-propagation -- if you can discern it in * such a primitive setting. * * The program starts with two players, one very cautious and the * other one a risk taker. That is, the initial values of the * threshold are deliberately set to be far from optimal, which causes * one player to stay even with a low score, and the other one to * overdraw. Therefore, at the beginning, one of the players has * typically a winning streak. As they play against each other, they * learn when to draw and when to stay and end up playing as peers, * winning on average every other game. * * Although the code is purely in ANSI C, it is written in a C++ * style. * * $Id: black-jack.c,v 1.2 1998/03/06 23:27:58 oleg Exp oleg $ * */ #include #include #include #include /*-------------- * This part of the code deals with a deck of cards */ struct Card { enum Suit { Spades, Clubs, Hearts, Diamonds } suit; int value; char * const name; } Deck [52] = { {Spades, 2, "2"}, {Spades, 3, "3"}, {Spades, 4, "4"}, {Spades, 5, "5"}, {Spades, 6, "6"}, {Spades, 7, "7"}, {Spades, 8, "8"}, {Spades, 9, "9"}, {Spades, 10, "10"}, {Spades, 10, "Jack"}, {Spades, 10, "Queen"}, {Spades, 10, "King"}, {Spades, 11, "Ace"}, {Clubs, 2, "2"}, {Clubs, 3, "3"}, {Clubs, 4, "4"}, {Clubs, 5, "5"}, {Clubs, 6, "6"}, {Clubs, 7, "7"}, {Clubs, 8, "8"}, {Clubs, 9, "9"}, {Clubs, 10, "10"}, {Clubs, 10, "Jack"}, {Clubs, 10, "Queen"}, {Clubs, 10, "King"}, {Clubs, 11, "Ace"}, {Hearts, 2, "2"}, {Hearts, 3, "3"}, {Hearts, 4, "4"}, {Hearts, 5, "5"}, {Hearts, 6, "6"}, {Hearts, 7, "7"}, {Hearts, 8, "8"}, {Hearts, 9, "9"}, {Hearts, 10, "10"}, {Hearts, 10, "Jack"}, {Hearts, 10, "Queen"}, {Hearts, 10, "King"}, {Hearts, 11, "Ace"}, {Diamonds, 2, "2"}, {Diamonds, 3, "3"}, {Diamonds, 4, "4"}, {Diamonds, 5, "5"}, {Diamonds, 6, "6"}, {Diamonds, 7, "7"}, {Diamonds, 8, "8"}, {Diamonds, 9, "9"}, {Diamonds, 10, "10"}, {Diamonds, 10, "Jack"}, {Diamonds, 10, "Queen"}, {Diamonds, 10, "King"}, {Diamonds, 11, "Ace"}, }; /* 0 means the corresponding card hasn't been drawn; otherwise, the card has been drawn */ static char Cards_drawn [52]; /* The number of cards that haven't been drawn yet */ static int Cards_left; /* Methods of the Cards "package" */ static void Cards_init(void) { Cards_left = sizeof(Deck)/sizeof(Deck[0]); memset(Cards_drawn,0,sizeof(Cards_drawn)); } static void Cards_print(const int index) { const struct Card * cp; assert( index >= 0 && index name); switch(cp->suit) { case Spades: printf("spades"); break; case Clubs: printf("clubs"); break; case Hearts: printf("hearts"); break; case Diamonds: printf("diamonds"); break; default: assert(0 /*can't happen*/); } } /* Draw a card and return its index */ static int Cards_draw(void) { int offset; register int i; if( Cards_left < 1 ) printf(" No cards left to draw! "), exit(4); offset = Cards_left == 1 ? 1 : 1 + (rand() % Cards_left); /* In the following loop, we search for a offset-th card in the deck that hasn't been drawn. In other words, we search for offseth-th zero element of an array Cards_drawn */ for(i=0; i= 0 && index Total %d game played. %s won %d, %s won %d ", n,Player1.name,Player1.no_wins,Player2.name,Player2.no_wins); }