Home > OS >  How to make an array's variables correspond to a for statement?
How to make an array's variables correspond to a for statement?

Time:10-10

I am doing an assignment where you have to do a tank battle application. I am having an issue in the method of createtanks() where when I assign each value of the array like

pantera[0] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));

when I write the for statement logic to fire at each tank and write

while (ammunition > 0 && armor > 0 && maneuver > 0)

it takes ammunition, armor and maneuver to all equal 0's when I assigned a random value to all from 0 to 100 (I used the debugger). The result that I try to display after the tanks fired from each other shows negative ammunitions, armor and maneuver (it subtracts from 0's instead of what the random assigned it in the first place). How do I make the values that I assign in the random generations of ammo, armor and manuever carry over to the for/while loop logic? Thanks!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;
    namespace C
    {
        class Tankstat
        {
            private string name { get; set; }
            private int ammunition { get; set; }
            private int armor { get; set; }
            private int maneuver { get; set; }
    
            private int ammunitionMinus { get; set; }
            private int armorMinus { get; set; }
            private int maneuverMinus { get; set; }
    
            public Tankstat(string name, int ammunition, int armor, int maneuver)
            {
                this.name = name;
                this.ammunition = ammunition;
                this.armor = armor;
                this.maneuver = maneuver;
            }
            public override string ToString()
            {
                return $"The name of the tank is {name} the amount of ammunition left is {ammunition} the strength of armor is {armor} the maneuver level is {maneuver}";
            }
    
            public static Tankstat operator *(Tankstat tank1, Tankstat tank2)
            {
                return tank1 * tank2;
            }
    
            public static Tankstat operator ^ (Tankstat tank1, Tankstat tank2)
            {
                return tank1 ^ tank2;
            }
            public Tankstat() { }
            public void Tankcreate()
            {
                Random rand = new Random();
                Tankstat[] pantera = new Tankstat[5];
                pantera[0] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
                pantera[1] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
                pantera[2] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
                pantera[3] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
                pantera[4] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
    
                Tankstat[] T34 = new Tankstat[5];
                T34[0] = new Tankstat("T-34", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
                T34[1] = new Tankstat("T-34", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
                T34[2] = new Tankstat("T-34", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
                T34[3] = new Tankstat("T-34", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
                T34[4] = new Tankstat("T-34", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
                foreach (Tankstat panteras in pantera)
                {
                    Console.WriteLine(panteras);
                }
                foreach (Tankstat T34S in T34)
                {
                    Console.WriteLine(T34S);
                }
    
            }
            Tankstat[] pantera = new Tankstat[5];
            Tankstat[] T34 = new Tankstat[5];
            public void attack()
            {
                for (int i = 0; i < pantera.Length && i < T34.Length; i  )
                    {
                    while (ammunition > 0 && armor > 0 && maneuver > 0)
                    Console.WriteLine("Initializer is going to attack the tank");
                    Random rambo = new Random();
                    ammunitionMinus = rambo.Next(1, 20);
                    ammunition = ammunition - ammunitionMinus;
                    armorMinus = rambo.Next(1, 10);
                    armor = armor - armorMinus;
                    maneuverMinus = rambo.Next(2, 15);
                    maneuver = maneuver - maneuverMinus;
                    Console.WriteLine($"The name of the tank {name} has {ammunition} ammunition left the armor is {armor} and the manuever is {maneuver}, wait for 2 seconds for next move");
                    Thread.Sleep(2000);
                }
            }
        }
    }

CodePudding user response:

You are creating new instance of pantera and T34 in tankcreate()

pantera[0] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));

when You write the for statement logic to fire at each tank and write. But You are not using any tank in while/for loop

while (ammunition > 0 && armor > 0 && maneuver > 0)

I have made some changes like commented the below lines in tank create()

Tankstat[] pantera = new Tankstat[5];
Tankstat[] T34= new Tankstat[5];

And Used pantera in while loop please do same for T34

Try this.

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
   using System.Threading;
   namespace C
   {
       class Tankstat
       {
           private string name { get; set; }
           private int ammunition { get; set; }
           private int armor { get; set; }
           private int maneuver { get; set; }
   
           private int ammunitionMinus { get; set; }
           private int armorMinus { get; set; }
           private int maneuverMinus { get; set; }
   
           public Tankstat(string name, int ammunition, int armor, int maneuver)
           {
               this.name = name;
               this.ammunition = ammunition;
               this.armor = armor;
               this.maneuver = maneuver;
           }
           public override string ToString()
           {
               return $"The name of the tank is {name} the amount of ammunition left is {ammunition} the strength of armor is {armor} the maneuver level is {maneuver}";
           }
   
           public static Tankstat operator *(Tankstat tank1, Tankstat tank2)
           {
               return tank1 * tank2;
           }
   
           public static Tankstat operator ^ (Tankstat tank1, Tankstat tank2)
           {
               return tank1 ^ tank2;
           }
           public Tankstat() { }
           public void Tankcreate()
           {
               Random rand = new Random();
               //Tankstat[] pantera = new Tankstat[5];
               pantera[0] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
               pantera[1] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
               pantera[2] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
               pantera[3] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
               pantera[4] = new Tankstat("Pantera", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
   
               //Tankstat[] T34 = new Tankstat[5];
               T34[0] = new Tankstat("T-34", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
               T34[1] = new Tankstat("T-34", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
               T34[2] = new Tankstat("T-34", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
               T34[3] = new Tankstat("T-34", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
               T34[4] = new Tankstat("T-34", rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
               foreach (Tankstat panteras in pantera)
               {
                   Console.WriteLine(panteras);
               }
               foreach (Tankstat T34S in T34)
               {
                   Console.WriteLine(T34S);
               }
   
           }
           Tankstat[] pantera = new Tankstat[5];
           Tankstat[] T34 = new Tankstat[5];
           public void attack()
           {
               for (int i = 0; i < pantera.Length && i < T34.Length; i  )
                   {
                   while (ammunition > 0 && armor > 0 && maneuver > 0)
                   {
                   Console.WriteLine("Initializer is going to attack the tank");
                   Random rambo = new Random();
                   ammunitionMinus = rambo.Next(1, 20);
                   pantera[i].ammunition = ammunition - ammunitionMinus;
                   armorMinus = rambo.Next(1, 10);
                   pantera[i].armor = pantera[i].armor - armorMinus;
                   maneuverMinus = rambo.Next(2, 15);
                   pantera[i].maneuver = pantera[i].maneuver - maneuverMinus;
                   Console.WriteLine($"The name of the tank {pantera[i].name} has {pantera[i].ammunition} ammunition left the armor is {pantera[i].armor} and the manuever is {pantera[i].maneuver}, wait for 2 seconds for next move");
                   Thread.Sleep(2000);
}
               }
           }
       }
   }

CodePudding user response:

The while statement will only operate on the next statement so you need { }

I think you are only doing this for one tank -- not the array you have in pantera and t34

public void attack()
{
  // for (int i = 0; i < pantera.Length && i < T34.Length; i  )
  // {
     while (ammunition > 0 && armor > 0 && maneuver > 0)
     {
        Console.WriteLine("Initializer is going to attack the tank");
        Random rambo = new Random();
        ammunitionMinus = rambo.Next(1, 20);
        ammunition = ammunition - ammunitionMinus;
        armorMinus = rambo.Next(1, 10);
        armor = armor - armorMinus;
        maneuverMinus = rambo.Next(2, 15);
        maneuver = maneuver - maneuverMinus;
        Console.WriteLine($"The name of the tank {name} has {ammunition} ammunition left the armor is {armor} and the manuever is {maneuver}, wait for 2 seconds for next move");
        Thread.Sleep(2000);
     }
  // }
}

It would make more sense to me to have attack function take the Tankstat as a parameter

public void attack(TankStat aTank)
{
   while (aTank.ammunition > 0 && aTank.armor > 0 && aTank.maneuver > 0)
   {
      int ammunitionMinus; 
      int armorMinus;
      int maneuverMinus;

      Console.WriteLine("Initializer is going to attack the tank");
      Random rambo = new Random();
      ammunitionMinus = rambo.Next(1, 20);
      aTank.ammunition = aTank.ammunition - ammunitionMinus;
      armorMinus = rambo.Next(1, 10);
      aTank.armor = aTank.armor - armorMinus;
      maneuverMinus = rambo.Next(2, 15);
      aTank.maneuver = aTank.maneuver - maneuverMinus;
      Console.WriteLine($"The name of the tank {aTank.name} has {aTank.ammunition} ammunition left the armor is {aTank.armor} and the manuever is {aTank.maneuver}, wait for 2 seconds for next move");
      Thread.Sleep(2000);
   }
}
  • Related