Risk Game (Only Die Roll) Using C++

October 19, 2010 at 15:49:28
Specs: Windows 7
I am having trouble finding a way to create a risk game, not worried about territories using C++. The basic rules are..

Each battle begins with an attacking army and a defending army (the sizes of these armies will be inputs to the program). The attacker may roll up to three dice, or one per army unit (whichever is smaller); the defender may roll up to two dice (whichever is smaller). In that respect, the attacker has an advantage.

To determine the outcome of the battle, compare the dice, starting with the largest values on each side. Whoever rolls the higher number wins, and the loser loses one army unit. For example, if the attacker rolls a 6, a 3, and a 1, and the defender rolls a 5 and a 4 -- the 6 beats the 5 and the 4 beats the 3, with each side losing one army. Only rolled dice are compared -- so if either side rolls only one die, then only one comparison occurs and only one army is lost.

In comparing dice, defender wins all ties (and the attacker loses one army).

For purposes of terminology, this assignment sheet will use the word 'skirmish' for each die-rolling event, where each skirmish will lead to one or two armies being lost. A larger 'battle' would consist of many skirmishes.

In the actual board game, which only has five physical dice available, the skirmishes are resolved one after another until the battle ends. The battle is over either when one of the two armies is reduced to zero, or the attacker chooses to cut his losses and leave. The program will simulate all of these options.

Where I am trying to use the int sortthree(); which is just a function with the parameters of three dice rolls which needs to sort them from lowest to highest however it can also take two or one dice roll with the other parameter being zero. Any help appreciated


See More: Risk Game (Only Die Roll) Using C++

Report •


#1
October 21, 2010 at 06:09:32
Why not just use a vector, and take advantage of its sort() method?

How To Ask Questions The Smart Way


Report •

#2
October 21, 2010 at 06:28:04
We haven't gotten that far yet and are not allowed. I have come up with a way using temp for the values however, it puts them from least to greatest, instead of greatest to least. For example, here is my code.. any ideas..

//Function to sort the roll values passed to it by rearranging them highest to lowest
void sortThree(int& rollA, int& rollB, int& rollC)
{
for(int i=4;i>1;i--)
{
if(rollA>rollB){int temp=rollA; rollA=rollB; rollB=temp;}
if(rollB>rollC){int temp=rollB; rollB=rollC; rollC=temp;}
}
}


Report •

#3
October 21, 2010 at 08:05:01
Your best bet would be to manually compare the three values, especially if you're not allowed to use an array, and swap on:
rollA < rollB
rollA < rollC
rollB < rollC

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
October 21, 2010 at 15:58:05
cout << endl << "Game Choices: " << endl;
cout << "1. Roll Just one skirmish " << endl;
cout << "2. Run battle to end " << endl;
cout << "3. Run battle until victory or near defeat " << endl;
cout << "4. Withdraw from the battlefield " << endl;
cout << "Enter your game choice: ";
cin >> choice;

if (choice == 1){
skirmish(attack, defend);
}

if (choice == 2){

while
skirmish(attack, defend);


}


Any idea how i can make number 2 run the whole program until it is over? I have one and 4 working, but cannot figure out 2..


Report •

#5
October 21, 2010 at 16:17:53
Shouldn't be that hard. Just do your skirmish until one side runs out of armies. I assume that's attack and defend?

How To Ask Questions The Smart Way


Report •

#6
October 21, 2010 at 16:35:05
Ok so one last question.. how can I restart the program from the beginning at main without having the user exit the program and restarting it?

Report •

#7
October 21, 2010 at 16:38:41
Use another loop. One that encompasses the entire game. (If your main function looks messy after you do this, it might be time to refactor.)

How To Ask Questions The Smart Way


Report •

#8
October 21, 2010 at 16:41:09
I have break's within my main function, within a while loop.
If I encompass this while loop within a do while loop that holds whole game, then will these breaks still end the program?
I can't seem to group it all into one loop and have it still work.

Report •

#9
October 21, 2010 at 16:47:43
break should only break out of one loop level.

I'm half afraid to do this, but show us what you got. And remember the pre tags.

How To Ask Questions The Smart Way


Report •

#10
October 21, 2010 at 16:49:30
Before I copy any code and stretch this page ridiculously, what are you referring to by pretags? I assume comments but I am not sure.

Report •

#11
October 21, 2010 at 16:57:12
Nevermind I understand what you mean now, one second.

Report •

#12
October 21, 2010 at 16:57:31
Envelop your code in <PRE></PRE> tags, so your indenting and line breaks are preserved.

How To Ask Questions The Smart Way


Report •

#13
October 21, 2010 at 16:59:06
#include <iostream>
using namespace std;

