Black Jack game using speaker sound, sd images, and joystick
Dependencies: 4DGL-uLCD-SE mbed
Revision 1:6874d500e0e8, committed 2016-03-14
- Comitter:
- najeekitchens
- Date:
- Mon Mar 14 18:24:16 2016 +0000
- Parent:
- 0:5b2b64f676b6
- Commit message:
- Publish
Changed in this revision
diff -r 5b2b64f676b6 -r 6874d500e0e8 PinDetect.h --- a/PinDetect.h Wed Mar 09 06:34:54 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,501 +0,0 @@ -/* - Copyright (c) 2010 Andy Kirkham - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. -*/ - -#ifndef AJK_PIN_DETECT_H -#define AJK_PIN_DETECT_H - -#ifndef MBED_H -#include "mbed.h" -#endif - -#ifndef PINDETECT_PIN_ASSTERED -#define PINDETECT_PIN_ASSTERED 1 -#endif - -#ifndef PINDETECT_SAMPLE_PERIOD -#define PINDETECT_SAMPLE_PERIOD 20000 -#endif - -#ifndef PINDETECT_ASSERT_COUNT -#define PINDETECT_ASSERT_COUNT 1 -#endif - -#ifndef PINDETECT_HOLD_COUNT -#define PINDETECT_HOLD_COUNT 50 -#endif - -namespace AjK { - -/** PinDetect adds mechanical switch debouncing to DigitialIn and interrupt callbacks. - * - * This is done by sampling the specified pin at regular intervals and detecting any - * change of state ( 0 -> 1 or 1 -> 0 ). When a state change is detected the attached - * callback handler is called. Additionally, if the pin stays in the same state after - * a state change for a defined period of time, an extra callback is made allowing a - * program to detect when a "key is pressed and held down" rather than a momentary - * key/switch press. - * - * All parameters are customisable which include:- - * <ul> - * <li> The sampling frequency. </li> - * <li> The number of continuous samples until a state change is detected. </li> - * <li> The number of continuous samples until a key is assumed held after a state change. </li> - * <li> The logic level which is assumed to be asserted (0volts or +volts). </li> - * </ul> - * - * Only callbacks that have been attached will be called by the library. - * - * Example: - * @code - * #include "mbed.h" - * #include "PinDetect.h" - * - * PinDetect pin( p30 ); - * DigitialOut led1( LED1 ); - * DigitialOut led2( LED2 ); - * DigitialOut led3( LED3 ); - * DigitialOut led4( LED4 ); - * - * void keyPressed( void ) { - * led2 = 1; - * led3 = 0; - * led4 = 0; - * } - * - * void keyReleased( void ) { - * led2 = 0; - * led3 = 0; - * led4 = 0; - * } - * - * void keyPressedHeld( void ) { - * led3 = 1; - * } - * - * void keyReleasedHeld( void ) { - * led4 = 1; - * } - * - * int main() { - * - * pin.mode( PullDown ); - * pin.attach_asserted( &keyPressed ); - * pin.attach_deasserted( &keyReleased ); - * pin.attach_asserted_held( &keyPressedHeld ); - * pin.attach_deasserted_held( &keyReleasedHeld ); - * - * // Sampling does not begin until you set a frequency. - * // The default is 20ms. If you want a different frequency - * // then pass the period in microseconds for example, for 10ms :- - * // pin.setSampleFrequency( 10000 ); - * // - * pin.setSampleFrequency(); // Defaults to 20ms. - * - * while( 1 ) { - * led1 = !led1; - * wait( 0.2 ); - * } - * } - * @endcode - * - * This example will flash led1 in a similar to a standard starting program. - * - * Applying a "1" (switch on) to pin 30 will switch on led2, removing the "1" to "0" - * (switch off) led2 goes out. Holding the "switch" at one for one second will switch - * on led3. An unasserted P30 (switched off) will, after one second illuminate led4 - * when the deasserted calledback is called. - * - * The above is a very basic introduction. For more details:- - * @see example.h - */ -class PinDetect { - -protected: - DigitalIn *_in; - Ticker *_ticker; - int _prevState; - int _currentStateCounter; - int _sampleTime; - int _assertValue; - int _samplesTillAssertReload; - int _samplesTillAssert; - int _samplesTillHeldReload; - int _samplesTillHeld; - FunctionPointer _callbackAsserted; - FunctionPointer _callbackDeasserted; - FunctionPointer _callbackAssertedHeld; - FunctionPointer _callbackDeassertedHeld; - - /** initialise class - * - * @param PinName p is a valid pin that supports DigitalIn - * @param PinMode m The mode the DigitalIn should use. - */ - void init(PinName p, PinMode m) { - _sampleTime = PINDETECT_SAMPLE_PERIOD; - _samplesTillAssert = PINDETECT_ASSERT_COUNT; - _samplesTillHeld = 0; - _samplesTillAssertReload = PINDETECT_ASSERT_COUNT; - _samplesTillHeldReload = PINDETECT_HOLD_COUNT; - _assertValue = PINDETECT_PIN_ASSTERED; - - _in = new DigitalIn( p ); - _in->mode( m ); - _prevState = _in->read(); - _ticker = new Ticker; - } - -public: - - friend class Ticker; - - PinDetect() { error("You must supply a PinName"); } - - /** PinDetect constructor - * - * By default the PinMode is set to PullDown. - * - * @see http://mbed.org/handbook/DigitalIn - * @param p PinName is a valid pin that supports DigitalIn - */ - PinDetect(PinName p) { - init( p, PullDown ); - } - - /** PinDetect constructor - * - * @see http://mbed.org/handbook/DigitalIn - * @param PinName p is a valid pin that supports DigitalIn - * @param PinMode m The mode the DigitalIn should use. - */ - PinDetect(PinName p, PinMode m) { - init( p, m ); - } - - /** PinDetect destructor - */ - ~PinDetect() { - if ( _ticker ) delete( _ticker ); - if ( _in ) delete( _in ); - } - - /** Set the sampling time in microseconds. - * - * @param int The time between pin samples in microseconds. - */ - void setSampleFrequency(int i = PINDETECT_SAMPLE_PERIOD) { - _sampleTime = i; - _prevState = _in->read(); - _ticker->attach_us( this, &PinDetect::isr, _sampleTime ); - } - - /** Set the value used as assert. - * - * Defaults to 1 (ie if pin == 1 then pin asserted). - * - * @param int New assert value (1 or 0) - */ - void setAssertValue (int i = PINDETECT_PIN_ASSTERED) { _assertValue = i & 1; } - - /** Set the number of continuous samples until assert assumed. - * - * Defaults to 1 (1 * sample frequency). - * - * @param int The number of continuous samples until assert assumed. - */ - void setSamplesTillAssert(int i) { _samplesTillAssertReload = i; } - - /** Set the number of continuous samples until held assumed. - * - * Defaults to 50 * sample frequency. - * - * @param int The number of continuous samples until held assumed. - */ - void setSamplesTillHeld(int i) { _samplesTillHeldReload = i; } - - /** Set the pin mode. - * - * @see http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode - * @param PinMode m The mode to pass on to the DigitalIn - */ - void mode(PinMode m) { _in->mode( m ); } - - /** Attach a callback function - * - * @code - * - * DigitalOut led1( LED1 ); - * PinDetect pin( p30 ); - * - * void myCallback( void ) { - * led1 = 1; - * }; - * - * main() { - * pin.attach_asserted( &myCallback ); - * } - * - * @endcode - * - * Call this function when a pin is asserted. - * @param function A C function pointer - */ - void attach_asserted(void (*function)(void)) { - _callbackAsserted.attach( function ); - } - - /** Attach a callback object/method - * - * @code - * - * class Bar { - * public: - * void myCallback( void ) { led1 = 1; } - * }; - * - * DigitalOut led1( LED1 ); - * PinDetect pin( p30 ); - * Bar bar; - * - * main() { - * pin.attach_asserted( &bar, &Bar::myCallback ); - * } - * - * @endcode - * - * Call this function when a pin is asserted. - * @param object An object that conatins the callback method. - * @param method The method within the object to call. - */ - template<typename T> - void attach_asserted(T *object, void (T::*member)(void)) { - _callbackAsserted.attach( object, member ); - } - - /** Attach a callback function - * - * @code - * - * DigitalOut led1( LED1 ); - * PinDetect pin( p30 ); - * - * void myCallback( void ) { - * led1 = 0; - * }; - * - * main() { - * pin.attach_deasserted( &myCallback ); - * } - * - * @endcode - * - * Call this function when a pin is deasserted. - * @param function A C function pointer - */ - void attach_deasserted(void (*function)(void)) { - _callbackDeasserted.attach( function ); - } - - /** Attach a callback object/method - * - * @code - * - * class Bar { - * public: - * void myCallback( void ) { led1 = 0; } - * }; - * - * DigitalOut led1( LED1 ); - * PinDetect pin( p30 ); - * Bar bar; - * - * main() { - * pin.attach_deasserted( &bar, &Bar::myCallback ); - * } - * - * @endcode - * - * Call this function when a pin is deasserted. - * @param object An object that conatins the callback method. - * @param method The method within the object to call. - */ - template<typename T> - void attach_deasserted(T *object, void (T::*member)(void)) { - _callbackDeasserted.attach( object, member ); - } - - /** Attach a callback function - * - * @code - * - * DigitalOut led2( LED2 ); - * PinDetect pin( p30 ); - * - * void myCallback( void ) { - * led2 = 1; - * }; - * - * main() { - * pin.attach_asserted_held( &myCallback ); - * } - * - * @endcode - * - * Call this function when a pin is asserted and held. - * @param function A C function pointer - */ - void attach_asserted_held(void (*function)(void)) { - _callbackAssertedHeld.attach( function ); - } - - /** Attach a callback object/method - * - * @code - * - * class Bar { - * public: - * void myCallback( void ) { led2 = 0; } - * }; - * - * DigitalOut led2( LED2 ); - * PinDetect pin( p30 ); - * Bar bar; - * - * main() { - * pin.attach_asserted_held( &bar, &Bar::myCallback ); - * } - * - * @endcode - * - * Call this function when a pin is asserted and held. - * @param object An object that conatins the callback method. - * @param method The method within the object to call. - */ - template<typename T> - void attach_asserted_held(T *object, void (T::*member)(void)) { - _callbackAssertedHeld.attach( object, member ); - } - - /** Attach a callback function - * - * @code - * - * DigitalOut led3( LED3 ); - * PinDetect pin( p30 ); - * - * void myCallback( void ) { - * led3 = 1; - * }; - * - * main() { - * pin.attach_deasserted_held( &myCallback ); - * } - * - * @endcode - * - * Call this function when a pin is deasserted and held. - * @param function A C function pointer - */ - void attach_deasserted_held(void (*function)(void)) { - _callbackDeassertedHeld.attach( function ); - } - - /** Attach a callback object/method - * - * @code - * - * class Bar { - * public: - * void myCallback( void ) { led3 = 0; } - * }; - * - * DigitalOut led3( LED3 ); - * PinDetect pin( p30 ); - * Bar bar; - * - * main() { - * pin.attach_deasserted_held( &bar, &Bar::myCallback ); - * } - * - * @endcode - * - * Call this function when a pin is deasserted and held. - * @param object An object that conatins the callback method. - * @param method The method within the object to call. - */ - template<typename T> - void attach_deasserted_held(T *object, void (T::*member)(void)) { - _callbackDeassertedHeld.attach( object, member ); - } - - /** operator int() - * - * Read the value of the pin being sampled. - */ - operator int() { return _in->read(); } - -protected: - /** The Ticker periodic callback function - */ - void isr(void) { - int currentState = _in->read(); - - if ( currentState != _prevState ) { - if ( _samplesTillAssert == 0 ) { - _prevState = currentState; - _samplesTillHeld = _samplesTillHeldReload; - if ( currentState == _assertValue ) - _callbackAsserted.call(); - else - _callbackDeasserted.call(); - } - else { - _samplesTillAssert--; - } - } - else { - _samplesTillAssert = _samplesTillAssertReload; - } - - if ( _samplesTillHeld ) { - if ( _prevState == currentState ) { - _samplesTillHeld--; - if ( _samplesTillHeld == 0 ) { - if ( currentState == _assertValue ) - _callbackAssertedHeld.call(); - else - _callbackDeassertedHeld.call(); - } - } - else { - _samplesTillHeld = 0; - } - } - } - -}; - -}; // namespace AjK ends. - -using namespace AjK; - -#endif
diff -r 5b2b64f676b6 -r 6874d500e0e8 SDFileSystem.lib --- a/SDFileSystem.lib Wed Mar 09 06:34:54 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
diff -r 5b2b64f676b6 -r 6874d500e0e8 main.cpp --- a/main.cpp Wed Mar 09 06:34:54 2016 +0000 +++ b/main.cpp Mon Mar 14 18:24:16 2016 +0000 @@ -1,12 +1,14 @@ #include "mbed.h" #include "Speaker.h" -#include "wave_player.h" -#include "SDFileSystem.h" #include "uLCD_4DGL.h" -//SDFileSystem sd(p5, p6, p7, p8, "sd"); -AnalogIn keep_playing(p15); + +//Mbed I/O DigitalIn player_hit(p10); // up direction, determine if the player wants another card from the navigation switch DigitalIn player_stay(p11); // down direction, determine if the player wants to stay with the current cards he has. +PwmOut speaker(p21); //play short sound when card placed on table +uLCD_4DGL uLCD(p28, p27, p30); //uLCD connection + +//global variables int money = 5; // picked 5 dollars to start with. each bet/round is only 1 dollar automatically taken out as long as player decides to keep playing. int dealer_hand[2]; // array of two to hold the dealers cards int player_hand[2]; // array of two to hold the players cards @@ -15,185 +17,295 @@ int player_card; // random card value to be assigned to player int player_total; // deterines the total value of the players hand int dealer_total; // determines the total value of the dealers hand -bool y = true; -int x1; -int x2; -int hi, lo; -int num,num2; -int card[13]= {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11}; +bool y = true; //determines when to be in a while loop in functions below +int hi, lo; //determines card picture address on sd card in uLCD device +int k,j; //determines the card position of player and dealer +int flip1, flip2; //store hi and lo value of dealer's second card to flip later +int num,num2; //determines the card face for player and dealer +int ace1=0; //keeps track of player's aces +int ace2=0; //keeps track of dealer's aces +int card[13]= {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11}; //used to choose a random value -//Speaker mySpeaker(p21); - -uLCD_4DGL uLCD(p28, p27, p30); - -void addr(int, int); -int place_cards(int); +void addr(int, int); //glocalize the function to find the card address for other function use +int place_cards(int); //globalize the funtion to determine the card position for other function use -void deal_hand(int num, int num2) +//deals out 2 cards faced up to player and one card faced up and one card faced down to the dealer +void deal_hand(void) { -int j,k; -int suit; + ace1=0; + ace2=0; + int m; + int mat[4]= {10,11,12,13}; //case values of cards with value 10 (10, J, Q, K) + int suit; //card suit srand (time(NULL)); - // First while loop will control the game. Continue to play until player runs out of money or does not wish to play anymore - //while (money > 0 || keep_playing ==1) { - // Second while loop controls the cards the dealer and player is dealt. Totally random with every reset of the mbed. + while (x<=1) { - player_card= rand() % (12 + 1 - 0) + 0; - player_card=card[player_card]; - - suit= rand() % 4 + 1; - addr(player_card, suit); - uLCD.set_sector_address(hi, lo); - j=place_cards(num); - uLCD.display_image(j,80); + player_card= rand() % (12 + 1 - 0) + 0; //random index value 0-12 + player_card=card[player_card]; //use random index to find value in array + if(player_card==10) { + m= rand() % (3 + 1 - 0) + 0; //use random card with value 10 in case statement + m=mat[m]; + } else if (player_card==11) { //ace is 1 in case statement + m=1; + ace1++; + } else { + m=player_card; //other numbers match case statement + } + suit= rand() % 4 + 1; //random suit + addr(m, suit); //find card address + uLCD.media_init(); + uLCD.set_sector_address(hi, lo); //use card address from sd + j=place_cards(num); //determine next card placement + uLCD.display_image(4,j); //display card + + speaker.period(1.0/500.0); // 500hz period + speaker =0.5; //50% duty cycle - max volume + wait(.1); + speaker=0.0; // turn off audio + wait(2); + num++; - - dealer_card= rand() % (12 + 1 - 0) +0; - dealer_card=card[dealer_card]; - - suit= rand() % 4 + 1; - addr(dealer_card, suit); - uLCD.set_sector_address(hi, lo); - k=place_cards(num2); - uLCD.display_image(k,40); + + dealer_card= rand() % (12 + 1 - 0) +0; //random index value 0-12 + dealer_card=card[dealer_card]; //use random index value to find value + + if(dealer_card==10) { + m= rand() % (3 + 1 - 0) + 0; //use random card with value 10 in case statement + m=mat[m]; + } else if (dealer_card==11) { //ace is 1 in case statement + m=1; + ace2++; + } else { + m=dealer_card; //other numbers match case statement + } + + suit= rand() % 4 + 1; //use a random suit + addr(m, suit); //find address + k=place_cards(num2); //determine next card placement + + //store the dealer's second card image address + if (num2==1) { + flip1=hi; + flip2=lo; + hi=0x0040; + lo=0x0001; + } + + uLCD.media_init(); + uLCD.set_sector_address(hi, lo); //use card address from sd + uLCD.display_image(70,k); //diplay card + + //play beep + speaker.period(1.0/500.0); // 500hz period + speaker =0.5; //50% duty cycle - max volume + wait(.1); + speaker=0.0; // turn off audio + wait(2); + num2++; - - //printf ("%i\n ",r); - dealer_hand[x]= dealer_card; - player_hand[x]= player_card; - uLCD.printf("dealerc %d\n\r ",dealer_hand[x]); - uLCD.printf("playerc %d\n\r ",player_hand[x]); - x= x+1; + + dealer_hand[x]= dealer_card; //put dealer card value in array + player_hand[x]= player_card; //put player card value in array + + x= x+1; //next array index slot } player_total= player_hand[0] + player_hand[1]; // determines the players total from his two original cards - uLCD.locate(80,100); - uLCD.printf ("playert %d\n\r ",player_total); dealer_total= dealer_hand[0] + dealer_hand[1]; // determines the dealers total from his two original cards - uLCD.locate(80,7); - uLCD.printf ("dealert %d\n\r ",dealer_total); - // display player wins - //return back to the beginning for new game - x= 0; + x= 0; //reset index to 0 } - -void player_decision(int num) +//player decides to hit or stay +void player_decision(void) { int suit; - int j; - printf ("Do you want to hit or stay\n\r"); + uLCD.locate(0,14); + uLCD.printf ("Hit (UP) or stay(DOWN)?"); //print user y= true; + int mat[4]= {10,11,12,13}; //case values of cards with value 10 (10, J, Q, K) + int m; while (y) { - if (player_hit ==0) { + if (player_hit ==0) { //player hits srand (time(NULL)); - player_card= rand() % (12 + 1 - 0) + 0; - player_card=card[player_card]; + player_card= rand() % (12 + 1 - 0) + 0; //random index 0-12 + player_card=card[player_card]; //use index to find value // Can display the extra card they recieve - - suit= rand() % 4 + 1; - addr(player_card, suit); - uLCD.set_sector_address(hi, lo); - j=place_cards(num); - uLCD.display_image(j,80); - num++; - - printf ("playerhit %d\n\r ", player_card); - player_total= player_total + player_card; - printf ("playertotal %d\n\r ", player_total); + + if(player_card==10) { + m= rand() % (3 + 1 - 0) + 0; //use random card with value 10 in case statement + m=mat[m]; + } else if (player_card==11) { //ace is 1 in case statement + m=1; + ace1++; + } else { + m=player_card; //other card values match case statement values + } + + suit= rand() % 4 + 1; //random suit + addr(m, suit); //find address of card image + uLCD.media_init(); // + uLCD.set_sector_address(hi, lo); //use address + j=place_cards(num); //find where to place card + uLCD.display_image(4,j); //display card + + //play beep + speaker.period(1.0/500.0); // 500hz period + speaker =0.5; //50% duty cycle - max volume + wait(.1); + speaker=0.0; // turn off audio + wait(1); + num++; + + player_total= player_total + player_card; //find total wait(.5); - if (player_total >21 && player_card == 11) { + + if (player_total >21 && ace1>=1) { //change ace to 1 player_total= player_total-10; - uLCD.locate(80,100); - uLCD.printf ("playertotal %d\n\r ", player_total); - player_decision(num); + ace1--; + player_decision(); //call this function } else if (player_total>21) { // print to the screen that the player bust - uLCD.locate(80,100); - uLCD.printf ("BUST!\n\r "); + uLCD.locate(1,7); + uLCD.printf ("You BUSTED!\n\r "); + wait(2); + uLCD.cls(); y= false; - } else if (player_total<21) { - //printf ("Do you want to hit or stay"); - player_decision(num); + } else if (player_total<21) { //continue prompt + uLCD.locate(0,14); + uLCD.printf ("Hit (UP) or stay(DOWN)?"); + player_decision(); } + } else if (player_stay ==0) { - y= false; + //get rid of prompt question + uLCD.textbackground_color(0x006600); + uLCD.color(0x006600); + uLCD.locate(0,14); + uLCD.printf ("Hit (UP) or stay(DOWN)?"); + uLCD.textbackground_color(BLACK); + uLCD.color(RED); + y= false; //get out of while loop + wait(.5); } } } -void dealer_decision(int num2) +void dealer_decision(void) { int suit; - int k; - while (dealer_total <17) { + int m; + int mat[4]= {10,11,12,13}; //case values of cards with value 10 (10, J, Q, K) + while (dealer_total <17) { //dealer will hit if it's total is under 17 srand (time(NULL)); - dealer_card= rand() % (12 + 1 - 0) + 0; - dealer_card=card[dealer_card]; - - suit= rand() % 4 + 1; - addr(player_card, suit); - uLCD.set_sector_address(hi, lo); - k=place_cards(num2); - uLCD.display_image(k,40); - num++; - - // Can display the extra card they recieve - printf ("dealerhit %d\n\r ", dealer_card); - dealer_total= dealer_total + dealer_card; - uLCD.locate(80, 7); - uLCD.printf ("dealertotal %d\n\r ", dealer_total); + dealer_card= rand() % (12 + 1 - 0) + 0; //find random index 0-12 + dealer_card=card[dealer_card]; //use index to find value in array + if(dealer_card==10) { //cards with 10 value in switch are 10, 11, 12, 13 + m= rand() % (3 + 1 - 0) + 0; + m=mat[m]; + } else if (dealer_card==11) { //ace value in switch is 1 + m=1; + ace2++; + } else { + m=dealer_card; //other card values match switch case value + } + suit= rand() % 4 + 1; + addr(m, suit); //find card address + uLCD.media_init(); + uLCD.set_sector_address(hi, lo); //use card address found + k=place_cards(num2); //find card loaction + uLCD.display_image(70,k); //display card + + //play beep + speaker.period(1.0/500.0); // 500hz period + speaker =0.5; //50% duty cycle - max volume + wait(.1); + speaker=0.0; // turn off audio + wait(.5); + num2++; //next card + + dealer_total= dealer_total + dealer_card; //total + wait(.5); - if (dealer_total> 21 && dealer_card== 11) { + if (dealer_total> 21 && ace2>=1) { //change ace to 1 dealer_total= dealer_total-10; - uLCD.locate(80,7); - uLCD.printf ("dealertotal %d\n\r ", dealer_total); - dealer_decision(num2); + ace2--; + dealer_decision(); } else if (dealer_total>21) { - // print to the screen that the player bust - uLCD.locate(80,7); - uLCD.printf ("Dealer BUSTED!\n\r "); - y= false; + // print to the screen that the dealer bust + uLCD.locate(4, 7); + uLCD.printf ("Dealer BUSTED!"); + wait(3); + uLCD.cls(); + y= false; //leave while loop } + wait(2); + } + //if dealer doesn't bust and has higher value than player + if(dealer_total>player_total && dealer_total<=21) { + uLCD.locate(2,7); + uLCD.printf("Dealer Wins!"); + wait(3); + uLCD.cls(); + } + //if player and dealer totals are equal it's a tie + else if(dealer_total==player_total && dealer_total<=21) { + uLCD.locate(5,5); + uLCD.printf("It's a Tie!"); + wait(3); + uLCD.cls(); + } + //if player's total is greater than dealers + else if(dealer_total<player_total && dealer_total<=21) { + uLCD.locate(2,7); + uLCD.printf("You Win!"); + wait(3); + uLCD.cls(); } } -//AnalogOut DACout(p18); -//int suit; +//find address of card using switch void addr(int num, int suit) { switch(num) { case 1: //Ace - if(suit==1) { + if(suit==1) { //clubs hi=0x003F; lo=0xFFC1; - } else if(suit==2) { - //addr - } else if(suit==3) { - //addr - } else if(suit==4) { - //addr + // break; + } else if(suit==2) { //diamond + hi=0x003F; + lo=0xFFD1; + // break; + } else if(suit==3) { //hearts + hi=0x003F; + lo=0xFFE1; + } else if(suit==4) { //spades + hi=0x003F; + lo=0xFFF1; } break; case 2: if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x02CB; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x02DB; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x02EB; } else if(suit==4) { - //addr + hi=0x0040; + lo=0x02FB; } break; @@ -201,213 +313,271 @@ case 3: if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x028B; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x029B; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x02AB; } else if(suit==4) { - //addr + hi=0x0040; + lo=0x02BB; } break; case 4: if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x0090; + // break; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x009F; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x00AF; } else if(suit==4) { - //addr + hi=0x0040; + lo=0x00BF; } break; case 5: if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x0050; + //break; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x0060; + // break; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x0070; + // break; } else if(suit==4) { - //addr + hi=0x0040; + lo=0x0080; } break; case 6: if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x020D; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x021D; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x022D; } else if(suit==4) { - //addr + hi=0x0040; + lo=0x023D; } break; case 7: if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x01CE; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x01DD; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x01ED; } else if(suit==4) { - //addr + hi=0x0040; + lo=0x01FD; } break; case 8: if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x0010; + // break; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x0020; + // break; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x0030; + // break; } else if(suit==4) { - //addr + hi=0x0040; + lo=0x0040; } break; case 9: if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x014F; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x015F; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x016F; } else if(suit==4) { - //addr + hi=0x0040; + lo=0x017F; } break; case 10: if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x024C; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x025B; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x026B; + } else if(suit==4) { - //addr + hi=0x0040; + lo=0x027B; } break; case 11: //Jack if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x00CF; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x00DF; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x00EF; } else if(suit==4) { - //addr + hi=0x0040; + lo=0x00FF; } break; case 12: //Queen if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x018F; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x019E; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x01AE; } else if(suit==4) { - //addr + hi=0x0040; + lo=0x01BE; } break; case 13: //King if(suit==1) { - // x=0x003F; - // y=0xFFC1; + hi=0x0040; + lo=0x010F; } else if(suit==2) { - //addr + hi=0x0040; + lo=0x011F; } else if(suit==3) { - //addr + hi=0x0040; + lo=0x012F; } else if(suit==4) { - //addr + hi=0x0040; + lo=0x013F; } break; } - // return x, y; + } -//wave_player waver(&DACout); +//displace cards down screen int place_cards(int num) { - int j=4*num+4; + int j=14*num+9; return(j); } int main() { - - uLCD.cls(); - uLCD.media_init(); - uLCD.color(RED); - uLCD.background_color(GREEN); + uLCD.background_color(0x006600); while (money > 0) { - int num=0; - int num2=0; + uLCD.cls(); + uLCD.color(RED); + uLCD.set_font(FONT_7X8); + uLCD.locate(1,0); + uLCD.printf("Player"); //print Player + uLCD.locate(11,0); + uLCD.printf("Dealer"); //print Dealer + num=0; + num2=0; srand (time(NULL)); - deal_hand(num,num2); - player_decision(num); - dealer_decision(num2); + //start with 2 cards for player and dealer + deal_hand(); + //prompt player + player_decision(); + //go to dealer and flip 2nd card over + if(player_total<=21) { + uLCD.media_init(); + uLCD.set_sector_address(flip1, flip2); //use saved address + uLCD.display_image(70,k); //display card + speaker.period(1.0/500.0); // 500hz period + speaker =0.5; //50% duty cycle - max volume + wait(.1); + speaker=0.0; // turn off audio + wait(2); + //if dealer is under 17 in total the dealer will hit + dealer_decision(); + } + //1 money point awarded for a win + //1 money point taken away for a loss if (player_total== dealer_total) { money=money; uLCD.locate(2, 2); - uLCD.printf ("money %d\n\r ", money); + uLCD.printf ("money= %d", money); } else if (player_total > 21) { money= money -1; uLCD.locate(2, 2); - uLCD.printf ("money %d\n\r ", money); + uLCD.printf ("money= %d", money); } else if (dealer_total >21) { money= money+1; uLCD.locate(2, 2); - uLCD.printf ("money %d\n\r ", money); + uLCD.printf ("money= %d", money); } else if ((player_total > dealer_total)) { money = money+1; uLCD.locate(2, 2); - uLCD.printf ("money %d\n\r ", money); + uLCD.printf ("money= %d", money); // Display that its a tie //return back to the beginning for new game } else if ((player_total < dealer_total)) { money= money-1; uLCD.locate(2, 2); - uLCD.printf ("money %d\n\r ", money); + uLCD.printf ("money= %d", money); // display dealer wins //return back to the beginning for new game } - wait(1); - } - - wait(1); -//FILE *mp4_file=fopen("/sd/mdir/clip.mp4-SD","r"); + wait(3); - //uLCD.display_video(0,0); + } +//Game Over + wait(1); + uLCD.cls(); + uLCD.locate(4,5); + uLCD.printf("Game Over!"); + wait(4); - // uLCD.printf(mp -//waver.play(wave_file); - -//fclose(mp4_file); }
diff -r 5b2b64f676b6 -r 6874d500e0e8 wave_player.lib --- a/wave_player.lib Wed Mar 09 06:34:54 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://developer.mbed.org/users/sravet/code/wave_player/#acc3e18e77ad