Академический Документы
Профессиональный Документы
Культура Документы
#include "myfunctions.h"
int main(void)
{
PrintName();
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);
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));
scanf("%d", &num_cards);
printf("\nYour hand:");
while (num_cards > 0)
{
suit = GetRandom(NUM_SUITS);
rank = GetRandom(NUM_RANKS);
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");
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 );
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);
}