PDA

View Full Version : Help with C++ program (game risk)




gohome190
Oct 26, 2009, 10:56 PM
hey guys, i know this isn't mac related, but I trust this forum more than my own mother, so I thought i'd ask. We are using functions now. The assignment is:



The board game named Risk simulates world conquest, by representing large armies taking over large territories. In each turn and invading army attempts to defeat the defenders in an adjacent territory.

Each battle is represented by a series of competitive die rolls, using six-sided dice. The invading army may roll up to three dice (but no more than the number of fighting units present). The defending army may roll up to two dice (again, not more than the number of fighting units present).

To resolve the outcome of the battle, the dice results are compared. The best die for each side, the second-best die for each side, etc. Ties favor the defender. The loss of a die competition translates into a loss of a fighting unit.

For example, if the attacker rolls a 3, 6, and a 4, and the defender rolls a 4 and a 5, the 6 pairs with the 5 (attacker wins) and the 4's pair with each other (defender wins). In this example, both the attacker and defender lose one fighting unit.

The battle continues until either of the two forces is eliminated, or until the attack simply decides to cease attacking. Sometimes the battle is very short (very small armies), but sometimes it can be very lengthy (very large armies).

Some players lose interest late in the Risk game when the armies are very large. This program would assist the players by managing a long combat for them.

PROGRAM SPECIFICATIONS

The program is to be designed to handle any number of complete battles in sequence (the user determines when the game ends).

Each battle is itself a series of competitive die rolls. Of course, before this can begin, the size of the two competing armies must be provided (as keyboard input). However, to reduce the necessity of many inputs while managing a large battle, the attacker is to be given the following options:

* Roll just one competitive die roll.
In the regular board game, the attacker usually decides these one at a time.

* Fight until either of the two armies is completely wiped out.
This would especially be chosen with vastly superior forces.

* Fight until the attacker's army is reduced to some specified size.
This would be yet another input value (which may even be typed in at the same moment as the command is issued).
This would be a more conservative approach to a long battle, and then further prospects would be analyzed.

* Withdraw from combat, ending the battle immediately.
Sometimes prudence is better than valor.
Note that this interface may itself be repeated within a large battle. The battle is only truly over until one side is eliminated, or the attacker chooses to withdraw.

Of course, the attacker is free to initiate a battle on a new front, with new armies involved. (So this program will repeat).

CODE REQUIREMENTS

Appropriate use of functions is absolutely required for this assignment. Not all functions will be specified in advance -- part of the exercise is to learn to identify the appropriate time to use them.

During program design, identify significant portions of code that can be easily described in a sentence or two, and define those to be functions. Sometimes a function may very well be further qualified into smaller steps which themselves would be functions.

A couple hints of common operations to get you started:
-- sorting two or three die rolls into decreaasing order
especially if you can have one function handle all sorting
-- managing exactly one competitive die roll, adjusting army sizes (also displaying the actual die rolls and their effect)



SO....



gohome190
Oct 26, 2009, 10:57 PM
I am reallly having trouble, I am getting frustrated and confused. Here's what i have so far:



// Homework 4
// Risk

#include<iostream>
#include<iomanip>
#include<cmath>

// four functions
// sorting two or three die rolls in decreasing order
// especially if you can have one function handle all sorting
// managing exactly one competitive dice roll
//adjusting army sizes
// displaying the actual die rolls and their effect
void armysize(int);

int main ()
{
armysize();

void armysize ()
{
int army1;
int army2;
cout << "Welcome to the game of Risk!" << endl;
cout << "Attacker, how large is your army?" << endl;
cin >> army1;
cout << "Defender, how large is your army?" << endl;
cin >> army2;
}


cout << army1;
cout << army2;

void options ()
{
cout << "You can either:" << endl;
cout << " << endl;
cout << "
void sorting ()
{


void roll ()
{

cout << "The Defender rolls a " << diceroll() << " and a " << diceroll() << endl;
cout << "The Attacker rolls a " << diceroll() << " , " << diceroll() " and a " << diceroll() << endl;
}

double diceroll ()
{
int roll1;
roll1 = 1 + rand % 6;
return roll1;
}



void display ()
{
cout << "You rolled a " << diceroll();
}

gohome190
Oct 26, 2009, 10:59 PM
This is nowhere near correct, i know. I guess my mind just doesn't work like a programmer's.

Thanks so much for evening reading this!

lloyddean
Oct 27, 2009, 12:00 AM
I'd say first you need to outline or diagram how the game is to be played.

For instance this as described is a "Risk" like game, not "Risk".

How many players are there? Two are implied but it is not specified.

How is it decided which player is defender and which attacker?

How do attacker and defender switch places?

What does the following actually mean?

"The invading army may roll up to three dice (but no more than the number of fighting units present). The defending army may roll up to two dice (again, not more than the number of fighting units present)."

What do the qualifiers "but no more than the number of fighting units present" and "not more than the number of fighting units present" refer?

chown33
Oct 27, 2009, 01:24 AM
Describe why you think it's not correct. Often, explaining why you think something is wrong will give you insight into how to correct it.

For example, you call diceroll(), which generates a random number, but then what do you do with it? So far, you're just printing the value. You have no code that keeps the number. No code that compares two numbers. Basically, you're only automating the roling of dice, not any of the comparisons to see who wins each individual contest.

In programming, how do you keep numbers (or other values) for subsequent use? Variables. So think about what variables will be needed to meet the requirements of the problem. Programming problems are frequently word problems involving math (arithmetic) as well as procedure (the actions and their order).

One approach I've often told students is to skip the specific computer-language aspect at first, and just write it out as simple steps that you would tell a person who doesn't know the game. Imagine they've never seen dice before, so you have to tell them every little thing. Imagine it's a 6-year-old kid who knows how to count, but not much else. For example, "Attacker rolls one die. Writes down the number. Defender rolls one die and writes down the number. Whoever has the lower number loses one army. Ties goes to defender. If one player has 0 armies, that player gains the territory. Attacker can choose to stop now with <list consequences here>."

Finally, if any one step is too big to express easily in code, break it down more. Breaking things down is called decomposition, and is one of the fundamental techniques in programming.