[Uni Assignment] 1 Oct 2008

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

[Uni Assignment] 1 Oct 2008

Post by ayu »

Newest assignment =)


It's a "simple version" of the Yatzy game =P
http://code.suck-o.com/83

Code: Select all

#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>

using namespace std;

int main()
{
	int player = 0, PlayerScore[2][7], dice[5];
	string players[2];

	/*Set scoreboard to 0*/
	for(int i=0; i<2; ++i)
		for(int j=0; j<7; ++j)
			PlayerScore[i][j] = 0;

	/*Prompt for player names*/
	for(int i=0; i<2; ++i)
	{
		cout << "Player " << i+1 <<":";
		cin >> players[i];
	}
	/*Tell next cin to ignore one character in buffer*/
	cin.ignore();

	/*Set seed*/
	srand((unsigned)time(NULL));

	/*Start dice throwing, first loop specifies the values the players are collecting, from 1-6 that is*/
	for(int i=1; i<=6;)
	{
		/*Each player throws j times or until the DiceLeft are out*/
		for(int j=3, DiceLeft=5; j>0; --j)
		{
			/*If first throw of turn, show who's turn it is*/
			if(j == 3)
				cout << "\n\nIt's " << players[player] << "'s turn\n\n";
			
			/*If there are still dices to throw*/
			if(DiceLeft > 0)
			{
				/*Print how many throws and dices current player has left*/
				cout << setfill('*') << setw(players[player].size() + 14) << "\n";
				cout << players[player] << " has " << DiceLeft << " dices left and " << j << " throws\n";
				cout << "Press \'ENTER\' to throw\n";

				/*Wait for button push*/
				cin.get();

				/*Define number of each value that has been collected*/
				int numdice=0;

				/*Throw 5 dices*/
				for(int n=0; n<5; ++n)
				{
					dice[n] = rand() % 6 +1;
					
					/*If the dice is the current number (refer to first loop)*/
					if(dice[n] == i)
					{
						/*Assign position i, the dice number of i, to current player*/
						PlayerScore[player][i] += i;
						/*remove dice*/
						DiceLeft--;
						/*increment number of times dice has occured*/
						numdice++;
					}
					/*if there are no dices left, break out of loop*/
					else if(DiceLeft == 0)
						break;
				}
				/*if there are turns under-going*/
				if(DiceLeft >= 0)
				{
					/*Print how many of the current value the player got*/
					cout << players[player] << " got " << numdice << " of " << i <<"\'s\n";
					cout << setw(players[player].size() + 16) << "\n\n\n";
				}


				
						
			}
			
		}
		/*Keep track of who's turn it is, and switch after each set of throws*/
		if(player == 1)
		{
			i++;
			player = 0;
		}
		else
			player = 1;

		
	}

	/*Sum up the scores, not that position 0 on the scoreboard is the total points position*/
	for(player = 0; player <= 1; ++player)
		for(int i = 1; i <= 6; ++i)
			PlayerScore[player][0] += PlayerScore[player][i]; 

	/*Who won?*/
	if(PlayerScore[0][0] > PlayerScore[1][0])
	{
		cout << "The winner is " << players[0] << " with a sum of " << PlayerScore[0][0] << "\n\n";
	}
	else 
	{
		cout << "The winner is " << players[1] << " with a sum of " << PlayerScore[1][0] << "\n\n";
	}

		cout << "-=Protocol=-\n\n";
		cout << players[0] << " " << players[1] << "\n";

		for(int i = 1; i <= 6; ++i)
			cout << setfill(' ') << setw(players[0].size()) << PlayerScore[0][i]<< " "
			     << setw(players[1].size()) << PlayerScore[1][i] << "\n";
		
	
	  
	return 0;
}
"The best place to hide a tree, is in a forest"

rob33n
On the way to fame!
On the way to fame!
Posts: 25
Joined: 09 May 2007, 16:00
16
Contact:

Post by rob33n »

nice assignement & nice comments.

Post Reply