Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 24, 2007, 09:18 PM   #1
chris200x9
macrumors 6502a
 
Join Date: Jun 2006
rand c++

Code:
 #include <iostream>
#include <ctime>

using namespace std;

enum Move
{
	rock,
	paper,
	scissors,
};

enum Outcome
{
	win,
	lose,
	draw
};

Move RandomMove()
{
	switch( rand() % 3 ) {
		case 0:
			return rock;
			break;
		case 1:
			return paper;
			break;
		default:
			break;
	} // end switch

	return scissors;
}

Move SelectMove( Move yourPrevMove, Move oppPrevMove )
{
	Move m;
	

	m = paper;
	
	return m;
}

/*
	Returns the results of a vs. b in terms of player a.
	Thus, either a wins, loses, or draws with player b.
*/
Outcome DetermineOutcome( const Move &a, const Move &b )
{
	if( a == rock ) {
		if( b == rock )
			return draw;
		else if( b == paper )
			return lose;
		// player b played scissors
		return win;
	}
	else if( a == paper ) {
		if( b == rock )
			return win;
		else if( b == paper )
			return draw;
		// player b played scissors
		return lose;
	}
	// a == scissors;
	if( b == rock )
		return lose;
	else if( b == paper )
		return win;
	// player b played scissors
	return draw;
}

int main()
{
	int rounds, winsA, winsB;
	Move moveA, moveB;

	// seed generator
	srand( (unsigned int) time(NULL) );

	// initialize variables
	winsA = 0;
	winsB = 0;

	// 100 rounds of rock/paper/scissors
	moveA = moveB = rock;
	for( rounds = 0; rounds < 1000; rounds++ ) {
		moveA = SelectMove( moveA, moveB );
		moveB = RandomMove();

		// who won?
		switch( DetermineOutcome( moveA, moveB ) ) {
			case win:
				winsA++;
				break;
			case lose:
				winsB++;
				break;
			case draw:
			default:
				break;
		} // end switch		
	} // end for

	// display summary
	cout << endl;
	cout << "Player 1 won " << winsA << " rounds (";
	cout << (winsA * 100) / rounds << "%)." << endl;

	cout << "Player 2 won " << winsB << " rounds (";
	cout << (winsB * 100) / rounds << "%)." << endl;

	cout << "There were " << rounds - winsA - winsB << " ties (";
	cout << ((rounds - winsA - winsB) * 100) / rounds << ")." << endl;
	cout << endl;
	
	system( "pause" );
	return 0;
}
why do I keep getting the EXACT same output?
chris200x9 is offline   0 Reply With Quote
Old Oct 24, 2007, 09:20 PM   #2
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: The Bamboo Forest
I didn't read your code and I'm not a C++ guru but you probably need to "seed" the random number with the time... otherwise your random numbers will be the same.
__________________
My 24 hour web cam!
SilentPanda is offline   0 Reply With Quote
Old Oct 24, 2007, 09:44 PM   #3
ChrisBrightwell
macrumors 68020
 
ChrisBrightwell's Avatar
 
Join Date: Apr 2004
Location: Huntsville, AL
Yeah, you need to seed the RNG.

Code:
srand ( time(NULL) );
__________________
"Simplicity is the ultimate sophistication."
ChrisBrightwell is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Rand Paul Not A Board Certified Doctor? XnavxeMiyyep Politics, Religion, Social Issues 13 Jun 17, 2010 04:53 AM
Sarah Palin on BP and Rand Paul macfan881 Politics, Religion, Social Issues 1 May 24, 2010 07:01 PM
What is Objectivism (as in Ayn Rand?) majordude Community Discussion 25 Jul 13, 2008 07:28 PM
Jobs on Rand iGav Design and Graphics 1 Mar 26, 2008 07:15 PM
Ayn Rand. princealfie Community Discussion 28 Jul 6, 2007 11:41 AM


All times are GMT -5. The time now is 11:35 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC