I'm writing a simple text game, all is well so far but I cannot figure out a solution to strategically writing a script to perform a randomized function...
if(canair>usair){
srand ( time(NULL) );
int warrand1=rand() % 100; \\ Makes a Random Number 1-100.
if (warrand1>35){ \\ Easy part, I understand how it works.
enemydmg=eair*2-usnavy;
attacking();}
if(warrand1<36){
\\ Basically if the number is less then 35 I want it to continue to other if statements but I need a more cleaner method.
if(cannavy>usnavy){
srand ( time(NULL) );
int warrand2=rand() % 100;
if(warrand2>35){
enemydmg=cannavy*2-usair;}
if(warrand2<36){
if(canarmy>usarmy){
srand ( time(NULL) );
int warrand3=rand() % 100;
if(warrand3>35){
enemydmg=canarmy*1.5;
}
else{enemydmg=canair+cannavy+canarmy+5;}
}
}
}
Sorry code is sloppy i'm not sure how understandable to other people will realize what exactly i'm trying to do but I hope so.
Outline-
Computer has 3 militaries - Navy, Army, Airforce. The code above is basically just determining what the Computer attacks with having a 65% chance to do it strategically to add some randomness. Just need something that is better then all the if statements.
a switch statement is tidier and easier to use, you could also create a whole load of functions that take arguments and have them called in a switch statement.
If you build all the methods you need then put it together in little easy chunks like this
publicint attackoutcomefuncion (int number)
{//switchstatment using number for outcome}
publicvoid Iamattacked(int whyiamattacked)
{//function of part of your game}
int main()
{
cout "do you want to play game?";
while (notchosenquit)
{
startgamefunction();
Iamattacked(attackoutcomefunction(randomnum));
}
showscoreboardfunction()
}