Code:
#include <stdio.h>#include <time.h>#include "stdafx.h"#include <string.h>
#define SUITS 4#define FACES 13#define RANKS 13
//global variablesint straight, flush, four, three;int pairs = 0;
void analyzeHand(int sInHand[], int fInHand[]){ int num_consec = 0; int rank, suit;
straight = 0; flush = 0; four = 0; three = 0; pairs = 0;
for (suit = 0; suit < SUITS; suit++) if (sInHand[suit] == 5) flush = 1;
rank = 0; while (fInHand[rank] == 0) rank++;
for (; rank < FACES && fInHand[rank]; rank++) num_consec++;
if (num_consec == 5) { straight = 1; return; }
for (rank = 0; rank < RANKS; rank++) { if (fInHand[rank] == 4) four = 1; if (fInHand[rank] == 3) three = 1; if (fInHand[rank] == 2) pairs++; }
}
void dealCard(int sInHand[], int fInHand[], char *suits[], char *faces[], int deck[][FACES]) { int suitIndex, faceIndex;
suitIndex = rand() % 4; // generates 1 random card faceIndex = rand() % 13; // generates 1 random card
while (deck[suitIndex][faceIndex] == 1) { // checks if array slot is empty suitIndex = rand() % 4; // checks if the card has been given out faceIndex = rand() % 13; // checks if the card has been given out
} deck[suitIndex][faceIndex] = 1; // checks if array slot is empty // fills the deck with 1's
// stores the power of the card
printf("%s of %s \n", faces[faceIndex], suits[suitIndex]); // number of the card in the array
fInHand[faceIndex]++; sInHand[suitIndex]++; printf("%d", fInHand[faceIndex]); printf("%d", sInHand[suitIndex]); }
void Question5(){ int i, j; int swap; int q; int ni, nj;
int DeckC[4][13] = { { 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 } }; printf("Before\n"); for (i = 0; i < 4; i++) { for (j = 0; j < 13; j++)
{ printf("%d ", DeckC[i][j]); } printf("\n"); } printf("\n"); for (q = 0; q < 3; q++) for (i = 0; i < 4; i++) for (j = 0; j < 13; j++) { ni = rand() % 4; nj = rand() % 13; swap = DeckC[i][j]; DeckC[i][j] = DeckC[ni][nj]; DeckC[ni][nj] = swap; } printf("After\n"); for (i = 0; i < 4; i++) { for (j = 0; j < 13; j++)
{ printf("%d ", DeckC[i][j]); } printf("\n"); }}
void dealHand(int sInHand[], int fInHand[], char *suits[], char *faces[], int deck[][FACES]) { int i; for (i = 0; i < 5; i++)
dealCard(sInHand, fInHand, suits, faces, deck); printf("\n");}
/* assign value to hand and print results */void printResult(int sInHand[], int fInHand[], int *pointValue)
{ int points;
analyzeHand(sInHand, fInHand);
if (straight && flush) { printf("Straight flush\n\n"); *pointValue = 9; points = *pointValue; }
else if (four) {
printf("Four of a kind\n\n");
*pointValue = 8;
points = *pointValue;
}
else if (three && pairs == 1) {
printf("Full house\n\n");
*pointValue = 7;
points = *pointValue;
}
else if (flush) { printf("Flush\n\n"); *pointValue = 6;
points = *pointValue;
}
else if (straight) {
printf("Straight\n\n");
*pointValue = 5;
points = *pointValue;
}
else if (three) {
printf("Three of a kind\n\n");
*pointValue = 4;
points = *pointValue;
}
else if (pairs == 2) {
printf("Two pairs\n\n");
*pointValue = 3;
points = *pointValue;
}
else if (pairs == 1) {
printf("Pair\n\n");
*pointValue = 2;
points = *pointValue;
}
else if (pairs == 0) { printf("High card\n\n"); *pointValue = 1; points = *pointValue;
}
}
int main() { char *suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" }; /// 8 char *faces[13] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" }; /// int deck[4][13] = { { 0 } }; /// initialises with "AVAILABLE"
int sInHand[4] = { 0 }; // player one int fInHand[13] = { 0 }; // player one int SInHand2[4] = { 0 }; // player two int FInHand2[13] = { 0 }; // player two int numbersw; int i; int j; int P1Points = 0; int P2Points = 0; srand(time(NULL)); printf("Press 1 for single player and 2 for multiplayer or 3 to display good shuffling"); scanf_s("%d", &numbersw); if (numbersw == 1) { printf("Player 1 was dealt:\n\n"); dealHand(sInHand, fInHand, suits, faces, deck); analyzeHand(sInHand, fInHand); printResult(sInHand, fInHand, &P1Points);
printf("Player 2 was dealt:\n\n"); dealHand(SInHand2, FInHand2, suits, faces, deck); analyzeHand(SInHand2, FInHand2); printResult(SInHand2, FInHand2, &P2Points);
if (P1Points > P2Points) { printf("Player One has won\n"); } else if (P1Points < P2Points) { printf("Player Two has won\n"); } else printf("A Tie\n"); system("pause"); }
else if (numbersw == 2) { printf("Player 1 was dealt:\n\n"); dealHand(sInHand, fInHand, suits, faces, deck); analyzeHand(sInHand, fInHand); printResult(sInHand, fInHand, &P1Points);
printf("Dealer was dealt:\n\n"); //dealHand(SInHand2, FInHand2, suits, faces, deck); // to hide the cards analyzeHand(SInHand2, FInHand2); printResult(SInHand2, FInHand2, &P2Points);
if (P1Points > P2Points) { printf("Player One has won\n"); } else if (P1Points < P2Points) { printf("Dealer has won\n"); } else printf("A Tie\n"); system("pause"); } else if (numbersw == 3) { Question5(); }
else if (numbersw != 1 || 2) { printf("Incorrect input\n"); printf("Press 1 for single player and 2 for multiplayer\n"); scanf_s("%d", &numbersw); } }