c ++ game help (1 Viewer)

Rahul

Dead Member
Joined
Dec 14, 2002
Messages
3,647
Location
shadowy shadows
i am making a game in c++ and i am having problems wid a small module.

the game is going to have 2 players, and they both "roll" a dice. whoever gets the highest number goes first.

now how would i make that player go first....i am having 2 records (user and comp) which store where they are and how much money they have.

any ideas....(please let me know if this doesnt make any sense, i'll try explaining it better then)

thanks.
 

tieki

Member
Joined
Feb 17, 2003
Messages
240
Location
"The 'Wong..." :-)
U have to seed the rand() function with the time (at least, but I've seen MUCH more complicated ones).

Try using this code for the random function, and it should be different everytime you run it:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int RandomNumber;

srand(time(NULL));

// For random numbers between 1 and 10:
RandomNumber = (rand() % 10) + 1

The plus one just makes it start at one, not zero - also make the other value (the ten) one more than your maximum desired value. For a dice, set it to:

RandomNumber = (rand() % 6) + 1

What I would do here, would just be to create two random numbers, compare them, and if player 1 is higher, set a Boolean variable to true, otherwise leave it at false. Then, depending on the value of the Boolean, go to the appropriate function from there.

Hope this helps :)
 

tieki

Member
Joined
Feb 17, 2003
Messages
240
Location
"The 'Wong..." :-)
Nope...got it off the MSDN stuff that came with C++...just look up the rand() function, and the example includes it

There's also a whole bunch of other ways, to create "truly" random numbers that I found on the net, but all of them used masses of code, whereas you only need two lines for this...

:)
 

tieki

Member
Joined
Feb 17, 2003
Messages
240
Location
"The 'Wong..." :-)
Yeah, it's pretty much the standard way of creating random numbers...without the srand() , the random numbers are random, but they always start from the same value, which isn't always good :)
 

tieki

Member
Joined
Feb 17, 2003
Messages
240
Location
"The 'Wong..." :-)
Cyph, you might be able to help me with this one...I have 5 random numbers, and need an algorithm to make sure none of them are repeated - if any are repeated, it replaces one of them with a new random number until none are repeated. Any ideas? I can't seem to make it work...
 

Rahul

Dead Member
Joined
Dec 14, 2002
Messages
3,647
Location
shadowy shadows
i dont think i explained myself well...i'll try again!

here is wat i get up to,
-i get the 2 random numbers for the 2 players
-i decide which one has to go first
-??? how do i 'start' with that player (which is stored as a record)

*'_user' and '_comp' are my two random numbers for the user and computer players
*'player1' is the record i start with as player one.....the game module has 'player1' and 'player2' as its players , if that makes any sense.
*'user' and 'comp' are the records

if (_user > _comp)
{ user = player1}
if (_user < _comp)
{ comp = player1}
....

i might be making a mistake in the structure of my solution. i am sure there is a way of doing wat i am trying to do.

i have hit a mental block with this problem.....just not thinking straight :mad1:
 
Last edited:

Rahul

Dead Member
Joined
Dec 14, 2002
Messages
3,647
Location
shadowy shadows
how about:

userRecord, compRecord, userRandom, compRandom

if (userRandom > compRandom)
{
userRecord = player1
compRecord = player2
}

if (userRandom < compRandom)
{
userRecord = player2
compRecord = player1
}

else { *run the random number generator module for userRandom and compRandom* }

i have been trying similar sort of thing, but how will i start the game with player1. will player1 be a record or what??!?! what about a pointer, could *player1 be used to point to the 'userRecord'/'compRecord' record?
 

tieki

Member
Joined
Feb 17, 2003
Messages
240
Location
"The 'Wong..." :-)
Then, just use a case structure...

switch(userRecord) // I think this line is right!
case player1:
{
// Arguments for player 1 going first here
}
case player2:
{
// Arguments for player 2 going first here
}
default: // Figure something out in case it drops through, for some reason

I think this is what you mean...the most efficient way would be to pass the variables to the function, as Cyph said earlier...

