Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c# help with this method i have this... public Game(int numPlayers, int numRound

ID: 3604307 • Letter: C

Question

c# help with this method

i have this...

public Game(int numPlayers, int numRounds)
{
if ((numPlayers >=1 && numPlayers <= 8) && (numRounds >= 1 && numRounds <= 100))
{
this.players = new TankController[numPlayers];
List<Attack>A = new List<Attack>();
this.rounds = numRounds;
}
}//end Game

public int GetNumPlayers()
{
return players.Length;
}// end GetNumPlayers

NEED HELP WITH THE METHOD BELOW...

public static int[] GetPlayerLocations(int numPlayers) {

-Given a number of players, this static method returns an array giving the horizontal positions of those players on the map. For example, if GetPlayerLocations(2) was called, it would return something like {39, 119}. Note that the total width of the map (160) is stored in the Terrain.WIDTH constant so use this in your calculations.

-Player positions in the array are to be strictly from left to right: e.g. {40, 120} is okay, but {120, 40} is not okay.

-The array size must equal numPlayers; GetPlayerLocations(4) must return an array of length 4, for example.

-Each player is to be placed equally close to its neighbours- for four players, if the first player is 40 tiles away from the second player, the second player should be (approximately) 40 tiles away from the third player and the third player should be (approximately) 40 tiles away from the last player.

-The leftmost and rightmost players should be placed a distance away from their respective horizontal border that is half the size of the distance between players.

-To ensure that tanks are attractively positioned, take into account that a Tank is 4 x 3 and these locations are based on the leftmost position of the tank.

-Because this method returns integers, it is not necessary that the distances described be exact as in many situations this may be impossible. It may be preferable to work with floats, then round them with Math.Round and cast them to ints before putting them in the array. This is not necessary, however.

-numPlayers will never be less than 2; however, it won't necessarily be limited to the maximum number of players. As a result you should calculate these positions rather than having predefined arrays for each player count. That said, if your method works for up to 8 players and doesn't work for a greater number of players you should still receive most of the marks for this method.

}

Explanation / Answer

Solution==============================

public static int[] GetPlayerLocations(int numPlayers)
        {
            //Resultant Array
            int[] tanks = new int[numPlayers];
            int tankSize = 4;
            int totalTankSize = tankSize * numPlayers;
            int remainingWIDTH = Terrain.WIDTH - totalTankSize;

            //Determining average spacing between tanks
            float spaces = remainingWIDTH / numPlayers;

            //Setting the first location
            float tankLoc = spaces / 2;
            int intLoc = (int)Math.Round(tankLoc);
            tanks[0]= intLoc;

            //Considering tank size
            tankLoc += tankSize;
          

            //Setting rest of the locations
            for (int i = 1; i < numPlayers; i++)
            {
                tankLoc = tankLoc + spaces;
                intLoc = (int)Math.Round(tankLoc);
                tanks[i]= intLoc;

                tankLoc += tankSize;
              
            }


            return tanks;
        }

Let me know if any doubts...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote