1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Can anyone help with this code?

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

  1. shadachi

    shadachi New Member

    Joined:
    Apr 13, 2008
    Messages:
    1
    Likes Received:
    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 [2];
    	p2 = new float [2];
    	p3 = new float [2];
    	p4 = new float [2];
    	p5 = new float [2];
    	p6 = new float [2];
    	p7 = new float [2];
    	p8 = new float [2];
    	
    	
    	
    	// 0 for x ; 1 for y
    	p1[0] = 0.0f; //x1
    	p1[1] = 0.0f; //y1
    	
    	p2[0] = 8.0f; // x2
    	p2[1] = 1.0f; //y2
    	
    	p3[0] = 8.0f ; //x3
    	p3[1] = 2.0f; //y3
    	
    	p4[0] = 8.0f ; //x4
    	p4[1] = 3.0f; //y4
    	
    	p5[0] = 8.0f ; //x5
    	p5[1] = 4.0f; //y5
    	
    	p6[0] = 8.0f ; //x6
    	p6[1] = 6.0f; //y6
    	
    	p7[0] = 8.0f ; //x7
    	p7[1] = 8.0f; //x7
    	
    	p8[0] = 8.0f ;
    	p8[1] = 10.0f;
    	
    	
    	
    	
    	
    	float SlopeR [7]; 
    	SlopeR[0]=getSlope1(p1,p2);
    	SlopeR[1]=getSlope2(p1,p3);
    	SlopeR[2]=getSlope3(p1,p4);
    	SlopeR[3]=getSlope4(p1,p5);
    	SlopeR[4]=getSlope5(p1,p6);
    	SlopeR[5]=getSlope6(p1,p7);
    	SlopeR[6]=getSlope7(p1,p8);
    	
    	
    	
    	float AttackNums [7];
    	AttackNums[0]= 6;
    	AttackNums[1]= 3;
    	AttackNums[2]= 4;
    	AttackNums[3]= 3;
    	AttackNums[4]= 4;
    	AttackNums[5]= 2;
    	AttackNums[6]= 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[1] - p1[1])/(p2[0]-p1[0]);
    	return result1;
    }
    
    
    float getSlope2(float * p1 ,float * p3 )
    {
    	float result2 = 0.0f;
    	result2 = (p3[1] - p1[1])/(p3[0]-p1[0]);
    	return result2;
    }
    
    float getSlope3(float * p1 ,float * p4 )
    {
    	float result4 = 0.0f;
    	result4 = (p4[1] - p1[1])/(p4[0]-p1[0]);
    	return result4;
    }
    float getSlope4(float * p1 ,float * p5 )
    {
    	float result5 = 0.0f;
    	result5 = (p5[1] - p1[1])/(p5[0]-p1[0]);
    	return result5;
    }
    float getSlope5(float * p1 ,float * p6 )
    {
    	float result6 = 0.0f;
    	result6 = (p6[1] - p1[1])/(p6[0]-p1[0]);
    	return result6;
    }
    float getSlope6(float * p1 ,float * p7)
    {
    	float result7 = 0.0f;
    	result7 = (p7[1] - p1[1])/(p7[0]-p1[0]);
    	return result7;
    }
    
    float getSlope7(float * p1 ,float * p8 )
    {
    	float result8 = 0.0f;
    	result8 = (p8[1] - p1[1])/(p8[0]-p1[0]);
    	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

Share This Page