Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Bradley Felix, bfelix2019@my.fit.edu
- Course: CSE 1001, Section E3
- Filename: war.c
- */
- #include <stdio.h>
- #include <math.h>
- #include <time.h>
- #include <stdlib.h>
- void shuffle(int *, int); //Used to shuffle the deck
- void deal_hand(int *, int *, int *);
- int check_hand(int, int);
- void card_declare(int *, int);
- void victory_royale(int *, int *, int , int);
- void loser_royale(int *, int *, int, int);
- void victory_royale_2(int *, int *, int, int);
- void shrink_growth(int *, int *, int, int, int, int);
- int main(void)
- {
- int war_deck[52]={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};
- int j = 26;
- int p1_growth=0, p2_growth=0;
- int p1_shrink=0, p2_shrink=0;
- int p1_hand[104],p2_hand[104];
- char p_e; // Play or exit
- int p1_hand_length = sizeof(p1_hand) / sizeof(p1_hand[0]);
- int p2_hand_length = sizeof(p2_hand) / sizeof(p2_hand[0]);
- printf("Welcome to War!\n");
- printf("Shuffling the deck...\n");
- shuffle(war_deck, 52);
- for (int i = 0; i < 52; i++){ // Printing out the war deck to make sure it shuffles correctly <Will remove after>
- printf("%d ", war_deck[i]);
- }
- printf("\nDealing out the cards...\n");
- deal_hand(war_deck, p1_hand, p2_hand); // Printing out the delt cards <Will remove after>
- printf("\nWould you like to play(p) or exit(e)?: ");
- scanf(" %c", &p_e);
- for(int i=0;i<52;i++){
- printf("i incriment: %d\n", i);
- printf("Player 1 hand: %d\n", p1_hand[i]);
- printf("Player 2 hand: %d\n", p2_hand[j]); // Using j because p2_hand starts on the 26th value
- if (p_e=='e' || p_e=='E'){
- if(p1_hand>0 && p2_hand>0){
- printf("The game is a tie!\n");
- }
- printf("Goodbye! Thanks for playing War!");
- break;
- }
- if (p_e=='p' || p_e=='P'){ // If the player chooses to play
- printf("Player 1's Card: ");
- card_declare(p1_hand, i);
- printf("Player 2's Card: ");
- card_declare(p2_hand, j);
- if(p1_hand[i] > p2_hand[j]){ // If player one wins the battle
- printf("Player 1 wins the round!\n");
- printf("Awarding played cards to Player 1...\n");
- victory_royale(p1_hand, p2_hand, i, j);
- p1_growth++;
- p2_growth++;
- shrink_growth(p1_hand, p2_hand, i, j, p1_hand_length, p2_hand_length);
- for (int i = 0; i < 26; i++){
- printf("%d ", p1_hand[i+p1_growth]); // Player one hand prints out their cards with the card they won
- }
- printf("\n\n");
- for (int i = 26; i < 52 ; i++){
- printf("%d ", p2_hand[i+p2_growth]); // Player two hand prints out their cards with the card they won
- }
- printf("\n");
- } else if(p2_hand[j] > p1_hand[p1_growth]){ // If player two wins the battle
- printf("Player 2 wins the round!\n");
- printf("Awarding played cards to Player 2...\n");
- victory_royale_2(p2_hand, p1_hand, i, j);
- p1_growth++;
- p2_growth++;
- shrink_growth(p2_hand, p1_hand, j, i, p2_hand_length, p1_hand_length);
- for (int i = 0; i < 26; i++){
- printf("%d ", p1_hand[i+p1_growth]);
- }
- printf("\n\n");
- for (int i = 26; i < 52 ; i++){
- printf("%d ", p2_hand[i+p2_growth]);
- }
- printf("\n");
- } else if(p1_hand[i] == p2_hand[j]){
- printf("The cards are equal! Delcaring WAR!\n");
- printf("Drawing the top 3 cards from both players decks for battle...\n");
- }
- }
- printf("\nWould you like to play(p) or exit(e)?: ");
- scanf(" %c", &p_e);
- j++; // Using to incriment instead of i so p2_hand incriments at the same time with p1_hand
- }
- }
- void victory_royale(int *winner, int *loser, int increment, int increment_2){
- winner[increment+26] = winner[increment]; // Hands the winner back their played card first
- winner[increment+27] = loser[increment_2]; // Hands the winner the card they got from the loser
- loser[increment_2+26] = loser[increment_2]; // Loser's card gets sent to the back of their deck <WIP>
- }
- void victory_royale_2(int *winner, int *loser, int increment, int increment_2 ){
- winner[increment+52] = winner[increment]; // Hands the winner back their played card first
- winner[increment+53] = loser[increment_2]; // Hands the winner the card they got from the loser
- loser[increment] = loser[increment+26]; // Loser's card gets sent to the back of their deck <WIP>
- }
- void shrink_growth(int *winner, int *loser, int increment, int increment_2, int len, int len2){ // <WIP> Deletes the last card they played by reducing the array size
- loser[increment] = loser[increment-1];
- }
- void card_declare(int *p_hand, int increment){
- char suit[4]={'C','S','D','H'};
- char c_value[4]={'J','Q','K','A'};
- char card_suite;
- char card_value_face;
- int card_value_num;
- // Declaring the card suite
- if(p_hand[increment]<13)
- {
- card_suite=suit[1];
- } else
- if(p_hand[increment]>=13 && p_hand[increment]<26)
- {
- card_suite=suit[2];
- } else
- if(p_hand[increment]>=26 && p_hand[increment]<39)
- {
- card_suite=suit[3];
- } else
- if(p_hand[increment]>=39 && p_hand[increment]<52)
- {
- card_suite=suit[0];
- }
- // Declaring the card value
- if(p_hand[increment]%13<=8)
- {
- card_value_num=(p_hand[increment]%13)+2;
- printf("%d", card_value_num);
- } else
- if(p_hand[increment]%13==9)
- {
- card_value_face=c_value[0];
- printf("%c", card_value_face);
- } else
- if(p_hand[increment]%13==10)
- {
- card_value_face=c_value[1];
- printf("%c", card_value_face);
- } else
- if(p_hand[increment]%13==11)
- {
- card_value_face=c_value[2];
- printf("%c", card_value_face);
- } else
- if(p_hand[increment]%13==12)
- {
- card_value_face=c_value[3];
- printf("%c", card_value_face);
- }
- printf("%c",card_suite);
- printf("\n");
- }
- void deal_hand(int *in_deck, int *hand1, int *hand2){
- int dealt,dealt2;
- // Splitting the deck into two seperate loops, the loops represent the hands
- for (int i=0;i<26;i++)
- {
- dealt=in_deck[i];
- hand1[i]=dealt;
- printf("%d ", hand1[i]);
- }
- printf("\n\n");
- for (int i=26;i<52;i++)
- {
- dealt2=in_deck[i];
- hand2[i]=dealt2;
- printf("%d ", hand2[i]);
- }
- }
- void shuffle(int *in_deck, int n){
- srand((unsigned) time(NULL));
- if (n > 1) {
- int i, k, t;
- for (i = 0; i < (n - 1); i++) {
- k = i + rand() / (RAND_MAX / (n - i) + 1);
- t = in_deck[k];
- in_deck[k] = in_deck[i];
- in_deck[i] = t;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement