Google Community
Latest Forums Rules Resources
Custom Search

Go Back   Google Community > The Community > Chit Chat

GoogleCommunity Sponsor

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2009, 04:32 AM   #1 (permalink)
Googler
 
Join Date: Dec 2008
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
wolfyy1 is on a distinguished road
Comp Prog 21 Game

/* Game of 21 program */
#include <iostream.h>
#include <stdlib.h>
#include "utility.h"
#include <time.h>
int DealCard();
int DealUser();
int DealComputer();
int FindWinner(int UserSum, int ComputerSum);
void ReportResult(int Result);
int PlayGame();
void UpdateCount(int Result, int &Wins, int &Losses, int &Draws);
char AskRepeat();
void Report(int Wins, int Losses, int Draws);

int main()
{
srand(time(NULL));
int Wins=0, Losses=0, Draws=0;
char Answer;
do{
int Result; //0=draw 1=user win 2=computer win
Result=PlayGame();
UpdateCount(Result, Wins, Losses, Draws);
Answer=AskRepeat();
} while((Answer!='N')&&(Answer!='n'));
Report(Wins, Losses, Draws);
cout<<Wins<<endl;
return(0);
}
//-------------------------------------------------------------------
int DealCard()
/*Returns a random card value
Post: A value from 1 to 10 returned */

{
int card=rand()%13+1;
if (card>10)
card=10;
return (card);
}
//-------------------------------------------------------------------
int DealUser()
/*Asks the user how many cards are desired, and deals them,
returning the sum
Post: Cards have been displayed, and their sum is returned */
{
char answer;
int NumCard;
int CardCount, Card, SumCards=0;
NumCard=2;
cout<<"You: ";
for(CardCount=0;CardCount<NumCard; CardCount++) {
Card=DealCard();
cout<<Card<<" | ";
SumCards+=Card; }
cout<<"="<<SumCards<<endl;

do{
cout<<"Would you like another card?"<<endl;
cin>>answer;
Card=DealCard();
SumCards=SumCards+Card;
cout<<Card<<" "<<endl;
}

while((answer=='Y')&&(answer=='y')&&(SumCards<16)) ;

cout<<"You: "<<SumCards<<endl;
return(SumCards);
}
//-------------------------------------------------------------------
int DealComputer()
/*DElas cards to the computer and returns the sum
Post: Three cards have been displayed, and their sum returned */
{
const int NumCards=3;
cout<<"Computer: ";
int CardCount, Card, SumCards=0;
for(CardCount=0; CardCount<NumCards; CardCount++) {
DealCard();
Card=DealCard();
cout<<Card<<" | ";
SumCards+=Card;
}
cout<<"="<<SumCards<<endl;
return(SumCards);
}
//-------------------------------------------------------------------
int FindWinner(int UserSum, int ComputerSum)
/*Determines winner and returns code indicating winner.
Post: Winner determined according to rules in the specification and
code returned indicating the winner: 0=Draw 1=User Won
2=Computer Won */
{
const int Limit=21;
if((UserSum==ComputerSum)||((UserSum>Limit)
&&(ComputerSum>Limit)))
return(0);
else if((ComputerSum>Limit)||((UserSum>ComputerSum)
&&(UserSum<=Limit)))
return(1);
else
return(2);
}
//-------------------------------------------------------------------
void ReportResult(int Result)
/*Reports the result of the game
Pre: Result is either 0, 1, or 2
Post: Result has been displayed. */
{
if(Result==0)
cout<<"A draw!\n";
else if(Result==1)
cout<<"You win!\n";
else
cout<<"Computer wins!\n";
}
//-------------------------------------------------------------------
int PlayGame()
/*Plays one game of 2 and returns an indication of the winner
Post: One game has been played, and a code returned indicating
the winner: 0=Draw 1=User Won 2=Computer Won */
{
int UserSum=DealUser();
int ComputerSum=DealComputer();
int Winner=FindWinner(UserSum, ComputerSum);
ReportResult(Winner);
return(Winner);
}
//-------------------------------------------------------------------
void UpdateCount(int Result, int &Wins, int &Losses, int &Draws)
/*Increments one of the counters as determined by Result
Pre: Result is either 0, 1, or 2
Post: Either Wins, Losses, or Draws has been incremented based
upon whether Result is 1, 2, or 0 respectively */
{
if(Result==0)
Draws++;
else if(Result==1)
Wins++;
else
Losses++;
}
//-------------------------------------------------------------------
char AskRepeat()
/*Asks the user if another game is desired and returns response
Pre: none
Post: User has been asked and has responded with Y, y, N, or n.
The response is returned. */
{
char Answer;
cout<<"Would you like to play again?(Y/N)?";
cin>>Answer;
while((Answer!='Y')&&(Answer!='y')&&(Answer!='N')
&&(Answer!='n')){
cout<<"Answer Y or N please:";
cin>>Answer;
}
return(Answer);
}
//-------------------------------------------------------------------
void Report(int Wins, int Losses, int Draws)
/*Reports the current results
Pre: Wins, Losses, Draws represented the current results
Post: The current results have been displayed */
{
cout<<"Your wins= "<<Wins<<endl;
cout<<"Computer wins= "<<Losses<<endl;
cout<<"Draws= "<<Draws<<endl;
}
//-------------------------------------------------------------------
wolfyy1 is offline   Reply With Quote
 
Sponsored Links
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
cpt prog 1 wolfyy1 Chit Chat 0 12-12-2008 04:45 AM
computer prog. help wolfyy1 Chit Chat 1 12-11-2008 04:52 AM
how do u rate this webhosting comp.? stream Web Hosting Forum 2 10-25-2007 08:33 AM
Prog Fans eljefe Music & Movies Forum 2 02-05-2006 01:46 AM


All times are GMT -8. The time now is 09:43 AM.


Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© 2004–2007 Google Community