C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #31  
Old November 15th, 2008, 10:41 AM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
Question

Is there anyway to write this program without the getch statemant and just use a cin ... because we haven't actually gone through that chapter in the book yet ...?

Reply With Quote
  #32  
Old November 15th, 2008, 11:04 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
it's a lot nicer with getch, slicker, tried it just now with cin and it's not quite as good, if people hit keys you are not expecting.


you can just say you got "getch" off an internet c++ FAQ you found in google. this is a good one to quote :


http://www.cpp-home.com/index/tiki-view_faq.php?faqId=3

I also i recommend you read most of this for tips. it's got a bit about 'getch' in it

Oh well, but here's and "un-getched" version too. i simplified some other obscure stuff too

Code:
#include <iostream>
#include <ctime>
using namespace std;
/////////////////////////////////////////////////////////////////
// Program Data
/////////////////////////////////////////////////////////////////

// Uncomment these two data lines to get "normal" rock paper scissors

//char *moves[4] = {"View Instructions", "Rock", "Paper", "Scissors"}; // names of the choices
//char *effects[4] = {"", "Blunts", "Wraps", "Cuts"}; // names of the effects

// Just for fun ... ANIMAL rock paper scissors

char *moves[4] = {"View Instructions", "Mouse", "Cat", "Elephant"}; // names of the choices
char *effects[4] = {"", "Scares", "Eats", "Stomps"}; // names of the effects

int results[3]; // to count wins, losses and draws

const int resultDraw = 0;
const int resultLose = 1;
const int resultWin = 2;

/////////////////////////////////////////////////////////////////
// Function Prototypes
/////////////////////////////////////////////////////////////////
bool getYesNo(); 
int getUserPick();
int getCompPick();
int decideWinner(int userPick, int comPick);
void outputResult(int userPick, int comPick, int outcome);

int beats(int i) // returns the choice which beats choice 'i'
{
	if(i == 3) 
		return 1;
	else
		return i + 1;
}
/////////////////////////////////////////////////////////////////
// MAIN
/////////////////////////////////////////////////////////////////
int main ()
{
	bool ok;

	do
	{
		system("cls");

		cout << "Welcome to " << moves[1] << ", " << moves[2] << ", "  << moves[3] << ".\n\n";
		cout << "By Blackout22\n\n";

		cout << "Score : Wins " << results[resultWin] << " Losses " << results[resultLose] << " Draws " << results[resultDraw] << "\n\n";

		cout<< "What do you want to choose?\n";

		for(int i= 0; i < 4; ++i) 
			cout << "\n " << i << ". " << moves[i];

		int userPick = getUserPick();

		if(userPick) // play game
		{
			int compPick = getCompPick();

			int outcome = decideWinner(userPick, compPick);

			results[outcome]++;

			outputResult(userPick, compPick, outcome);
		}
		else // show rules
		{
			system("cls");

			cout << moves[1] << ", " << moves[2] << ", "  << moves[3] << ".\n\n";
			cout << "Try and beat the computers choice.\n\n\n";

			for(int i = 1; i < 4; ++i)
				cout << moves[beats(i)] << " " << effects[beats(i)] << " " << moves[i] << ".\n\n\n";
		}

		cout << "\nDo you want to keep playing ? (y/n)";
		ok = getYesNo();
	}
	while (ok);

	return 0;
}
/////////////////////////////////////////////////////////////////
// Yes No Selector Function
/////////////////////////////////////////////////////////////////
bool getYesNo() // Tested Y/N key getter
{
	char resp = 0;

	do
	{
		cin >> resp;
		resp = toupper(resp); 
	}
	while (resp != 'N' && resp != 'Y');

	if(resp == 'Y')
		return true;
	else
		return false;
}
/////////////////////////////////////////////////////////////////
// User Move Function
/////////////////////////////////////////////////////////////////
int getUserPick()
{
	char resp;

	cout << "\n\n Enter you choice (0 - 3): ";

	do // get used to this sort of loop, you will end up relying on them
	{
		cin >> resp;
	}
	while( resp < '0' || resp > '3'); // end userpick loop

	return resp - '0'; // convert ASCII code to actual number
}
/////////////////////////////////////////////////////////////////
// Game Decision Function
/////////////////////////////////////////////////////////////////
int getCompPick()
{
	srand (time(0));	
	return rand () % 3 + 1;
}
int decideWinner(int userPick, int compPick)
{
	if(compPick == userPick) 
	{
		return resultDraw;
	}
	else if (userPick == beats(compPick)) 
	{
		return resultWin;
	}
	else return resultLose; 	
}
/////////////////////////////////////////////////////////////////
// Game Result Output Function
/////////////////////////////////////////////////////////////////
void outputResult(int userPick, int compPick, int outcome)
{
	cout <<"\n\nThe computer picked " << moves[compPick] << ", ";
	cout <<"and you picked " << moves[userPick] << ".\n\n";

	switch(outcome)
	{
		case resultLose: cout << moves[compPick] << " " << effects[compPick] << " " << moves[userPick] << " So You Lose!\n";break;
		case resultDraw: cout << "They're The Same ! So It's A Draw!\n";	break;
		case resultWin: cout << moves[userPick] << " " << effects[userPick] << " " << moves[compPick] << " So You Win!\n"; break;
	}
}

Reply With Quote
  #33  
Old November 15th, 2008, 12:02 PM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
Smile

I just wanted to take a moment to thank you for all the help you've given me!

Reply With Quote
  #34  
Old November 16th, 2008, 02:49 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
Hey no worries i enjoyed working on that program

btw i fixed a slight typo in the final version of decideWinner

else resultLose;

should have said :

else return resultLose;

i edited the prev code post with the correct code

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Need Help with functions please


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap