cards
Apr 25, 2012 at 8:21pm UTC
im making a program which simulates poker. i need a way to evaluate if the player has a flush, straight flush, or 4 of a kind. i am just wondering if there is any quick way to do this without a whole set of if statements. here is my code at the moment, note that i cannot edit the cpp file for the assignment, only the main.cpp.
main.cpp -
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
//
// 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>
#define STRAIGHT_FLUSH 3
#define FLUSH 2
#define FOUR_OF_A_KIND 1
void newshift(aCard,int ,DeckofCards &);
bool sortorder(const aCard &a, const aCard &b);
void swap(aCard &, aCard &);
void sort(aCard[]);
int quat();
aCard operator +(aCard a, int i){//not sure if i need this
aCard temp;
temp.suit = a.suit;
temp.value = a.value + i;
return temp;
}
int main(){
int wager = 0;
int bank = quat();
DeckofCards deck; deck.shuffle(); aCard hand[5];
for (int i = 0; i < 5; i++){hand[i] = deck.deal();}//deal first hand
sort(hand); //sorts hand
displayHand(hand, "Your cards so far..." ); //displays hand
return EXIT_SUCCESS;
}
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){
std::cout << "An error has occured: " << &e;
bank.close();
exit(3);
}
bank >> i;
bank.close();
return i;
}
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]);
}
}
}
file.cpp -
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
#include <iostream>
#include <iomanip.h>
#include <stdlib.h>
#include <string>
#include "Header.h"
DeckofCards::DeckofCards(){
nextCard_ = 0; // next card drawn will be 'top' card in deck
char suits[4];
suits[0] = 'C' ;
suits[1] = 'D' ;
suits[2] = 'H' ;
suits[3] = 'S' ;
int k = 0;
for (int i = 0; i < 4; i++)
for (int j = 2; j < 15; j++) {
deck_ [k].suit = suits[i];
deck_ [k++].value = j;
}
}
void DeckofCards::shuffle(){
if (isEmpty() && nextCard_ ){//correct this...
cerr << "shuffle: Must have full deck to shuffle..." << endl;
exit(2);
}
aCard temp[52];
int i;
int r;
int seed;
seed = int (time(NULL));
srand(seed);
r = rand()/100%52;
for ( i = 0; i < 52; i++) {
while (deck_[r].value == 0){
r = rand()/100%52;
}
temp[i] = deck_[r];
deck_[r].value = 0;
}
for ( i = 0; i < 52; i++)
deck_[i] = temp[i];
}
bool DeckofCards::isEmpty() const {
return (nextCard_ == 52) ;
}
aCard DeckofCards::deal(){
if (isEmpty()){//correct this...
cerr << "deal: All cards have been dealt..." << endl;
exit(1);
}
// return next card and 'remove' it from the deck
return deck_ [ nextCard_++ ];
}
void displayHand(aCard x[], std::string words) {
cout << setw(40) << words <<endl;
toporBottom();
edges();
displayRowofSuits(x);
displayRowofValues(x);
edges();
toporBottom();
}
void displaySuite(char suit, int line) {
switch (suit) {
case 'D' : switch (line) {
case 0:
case 4: cout << " D " ; break ;
case 1:
case 3: cout << " D D " ; break ;
case 2: cout << " D D " ; break ;
} break ;
case 'H' : switch (line) {
case 0:
case 2: cout << " H H " ; break ;
case 1: cout << " H H H " ; break ;
case 3: cout << " H H " ; break ;
case 4: cout << " H " ; break ;
} break ;
case 'S' : switch (line) {
case 0: cout << " S " ; break ;
case 1: cout << " S S " ; break ;
case 2: cout << " S S " ; break ;
case 3: cout << " S S S " ; break ;
case 4: cout << " S " ; break ;
} break ;
case 'C' : switch (line) {
case 0: cout << " CCC " ; break ;
case 1: cout << " C " ; break ;
case 2: cout << " C C " ; break ;
case 3: cout << " CC C CC " ; break ;
case 4: cout << " C C " ; break ;
}
}
}
void displayRowofSuits(aCard x[]){
for (int i = 0; i < 5; i++) { //5 lines
cout << " " ;
for (int j = 0; j < 5; j++) {
cout << "|" ;
displaySuite( x[j].suit, i); // suit of card i on line j
cout << "| " ;
}
cout << endl;
}
}
void toporBottom(){
cout << " +++++++++++ +++++++++++ +++++++++++ +++++++++++ " ;
cout << " +++++++++++ " <<endl;
}
void edges() {
cout << " | | | | | | | | | " ;
cout << " | " <<endl;
}
void displayRowofValues(aCard x[]) {
cout << " " ;
for (int i = 0 ; i < 5; i++ ){
cout << "|" << setw(5) ;
if (x[i].value >= 2 && x[i].value <= 10){
cout << x[i].value ;
}
else switch (x[i].value) {
case 11: cout << " J" ; break ;
case 12: cout << " Q" ; break ;
case 13: cout << " K" ; break ;
case 14: cout << " A" ; break ;
default : cout << " bad" ;
}
cout << setw(3) << " " <<" | " ;
}
cout <<endl;
}
void DeckofCards::displayDeck(){
displayHand(deck_, "Your Cards" );
}
note: i did write the cpp file i can only comment it. so i cannot change or add any functions within the class.
Topic archived. No new replies allowed.