//Function to calculate the rolling of one die and return the value of said roll
int rollDie()
{
    int roll;
    roll = (1 + rand() % 6);
    return roll;
}

//Function to sort the roll values passed to it by rearranging them highest to lowest
void sortThree(int& a, int& b, int& c){
    if (c>b)
        swap(c, b);
    if (c>a)
        swap(c,a);
    if (b>a)
        swap(b,a);
}



//Function to simulate the actual skirmish, calls the roll function and compares the dice
void skirmish(int& attack, int& defend){
    int a, b, c, d, e, f;
        if (attack >= 3){
            a = rollDie();
            b = rollDie();
            c = rollDie();
        }
        else if (attack == 2){
            a = rollDie();
            b = rollDie();
            c = 0;
        }
        else if (attack == 1){
            a = rollDie();
            b = 0;
            c = 0;
        }
        if (defend >= 2){
            d = rollDie();
            e = rollDie();
            f = 0;
        }
        else if (defend == 1){
            d = rollDie();
            e = 0;
            f = 0;
        }
        //Sort the rolls for the attackers and the defenders
      
		sortThree(a, b, c);
      
		sortThree(d, e, f);
        
		//Output the rolls for the user
      
		cout << endl << "The attacker has rolled: " << a << " " << b << " " << c;
      
		cout << endl << "The defender has rolled: " << d << " " << e << " " << f << endl;
      
		//Compare the rolls of the armies
      
		if (a == d){
        attack = attack - 1;
        }
        else if (a != d){
            if (a > d){
                defend = defend - 1;
            }
            else if (d > a){
                attack = attack - 1;
            }
        }

		
		if (attack > 0 && defend > 0){



        if (b == e){
            attack = attack - 1;
        }
        else if (b != e){
            if (b > e){
                defend = defend - 1;
            }
            else if (e > b){
                attack = attack - 1;
            }
        }
		}
}




int main()
{
    int attack, defend, army;
    int choice;
    
	//Initialize roll variables: 
    //(a, b, c) will be attacker's maximum of 3 rolls
    //(d, e, f) will be defender's maximum of 2 rolls and a placeholder for a 0
    //Ask the user for inputs regarding army sizes
   
	cout << "How large is the attacking army? ";
    cin >> attack;
    cout << "How large is the defending army? ";
    cin >> defend;
    
	//Determine rolls for attackers and defenders based on army sizes
       
	while((attack >= 1) && (defend >= 1)){
           
      
			cout << endl<< "The armies currently stand at: " << endl;
       		cout << "Attackers: " << attack << "     " << "Defenders: " << defend << endl;
       
			// Game Choices
			cout << endl << "Game Choices: " << endl;
			cout << "1. Roll Just one skirmish " << endl;
			cout << "2. Run battle to end " << endl;
			cout << "3. Run battle until victory or near defeat " << endl;
			cout << "4. Withdraw from the battlefield " << endl;
			cout << "Enter your game choice: ";
        	cin >> choice;


			if (choice == 1){
			skirmish(attack, defend);
				}


			if (choice == 2){
			do{
			skirmish(attack, defend);
				}while ((attack != 0) && (defend !=0));
			cout << "After skirmishing, the forces are, Attackers: " << attack << "  " << "Defenders: " << defend << endl;						
			
			}


			if (choice == 3){
			cout << "What number would you like to end the battle with?";
			cin >> army;
			while ((attack != 0) && (defend !=0))
			skirmish(attack, defend);
							
			}

			if (choice == 4){
				break;
			}
			    		
         // End of game declare winner
		  if (attack < 1){
          cout << endl << "Skirmish over, the defending forces win with " << defend << endl;
          return 0;
			}


           else if (defend < 1){
           cout << endl << "Skirmish over, the attacking forces win with " << attack << endl;
           return 0;
			}

	   }
	

    return 0;
	}


Report •

#14
October 21, 2010 at 17:26:49
I fixed the do while loop, it was a simple error with order, sorry about that.
However, I do have one final problem that's still irking me.
When the user inputs 3 as their menu choice, then if they put an even number for the army they want to have left, then it goes 1 lower than this value. But if the input is odd than it matches this value.

Is there a simple way to fix this without altering a large amount of code?


Report •

#15
October 22, 2010 at 05:53:48
I'm not sure what you're asking here, but the old source asks for "What number would you like to end the battle with?", sends the response to army, but never does anything with it. If that's the bug, the change would be as follows:
cin >> army;
army = (army > 0)? army : 0;
while ((attack > army) && (defend > 0))

How To Ask Questions The Smart Way


Report •

#16
October 22, 2010 at 09:00:28
Yes that was it I needed to call another int& to reference army, thanks for all your help this feels great to finally have working.

Report •


Ask Question