Need help with Computer AI script.

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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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.

Thanks.
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

lots of little easy thincky blocks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

public int attackoutcomefuncion (int number)
{//switchstatment using number for outcome}

public void 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()
}

Last edited on
I figured I should probably impliment switch statements and this will probably be a good way to practice them. Thank you
Topic archived. No new replies allowed.