Let's say we are the owners of the Stratosphere casino at Las Vegas.
We want to know the status of all blackjack players located at stratosphere.
Stratosphere can have at max 6 tables of blackjack.
Each blackjack table can have at max 4 players simultaneously and each player can have at his hands at max 8 cards.
One could say that the each Stratosphere Blackjack player should have three integer values in order to determine his status.
unsigned int table_number; //The table number that he plays
unsigned int player_position; //His position at the table 0,1,2,3 (first,second,third or forth player)
unsigned int cards_number; //His amount of cards
Thinking even better the unsigned integers (two bytes) can become a byte (unsigned char) since a value 256 is way too high for table, position and card number.
So we could have defined our own player status struct in a BlackjackPlayerStatus.h header file like this :
struct BlackjackPlayerStatus { unsigned char table_number; //byte is just an 'unsigned char' unsigned char player_position; //byte is just an 'unsigned char' unsigned char cards_number; //byte is just an 'unsigned char' }; Then we could have done :
BlackjackPlayerStatus JohnBlackjackStatus = {2,3,1}; //So John is located at the 2nd table 3rd position and has 1 card In order to get these values we can do something like this : unsigned char table = JohnBlackjackStatus.table_number; unsigned char position = JohnBlackjackStatus.player_position; unsigned char cards = JohnBlackjackStatus.cards_number; Let's see with more details this example at arduino. Here is the BlackjackPlayerStatus.h header file :
#ifndef BlackjackPlayerStatus_h #define BlackjackPlayerStatus_h struct BlackjackPlayerStatus { unsigned char table_number; //byte is just an 'unsigned char' unsigned char player_position; //byte is just an 'unsigned char' unsigned char cards_number; //byte is just an 'unsigned char' }; #endif
Here is the StructExample.pde file which use the BlackjackPlayerStatus.h
#include "BlackjackPlayerStatus.h" void PrintStatus(BlackjackPlayerStatus &PlayerStatus){ Serial.print("Table : "); Serial.print(PlayerStatus.table_number,DEC); Serial.print(" ,Position : "); Serial.print(PlayerStatus.player_position,DEC); Serial.print(" ,Cards : "); Serial.println(PlayerStatus.cards_number,DEC); } void setup() // the set up part (runs only once) { Serial.begin(9600); // set up Serial library at 9600 bps //Assign status at John Mary and Bob BlackjackPlayerStatus JohnBlackjackStatus = {2,3,1}; //John Sits at table 2 at position 3with one card in hand BlackjackPlayerStatus MaryBlackjackStatus = {1,2,3}; //Mary sits at table 1 at position 2 with three cards in hand BlackjackPlayerStatus BobBlackjackStatus = {5,4,3}; //Bob sits at table 5 at position 4 with three cards in hand //Print status Serial.println("John's status : "); PrintStatus(JohnBlackjackStatus); Serial.println("Mary's status : "); PrintStatus(MaryBlackjackStatus); Serial.println("Bob's status : "); PrintStatus(BobBlackjackStatus); Serial.println("It is Bob's turn. Bob thinks ..."); delay(3000); Serial.println("Bob decides to hit (take a card)"); BobBlackjackStatus.cards_number = BobBlackjackStatus.cards_number + 1; Serial.println("Bob's new status : "); PrintStatus(BobBlackjackStatus); } void loop() // The main loop (runs over and over again) { // Do nothing... }
Here you can download the source code for this demo : StructExample
As you can see everything looks great but how about a little tweak using the bitwise operators instead of the struct header file?