# Robson » Little Red Book » Pontoon

## Chapter 9 - Question 7

Card games are another source of programming ideas. Simple games such as pontoon can be much improved by graphically displaying the cards as they are dealt.

In pontoon the computer has the obvious role of banker. In other games, such as poker or bridge, the computer could be programmed to be a player.

### Solution 1

This is my half-finished solution of Pontoon. It needs a lot of work to make it nearer to the game, but I'll get to that at some point in the future.

The player and computer play against each other, there is no banker although the player always draws first.

`#include <stdio.h>`
`#include <math.h>`
`#include <time.h>`
`#include <iostream.h>`
` `
`using namespace std;`
` `
`void ic_ResetCards();`
`int ic_CardsTotal(int iCurrentCards[]);`
`int ic_BestCardsTotal(int iCurrentCards[]);`
`char* ic_ShowCards(int iCurrentCards[]);`
`int ic_CardValue(int iCard);`
` `
`int iHighest = 12;`
`int iPlayerCards[12];`
`int iComputerCards[12];`
`char *cNames[] = {`
`               "", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",`
`               "Eight", "Nine", "Ten", "Jack", "Queen", "King"`
`                   };`
`                         `
`int ic_CardsTotal(int iCurrentCards[])`
`{`
`    int iTotal = 0;`
`    for (int n = 0; n < iHighest; n++)`
`        iTotal += ic_CardValue(iCurrentCards[n]);`
`    return iTotal;`
`}   `
` `
`int ic_BestCardsTotal(int iCurrentCards[])`
`{`
`    int iAces = 0, iTotal = 0;`
`    for (int n = 0; n < iHighest; n++)`
`    {`
`        if (iCurrentCards[n] == 1)`
`           iAces++;`
`        else`
`           iTotal += ic_CardValue(iCurrentCards[n]);`
`    }`
`    if (iAces)`
`    {`
`        if (iTotal + 11 + (iAces - 1) <= 21)`
`           iTotal += 11 + (iAces - 1);`
`        else`
`           iTotal += iAces;`
`    }       `
`    return iTotal;`
`} `
` `
`void ic_ResetCards()`
`{`
`    for (int n = 0; n < iHighest; n++)`
`    {`
`        iPlayerCards[n] = 0;                       `
`        iComputerCards[n] = 0;`
`    }`
`} `
` `
`int ic_CardValue(int iCard)`
`{`
`    if (iCard > 10)`
`        return 10;`
`    else`
`        return iCard;`
`}    `
` `
`char* ic_ShowCards(int iCurrentCards[])`
`{`
`     static char output[255];`
`     strcpy(output, "");`
`     for (int n = 0; n < iHighest; n++)`
`     {`
`         if (iCurrentCards[n] > 0)`
`         {                       `
`            strcat(output, cNames[iCurrentCards[n]]);`
`            strcat(output, " ");    `
`         }`
`     }`
`     return output;`
`}`
` `
`int ic_RandomCard()`
`{`
`    return ((13 * rand()) / RAND_MAX) + 1;`
`}`
` `
`int main(int argc, char* argv[])`
`{`
`      srand(time(NULL));`
`    rand();`
`    `
`    ic_ResetCards();  `
`    iPlayerCards[0] = ic_RandomCard();`
`    iPlayerCards[1] = ic_RandomCard();    `
`    iComputerCards[0] = ic_RandomCard();`
`    iComputerCards[1] = ic_RandomCard();   `
`    printf("The computer has been dealt two cards\n");`
`    printf("Your starting cards are: %s\n", ic_ShowCards(iPlayerCards));`
`    if (argc > 1)`
`        printf("[Computer starting cards are: %s]\n", ic_ShowCards(iComputerCards));    `
`    `
`    if (ic_BestCardsTotal(iComputerCards) == 21)`
`    {`
`       if (ic_BestCardsTotal(iPlayerCards) == 21)`
`          printf("You and the computer have made 21 - Draw!");`
`       else`
`          printf("The computer has made 21 with their starting cards - You lose!");`
`    }`
`    else if (ic_BestCardsTotal(iPlayerCards) == 21)`
`         printf("You have made 21 with your starting cards - You win!");`
`    else     `
`    {`
`        bool bFinished;     `
`        int iCard;`
`        int iCounter = 2;`
`        int iPlayerDraw, iComputerDraw;`
`        char cEndMessage[18] = "";`
`        // make up a semi-random amount for the computer to aim for`
`        // 15,16,17,18`
`        int iComputerAim = ((4 * rand()) / RAND_MAX) + 15;`
`        `
`        if (argc > 1)`
`                printf("[Computer aim: %d]\n", iComputerAim);`
` `
`        while (!bFinished)`
`        {`
`              if (iCounter > 2)`
`              {`
`                   printf("Your cards are: %s\n", ic_ShowCards(iPlayerCards));`
`                   if (argc > 1)`
`                        printf("[Computer cards are: %s]\n", ic_ShowCards(iComputerCards));`
`              }`
`              printf("Best total: %d\nDraw again?\n>", ic_BestCardsTotal(iPlayerCards));`
`              scanf("%d", &iPlayerDraw);`
`              if (iPlayerDraw)`
`              {`
`                   iCard = ic_RandomCard();`
`                   printf("You were dealt: %s\n", cNames[iCard]);`
`                   iPlayerCards[iCounter] = iCard;`
`              }  `
`              // check if player won or bust`
`              if (ic_BestCardsTotal(iPlayerCards) >= 21)`
`                   bFinished = true;`
`              else   `
`              {`
`                   if (ic_CardsTotal(iComputerCards) < iComputerAim)`
`                   {`
`                        iCard = ic_RandomCard();`
`                        printf("The computer was dealt a card\n", cNames[iCard]);                                `
`                        if (argc > 1)`
`                             printf("[The computer was dealt: %s]\n", cNames[iCard]);                                `
`                        iComputerCards[iCounter] = iCard;`
`                        iComputerDraw = 1;`
`                        if (ic_CardsTotal(iComputerCards) >= 21)`
`                            bFinished = true;`
`                   }                                `
`                   else`
`                   {`
`                        printf("The computer has decided not to take a card\n");`
`                        iComputerDraw = 0;`
`                   }`
`                   // check if computer and player didn't take a card`
`                   if (!iPlayerDraw && !iComputerDraw)`
`                      bFinished = true;`
`              }       `
`                                            `
`              iCounter++;               `
`        }`
`           `
`        printf("Player best total: %d\nComputer best total: %d\n",`
`             ic_BestCardsTotal(iPlayerCards), ic_BestCardsTotal(iComputerCards)); `
`             `
`        if (ic_BestCardsTotal(iPlayerCards) > 21)`
`             printf("Computer won!");`
`        else if (ic_BestCardsTotal(iComputerCards) > 21)`
`             printf("You won!");`
`        else if (ic_BestCardsTotal(iPlayerCards) > ic_BestCardsTotal(iComputerCards))`
`             printf("You won!");`
`        else`
`             printf("Computer won!");`
`    }`
`    printf("\n");`
`}`

Here's a screenshot of three games where I got thrashed by the computer:

I'll admit it doesn't have the visual appeal of Half-Life 2, but it's just as exciting!*

## Log

• May 6, 2005 - Added solution 1.
© 2004-18 robson | cc unless stated