# Thread: callNumber method for Bingo simulation

1. Touhou-Stepmania Fan
Join Date
May 2014
Location
USA
Posts
11
Rep Power
0

## callNumber method for Bingo simulation

As you may have known, I'm creating a Bingo simulator from scratch.

Precondition: an n amount of randomly generated cards are created.

Postcondition: After a number is called, cards that has that number will have its value be true.

For example:

Precondition
Java Code:
```Card 1 //Name of card

1 24 32 48 63
2 22 41 51 64
3 16 37 52 61
4 19 33 57 72
5 20 34 54 71 //Card with numbers

false false false false false
false false false false false
false false false false false
false false false false false
false false false false false //Card with boolean values to indicate marked values

Card 2

7 16 41 52 64
3 28 40 56 62
4 22 38 49 68
1 25 31 47 75
9 27 45 50 71

false false false false false
false false false false false
false false false false false
false false false false false
false false false false false```
Here's the main class.
Java Code:
```BingoCard.initiateColumns();
BingoCards game = new BingoCards(2);
Random rnd = new Random();
System.out.println(game);
game.callRandomNumber(rnd); //In this case, the random number is 52.
System.out.println(game);```
Postcondition
Java Code:
```Card 1

1 24 32 48 63
2 22 41 51 64
3 16 37 52 61
4 19 33 57 72
5 20 34 54 71

false false false false false
false false false false false
false false false true false
false false false false false
false false false false false //True means the value has been called.

Card 2

7 16 41 52 64
3 28 40 56 62
4 22 38 49 68
1 25 31 47 75
9 27 45 50 71

false false false true false
false false false false false
false false false false false
false false false false false
false false false false false //Both cards have the position at number 52 become true.```
This is some of the code in my program so far.
Java Code:
```public class BingoCard
{
protected int[][] card;
private String myName;
protected boolean [][] called;
static int[] values = new int[75];
public ArrayList<Integer> calledNumbers = new ArrayList<Integer>();

public static void initiateColumns()
{
code not shown
}

public BingoCard()
{
code not shown
}

public BingoCard(String name)
{
code not shown
}

public String getBingoCard()
{
String board = "";
for(int r = 0; r < 5; r++)
{
for(int c = 0; c < 5; c++)
board += card[r][c] + " ";
board += "\n";
}
return board;
}

public String getMarkedAreas()
{
String board = "";
for(int r = 0; r < 5; r++)
{
for(int c = 0; c < 5; c++)
board += called[r][c] + " ";
board += "\n";
}
return board;
}

public void callNumber(int number)
{
for(int r = 0; r < 5; r++)
for(int c = 0; c < 5; c++)
if(card[r][c] == number)
called[r][c] = true;
}

public void callRandomNumber(Random rnd)
{
int number = 0;
boolean yikes;
boolean done = false;
while(done != true)
{
yikes = false;
number = values[rnd.nextInt(75)];
for (int i : calledNumbers)
if (number == i)
yikes = true;
if (yikes == false)
done = true;
}
callNumber(number);
System.out.println(number);
}

public String toString()
{
String display = "";
display += myName + "\n\n";
display += getBingoCard();
display += "\n";
display += getMarkedAreas();
return display;
}
}```
Java Code:
```public class BingoCards extends BingoCard
{
private BingoCard[] cards;

public BingoCards(int participants)
{
code not shown
}

public String printBingoCardAt(int i)
{
return cards[i].getBingoCard();
}

public String printMarkedAreasAt(int i)
{
return cards[i].getMarkedAreas();
}

public int[][] getBingoBoardAt(int i)
{
return cards[i].card; //Gets a number card from position i
}

public boolean[][] getMarkedAreasAt(int i)
{
return cards[i].called; //Gets a boolean card from position i
}

public String toString()
{
String display = "";
for(int i = 0; i < getLength(); i++)
display += cards[i].toString() + "\n";
return display;
}
}```
So what I want to do is have a method that allows me to use the callRandomNumber method so that the matrix location with that random value is marked true for all of the boards.
Last edited by Nazrininator; 05-26-2014 at 10:24 PM.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,641
Rep Power
9

## Re: callNumber method for Bingo simulation

Well, if I were going to do this, I would probably have 75 shared objects each of which has a boolean to show if the value was taken. If I am using those objects to populate my card, then all I need to do is set the selected number to true and all the cards which have that number (i.e using that shared object) will be set to true in the proper location.

Regards,
Jim

3. Touhou-Stepmania Fan
Join Date
May 2014
Location
USA
Posts
11
Rep Power
0

## Re: callNumber method for Bingo simulation

So I have an array list that stores called numbers. Once a number is called, that number is stored in the array list. Then that number is used to find which cards have that value. If it does, then the position that has that value will be marked true.

Again, here's the precondition
Java Code:
```Card 1 //Name of card

1 24 32 48 63
2 22 41 51 64
3 16 37 52 61
4 19 33 57 72
5 20 34 54 71 //Card with numbers

false false false false false
false false false false false
false false false false false
false false false false false
false false false false false //Card with boolean values to indicate marked values

Card 2

7 16 41 52 64
3 28 40 56 62
4 22 38 49 68
1 25 31 47 75
9 27 45 50 71

false false false false false
false false false false false
false false false false false
false false false false false
false false false false false```
and here's the postcondition.

Java Code:
```Card 1

1 24 32 48 63
2 22 41 51 64
3 16 37 52 61
4 19 33 57 72
5 20 34 54 71

false false false false false
false false false false false
false false false true false
false false false false false
false false false false false //True means the value has been called.

Card 2

7 16 41 52 64
3 28 40 56 62
4 22 38 49 68
1 25 31 47 75
9 27 45 50 71

false false false true false
false false false false false
false false false false false
false false false false false
false false false false false //Both cards have the position at number 52 become true.```
This is what I use to call the methods.
Java Code:
```for(int i = 0; i < objectArray.getLength(); i++)
objectArray.callNumber(call);```
What I'm dealing with is an array of a string, a boolean, and two matrices.
Last edited by Nazrininator; 05-27-2014 at 02:29 PM.

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,641
Rep Power
9

## Re: callNumber method for Bingo simulation

It appears to me that you have answered your own question (unless I am missing something).

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•