pointer
This commit is contained in:
96
DeitelC/Chapter7/deckOfCards17-21.c
Normal file
96
DeitelC/Chapter7/deckOfCards17-21.c
Normal file
@@ -0,0 +1,96 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define SUITS 4
|
||||
#define FACES 13
|
||||
#define CARDS 52
|
||||
|
||||
void shuffle(int deck[][FACES]);
|
||||
void deal(int deck[][FACES], const char *suits[], const char *faces[]);
|
||||
void fiveCard(int deck[][FACES], const char *suits[], const char *faces[]);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int deck[SUITS][FACES] = {0};
|
||||
srand((unsigned)time(NULL));
|
||||
shuffle(deck);
|
||||
|
||||
const char *suits[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};
|
||||
const char *faces[FACES] = {"Ace", "Deuce", "Three", "Four", "Five",
|
||||
"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
|
||||
|
||||
deal(deck, suits, faces);
|
||||
fiveCard(deck, suits, faces);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void shuffle(int deck[][FACES]) {
|
||||
for(size_t card = 1; card <= CARDS; ++card) {
|
||||
size_t row, column;
|
||||
do {
|
||||
row = rand() % SUITS;
|
||||
column = rand() % FACES;
|
||||
} while(deck[row][column] != 0);
|
||||
deck[row][column] = (int)card;
|
||||
}
|
||||
}
|
||||
|
||||
void deal(int deck[][FACES], const char *suits[], const char *faces[]) {
|
||||
for(size_t card = 1; card <= CARDS; ++card) {
|
||||
for(size_t suit = 0; suit < SUITS; ++suit) {
|
||||
for(size_t face = 0; face < FACES; ++face) {
|
||||
if(deck[suit][face] == (int)card) {
|
||||
/*printf("Card: %5s of %-8s%c",
|
||||
faces[face],
|
||||
suits[suit],
|
||||
(card % 4 == 0) ? '\n' : '\t');*/
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
void fiveCard(int deck[][FACES], const char *suits[], const char *faces[]) {
|
||||
int hand[5][2] = {0};
|
||||
int count5 = 0;
|
||||
|
||||
while (count5 < 5) {
|
||||
int row = rand() % SUITS;
|
||||
int column = rand() % FACES;
|
||||
|
||||
if(hand[row][column] != 0) {
|
||||
hand[count5][0] = row;
|
||||
hand[count5][1] = column;
|
||||
printf("Card: %d: %s, %s\n", count5, faces[column], suits[row]);
|
||||
}
|
||||
deck[row][column] = 0;
|
||||
count5++;
|
||||
}
|
||||
checkHand(hand, suits, faces);
|
||||
}
|
||||
|
||||
void checkHand(hand[][2], const char *suits[], const char *faces[]) {
|
||||
int fourOfaKind = 0;
|
||||
int threeOfaKind = 0;
|
||||
int twoPairs = 0;
|
||||
int onePair = 0;
|
||||
int flush = 0;
|
||||
int straight = 0;
|
||||
int same = 0;
|
||||
|
||||
for(int i = 0; i < 5; i++) {
|
||||
int sameVal[FACES] = {0};
|
||||
int pairs = 0;
|
||||
|
||||
sameVal[hand[i][1]]++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@@ -8,6 +8,8 @@ int main(void)
|
||||
int alice = 0;
|
||||
int superAlice = 0;
|
||||
int notSuperBob = 0;
|
||||
int superBob = 0;
|
||||
int notSuperAlice = 0;
|
||||
srand(time(NULL));
|
||||
|
||||
for(int i = 0; i < 1000; ++i){
|
||||
@@ -23,16 +25,30 @@ int main(void)
|
||||
int tempDie = (die % 2) ? die - 1 : die;
|
||||
if(tempDie % 2){
|
||||
++notSuperBob;
|
||||
++superBob;
|
||||
} else {
|
||||
++superAlice;
|
||||
if(tempDie > 3) {
|
||||
++notSuperAlice;
|
||||
} else {
|
||||
++superBob;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
++notSuperBob;
|
||||
++superBob;
|
||||
++superAlice;
|
||||
}
|
||||
}
|
||||
|
||||
/* int bobtempDie = tempDie;
|
||||
if (tempDie < 4 && tempDie % 2 != 0) {
|
||||
bobtempDie = tempDie * 2;
|
||||
(bobtempDie % 2) ? ++superBob : ++notSuperAlice;
|
||||
printf("bobtempDie: %d\n", bobtempDie);
|
||||
}
|
||||
*/
|
||||
printf("Normal Alice: %d, Normal Bob: %d\n", alice, bob);
|
||||
printf("Super Alice: %d, NotSuperBob: %d\n", superAlice, notSuperBob);
|
||||
printf("Super Bob: %d, notSuperAlice: %d\n", superBob, notSuperAlice);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
46
DeitelC/Chapter7/dieP.c
Normal file
46
DeitelC/Chapter7/dieP.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void useAliceSuperPower(int *num);
|
||||
void useBobSuperPower(int *num);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int bob = 0; //Bob
|
||||
int alice = 0;
|
||||
srand(time(NULL));
|
||||
|
||||
|
||||
int die = 1 + rand() % 6;
|
||||
|
||||
printf("%d\n", die);
|
||||
puts("Die rolled! Alice super power ? (1/x)");
|
||||
scanf("%d", &alice);
|
||||
puts("Bob super power? (1/x)");
|
||||
scanf("%d", &bob);
|
||||
|
||||
if (alice == 1) useAliceSuperPower(&die);
|
||||
if (bob == 1) useBobSuperPower(&die);
|
||||
|
||||
if(die % 2 == 0) {
|
||||
printf("Winner Bob! Die: %d\n", die);
|
||||
} else {
|
||||
printf("Winner Alice! Die: %d\n", die);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void useAliceSuperPower(int *num) {
|
||||
if((*num) % 2 && (*num) != 1) {
|
||||
(*num)--;
|
||||
}
|
||||
}
|
||||
|
||||
void useBobSuperPower(int *num) {
|
||||
if((*num) < 4 && (*num) % 2 != 0) {
|
||||
(*num) = (*num) * 2;
|
||||
}
|
||||
}
|
45
DeitelC/Chapter7/test2.c
Normal file
45
DeitelC/Chapter7/test2.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void useAliceSuperPower(int *num);
|
||||
void useBobSuperPower(int *num);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int bob = 0;
|
||||
int alice = 0;
|
||||
srand(time(NULL));
|
||||
|
||||
for(int i = 0; i < 1000; ++i){
|
||||
int die = 1 + rand() % 6;
|
||||
|
||||
useAliceSuperPower(&die);
|
||||
useBobSuperPower(&die);
|
||||
|
||||
// Apply modulo 6 to keep within 1-6 range
|
||||
die = (die - 1) % 6 + 1;
|
||||
|
||||
if(die % 2) {
|
||||
++bob;
|
||||
} else {
|
||||
++alice;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Alice: %d, Bob: %d\n", alice, bob);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void useAliceSuperPower(int *num) {
|
||||
if(*num % 2 && *num > 1) {
|
||||
(*num)--;
|
||||
}
|
||||
}
|
||||
|
||||
void useBobSuperPower(int *num) {
|
||||
// More reasonable power: add 1 to even numbers
|
||||
if(*num % 2 == 0 && *num < 6) {
|
||||
(*num)++;
|
||||
}
|
||||
}
|
47
DeitelC/Chapter7/test3.c
Normal file
47
DeitelC/Chapter7/test3.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void useAliceSuperPower(int *num);
|
||||
void useBobSuperPower(int *num);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int bob = 0;
|
||||
int alice = 0;
|
||||
srand(time(NULL));
|
||||
|
||||
for(int i = 0; i < 1000; ++i){
|
||||
int die = 1 + rand() % 6;
|
||||
|
||||
useAliceSuperPower(&die);
|
||||
useBobSuperPower(&die);
|
||||
|
||||
// Keep within 1-6 range
|
||||
if(die < 1) die = 1;
|
||||
if(die > 6) die = 6;
|
||||
|
||||
if(die % 2) {
|
||||
++bob;
|
||||
} else {
|
||||
++alice;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Alice: %d, Bob: %d\n", alice, bob);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void useAliceSuperPower(int *num) {
|
||||
// Only subtract 1 if it's odd and greater than 1
|
||||
if(*num % 2 && *num > 1) {
|
||||
(*num)--;
|
||||
}
|
||||
}
|
||||
|
||||
void useBobSuperPower(int *num) {
|
||||
// HALF the power: Only add 1 (not double)
|
||||
if(*num < 6) {
|
||||
(*num)++;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user