### Thread: Bingo 3x5

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
3
Rep Power
0

#### Bingo 3x5

Thank you for reading this post, i was wondering if you would be so kind as to help me with this code.
I have to give this project this friday night, and im kind of desperate because i cant figure out how to continue.

This bingo is supposed to be composed of an 3x5 array of numbers which the user should be able to introduce using the keyboard or ask the program to generate them randomly,they cannot be repeated or out of a 1-99 range (both inclusive).

Then, after you have those 15 numbers, random numbers representing the bingo balls have to be generated. They cannot be repeated (a number cannot appear twice), and finally the numbers have to be checked each ball appears to know whether you have line/bingo or not.

Ive been trying with no luck so far. At the moment the only thing which works is the array of random balls and a menu in which you could choose between those functions (entering the numbers, random numbers and play).
Here you have some of my tries:

Method for random balls :
Code:
``` public static void main(String[] args) {
bolasAle();
}

public static int [] bolasAle() {
int []bola=new int [99];

for (int i=0; i<99; i++)
{ bola[i]=(int)(Math.random() * (99)+1);
for (int j=0; j<=i; j++)
{if (bola[i]==bola[j] && i!=j) {
while (bola[i]==bola[j]) {
bola[i]=(int)(Math.random() * (99)+1);
}
j=0;
}
}
}
for (int k=0;k<99;k++) { System.out.print(bola[k] +" "); }
return bola;
}```
Method for random numbers (the 15 you are playing with), this one has some problems, if there is a single repeated number it is ok, but if a second one was about to repeat, it repeats.

Code:
```public static int [][] CartonAle() {
int [][]carton=new int [3][5];
for (int i=0; i<carton.length; i++) {
for (int j=0; j<carton[i].length; j++){
if (i==0 && j == 0){
carton[i][j]=(int)(Math.random() * (99)+1);
}else{

generacion(i,j,carton);
}
}
}
for (int k=0;k<3;k++)
{ for(int i=0;i<5;i++) {
System.out.print(carton[k][i] +" ");
if (i==4) { System.out.println();}
}
}
return carton;}

public static void generacion(int i, int j, int [][]carton){
carton[i][j]=(int)(Math.random() * (99)+1);
for (int k=0; k<=i; k++){
for (int l=0; l<=j; l++){
if ((carton[i][j]==carton[k][l]) && (i!=k || j!=l)){
generacion(i,j,carton);

}
}
}
}```
This method would check if there is line or bingo. I thought giving a negative value to a winner number (*-1) would be a good idea, so if every 5 numbers of a line are < 0 there is LINE, if every line has LINE then, BINGO.
Code:
```public static void estadoActual(int [][] carton){
// se comprueba cuantos numeros negativos (aciertos) hay en el carton
// si hay cinco en una linea, ¡linea!. si todas tienen linea, ¡bingo!.
int aciertosL1=0,aciertosL2=0,aciertosL3=0;
for (int i=0;i<5;i++){
if (carton[1][i]<0) {
aciertosL1++;
}
if (carton[2][i]<0){
aciertosL2++;
}
if (carton[1][i]<0){
aciertosL3++;
}
}
if (aciertosL1==5 || aciertosL2==5 || aciertosL3==5){
System.out.println("LINEA!");
}
if (aciertosL1==5 && aciertosL2==5 && aciertosL3==5) {
System.out.println("BINGO!");
}
//Vuelve a poner a 0 el numero de aciertos para por si
// hay una nueva comprobacion
aciertosL1=0; aciertosL2=0; aciertosL3=0;
}```
I guess it would be finished with the play method, some people adviced me to do it using a boolean array so i dont know wether it would be a good idea or not.
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
74
Rep Power
19
As for "generating without duplicating" random numbers, make a collection of all the numbers, randomize the order, then get and remove the first item in the list.
Code:
`Collections.shuffle(Arrays.asList(array));`
As for checking for a line or a bingo, there are two issues with your approach:

Code:
`            if (carton[1][i]<0){`
is used for the second time, it should be:

Code:
`            if (carton[3][i]<0){`

Code:
`        aciertosL1=0; aciertosL2=0; aciertosL3=0;`
is unnecessary, since those are local variables
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
3
Rep Power
0
Originally Posted by Aurum84
As for "generating without duplicating" random numbers, make a collection of all the numbers, randomize the order, then get and remove the first item in the list.
Code:
`Collections.shuffle(Arrays.asList(array));`
Thank you, but i cant understand any of that, first of all i dont know whats that line of code.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
74
Rep Power
19
Originally Posted by Br6891
Thank you, but i cant understand any of that, first of all i dont know whats that line of code.
Ok, let's look at it this way.
Let's say you have a collection of integers, ranging from 1 to 10:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
You want to have 3 random entries from this collection. A way to do this , like you did, is to randomly select an entry for 3 times, but then there is a risk of having duplicates. For example the first random selection is a 2, and the second random selection is also a 2.
Now, what if there would be a function that could transform a (sorted) collection to a randomized collection, in such a way that the list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] to for example [4, 6, 2, 1, 9, 10, 8, 7, 3, 5]? Then, take any number of integers from the front of the list: [4, 6, 2], and there you have it: 3 random integers from the list.
This same can be applied to your scenario: create a list of 99 integers, randomize it, and then pop the first 15 items from it.

Now, the line of code I offered is a shortcut, it can be written in multiple lines as well.
This line of code:
Code:
`Collections.shuffle(Arrays.asList(array));`
says:
In the class Selection, there is a static method (static meaning: you do not need an instance of the class to access this method) called 'shuffle'. This method takes a List object as argument, but you don't have a list, but an array. In the Array class, there is a static method 'asList' that converts an array to a List. (actually, List is an abstract type, but let's forget that for some other type). Now your array is shuffled and you can pick your items from it.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
3
Rep Power
0
Sorry... Ive been learning java for a month and a half, i still cant figure out what do you mean, ive got less than 24 hours for finishing this project... It would be really nice if you could write that code, please.
6. Maybe you can take a look at this alternative to Generate random numbers