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

Similar Threads
thread Thread Starter Forum Replies Last Post
Rand Paul Is Filing a Class-Action Lawsuit Against the NSA MacNut Politics, Religion, Social Issues 51 Feb 13, 2014 05:01 PM
Rand Paul stands tough against drones 63dot Politics, Religion, Social Issues 78 Mar 15, 2013 08:30 AM
Ayn Rand and Libertarianism eric/ Politics, Religion, Social Issues 46 Feb 11, 2013 03:00 AM

Forum Jump

All times are GMT -5. The time now is 10:02 PM.

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

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