C# help with these methods... public class Game { public Game(int numPlayers, in
ID: 3602833 • Letter: C
Question
C# help with these methods...
public class Game {
public Game(int numPlayers, int numRounds) {
/*Game's constructor. This is called with the number of players in the game and the number of rounds that will be played. This method:
+Creates a numPlayers size array of TankController (which is stored as a private field of Game)
+Sets another private field to the number of rounds that will be played
+Creates an array or list collection of Attack (if it's an array, a suitably large array should be created for it, e.g. 100).
No objects of type TankController or Attack should be created here; only the arrays.
-numPlayers will always be between 2 and 8 (inclusive)
-numRounds will always be between 1 and 100 (inclusive)*/
}
public void CreatePlayer(int playerNum, TankController player) {
This method takes a player number (guaranteed to be between 1 and the number of players) and sets the appropriate field in Game's TankController array to player. Note that the value of playerNum is indexed from 1 while arrays are indexed from 0, so CreatePlayer() will need to account for this.
}
public TankController GetPlayerNumber(int playerNum){
//This method takes a player number (playerNum, between 1 and the number of players) and returns the appropriate TankController from the TankController array. Again, note that arrays are indexed from 0 while playerNum is indexed from 1.
}
}
Explanation / Answer
//I think this might be the solutuion for the following problem
we have to declare TankController Object as the private member of the class game and then give size of it in the constructor of the class.
//Before running the code you have to create TankController class in another window.
public class Game
{
private TankController[] t;
public Game(int numOfPlayers,int numRounds){
if((numOfPlayers>=1 && numOfPlayers<=8) &&( numRounds>=1 && numRounds<=100)){
this.t[]=new TankController[numOfPlayers];
List<Attack> A=new List<Attack>();
}
}
public void CreatePlayer(int playerNum, TankController player) {
if(playerNum >1 && playerNum<this.t.Length ){
t[playerNum-1]=new TankController();
t[playerNum-1]=player;
}
}
public TankController GetPlayerNumber(int playerNum){
if(playerNum>=1 && playerNum<=this.t.Length){
return t[playerNum];
}
else
return null;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.