Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void shuffle(int [][13]);
void deal(int [][13], char *[], char*[], int[][2]);
void hand(int [][13], char *[], char*[]);
void pair(int [][2]);
void bubblesort(int [][2]);
void main()
{
int cards[5][2] = {0};//hand
int i, k, j, x=0, z, y, f;//counters
int fullhouse=0, straight=0, flush=0, fourkind=0, threekind=0, twokind=0, pair=0, pairarr[2]={0}, twopairarr[4]={0};
char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int deck[4][13] = {0};
srand(time(0));
shuffle(deck);
deal(deck, face, suit, cards);
for(x=0; x<=4; x++)
puts("");
for(i = 0; i < 5; i++)
{
printf("%-8sof %8s\n", face[cards[i][0]],suit[cards[i][1]]);//prints hand
}
bubblesort(cards);
/////pair/////
for(j=1; j<5; j++)
{
for(k=0; k<5; k++)
if(cards[k][0] == cards[k+j][0])
twokind += 1;
if(twokind == 1)
pair = 1;
}
/////three of a kind/////
for(x=0; x<=4; x++)
if(cards[x][0] == cards[x+1][0] && cards[x][0] == cards[x+2][0])
threekind = 1;
if(threekind == 1 && pair == 1)
fullhouse = 1;
else
pair = 0;
/////four of a kind/////
for(x=0; x<4; x++)
{
if(cards[x][0] == cards[x+1][0])
fourkind += 1;
if(fourkind == 3)
threekind = 0;
}
/////flush/////
for(z=1; z<3; z++)
for(x=0; x<5; x++)
{
if(cards[x][1] == cards[0+z][1])
flush = 1;
else
flush = 0;
if(flush == 0)
break;
}
/////straight/////
for(f=1; f<=4; f++)
{
if(cards[0][0] == (cards[f][0] - f))
straight = 1;
else
straight = 0;
if(straight == 0)
break;
}
puts("");
if(flush == 1)
printf("You have a flush!");
if(straight == 1)
printf("You have a straight!");
if(fullhouse == 1 && threekind == 1)
printf("You have a full house!");
else if(fourkind == 3)
printf("You have four of a kind.");
else if(threekind == 1)
printf("You have three of a kind.");
else if(twokind == 2)
printf("You have two pairs.");
else if(twokind == 1)
printf("You have a pair of ");
else
y=0;
if(y==0 && flush != 1 && straight != 1)
printf("You have a bad hand :(");
}
void shuffle(int workdeck[][13])
{
int card, row, column;
for(card = 1; card <=52; card++)
{
row = rand() % 4;
column = rand() % 13;
while(workdeck[row][column] != 0)
{
row = rand() % 4;
column = rand() % 13;
}
workdeck[row][column] = card;
}
}
void deal(int workdeck2[][13], char *workface[], char *worksuit[], int cards[5][2])
{
int card, row, column;
for(card = 1; card <=5; card++)
for(row = 0; row <=3; row++)
for(column = 0; column <= 12; column++)
if(workdeck2[row][column] == card)
{
cards[card - 1][0] = column;
cards[card - 1][1] = row;
printf("%5s of %-8s", workface[column], worksuit[row]); //prints hand
if(card % 2 == 0)
puts("");
else
printf("\t");
break;
}
cards[0][0] = 0; //manual hand to test displaying of hand types, etc
cards[0][1] = 1;
cards[1][0] = 12;
cards[1][1] = 3;
cards[2][0] = 2;
cards[2][1] = 3;
cards[3][0] = 0;
cards[3][1] = 3;
cards[4][0] = 2;
cards[4][1] = 1;
}
void bubblesort(int hand[][2])
{
int temp = 0, i,j;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
if(hand[j][0] > hand[j+1][0])
{
temp = hand[j][0];
hand[j][0] = hand[j+1][0];
hand[j+1][0] = temp;
}
}
}
}