mardi 24 décembre 2019

How do I fix my For loop and Random problem in Android?

 public class PlayerData{

int id; // id means Player's order
boolean isTeacher;

public PlayerData() {

}

public void setID(int id) {
    this.id = id;
}

public void setTeacher() {
    isTeacher = true;
}

}

public class Singleton {

public int time;
public int total; // number of total players
public int teacher; // number of teachers
public int student; // number of students
-
public int count = 1;
public String keyWord; // 
ArrayList<PlayerData> playerArrayList = new ArrayList<PlayerData>();

// method init creates Objects of players
public void init() {
    for (int i = 1; i <= total; i++) {
        PlayerData player = new PlayerData();
        player.setID(i);  // set id
        playerArrayList.add(player); // add player Object to ArrayList
    }
}

public void JobSelection() {

    int a[] = new int[teacher]; // Make array to the number I will select
    Random r = new Random(); // Random Object
    PlayerData player = new PlayerData();
    for (int i = 0; i < teacher; i++)    // for loop to select Teachers 
    {
        a[i] = r.nextInt(total) + 1; //select a number from 1~total and Save in a[0]~a[total]
        for (int j = 0; j < i; j++) //for loop to avoid overlap
        {
            if (a[i] == a[j]) {
                i--;
            }
        }

        if (a[i] == player.id) { 
            playerArrayList.get(player.id - 1).isTeacher = true;
        }
    }
} ...

public class RoleSelection extends AppCompatActivity {
Singleton s1 = Singleton.getInstance();
PlayerData player = new PlayerData();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_role_selection07_03);

    TextView t1 = (TextView) findViewById(R.id.textJobConfirm);

    if (s1.playerArrayList.get(s1.player.id-1).isTeacher == true) {
        t1.setText(" You job is Teacher ");
    } else {
        t1.setText(" Your job is Student ");

I am making an app game whose job consists of only Teacher and Student. Codes above shows my app.

  1. There is a player class that has its data and setters
  2. There is a Singleton class that saves values in a variable such as total(total players), teacher, student etc.. Also Singleton class has ArrayList that creates Player's Objects.
  3. In Singleton class I made 2 methods: One for creating Objects in a ArrayList, the other for giving objects their jobs.
  4. JobSelection Method is trying to give a job to each player. For example if the total player chosen at game setting is 10 and teacher chosen is 4, I have to give 4 teacher jobs randomly to 10 player objects.
  5. After giving the players a job, I wrote code in 'Role Selection' class to setText differently considering player's job.

I don't have any errors in my logcat or in my app. However, I am only receiving "Your Job is Student". I guess the JobSelection method haven't worked well. So I tried to fix these codes ` for (int i = 0; i < teacher; i++) // for loop to select Teachers { a[i] = r.nextInt(total) + 1; //select a number from 1~total and Save in a[0]~a[total] for (int j = 0; j < i; j++) //for loop to avoid overlap { if (a[i] == a[j]) { i--; } }

    if (a[i] == player.id) { 
        playerArrayList.get(player.id - 1).isTeacher = true;
    }
}

`

But I failed to solve these problems... I will be grateful if someone answers me how can I make jobs distributed and see different TextView according to their jobs. Thank you




Aucun commentaire:

Enregistrer un commentaire