In the meantime, I already tried that in my program Cyph, problem is, for example, if array[3] and array[4] are the same, it puts a new number in array[3], but there's no checks to see if that number is the same as, say, array[1]... In that case, should I just use another check on the same value until its not the same?

Also, could I code the elimination of checking itself as:

for (count = 0; count < 6; count++; count != the array index)

...?
 

Lazarus

Retired
Joined
Jul 6, 2002
Messages
5,965
Location
CBD
Gender
Male
HSC
2001
Originally posted by Rahul
i have been trying similar sort of thing, but how will i start the game with player1. will player1 be a record or what??!?! what about a pointer, could *player1 be used to point to the 'userRecord'/'compRecord' record?
I don't see what the problem is - you'll need to elaborate.

Surely you would simply use a global variable called 'currentPlayer' (or even just 'turn') to act as either a boolean or to store a pointer to the player itself.

Originally posted by tieki
Then, just use a case structure...
Case structures and switch statements are redundant when you're only making a binary selection - stick to if/else statements unless you have more than two alternatives.
 

-=«MÄLÅÇhïtÊ»=-

Gender: MALE!!!
Joined
Jul 25, 2002
Messages
1,678
Location
On Top
Gender
Male
HSC
2002
hey how'd uno we're doing c++?


there should be plenty of dice classes already made. so there prolly is one online somewhere.

im not quite sure how complex ur game is. When you say whoever wins goes first, do u mean whoever wins plays your game(something else) first? or do you mean gets roll the dice again first? I'm assuming its the first, because the latter's useless. Coz it doesnt really matter who roles first.

int pRoll; // what you roll
int cRoll; // what comp rolls
bool yourTurn = true;
pRoll = int(rand() * 6) + 1; // btw ive never used rand. but if it's
cRoll = int(rand() * 6) + 1; // like excel, then that's how u'd do it
if (pRoll < cRoll)
yourTurn = false;

if (yourTurn)
{
// your game function/class
}
else
{
// your game function/class
}


i introed a bool variable juz to make it easier if ur gonna make it into a function

i dun fink i've helped you. but if ur game's complex, u should be using a class. And perhaps even use arrays to have optional number of players.
 
Last edited:

Rahul

Dead Member
Joined
Dec 14, 2002
Messages
3,647
Location
shadowy shadows
Code:
#include <iostream.h>

using namespace std;

int userRandom;
int compRandom;

void gamestart();
int random();


struct recordTemplate
{
	char person; //this variabel sets whether the player is comp/user 
	int money;
	char place;
};

recordTemplate player1, player2;


void main()
{
random();
	if (userRandom > compRandom)
	{	
		player1.person = 'User';
		player2.person = 'Computer';
	}
	if (userRandom < compRandom)//this if function is ignored
	{
		player1.person = 'Computer';
		player2.person = 'User';
	}
gamestart();
}

int random()
{
userRandom=5;
compRandom=2;
}

void gamestart()
{
cout << player1.person << " will be starting.\n";
}
GOT IT!!!

thanks for trying to answer my problem.....i know i didnt explain it well


jus one problem, the statement printed is " r will be goiing first"
only the last character of 'User' is selected. what type should 'player1.person' be?
 

tieki

Member
Joined
Feb 17, 2003
Messages
240
Location
"The 'Wong..." :-)
I eventually got it working, but my code is horrendously long...I've been trying to figure out how to shorten it using a loop or something...here it is:

Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

