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;
}