1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
//
// main.cpp
// dealer2
//
// Created by Home on 4/23/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#include <iostream>
#include "Header.h"
#include <fstream>
#include <exception>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#define STRAIGHT_FLUSH 6
#define FLUSH 5
#define FOUR_OF_A_KIND 4
#define STRAIGHT 3
#define THREE_OF_A_KIND 2
#define PAIR 1
#define EMPTY 0
void wait(int);
void newshift(aCard,int,DeckofCards&);
void swap(aCard&,aCard &);
void sort(aCard[]);
void replace(aCard[],DeckofCards&);
int determinate(aCard[]);
int quat();
std::string won(int);
bool sortorder(const aCard&,const aCard&);
aCard operator+(aCard a, int i){
aCard temp;//creates returned card
temp.suit = a.suit;
temp.value = a.value + i;
return temp;
}
bool operator==(aCard a, aCard b){
return ((a.suit == b.suit) && (b.value == a.value));
}
int main(){
int wager = 0;
int bank = quat();
char re = 'y';
DeckofCards deck; deck.shuffle(); aCard hand[5];
if(bank == EXIT_FAILURE){ bank = 100;
std::cout << "\nNo bank was found. Starting with " << bank << " Quatloos\n";
}
//starts the IO exchange
start: {
for(int i = 0; i < 5; i++){hand[i] = deck.deal();}//deal first hand
sort(hand); //sorts hand
std::cout << "Your bank has " << bank << " quatloos.\n";
wait(1);
displayHand(hand, "Your cards so far..."); //displays hand
while(wager > bank || wager == 0){
std::cout << "What is your wager?";
std::cin >> wager;
std::cin.ignore(std::numeric_limits<int>::max(), '\n'); //clears the \n
}
bank -= wager;
replace(hand, deck);
sort(hand);
displayHand(hand, "Your Cards");
int profit = determinate(hand) * wager;
bank += profit;
//deposits
std::ofstream deposit;
deposit.open("/Users/home/Desktop/dealer2/dealer2/bank.txt");
deposit << bank;
deposit.close();
if(profit == 0) std::cout << "You lost\n";
else { std::cout << "You Have a " << won(profit/wager) << " And made " << profit << "$\n"; }
std::cout << "Would you like to play again?";
std::cin >> re;
std::cin.ignore(std::numeric_limits<int>::max(), '\n' );
wager = 0;
}
if((tolower(re) == 'y') && !(bank == 0)) goto start;
std::cout << "Thank you for giving us your money";
return EXIT_SUCCESS;
}
std::string won(int i){
switch (i) {
case 6:
return "Straight Flush";
break;
case 5:
return "Flush";
break;
case 4:
return "Four of a Kind";
break;
case 3:
return "Straight";
break;
case 2:
return "Three of a kind";
break;
case 1:
return "Pair";
break;
default:
break;
}
return NULL;
}
bool sortorder(const aCard &a, const aCard &b){
if(a.suit < b.suit)
return true;
if(a.suit == b.suit){
if(a.value < b.value)
return true;
}
return false;
}
int quat(){
std::ifstream bank; int i;
bank.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try{bank.open("/Users/home/Desktop/dealer2/dealer2/bank.txt");}
catch(std::ifstream::failure &e){
return EXIT_FAILURE;
}
bank >> i;
bank.close();
return i;
}
void wait(int seconds){
clock_t endwait;
endwait = clock() + seconds * CLOCKS_PER_SEC;
while(clock() < endwait) {}
}
void swap(aCard &a, aCard &b){
aCard temp;
temp = b;
b = a;
a = temp;
}
void sort(aCard x[]){
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
if(sortorder(x[i], x[j])) swap(x[i], x[j]);
}
}
}
void replace(aCard x[], DeckofCards &y){ //error occurs here
std::string s;
int r = 0;
std::cout << " Card 1 Card 2 Card 3 Card 4 Card 5\n";
std::cout << "Which cards would you like to replace: ";
getline(std::cin, s); //this skips before i added
for(int i = 0; i < s.length() && r < 3; i++){
if(!isspace(s[i])){
int j = (atoi(&s[i]))%5;
x[--j] = y.deal();
r++;
}
}
}
int determinate(aCard x[]){
int q = 1;
int suit_match = 1, value_match = 1, consecutive_value = 1;
for(int i = 0; i < 5; i++){
for(int j = i+1; j < 5; j++){
//evaluation
if(x[i].suit == x[j].suit) ++suit_match;
if(x[i].value == x[j].value) ++value_match;
if(x[i] + q++ == x[j]) ++consecutive_value;
//evals needed per inner loop run.
if( suit_match == 5 && consecutive_value == 5) return STRAIGHT_FLUSH;
if( suit_match == 5 ) return FLUSH;
if( consecutive_value == 5) return STRAIGHT;
}
q += 10;
suit_match = 0;
consecutive_value = 0;
}
if( value_match == 4) return FOUR_OF_A_KIND;
if( value_match == 3) return THREE_OF_A_KIND;
if( value_match == 2) return PAIR;
return EMPTY;
}
|