1 player Black jack for mbed
Dependencies: 4DGL-uLCD-SE PinDetect mbed
Revision 0:d2e6b3ce6988, committed 2015-03-12
- Comitter:
- jayvpoddar
- Date:
- Thu Mar 12 19:48:26 2015 +0000
- Commit message:
- First push
Changed in this revision
diff -r 000000000000 -r d2e6b3ce6988 4DGL-uLCD-SE.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Thu Mar 12 19:48:26 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mtaylor33/code/4DGL-uLCD-SE/#10493f7d097a
diff -r 000000000000 -r d2e6b3ce6988 PinDetect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Thu Mar 12 19:48:26 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
diff -r 000000000000 -r d2e6b3ce6988 card.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/card.cpp Thu Mar 12 19:48:26 2015 +0000 @@ -0,0 +1,67 @@ +#include "card.h" +Card::Card() +{ + _rank = 0; + _suit = 0; +} +Card::Card(int rank, int suit) +{ + _rank = rank; + _suit = suit; + +} + +string Card::convertRank() +{ + switch(_rank) { + case 0: + return "INVALID"; + case 1: + return "A"; + case 2: + return "2"; + case 3: + return "3"; + case 4: + return "4"; + case 5: + return "5"; + case 6: + return "6"; + case 7: + return "7"; + case 8: + return "8"; + case 9: + return "9"; + case 10: + return "10"; + case 11: + return "J"; + case 12: + return "Q"; + case 13: + return "K"; + } +} +string Card::convertSuit() +{ + switch(_suit) { + case 0: + return "C"; + case 1: + return "D"; + case 2: + return "H"; + case 3: + return "S"; + } +} +void Card::swapCards(Card cards[] ,int i ,int j) +{ + Card temp =cards[i]; + cards[i]=cards[j]; + cards[j]=temp; +} + +
diff -r 000000000000 -r d2e6b3ce6988 card.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/card.h Thu Mar 12 19:48:26 2015 +0000 @@ -0,0 +1,16 @@ +#include "mbed.h" +#include <string> +class Card +{ + + + +public: + int _rank; + int _suit; + Card(); + Card(int rank, int suit); + string convertRank(); + string convertSuit(); + static void swapCards(Card cards[],int i,int j); +}; \ No newline at end of file
diff -r 000000000000 -r d2e6b3ce6988 deck.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/deck.cpp Thu Mar 12 19:48:26 2015 +0000 @@ -0,0 +1,32 @@ +#include "deck.h" +#include <time.h> +Deck::Deck () +{ + int index = 0; + for (int suit = 0; suit <= 3; suit++) { + for (int rank =1; rank < 14; rank++) { + cards[index]._rank= rank; + cards[index]._suit = suit; + index++; + } + } +} +void Deck::shuffleDeck() +{ + int ran; + srand(time(NULL)); + for (int i=0; i< 52; i++) { + + ran = randomInt(i,51); + + Card::swapCards(cards,i,ran); + + } +} +int Deck::randomInt(int low ,int high) +{ + float ran = (rand()%52)*1.0; + ran = ran/52.0; + return (int)(((high-low)*(ran))+low); +} +
diff -r 000000000000 -r d2e6b3ce6988 deck.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/deck.h Thu Mar 12 19:48:26 2015 +0000 @@ -0,0 +1,13 @@ +#include "mbed.h" +#include "card.h" +class Deck +{ + private: + + int randomInt(int low,int high); +public: + Deck(); + Card cards[52]; + void shuffleDeck(); + +}; \ No newline at end of file
diff -r 000000000000 -r d2e6b3ce6988 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Mar 12 19:48:26 2015 +0000 @@ -0,0 +1,317 @@ +#include "mbed.h" +#include "uLCD_4DGL.h" +#include <string> +#define PURPLE 0x800080 +#define ORANGE 0xFFA500 +#include "deck.h" +#include <mpr121.h> +#include "PinDetect.h" +uLCD_4DGL uLCD(p28, p27, p30); +PinDetect pb1(p8); +PinDetect pb2(p11); +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); +// Setup the i2c bus on pins 9 and 10 +I2C i2c(p9, p10); +// Setup the Mpr121: +// constructor(i2c object, i2c address of the mpr121) +Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); +//InterruptIn interrupt(p26); +Deck gameDeck; +Card playerHand[10]; +Card dealerHand[10]; +int playerCount=2; +int dealerCount=2; +bool isPlayer=true; +int playerPoints; +int dealerPoints; +int money = 1000; +int bet=100; +int sumHand(Card[]); + +void printPoints(int points) +{ + if(points>21) { + uLCD.printf("Pts=bust"); + } else { + uLCD.printf("Pts=%d",points); + } +} +void initialDeal() +{ + playerCount=2; + dealerCount=2; + playerHand[0]= gameDeck.cards[0]; + dealerHand[0]=gameDeck.cards[1]; + playerHand[1] = gameDeck.cards[2]; + dealerHand[1] = gameDeck.cards[3]; + uLCD.locate(1,8); + uLCD.printf("%s%s",playerHand[0].convertRank(),playerHand[0].convertSuit()); + uLCD.locate(4,8); + uLCD.printf("%s%s",playerHand[1].convertRank(),playerHand[1].convertSuit()); + uLCD.locate(1,3); + uLCD.printf("%s%s",dealerHand[0].convertRank(),dealerHand[1].convertSuit()); + + uLCD.locate(6,10); + playerPoints = sumHand(playerHand); + printPoints(playerPoints); +} + + +void hit() +{ + if(isPlayer) { + playerHand[playerCount]=gameDeck.cards[playerCount+dealerCount]; + playerCount++; + uLCD.locate((playerCount-1)*3+1,8); + uLCD.printf("%s%s",playerHand[playerCount-1].convertRank(),playerHand[playerCount-1].convertSuit()); + uLCD.locate(6,10); + playerPoints = sumHand(playerHand); + printPoints(playerPoints); + wait(1); + } else { + dealerHand[dealerCount] = gameDeck.cards[playerCount+dealerCount]; + dealerCount++; + uLCD.locate((dealerCount-1)*3+1,3); + uLCD.printf("%s%s",dealerHand[dealerCount-1].convertRank(),dealerHand[dealerCount-1].convertSuit()); + uLCD.locate(6,6.5); + dealerPoints = sumHand(dealerHand); + printPoints(dealerPoints); + wait(1); + } +} + +void getBet() +{ + uLCD.cls(); + uLCD.printf("How much will you bet?\n(10-500)"); + int len=0; + int amount=0; + while(len<3) { + int key=0; + int i=0; + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + // LED demo mod + i=0; + // puts key number out to LEDs for demo + for (i=0; i<12; i++) { + if (((value>>i)&0x01)==1) key=i+1; + + } + if(key != 0 ) { + if(key>10) { + break; + } else { + len++; + amount*=10; + amount+=key-1; + uLCD.printf("%d",key-1); + wait(.25); + } + } + } + bet=max(min(amount,500),10); + bet = min(bet,money); + uLCD.printf("\nBet = %d",bet); + +} +void setup() +{ + uLCD.color(ORANGE); + + + getBet(); + uLCD.cls(); + uLCD.locate(6,0.5); + uLCD.printf("Dealer"); + + uLCD.locate(6,7.5); + uLCD.printf("Player"); + + uLCD.filled_rectangle(0,112,127,148,DGREY); + gameDeck.shuffleDeck(); + initialDeal(); + +} + +// Callback routine is interrupt activated by a debounced pb1 hit +void pb1_hit_callback (void) +{ + hit(); + +} + +// Callback routine is interrupt activated by a debounced pb2 hit +void pb2_hit_callback (void) +{ + isPlayer=false; + +} +void walletSize() +{ + uLCD.cls(); + uLCD.printf("NOTE:\nEnter input on pad followed by key 10\n\n"); + uLCD.printf("top pushbutton is hit and bottom is stand"); + + wait(5); + uLCD.cls(); + uLCD.printf("How much will you start with?\n(1000-10000)"); + int len=0; + int amount=0; + while(len<5) { + int key=0; + int i=0; + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + // LED demo mod + i=0; + // puts key number out to LEDs for demo + for (i=0; i<12; i++) { + if (((value>>i)&0x01)==1) key=i+1; + + } + if(key != 0 ) { + if(key>10) { + break; + } else { + len++; + amount*=10; + amount+=key-1; + uLCD.printf("%d",key-1); + wait(.25); + } + } + } + money=max(min(amount,50000),1000); + + uLCD.printf("\nWallet = %d",money); + +} +void game(); +int main() +{ + pb1.mode(PullUp); + pb2.mode(PullUp); + // Delay for initial pullup to take effect + wait(.01); + // Setup Interrupt callback functions for a pb hit + pb1.attach_deasserted(&pb1_hit_callback); + pb2.attach_deasserted(&pb2_hit_callback); + // Start sampling pb inputs using interrupts + pb1.setSampleFrequency(); + pb2.setSampleFrequency(); + //uLCD.color(ORANGE); + //uLCD.printf("How much money to play with?(minimum bet $10)"); + walletSize(); + game(); + +} +int sumHand(Card[]); +void result(int); +void dealerTurn(); +void game() +{ + while(money>=100) { + isPlayer = true; + setup(); + uLCD.locate(0,14); + uLCD.printf("Cash=%d",money); + uLCD.locate(10,14); + uLCD.printf("Bet=%d",bet); + while(isPlayer) { + if(sumHand(playerHand)>21) { + result(2); + } + } + dealerTurn(); + } + while(1) { + uLCD.cls(); + uLCD.locate(3,7); + uLCD.printf("GAME OVER"); + wait(1); + } +} +void result(int outcome) +{ + uLCD.cls(); + if(outcome == 0) { + + uLCD.locate(6,7); + uLCD.printf("Player Won "); + money += bet; + } else if(outcome == 1) { + uLCD.locate(6,7); + uLCD.printf("Game Draw"); + + } else if(outcome == 2) { + uLCD.locate(6,7); + uLCD.printf("Player Lost "); + money -=bet; + } + wait(1); + game(); +} +void dealerTurn() +{ + isPlayer = false; + uLCD.locate(4,3); + uLCD.printf("%s%s",dealerHand[1].convertRank(),dealerHand[1].convertSuit()); + uLCD.locate(6,6.5); + dealerPoints = sumHand(dealerHand); + printPoints(dealerPoints); + wait(1); + while(dealerPoints<17) { + hit(); + } + if(dealerPoints>21) { + result(0); + } else if(playerPoints>dealerPoints) { + result(0); + } else if(playerPoints<dealerPoints) { + result(2); + } else { + result(1); + } +} + + +int sumHand(Card hand[]) +{ + int sum=0; + int aces=0; + int count= isPlayer? playerCount:dealerCount; + for(int i=0; i<count; i++) { + Card card = hand[i]; + if(card._rank==1) { + sum+=11; + aces++; + } else if(card._rank < 11) { + sum+=card._rank; + } else { + sum+=10; + } + } + if(sum>21) { + if(aces>0) { + if(isPlayer) { + while(aces>0 && sum > 21) { + sum-=10; + aces--; + } + } else { + if(sum>=27) { + while(aces>0 && sum > 21) { + sum-=10; + aces--; + } + } + + } + } + } + return sum; +} \ No newline at end of file
diff -r 000000000000 -r d2e6b3ce6988 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Mar 12 19:48:26 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 d2e6b3ce6988 mpr121.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.cpp Thu Mar 12 19:48:26 2015 +0000 @@ -0,0 +1,221 @@ +/* +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. +*/ + +#include <mbed.h> +#include <sstream> +#include <string> +#include <list> + +#include <mpr121.h> + +Mpr121::Mpr121(I2C *i2c, Address i2cAddress) +{ + this->i2c = i2c; + + address = i2cAddress; + + // Configure the MPR121 settings to default + this->configureSettings(); +} + + +void Mpr121::configureSettings() +{ + // Put the MPR into setup mode + this->write(ELE_CFG,0x00); + + // Electrode filters for when data is > baseline + unsigned char gtBaseline[] = { + 0x01, //MHD_R + 0x01, //NHD_R + 0x00, //NCL_R + 0x00 //FDL_R + }; + + writeMany(MHD_R,gtBaseline,4); + + // Electrode filters for when data is < baseline + unsigned char ltBaseline[] = { + 0x01, //MHD_F + 0x01, //NHD_F + 0xFF, //NCL_F + 0x02 //FDL_F + }; + + writeMany(MHD_F,ltBaseline,4); + + // Electrode touch and release thresholds + unsigned char electrodeThresholds[] = { + E_THR_T, // Touch Threshhold + E_THR_R // Release Threshold + }; + + for(int i=0; i<12; i++){ + int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2); + } + + // Proximity Settings + unsigned char proximitySettings[] = { + 0xff, //MHD_Prox_R + 0xff, //NHD_Prox_R + 0x00, //NCL_Prox_R + 0x00, //FDL_Prox_R + 0x01, //MHD_Prox_F + 0x01, //NHD_Prox_F + 0xFF, //NCL_Prox_F + 0xff, //FDL_Prox_F + 0x00, //NHD_Prox_T + 0x00, //NCL_Prox_T + 0x00 //NFD_Prox_T + }; + writeMany(MHDPROXR,proximitySettings,11); + + unsigned char proxThresh[] = { + PROX_THR_T, // Touch Threshold + PROX_THR_R // Release Threshold + }; + writeMany(EPROXTTH,proxThresh,2); + + this->write(FIL_CFG,0x04); + + // Set the electrode config to transition to active mode + this->write(ELE_CFG,0x0c); +} + +void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){ + + if(electrode > 11) return; + + // Get the current mode + unsigned char mode = this->read(ELE_CFG); + + // Put the MPR into setup mode + this->write(ELE_CFG,0x00); + + // Write the new threshold + this->write((ELE0_T+(electrode*2)), touch); + this->write((ELE0_T+(electrode*2)+1), release); + + //Restore the operating mode + this->write(ELE_CFG, mode); +} + + +unsigned char Mpr121::read(int key){ + + unsigned char data[2]; + + //Start the command + i2c->start(); + + // Address the target (Write mode) + int ack1= i2c->write(address); + + // Set the register key to read + int ack2 = i2c->write(key); + + // Re-start for read of data + i2c->start(); + + // Re-send the target address in read mode + int ack3 = i2c->write(address+1); + + // Read in the result + data[0] = i2c->read(0); + + // Reset the bus + i2c->stop(); + + return data[0]; +} + + +int Mpr121::write(int key, unsigned char value){ + + //Start the command + i2c->start(); + + // Address the target (Write mode) + int ack1= i2c->write(address); + + // Set the register key to write + int ack2 = i2c->write(key); + + // Read in the result + int ack3 = i2c->write(value); + + // Reset the bus + i2c->stop(); + + return (ack1+ack2+ack3)-3; +} + + +int Mpr121::writeMany(int start, unsigned char* dataSet, int length){ + //Start the command + i2c->start(); + + // Address the target (Write mode) + int ack= i2c->write(address); + if(ack!=1){ + return -1; + } + + // Set the register key to write + ack = i2c->write(start); + if(ack!=1){ + return -1; + } + + // Write the date set + int count = 0; + while(ack==1 && (count < length)){ + ack = i2c->write(dataSet[count]); + count++; + } + // Stop the cmd + i2c->stop(); + + return count; +} + + +bool Mpr121::getProximityMode(){ + if(this->read(ELE_CFG) > 0x0c) + return true; + else + return false; +} + +void Mpr121::setProximityMode(bool mode){ + this->write(ELE_CFG,0x00); + if(mode){ + this->write(ELE_CFG,0x30); //Sense proximity from ALL pads + } else { + this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active. + } +} + + +int Mpr121::readTouchData(){ + return this->read(0x00); +} \ No newline at end of file
diff -r 000000000000 -r d2e6b3ce6988 mpr121.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.h Thu Mar 12 19:48:26 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