Made by George and Pahul

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Files at this revision

API Documentation at this revision

Comitter:
gevell1
Date:
Thu Mar 12 18:59:00 2015 +0000
Commit message:
Blackjack V1.0

Changed in this revision

4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
Hand.h Show annotated file Show diff for this revision Revisions of this file
PinDetect.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
mpr121.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 8798a72d6580 4DGL-uLCD-SE.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/4DGL-uLCD-SE.lib	Thu Mar 12 18:59:00 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/gevell1/code/4DGL-uLCD-SE/#1660906507aa
diff -r 000000000000 -r 8798a72d6580 Hand.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Hand.h	Thu Mar 12 18:59:00 2015 +0000
@@ -0,0 +1,146 @@
+using namespace std;
+class Hand
+{
+private:
+    std::vector <std::string> card_types;   // the card names in the hand 
+    std::vector <int> card_values;   //  the card values in the hand
+    std::string hand_holder;  // either 'Player' or 'Dealer'
+    int aceCount;  //  the number of aces, useful for calculating hand value
+
+public:
+    explicit Hand(std::string);  // constructor, sets hand_holder and calls addCard twice
+    void addCard();  //  adds a randomly chosen card to the hand
+    void printHand();  //  prints the card_types vector
+    void printFirstCard();  // prints only the first card in the hand (used for dealer)
+    int handValue();  // calculates the hand's value
+    int getCardValues(int i);
+};
+
+Hand::Hand(string player)
+{
+    hand_holder = player;
+    aceCount = 0;
+    card_types.clear();
+    card_values.clear();
+    for(int i = 0; i<2; i++)
+    {
+        addCard();
+    }
+    
+}
+void Hand::addCard()
+{
+    //card selection was changed
+    int cardSelection = 1 + rand() % 13;
+    int cardValue = 0;
+    string cardType;
+
+    switch (cardSelection)
+    {
+    case 1:
+        cardType = "Ace";
+        cardValue = 11;
+        aceCount = aceCount + 1;
+        break;
+
+    case 2:
+        cardType = "King";
+        cardValue = 10;
+        break;
+
+    case 3:
+        cardType = "Queen";
+        cardValue = 10;
+        break;
+
+    case 4:
+        cardType = "Jack";
+        cardValue = 10;
+        break;
+
+    case 5:
+        cardType = "Ten";
+        cardValue = 10;
+        break;
+
+    case 6:
+        cardType = "Two";
+        cardValue = 2;
+        break;
+
+    case 7:
+        cardType = "Three";
+        cardValue = 3;
+        break;
+
+    case 8:
+        cardType = "Four";
+        cardValue = 4;
+        break;
+
+    case 9:
+        cardType = "Five";
+        cardValue = 5;
+        break;
+
+    case 10:
+        cardType = "Six";
+        cardValue = 6;
+        break;
+
+    case 11:
+        cardType = "Seven";
+        cardValue = 7;
+        break;
+
+    case 12:
+        cardType = "Eight";
+        cardValue = 8;
+        break;
+
+    case 13:
+        cardType = "Nine";
+        cardValue = 9;
+        break;
+    }
+    card_values.push_back(cardValue);
+    card_types.push_back(cardType);
+}
+
+int Hand::handValue()
+{
+    int sumOfCards = 0;
+
+    for (int i = 0; i < card_values.size(); i++)
+    {
+        sumOfCards = card_values[i] + sumOfCards;
+    }
+
+    int numAces = aceCount;
+
+    while ((sumOfCards > 21) && (numAces > 0))
+    {
+        sumOfCards = sumOfCards - 10;
+        numAces--;
+    }
+
+    return sumOfCards;
+}
+
+void Hand::printHand()
+{
+    for (int i = 0; i < card_values.size(); i++)
+    {
+        cout << card_values[i] << " ";
+    }
+}
+
+int Hand::getCardValues(int i)
+{
+     return card_values[i];   
+}
+
+void Hand::printFirstCard()
+{
+    cout << card_values[0];
+}
\ No newline at end of file
diff -r 000000000000 -r 8798a72d6580 PinDetect.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PinDetect.lib	Thu Mar 12 18:59:00 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
diff -r 000000000000 -r 8798a72d6580 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Mar 12 18:59:00 2015 +0000
@@ -0,0 +1,376 @@
+#include <vector>
+#include <string>
+#include <cstdlib>
+#include <iostream>
+#include <iomanip>
+#include "mbed.h"
+#include "uLCD_4DGL.h"
+#include "Hand.h"
+#include "PinDetect.h"
+using namespace std;
+//mbed declarations
+Serial pc(USBTX, USBRX);
+uLCD_4DGL lcd(p28, p27, p30);
+DigitalIn pb1(p12);
+DigitalIn pb2(p15);
+DigitalIn pb3(p16);
+DigitalIn pb4(p17);
+DigitalIn pb5(p19);
+DigitalIn pb6(p20);
+DigitalIn pb7(p21);
+DigitalIn pb8(p23);
+//draw player hand
+void drawPlayerHand()
+{
+    lcd.locate(lcd.max_col/4,0);
+    lcd.printf("Dealer: ");
+
+    lcd.locate(lcd.max_col/4,lcd.max_row/2);
+    lcd.printf("Player: ");
+
+    lcd.filled_rectangle(0,76,20,110,BLUE);
+    lcd.filled_rectangle(23,76,43,110,BLUE);
+}
+//draw player hit cards
+void drawPlayerHit(int count)
+{
+    switch(count) {
+        case 1:
+            lcd.filled_rectangle(46,76,66,110,BLUE);
+            break;
+        case 2:
+            lcd.filled_rectangle(69,76,89,110,BLUE);
+            break;
+        case 3:
+            lcd.filled_rectangle(92,76,112,110,BLUE);
+            break;
+    }
+}
+//draw card for dealer hand
+void drawDealerHand()
+{
+    lcd.filled_rectangle(0,16,20,50,RED);
+    lcd.filled_rectangle(23,16,43,50,RED);
+}
+//draw card for dealer hit
+void drawDealerHit(int count2)
+{
+    switch(count2) {
+        case 1:
+            lcd.filled_rectangle(46,16,66,50,RED);
+            break;
+        case 2:
+            lcd.filled_rectangle(69,16,89,50,RED);
+            break;
+        case 3:
+            lcd.filled_rectangle(92,16,112,50,RED);
+            break;
+    }
+}
+//draw background
+void drawTable()
+{
+    lcd.background_color(POKERGREEN);
+    lcd.color(WHITE);
+    lcd.cls();
+}
+//draw text
+void drawText(int betVal)
+{
+    lcd.locate(lcd.max_col/4,lcd.max_row-1);
+    lcd.color(WHITE);
+    lcd.printf("Bet: %i",betVal);
+}
+//draw both character player cards
+void drawCharacterPlayer(int card1,int card2)
+{
+    lcd.locate(lcd.max_col/5 - 2,lcd.max_row/2 + 3);
+    lcd.printf("%i",card1);
+    lcd.locate((lcd.max_col*2)/5 - 3 ,lcd.max_row/2 + 3);
+    lcd.printf("%i",card2);
+}
+//draw dealer character card1
+void drawCharDealer(int car1)
+{
+    lcd.locate(lcd.max_col/5 - 2,lcd.max_row/3 - 1);
+    lcd.printf("%i",car1);
+}
+//draw dealer charcter card2
+void drawDealerCard2(int cardvalue)
+{
+    lcd.locate((lcd.max_col*2)/5 - 3,lcd.max_row/3 - 1);
+    lcd.printf("%i",cardvalue);
+}
+//draw hit cards characters for player
+void drawHitChar_player(int cardval, int hit)
+{
+    switch(hit) {
+        case 1:
+            lcd.locate((lcd.max_col*2)/5,lcd.max_row/2 + 3);
+            lcd.printf("%i",cardval);
+            break;
+        case 2:
+            lcd.locate((lcd.max_col*3)/5 + 1 ,lcd.max_row/2 + 3);
+            lcd.printf("%i",cardval);
+            break;
+        case 3:
+            lcd.locate((lcd.max_col*4)/5 + 1 ,lcd.max_row/2 + 3);
+            lcd.printf("%i",cardval);
+            break;
+    }
+}
+//draw Hit card characters for dealer
+void drawHitChar_dealer(int cardv, int hit2)
+{
+    switch(hit2) {
+        case 1:
+            lcd.locate((lcd.max_col*2)/5,lcd.max_row/3 - 1);
+            lcd.printf("%i",cardv);
+            break;
+        case 2:
+            lcd.locate((lcd.max_col*3)/5 + 1 ,lcd.max_row/3 - 1);
+            lcd.printf("%i",cardv);
+            break;
+        case 3:
+            lcd.locate((lcd.max_col*4)/5 + 1 ,lcd.max_row/3 - 1);
+            lcd.printf("%i",cardv);
+            break;
+    }
+}
+
+int main()
+{
+    //stepup and variables
+    lcd.baudrate(3000000);
+    lcd.cls();
+    pb1.mode(PullUp);
+    pb2.mode(PullUp);
+    pb3.mode(PullUp);
+    pb4.mode(PullUp);
+    pb5.mode(PullUp);
+    pb6.mode(PullUp);
+    pb7.mode(PullUp);
+    pb8.mode(PullUp);
+    bool overshot = false; // if Player busts
+    string action; //Whether player hits, or stands
+    int hitCount = 0; //number of hits by player
+    int dealerCount = 0; //number of hits by dealer
+    int bet = 0; //bet amount
+    int round = 1; 
+    int i=40;
+    int cardArray_player[7];
+    int cardArray_dealer[7];
+    unsigned int dollars = 500;
+    //begin game
+    while(1) {
+        drawTable();//draws background color
+        srand((i)); //change everytime you compile
+        i++;
+        while (1) {
+            wait_ms(1000);
+            lcd.cls();
+            hitCount = 0; 
+            dealerCount = 0; 
+            if(dollars == 0) {
+                pc.printf("You are broke! Go Home:)\n");
+                lcd.locate(lcd.max_col/4,lcd.max_row/2);
+                lcd.printf("YOU LOSE");
+                wait_ms(10000);
+                break;
+            }
+            Hand Player1("Player");
+            Hand Player2("Dealer");
+            cout << "Round " << round << endl;
+            cout << "Player1 has " << dollars << " Dollars" << endl;
+            cout << "Player's hand is: ";
+            Player1.printHand();
+            for(int a=0; a<2; a++) {
+                cardArray_player[a] = Player1.getCardValues(a);
+            }
+            cout << endl << "Player's hand value is: " << Player1.handValue()<< endl;
+
+            drawPlayerHand();
+            drawCharacterPlayer(cardArray_player[0],cardArray_player[1]);
+            cout << "Dealer's first card is: ";
+            Player2.printFirstCard();
+            cout << endl;
+
+            drawDealerHand();
+            for(int b=0; b<2; b++) {
+                cardArray_dealer[b] = Player2.getCardValues(b);
+            }
+            drawCharDealer(cardArray_dealer[0]);
+            cout << "Select a bet amount. If you want to fold, bet 0 "<< endl<<endl<<endl;
+
+            //Enter pushbutton for bet value
+            while(pb1 && pb2 && pb3 && pb4);
+            if(!pb1) {
+                bet = 0;
+            } else if(!pb2) {
+                bet = 25;
+            } else if(!pb3) {
+                bet = 100;
+            } else if(!pb4) {
+                bet = dollars;
+            }
+
+            if (bet == 0) {
+                pc.printf("Please leave the table or restart to play again");
+                break;
+            } else if (bet > dollars && dollars != 0) {
+                cout << "You don't have that much money!  Enter a lower bet.";
+                wait_ms(200);
+                continue;
+            }
+            drawText(bet);
+
+            while (1) {
+                pc.printf("Would you like to hit or stand?\n");
+                while(pb5 && pb6);
+                if(!pb5) {
+                    action = "hit";
+                } else if(!pb6) {
+                    action = "stand";
+                }
+                if (action == "hit") {
+                    Player1.addCard();
+                    hitCount++;
+                    cout << endl << "Player's hand value is: " << Player1.handValue()<<endl;
+                    Player1.printHand();
+                    cout <<endl;
+                    drawPlayerHit(hitCount);
+                    switch(hitCount) {
+                        case 1:
+                            cardArray_player[2] = Player1.getCardValues(2);
+                            drawHitChar_player(cardArray_player[2],hitCount);
+                            break;
+                        case 2:
+                            cardArray_player[3] = Player1.getCardValues(3);
+                            drawHitChar_player(cardArray_player[3],hitCount);
+                            break;
+                        case 3:
+                            cardArray_player[4] = Player1.getCardValues(4);
+                            drawHitChar_player(cardArray_player[4],hitCount);
+                            break;
+                    }
+                    cout << endl;
+
+                    if (Player1.handValue() > 21) {
+
+                        cout << "Player's hand is bust " << endl;
+                        dollars -= bet;
+                        round++;
+                        cout << "Player has " << dollars << " dollars" << endl;
+                        lcd.cls();
+                        lcd.locate(lcd.max_col/4,lcd.max_row/2);
+                        lcd.printf("Dealer Wins :()");
+                        overshot = true;
+                        wait_ms(1000);
+                        break;
+                    }
+                }
+
+                else if (action == "stand") {
+                    round++;
+                    overshot = false;
+                    break;
+                }
+                wait_ms(500);
+            }
+
+            if (!overshot) {
+                cout << "Dealer's hand is: ";
+                Player2.printHand();
+                drawDealerCard2(cardArray_dealer[1]);
+                cout << endl << "Dealer's hand value is " << Player2.handValue() << endl;
+
+                while (Player2.handValue() < 17) {
+                    dealerCount++;
+                    Player2.addCard();
+                    cout << "Dealer's hand is: ";
+                    Player2.printHand();
+                    drawDealerHit(dealerCount);
+                    switch(dealerCount) {
+                        case 1:
+                            cardArray_dealer[2] = Player2.getCardValues(2);
+                            drawHitChar_dealer(cardArray_dealer[2],dealerCount);
+                            break;
+                        case 2:
+                            cardArray_dealer[3] = Player2.getCardValues(3);
+                            drawHitChar_dealer(cardArray_dealer[3],dealerCount);
+                            break;
+                        case 3:
+                            cardArray_dealer[4] = Player2.getCardValues(4);
+                            drawHitChar_dealer(cardArray_dealer[4],dealerCount);
+                            break;
+                    }
+                    cout << endl << "Dealer's hand value is " << Player2.handValue() << endl;
+                    wait_ms(600);
+                }
+
+                if (Player2.handValue() > 21) {
+                    cout << "Dealer's hand is bust.  Player wins!" << endl<<endl;
+                    dollars += bet;
+                    lcd.cls();
+                    lcd.locate(lcd.max_col/4,lcd.max_row/2);
+                    lcd.printf("Player Wins!");
+                    wait_ms(1000);
+                    continue;
+                }
+
+                if (Player1.handValue() == Player2.handValue()) {
+                    cout << "Draw." << endl;
+                    bet = bet / 2;
+                    dollars += bet;
+                    lcd.cls();
+                    lcd.locate(lcd.max_col/4,lcd.max_row/2);
+                    lcd.printf("Draw.");
+                    wait_ms(1000);
+                    continue;
+                }
+
+                if (Player1.handValue() < Player2.handValue()) {
+                    cout << "Dealer wins." << endl;
+                    lcd.cls();
+                    lcd.locate(lcd.max_col/4,lcd.max_row/2);
+                    lcd.printf("Dealer Wins :(");
+                    dollars -= bet;
+                    wait_ms(1000);
+                    break;
+                }
+
+                if (Player1.handValue() > Player2.handValue()) {
+                    cout << "Player wins." << endl;
+                    dollars += bet;
+                    lcd.cls();
+                    lcd.locate(lcd.max_col/4,lcd.max_row/2);
+                    lcd.printf("Player Wins!");
+                    wait_ms(600);
+                    continue;
+                }
+            }
+            if(overshot) {
+                break;
+            }
+        }
+        wait_ms(400);
+        if(dollars != 0) {
+            lcd.cls();
+            lcd.locate(lcd.max_col/4,lcd.max_row/2);
+            lcd.printf("Continue?");
+            pc.printf("Continue?\n\n\n");
+        } else {
+            pc.printf("You Lose");
+            lcd.cls();
+            lcd.locate(lcd.max_col/4,lcd.max_row/2);
+            lcd.printf("YOU LOSE");
+            break;
+        }
+        while(pb7 && pb8);
+        if(!pb7) {
+            wait_ms(200);
+        } else if(!pb8) {
+            break;
+        }
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 8798a72d6580 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Mar 12 18:59:00 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/7e07b6fb45cf
\ No newline at end of file
diff -r 000000000000 -r 8798a72d6580 mpr121.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mpr121.h	Thu Mar 12 18:59:00 2015 +0000
@@ -0,0 +1,157 @@
+/*
+Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
+
+ 
+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.
+
+   Parts written by Jim Lindblom of Sparkfun
+   Ported to mbed by A.Buckton, Feb 2011
+*/
+
+#ifndef MPR121_H
+#define MPR121_H
+
+//using namespace std;
+
+class Mpr121 
+{
+
+public:
+    // i2c Addresses, bit-shifted
+    enum Address { ADD_VSS = 0xb4,// ADD->VSS = 0x5a <-wiring on Sparkfun board
+                   ADD_VDD = 0xb6,// ADD->VDD = 0x5b
+                   ADD_SCL = 0xb8,// ADD->SDA = 0x5c
+                   ADD_SDA = 0xba // ADD->SCL = 0x5d
+                 };
+
+    // Real initialiser, takes the i2c address of the device.
+    Mpr121(I2C *i2c, Address i2cAddress);
+    
+    bool getProximityMode();
+    
+    void setProximityMode(bool mode);
+    
+    int readTouchData();
+               
+    unsigned char read(int key);
+    
+    int write(int address, unsigned char value);
+    int writeMany(int start, unsigned char* dataSet, int length);
+
+    void setElectrodeThreshold(int electrodeId, unsigned char touchThreshold, unsigned char releaseThreshold);
+        
+protected:
+    // Configures the MPR with standard settings. This is permitted to be overwritten by sub-classes.
+    void configureSettings();
+    
+private:
+    // The I2C bus instance.
+    I2C *i2c;
+
+    // i2c address of this mpr121
+    Address address;
+};
+
+
+// MPR121 Register Defines
+#define    MHD_R        0x2B
+#define    NHD_R        0x2C
+#define    NCL_R        0x2D
+#define    FDL_R        0x2E
+#define    MHD_F        0x2F
+#define    NHD_F        0x30
+#define    NCL_F        0x31
+#define    FDL_F        0x32
+#define    NHDT         0x33
+#define    NCLT         0x34
+#define    FDLT         0x35
+// Proximity sensing controls
+#define    MHDPROXR     0x36
+#define    NHDPROXR     0x37
+#define    NCLPROXR     0x38
+#define    FDLPROXR     0x39
+#define    MHDPROXF     0x3A
+#define    NHDPROXF     0x3B
+#define    NCLPROXF     0x3C
+#define    FDLPROXF     0x3D
+#define    NHDPROXT     0x3E
+#define    NCLPROXT     0x3F
+#define    FDLPROXT     0x40
+// Electrode Touch/Release thresholds
+#define    ELE0_T       0x41
+#define    ELE0_R       0x42
+#define    ELE1_T       0x43
+#define    ELE1_R       0x44
+#define    ELE2_T       0x45
+#define    ELE2_R       0x46
+#define    ELE3_T       0x47
+#define    ELE3_R       0x48
+#define    ELE4_T       0x49
+#define    ELE4_R       0x4A
+#define    ELE5_T       0x4B
+#define    ELE5_R       0x4C
+#define    ELE6_T       0x4D
+#define    ELE6_R       0x4E
+#define    ELE7_T       0x4F
+#define    ELE7_R       0x50
+#define    ELE8_T       0x51
+#define    ELE8_R       0x52
+#define    ELE9_T       0x53
+#define    ELE9_R       0x54
+#define    ELE10_T      0x55
+#define    ELE10_R      0x56
+#define    ELE11_T      0x57
+#define    ELE11_R      0x58
+// Proximity Touch/Release thresholds
+#define    EPROXTTH     0x59
+#define    EPROXRTH     0x5A
+// Debounce configuration
+#define    DEB_CFG      0x5B
+// AFE- Analogue Front End configuration
+#define    AFE_CFG      0x5C 
+// Filter configuration
+#define    FIL_CFG      0x5D
+// Electrode configuration - transistions to "active mode"
+#define    ELE_CFG      0x5E
+
+#define GPIO_CTRL0      0x73
+#define GPIO_CTRL1      0x74
+#define GPIO_DATA       0x75
+#define    GPIO_DIR     0x76
+#define    GPIO_EN      0x77
+#define    GPIO_SET     0x78
+#define GPIO_CLEAR      0x79
+#define GPIO_TOGGLE     0x7A
+// Auto configration registers
+#define    AUTO_CFG_0   0x7B
+#define    AUTO_CFG_U   0x7D
+#define    AUTO_CFG_L   0x7E
+#define    AUTO_CFG_T   0x7F
+
+// Threshold defaults
+// Electrode touch threshold
+#define    E_THR_T      0x0F   
+// Electrode release threshold 
+#define    E_THR_R      0x0A    
+// Prox touch threshold
+#define    PROX_THR_T   0x02
+// Prox release threshold
+#define    PROX_THR_R   0x02
+
+#endif