# Can anyone help with this code?

Discussion in 'C++' started by shadachi, Apr 13, 2008.

Joined:
Apr 13, 2008
Messages:
1
0
Trophy Points:
0
I've written a C++ code regarding a tank and a monster.And uses slope range to damage the monster.The problem is the damage is wrong and the code is too long anyway to make it betteR?

Anyone please can help me on correcting or teach me how to make this code better so i can learn more =) Please and Thank you in advanced.

Assume that the tank is located in position (0,0) and monster y is 8 km from you. Different slopes of missile lunaching will giv different impacts against the monster.

Slope Range Impact Hit
>=4 AND < 5 2%
>= 3 AND <4 5%
>=2 AND <3 7%
>=1 AND <2 9%
exactly 1 12 %

other slopes values will get 0 % hit . And the monster has 100% health initially.

Points (8,1) with 6 attacks
points (8,2) with 3 attacks
points (8,3) with 4 attacks
Points (8,4) with 3 attacks
points (8,6) with 4 attacks
points (8,7) with 2 attacks
points (8,8) with 6 attacks

Here is the code

Code:
```#include <iostream>
#include <math.h>

using namespace std;

float getSlope1(float * p1 ,float * p2 );
float getSlope2(float * p1 ,float * p3 );
float getSlope3(float * p1 ,float * p4 );
float getSlope4(float * p1 ,float * p5);
float getSlope5(float * p1 ,float * p6 );
float getSlope6(float * p1 ,float * p7 );
float getSlope7(float * p1 ,float * p8 );

int main()
{
float * p1 ,* p2, *p3,*p4,*p5,*p6,*p7,*p8;
p1 = new float ;
p2 = new float ;
p3 = new float ;
p4 = new float ;
p5 = new float ;
p6 = new float ;
p7 = new float ;
p8 = new float ;

// 0 for x ; 1 for y
p1 = 0.0f; //x1
p1 = 0.0f; //y1

p2 = 8.0f; // x2
p2 = 1.0f; //y2

p3 = 8.0f ; //x3
p3 = 2.0f; //y3

p4 = 8.0f ; //x4
p4 = 3.0f; //y4

p5 = 8.0f ; //x5
p5 = 4.0f; //y5

p6 = 8.0f ; //x6
p6 = 6.0f; //y6

p7 = 8.0f ; //x7
p7 = 8.0f; //x7

p8 = 8.0f ;
p8 = 10.0f;

float SlopeR ;
SlopeR=getSlope1(p1,p2);
SlopeR=getSlope2(p1,p3);
SlopeR=getSlope3(p1,p4);
SlopeR=getSlope4(p1,p5);
SlopeR=getSlope5(p1,p6);
SlopeR=getSlope6(p1,p7);
SlopeR=getSlope7(p1,p8);

float AttackNums ;
AttackNums= 6;
AttackNums= 3;
AttackNums= 4;
AttackNums= 3;
AttackNums= 4;
AttackNums= 2;
AttackNums= 6;

for ( int n = 0 ; n < 7 ; n ++ )
{
float MonsterHP = 100;

float ReMainHp= 0;

if (SlopeR[n] >=4 && SlopeR[n]< 5)
{
SlopeR[n] = 2;
}
else if (SlopeR[n] >=3 && SlopeR[n]< 4)
{
SlopeR[n] = 5;
}
else if (SlopeR[n] >=2 && SlopeR[n]< 3)
{
SlopeR[n] = 7;
}
else if (SlopeR[n] >=1 && SlopeR[n]< 2)
{
SlopeR[n] = 9;
}
else if (SlopeR[n] == 1)
{
SlopeR[n] = 12;
}
else
{
SlopeR[n] = 0;
}

for ( int m = 0 ; m < AttackNums[n] ; m++)
{

MonsterHP =MonsterHP -SlopeR[n];

ReMainHp = (MonsterHP -SlopeR[n]) * AttackNums[n];

}

if (SlopeR[n] > 0 )

{
cout <<"The tank has fired "<< AttackNums[n]<< "times"<< endl;
cout << "The monster has left " << MonsterHP<< endl;
cout << "The monster has left " << ReMainHp << endl;

}

else
{
cout << "The tank has missed the monster by" <<AttackNums[n] <<"times" <<endl;
cout << "The monster has left " << MonsterHP<< endl;
cout << "The monster has left " << ReMainHp << endl;
}

}

return 0 ;

}

float getSlope1(float * p1 ,float * p2 )
{
float result1 = 0.0f;
result1 = (p2 - p1)/(p2-p1);
return result1;
}

float getSlope2(float * p1 ,float * p3 )
{
float result2 = 0.0f;
result2 = (p3 - p1)/(p3-p1);
return result2;
}

float getSlope3(float * p1 ,float * p4 )
{
float result4 = 0.0f;
result4 = (p4 - p1)/(p4-p1);
return result4;
}
float getSlope4(float * p1 ,float * p5 )
{
float result5 = 0.0f;
result5 = (p5 - p1)/(p5-p1);
return result5;
}
float getSlope5(float * p1 ,float * p6 )
{
float result6 = 0.0f;
result6 = (p6 - p1)/(p6-p1);
return result6;
}
float getSlope6(float * p1 ,float * p7)
{
float result7 = 0.0f;
result7 = (p7 - p1)/(p7-p1);
return result7;
}

float getSlope7(float * p1 ,float * p8 )
{
float result8 = 0.0f;
result8 = (p8 - p1)/(p8-p1);
return result8;
}

```

The Output

The tank has missed the monster by6times
The monster has left 100
The monster has left 600
The tank has missed the monster by3times
The monster has left 100
The monster has left 300
The tank has missed the monster by4times
The monster has left 100
The monster has left 400
The tank has missed the monster by3times
The monster has left 100
The monster has left 300
The tank has missed the monster by4times
The monster has left 100
The monster has left 400
The tank has fired 2times
The monster has left 82
The monster has left 146
The tank has fired 6times
The monster has left 46
The monster has left 222

Last edited by a moderator: Apr 14, 2008