Hi, I am new to c++, and i have been teaching myself from the tutorials listed on this website, and a book that I have. I just finished my first real program whose idea i did not get from a tutorial. It's a program that rolls dice for the boardgame Risk. I know other programs exist for this, i just wanted to try my hand at making my own. What i was wondering was, if anyone wouldnt mind taking a look at the code, or maybe compiling it, and offering some comments, copliments or criticisms. Thanks a lot!
Here is the code.
Code:
/*****************************************************************************
* This program is designed to facilitate the process of rolling dice in the
* boardgame Risk. The program asks the user how many armies should attack, and
* how many should defend, and how many rounds this should continue for. There
* is also a simple dice roll function included in the program. Written by
* Nathan Petts 3.7.06
******************************************************************************/
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using std::cout;
using std::cin;
using std::endl;
int diceroll (int& dicerollrun); //this function rolls y n-sided die x times
int riskroll (int& riskrun); //this function exectues the process of
// determening the winner of an attack in risk
void clrscr (void); // this windows-specific function clears the
// screen. Written by Sunlight.
int main()
{
int run = 1; //controlls the loop that keeps the program running
int input = 0; // menu input
int dicerollrun; // controlls the loop that keeps the dice roll funct. up
int riskrun; // controlls the loop that keeps the riskroll fucnt. up
do {
cout << "Welcome to RiskCalc. Written by Nathan Petts 3.7.06" << endl;
cout << "Please make your selection" << endl;
cout << "1: Risk Calculations" << endl;
cout << "2: Diceroll" << endl;
cout << "0: Exit" << endl;
cin >> input;
switch (input)
{
case 1:
do {
riskrun = 1;
riskroll (riskrun); // calls the riskroll function
}
while (riskrun !=0);
break;
case 2:
do
{
dicerollrun = 1;
diceroll (dicerollrun); //calls the diceroll function
}
while (dicerollrun !=0);
break;
case 0:
run = 0;
break;
default:
cout << "invalid input" << endl;
run = 0;
break;
}
}
while (run != 0);
return 0;
}
int diceroll (int& dicerollrun)
{
int numdice; //number of dice to roll
int numsides; // number of sides to each die
int l; //loop controll
srand(time(NULL)); // seed rand w/ time
cout << "Please enter the number of dice sides. Enter 0 to exit to Main Menu" << endl;
cin >> numsides;
cout << "Please enter number of dice." << endl;
cin >> numdice;
if (numsides != 0)
{
cout << "The dice rolls are:" << endl;
for (l = 0; l < numdice; l++)
{
cout << rand () %numsides + 1 << endl;
}
system ("pause");
clrscr();
}
else
{
dicerollrun = 0;
clrscr();
}
return dicerollrun;
}
int riskroll (int& riskrun)
{
int numattackers; // the number of attacking armies
int numdefenders; // the number of defending armies
int attackingarmieslost = 0; // number of attacking armies lost
int defendingarmieslost = 0; // number of defending armies lost
int rounds; // the number of rounds the dice will be rolled
int x; // for-loop control attackarray
int y; // for-loop control defendarray
int z; // for-loop control compairing the arrays
int n; // for-loop controll for number of rounds
srand(time(NULL)); // seeding random
int temp; //temp int for sorting
cout << "enter number of rounds to attack. enter 0 to exit" << endl;
cin >> rounds;
cout << "enter number of attacking armies. enter 0 to exit" << endl;
cin >> numattackers;
cout << "enter number of defending armies. enter 0 to exit" << endl;
cin >> numdefenders;
int attackarray [numattackers]; // the dice rolls of the attackers
int defendarray [numdefenders]; // the dice rolls of the defenders
if (numattackers != 0 && numdefenders != 0 && rounds != 0)
{
for (n = 0; n < rounds; n++)
{
for (x = 0; x < numattackers; x++)
{
attackarray [x] = rand () %6 + 1;
}
// sorts attackarray
for(int x=0; x< numattackers; x++)
{
for(int y=0; y<numattackers-1; y++)
{
if(attackarray[y]<attackarray[y+1])
{
int temp = attackarray[y+1];
attackarray[y+1] = attackarray[y];
attackarray[y] = temp;
}
}
}
/*
cout << "these are the attackers rolls" << endl;
for (x = 0; x < numattackers; x++)
{
cout << attackarray [x] << endl;
}*/
for (y = 0; y < numdefenders; y++)
{
defendarray [y] = rand () %6 + 1;
}
// sorts the defendarray
for(int x = 0; x < numdefenders; x++)
{
for(int y=0; y<numdefenders-1; y++)
{
if(defendarray[y]<defendarray[y+1])
{
int temp = defendarray[y+1];
defendarray[y+1] = defendarray[y];
defendarray[y] = temp;
}
}
}
/*cout << "these are the defender rolls" << endl;
for (y = 0; y < numdefenders; y++)
{
cout << defendarray [y] << endl;
}*/
for (z = 0; z < numdefenders; z++)
{
if (defendarray [z] >= attackarray [z])
{
attackingarmieslost++;
}
else
{
defendingarmieslost++;
}
}
}
cout << "the attackers lost " << attackingarmieslost << " armies." << endl;
cout << "the defenders lost " << defendingarmieslost << " armies." << endl;
system ("pause");
clrscr();
riskrun = 1;
}
else
{
riskrun = 0;
clrscr();
}
return riskrun;
}
void clrscr(void)
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '),
dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}