int main()
{
	int Random[5];
	int count = 0;
	int checkCount = 0;
	
	srand(time(NULL));

	for (count = 0; count < 5; count++)
	{
		Random[count] = 0;
	}

	Random[0] = ((rand() % 52) + 1);

	Random[1] = ((rand() % 52) + 1);
	while (Random[1] == Random[0])
	{
		Random[1] = ((rand() % 52) + 1);
	}

	Random[2] = ((rand() % 52) + 1);
	while (Random[2] == Random[1])
	{
		Random[2] = ((rand() % 52) + 1);
	}
	while (Random[2] == Random[0])
	{
		Random[2] = ((rand() % 52) + 1);
	}

	Random[3] = ((rand() % 52) + 1);
	while (Random[3] == Random[2])
	{
		Random[3] = ((rand() % 52) + 1);
	}
	while (Random[3] == Random[1])
	{
		Random[3] = ((rand() % 52) + 1);
	}
	while (Random[3] == Random[0])
	{
		Random[3] = ((rand() % 52) + 1);
	}

	Random[4] = ((rand() % 52) + 1);
	while (Random[4] == Random[3])
	{
		Random[4] = ((rand() % 52) + 1);
	}
	while (Random[4] == Random[2])
	{
		Random[4] = ((rand() % 52) + 1);
	}
	while (Random[4] == Random[1])
	{
		Random[4] = ((rand() % 52) + 1);
	}
	while (Random[4] == Random[0])
	{
		Random[4] = ((rand() % 52) + 1);
	}

	return 0;
}
I need some or statements in there or something, just to save putting each test individually...I could try just using a counted loop to count for the array index for each one, that should shorten the code a bit...I'll give it a try in class today

Thanks :)
 

tieki

Member
Joined
Feb 17, 2003
Messages
240
Location
"The 'Wong..." :-)
Fixed it with a loop, but C++ isn't compiling or running on the computers here at school, so I can't test the code 'til I get home...here it is, though:

Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

int main()
{
	int Random[5];
	int count = 0;
	int checkCount = 0;
	
	srand(time(NULL));

	Random[0] = ((rand() % 52) + 1);

	Random[1] = ((rand() % 52) + 1);
	while (Random[1] == Random[0])
	{
		Random[1] = ((rand() % 52) + 1);
	}

	Random[2] = ((rand() % 52) + 1);
	for (count = 0; count < 2; count++)
	{
		while (Random[2] == Random[count])
		{
			Random[2] = ((rand() % 52) + 1);
		}
	}

	Random[3] = ((rand() % 52) + 1);
	for (count = 0; count < 3; count++)
	{
		while (Random[3] == Random[count])
		{
			Random[3] = ((rand() % 52) + 1);
		}
	}

	Random[4] = ((rand() % 52) + 1);
	for (count = 0; count < 4; count++)
	{
		while (Random[4] == Random[count])
		{
			Random[4] = ((rand() % 52) + 1);
		}
	}

	return 0;
}
:)
 

Lazarus

Retired
Joined
Jul 6, 2002
Messages
5,965
Location
CBD
Gender
Male
HSC
2001
At the very least, you could subsume the last portion of your code into another 'for' loop. :)

Code:
#include <stdlib.h>
#include <time.h>
using namespace std;

const int MAX_SIZE = 5;
const int MAX_RAND = 52;

int main()
{
	int Random[MAX_SIZE];
	int i = 0, count = 0;
	int checkCount = 0;

	srand(time(NULL));

	Random[0] = ((rand() % MAX_RAND) + 1);

	for (i = 1; i < SIZE; i++)
	{
		Random[i] = ((rand() % MAX_RAND) + 1);

		for (count = 0; count < i; count++)
		{
			while (Random[i] == Random[count])
			{
				Random[i] = ((rand() % MAX_RAND) + 1);
			}
		}

	}

	return 0;
}
Note that this code doesn't actually guarantee that each element of the array will be a unique random number. It's highly likely. But not guaranteed. Can you tell me why? :)
 

tieki

Member
Joined
Feb 17, 2003
Messages
240
Location
"The 'Wong..." :-)
Does it have anything to do with the array being declared as int? Or maybe with the seeding with the time...?

Thanks for the help with the code, though...it's working nicely :)
 

Lazarus

Retired
Joined
Jul 6, 2002
Messages
5,965
Location
CBD
Gender
Male
HSC
2001
Originally posted by tieki
Does it have anything to do with the array being declared as int? Or maybe with the seeding with the time...?
Nope! It's just a small logic error. :) Try tracing your program and see if at any stage there is an opportunity for a duplicate element to be added.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top