[Assignement] Dice game

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[Assignement] Dice game

Post by ayu »

Well, I guess every programmer has had his or her share of the dice game ^^

Got an assignment today that's due monday, just finished it and thought id post it =)

Since the code might be hard to read on the boards, I also posted it in the codebin HERE

Code: Select all

/*
Don't like writing my applications in Swedish, 
hope you don't mind it in English.
*/



#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
	/*
	definition of variables....

	throws: how many throws each player gets.
	pleader: stands for "points leader" and is set to the leading players dice sum.
	sum: is the sum of the dices after every players turn.
	player: is the current players name.
	nleader: stands for "name leader" and is the name of the leading player
	*/
	int throws, pleader, sum;
	string player, nleader;

	/*Pet seed for the rand function*/
	srand((unsigned)time(NULL));

	/*Present the welcome text for the game, as well as some instructions if needed*/
	cout << "********************************************\n"
		 << "*  Welcome to the multiplayer dice game    *\n"
		 << "*  To stop inserting players you can write *\n"
		 << "*  \"none\" as a player name                 *\n"
		 << "********************************************\n";          

	/*Prompt user to insert number of dice throws for each player*/
	cout << "How many dice throws per player?:";
	cin >> throws;

	/*Set cin to ignore the escape character that is still in the buffer from inserting throws
	  since cin doesn't handle those...*/
	cin.ignore();

	/*Start the game loop*/
	for(int i=1;; i++)
	{
		/*Prompt for player "i"'s name*/
		cout << "\nPlayer [" << i << "] name:";
		getline(cin, player);

		/*(Re)Set sum to 0, else it will add dices from all players after every turn*/
		sum=0;

		/*Prevent player name from being to long or to short (empty)*/
		if(player.length() > 15 || player.length() < 1)
		{
			cout << "\n\nError: Player name is incorrect, game closing\n\n";
			/*Exit if...*/
			exit(1);
		}
		/*If player name is none, end game*/
		else if(player.compare("none") == 0)
			break;

		/*Start player "i"'s throws*/
		cout << "The dice throws were: ";

		/*Throw dice 'throws' times, present numbers and add to sum*/
		for(int n=0, dice; n<throws; n++)
		{
			dice = rand() % 6 + 1;
			cout << dice << " ";
			sum += dice;
		}

		/*Print sum of (all fears?) dices */
		cout << " which sums up to " << sum << "\n";

		/*If the first player is throwing, set him/her as leader*/
		if(i == 1)
		{
			nleader = player;
			pleader = sum;
		}
		/*If current thrower's sum is higher then leader, set new leader*/
		else if(pleader < sum)
		{
			nleader = player;
			pleader = sum;
		}


	}

	/*When/if out of throw dice loop, present winner with score, and end game*/
	cout << "\n\nThe winner is " << nleader << " with the dice sum of " << pleader << "\n\n";
	cout << "***********\n"
	     << "*GAME OVER*\n"
		 << "***********\n";

	return 0;


}
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Post by bad_brain »

tehee....memories....^^ I had to code the same thing in school (in the 80's) on an Amstrad CPC, of course not in C++, back in the days Basic was THE language...^^

have you done the 2nd classic too: the lottery program that picks 6 random numbers out of 49 or similar? :)

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

bad_brain wrote:have you done the 2nd classic too: the lottery program that picks 6 random numbers out of 49 or similar? :)
Yeah we had an assignment like that, but it was optional, so I skipped it. Me and a friend discuss most of the assignments instead of writing them, until we come to the more juicy ones.
"The best place to hide a tree, is in a forest"

G-Brain
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 467
Joined: 08 Nov 2007, 17:00
16
Location: NL

Re: [Assignement] Dice game

Post by G-Brain »

Cool! :D

A couple of things though:

Code: Select all

		/*If the first player is throwing, set him/her as leader*/
		if(i == 1)
		{
			nleader = player;
			pleader = sum;
		}
		/*If current thrower's sum is higher then leader, set new leader*/
		else if(pleader < sum)
		{
			nleader = player;
			pleader = sum;
		}
That could be a lot shorter.

What are you using ctime and cstdlib for?

0 throws are allowed.
I <3 MariaLara more than all of you

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

heh yeah you're right, it could be shorter, didn't notice that, just kinda debugged it and sent it, so no use changing it now anyway.

ctime is for the time function in the srand and cstdlib is for srand and rand functions.
"The best place to hide a tree, is in a forest"

User avatar
Swan
Knight of the Sword
Knight of the Sword
Posts: 827
Joined: 18 Oct 2006, 16:00
17
Contact:

Post by Swan »

Cats, as an idea....why not make a C++ program that calculates the odds of various gambling games?

For example, in craps, the player bets on the a certain number say the number 7. Each of the different numbers from 2-12 (note that there are 2 dice involved) will have a different probability of appearing. the number 7 has a 20% chance of appearing.

Just thought it would be a cool random idea :?

Im actually working on something similar myself...so maybe we can compare :wink:

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

@cat

I'm glad you haven't done a password generator...

It seems that EVERYONE who learn random function do a password generator...
Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Nerdz wrote:@cat

I'm glad you haven't done a password generator...

It seems that EVERYONE who learn random function do a password generator...
Good idea! XD


@Swan, yeah well, I just try to do the assignments at the moment... but you know what we could try that would be cool? you know those kind of programs where you can work on the same code at the same time? don't remember the name, but a friend from my programming and math classes told me that her husband does it, and it's really great : D we should try it!
"The best place to hide a tree, is in a forest"

Post Reply