I can't figure out how to get and set array values from a instance of a class that's in a arraylist. Someone who knows how?
Adding players code:
This code is for a school bingo project. In this code "SpelerLijst" is a bingo Players list. It is a random amount of players between 1 and 5.
import java.util.ArrayList;
import java.util.Random;
public class BingoPaneel {
public BingoPaneel(){
Random rand = new Random();
ArrayList<BingoKaart> SpelerLijst = new ArrayList<BingoKaart>();
//Aanmaken van spelers
int spelers = rand.nextInt((5-2 +1)+2);
while (spelers == 0){
spelers = rand.nextInt((5-2 +1)+2);
}
System.out.println(spelers + " Spelers");
for(int i = 0; i < spelers; i++){
SpelerLijst.add(new BingoKaart());
}
}
}
Player code: This code gives the player a card thats named "Kaart"(The array) with 25 random ints.
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class BingoKaart {
Random random = new Random();
public BingoKaart(){
int[][] kaart = new int[5][5];
System.out.println("Spelerkaart: ");
kaart= GetNumer(kaart);
}
private int[][] GetNumer(int[][] kaart) {
for(int i= 0; i < 5 ; i++){
int[] getallen = new int[15];
for(int k= 0; k < 15; k++){
getallen[k] = k + (i*15);
}
shuffleArray(getallen);
for(int j = 0; j < 5; j++){
kaart[i][j] = getallen[j];
}
}
//KAART PRINTEN
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
System.out.print(kaart[i][j]+ " ");
}
System.out.println();
}
System.out.println();
return kaart;
}
static void shuffleArray(int[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
}
Master code: This code runs trough random ints and checks if one of the players has the number in its array.
import java.util.Random;
public class BingoMaster {
public BingoMaster(){
//Vars
int[] getrokkenGetallen = new int[75];
boolean bingo = false;
//Vullen en randomizen trekbare getallen
VulGetrokkenGetallen(getrokkenGetallen);
System.out.println("Het spel Start!");
while(!bingo){
TrekGetal(getrokkenGetallen);
}
}
private int[] VulGetrokkenGetallen(int[] getrokkenGetallen) {
for(int i = 0; i < 75; i++){
getrokkenGetallen[i] = i;
}
shuffleArray(getrokkenGetallen);
return getrokkenGetallen;
}
private void TrekGetal(int[] getrokkenGetallen) {
for(int i = 0; i < 75; i++){
int getal = getrokkenGetallen[i];
}
}
static void shuffleArray(int[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
}
I just can't figure out how to check if the number is in the array of a player. The system to save the checked number is a thing that i'll build as the next step.
shuffleArray(int[] ar)in bothBingoMasterandBingoKaart. You should just have it in one place (one of those classes, or a utility class if it makes sense).