Вы находитесь на странице: 1из 9

// lab02.c : Defines the entry point for the console application.

#include "myfunctions.h"

int main(void)

{
PrintName();

// Prints student data to the screen

DealCards();
//Pause();
DiceGame();
return;

}
//myfunctions.h
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H

// Constants
#define NUM_SUITS 4
#define NUM_RANKS 13
#define TRIALS 99999.0
// Function Prototypes
void PrintName(void);
void Pause(void);
void DealCards(void);

int GetRandom(int range);

void PlayGame(void);
void DiceGame(void);
void TestDice(void);

#endif

//myfunctions.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "myfunctions.h"
//Task2
void DealCards(void)
{
int in_hand[NUM_SUITS][NUM_RANKS] = {0};
int num_cards, rank, suit;
const char rank_code[NUM_RANKS] = {'2','3','4','5','6','7','8',
'9','t','j','q','k','a'};
const char suit_code[NUM_SUITS] = {'c','d','h','s'};

srand((unsigned) time(NULL));

printf("\nDeal a hand of cards");


printf("\nHow many cards? ");

scanf("%d", &num_cards);
printf("\nYour hand:");
while (num_cards > 0)
{
suit = GetRandom(NUM_SUITS);
rank = GetRandom(NUM_RANKS);

/* picks a random suit */


/* picks a random rank */

if (!in_hand[suit][rank])
{
in_hand[suit][rank] = 1;
num_cards--;
printf(" %c%c", rank_code[rank], suit_code[suit]);
}
}
printf("\n");
}

//Task3
void DiceGame(void)
{
char option;
srand((unsigned) time(NULL));
while (1)
{
printf("\nDice Games");
printf("\nEnter 1 to play");

printf("\nEnter 2 to test dice");


printf("\nAnything else to exit\n");
scanf(" %c", &option);

switch(option)
{
case'1':
PlayGame();
break;
case'2':
TestDice();
break;
default:
return;
}
}

void PlayGame(void)
{
int i = 0;
char choice;
printf("\nRoll two dice\nEnter r to roll, x to exit\n");
while (i == 0)

{
scanf(" %c",&choice);
if ((choice == 'r')||(choice == 'R'))
{
printf("%d and %d were rolled\n",GetRandom(6)+1,
GetRandom(6)+1);

}
else if ((choice == 'x')||(choice =='X'))
{
return;
}
else{

}
}
}

void TestDice(void)
{
int i = 1;
int temp = 0;
float rolledArray[6] = {0,0,0,0,0,0};
for (i = 1; i <=TRIALS; i++){
temp = GetRandom(6) +1;

if (temp==1 )

{
rolledArray[0]= rolledArray[0] +1;
}
else if (temp == 2)
{
rolledArray[1] = rolledArray[1]+1;
}
else if (temp == 3)
{
rolledArray[2] = rolledArray[2]+1;
}
else if (temp == 4)
{
rolledArray[3] = rolledArray[3]+1;
}
else if (temp == 5)
{
rolledArray[4] = rolledArray[4]+1;
}
else if (temp == 6)
{
rolledArray[5] = rolledArray[5]+1;
}

}
printf("\nAfter %.0lf rolls\nA 1 was rolled %.0lf (%.4lf%%) ",
TRIALS,rolledArray[0],100*rolledArray[0]/TRIALS );

printf("\nA 2 was rolled %.0lf (%.4lf%%)


",rolledArray[1],100*rolledArray[1]/TRIALS);
printf("\nA 3 was rolled %.0lf (%.4lf%%)
",rolledArray[2],100*rolledArray[2]/TRIALS);
printf("\nA 4 was rolled %.0lf (%.4lf%%)
",rolledArray[3],100*rolledArray[3]/TRIALS );
printf("\nA 5 was rolled %.0lf (%.4lf%%)
",rolledArray[4],100*rolledArray[4]/TRIALS);
printf("\nA 6 was rolled %.0lf (%.4lf%%)\n
",rolledArray[5],100*rolledArray[5]/TRIALS);

// Returns a random number in the range of 1-range


int GetRandom(int range)
{
return rand() % range;
}

void PrintName()
{
printf("\n Hello my name is:\ \n");
printf("\n My student number is: n");

void Pause(void)
{
fprintf(stdout,"\n\nPress the ENTER key to continue...\n");
rewind(stdin);
getc(stdin);
}

Вам также может понравиться