User Tools

Site Tools


notes:discrete:fall2023:projects:cgf0

This is an old revision of the document!


CGF0

STANDARD 52-CARD DECK

VALUES

A standard deck of playing cards has 52 cards from each of the four suits. There are 13 cards in each suit: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, and King.

SUITES

The four suits in a standard deck include hearts, diamonds, spades, and clubs.

Structure For Doubly Linked List

A doubly linked list offers us the option to move both backward and forwards in a list. This is done by adding a pointer to the previous node. Here is an example of a structure of a doubly linked list:

struct cardnode
{
    bool Active;
    int cardID;         // Card id to call for displaying
    cardnode* next;    // Pointer to the next card node
    cardnode* prev;    // Pointer to the previous card node
};

We originally had just a pointer next for a single linked list but now we have added a pointer for prev.

Creating a Doubly-linked List

To create our doubly-linked list, we'll undergo a similar process to how we handled our singly-linked list in ttb1. First, we create the first node in our list, then we append onto it.

Creating the first node of the doubly-linked list is almost identical to our previous projects. The only change is that we need to set current→prev (our pointer to the currently selected node on the list) to NULL. Since this is the first node in our list, current→prev will always be equal to NULL.

Setting Up Buttons

To use a keyboard and take input you first need to call the following inside your main function but outside your game loop:

select_gamepad(0);

If you want to set up two buttons, one for moving forward in a deck and one for moving backwards then use the following line:

// Buttons to go back and forth between deck
bool nextcard = (gamepad_button_a() == 1);
bool prevcard = (gamepad_button_b() == 1);

To use button “a” press X and to use button “b” press Z.

By adding these two lines you now have set up your buttons but now need to add some if statements for when they are pressed.

Here is an example of an if statement using button “nextcard”:

if (nextcard)
{
    // Next card in the deck is displayed
}

If button “a” is clicked then the next card in the deck is displayed.

Randomizing Your Deck

To randomize your deck you can use arrays.

int array[a]
 
for (int i = 0; i < a; i++) {
     array[i] = i;
     }
 
for (int i = 0; i < a; i++) {
     int temp = array[i];
     int randomIndex = rand() % 13;
     array[i] = array[randomIndex];
     array[randomIndex] = temp;
}
notes/discrete/fall2023/projects/cgf0.1696468005.txt.gz · Last modified: 2023/10/05 01:06 by walley