All pastes #526036 Raw Edit

Something

public text v1 · immutable
#526036 ·published 2007-05-31 17:03 UTC
rendered paste body
/**************************************************
**                  bridge.cpp                   **
** please note that card rank 10 is defined as T **
**************************************************/
#include<iostream>
using namespace std;

const int hand=13;
const int deckSize=52;

int shuffle(int a[ ]) {
    srand(time(NULL));
    for(int l=0; l<deckSize; l++) {
            int m= rand() % (deckSize-l);
            int tmp=a[l];
            a[l]=a[l+m];
            a[l+m]=tmp;
            }
}

char convertRank(int a){
     char rank;
     int b = a % 13;
     char ranks[hand] = { 'A','2','3','4','5','6','7','8','9','T','J','Q','K' };
     rank = ranks[b];                                             
     return rank;
}

void intoSuit(int a[ ]) {
	string clubs, spades, hearts, diamonds;
	spades = " ";
	clubs = " ";
	hearts = " ";
    diamonds = " ";
	
	for(int n=0;n<hand;n++){ 
		if (a[n]<13) {
			spades = spades + convertRank(a[n]) + " ";
			}
		else if (a[n]>38) {
			clubs = clubs + convertRank(a[n]) + " ";
			}
		else if ((a[n]>12) && (a[n]<26)) {
			hearts = hearts + convertRank(a[n]) + " ";	
			}
		else if ((a[n]>25) && (a[n]<39)) {
			diamonds = diamonds + convertRank(a[n]) + " ";
			}
		}
	cout << "\nSpades: " << spades << endl;
	cout << "Hearts: " << hearts << endl;
	cout << "Clubs: " << clubs << endl;
	cout << "Diamonds: " << diamonds << endl;
}

int count(int a[ ]){
    int total=0;                    
    for(int u=0; u<hand; u++) {
        if (a[u] % 13 == 0) {
            total = total + 4;
            }
        else if (a[u] % 13 == 10) {
            total = total + 1;
            }
        else if (a[u] % 13 == 11) {
            total = total + 2;
            }
        else if (a[u] % 13 == 12) {
            total = total + 3;
            }
    }
    return total;
}

void defineWinner(int w, int x, int y, int z){
    if ((w > x) && (w > y) && (w > z)){
                   cout << "Hand 1 is the winner!" << endl; 
                   }
    else if ((x > w) && (x > y) && (x > z)){
                   cout << "Hand 2 is the winner!" << endl; 
                   }                   
    else if ((y > w) && (y > w) && (y > z)){
                   cout << "Hand 3 is the winner!" << endl; 
                   }                   
    else if ((z > w) && (z > x) && (z > y)){
                   cout << "Hand 4 is the winner!" << endl; 
                   }                   
    else {
         cout << "There is no clear winner please try again" << endl;
         }
}

int main( ) {
    int hand1total=0, hand2total=0, hand3total=0, hand4total=0;
    int deck[deckSize] = { 0,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 };
    
    shuffle(deck);
 
    int *hand1, *hand2, *hand3, *hand4;

    hand1 = &deck[0];
    hand2 = &deck[14];
    hand3 = &deck[27];
    hand4 = &deck[40];

    //output
    cout << "Hand 1: " << endl;
    intoSuit(hand1);
    hand1total = count(hand1);
    cout << "Hand 1 total is: " << hand1total << " points." << endl;

    cout << "\nHand 2: " << endl;
    intoSuit(hand2);
    hand2total = count(hand2);
    cout << "Hand 2 total is: " << hand2total << " points." << endl;

    cout << "\nHand 3: " << endl;
    intoSuit(hand3);
    hand3total = count(hand3);
    cout << "Hand 3 total is: " << hand3total << " points." << endl;
     
    cout << "\nHand 4: " << endl;
    intoSuit(hand4);
    hand4total = count(hand4);
    cout << "Hand 4 total is: " << hand4total << " points." << endl;
    
    defineWinner(hand1total,hand2total,hand3total,hand4total);
    
    system("pause");         
    return 0;   
}