Bingo Card Game Issue With Repeating Random Integers - java

I have this static method created for a Bingo game.
public static void bingoCard(){
int [][]card = new int [5][5];
ArrayList<Integer> alreadyUsed = new ArrayList<Integer>();
boolean valid = false;
// First row
for(int row = 0; row < card.length; row++){
int tmp = 0;
while(!valid){
tmp = (int)(Math.random()*15)+1;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][0] = tmp;
valid = false;
}
// Second row
for(int row = 0; row < card.length; row++){
int tmp = 0;
while(!valid){
tmp = (int)(Math.random()*15)+1;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][1] = tmp;
valid = false;
}
// Third row
for(int row = 0; row < card.length; row++){
int tmp = 0;
while(!valid){
tmp = (int)(Math.random()*15)+1;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][2] = tmp;
valid = false;
}
card[2][2] = 0; // The 3rd matrix to the left and right is a 0.
// Fourth row
for(int row = 0; row < card.length; row++){
int tmp = 0;
while(!valid){
tmp = (int)(Math.random()*15)+1;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][3] = tmp;
valid = false;
}
// Fifth row
for(int row = 0; row < card.length; row++){
int tmp = 0;
while(!valid){
tmp = (int)(Math.random()*15)+1;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][4] = tmp;
valid = false;
}
// Creates an array to make title
String title[] = {"B","I","N","G","O"};
for(int i = 0; i < title.length;i++){
System.out.print(title[i] + "\t");
}
System.out.println();
for(int row = 0; row < card.length; row++){
for(int col = 0; col < card[row].length; col++){
System.out.print(card[row][col] + "\t");
}
System.out.println();
}
}
In the output, this piece of code outputs to this console bingo card: http://puu.sh/487mz/939c8d7a59.png
My main issue is that repeating digits. I am interested in knowing how to get rid of the repeating digits within the 5x5 arrays. Thank you!
Second EDIT: I am also interested in having the game play by itself. Meaning, it would pull out random numbers and correspond to whether or not the digits are on the board. If the condition is met for a BINGO condition, then do something. Does anyone have suggestions in regards to this?

When I've written BINGO boards, I have made an ArrayList containing all possible unique values, then made a call to Collections.shuffle( mylist) which will randomly re-order the values. Then you can iterate over the list to populate your matrix.
Just make sure you re-shuffle for each new board you make

One solution would be to have another data structure that holds all random numbers that have been generated and added into the 2D array that represents the card.
After creating a random number you could check to see if that number already exists in the data structure. If it does then generate a different number. If it doesn't then add it to the card and the data structure.
An ArrayList would be good to use here since it has a nice contains method already written for you. Here's an example.
import java.util.ArrayList;
int [][]card = new int [5][5];
ArrayList<Integer> alreadyUsed = new ArrayList<Integer>();
boolean valid = false;
for(int row = 0; row < card.length; row++){
int tmp = 0;
while(!valid){
tmp = (int)(Math.random()*15)+1;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][0]= tmp;
valid = false;
}
Also in all of your nested for loops you never use the variable col. You could simply get rid of the inner for loop in each of these nested loops.
for(int row=0; row < card.length; row++){
for(int col=0; col < card[row].length; col++){
card[row][0]=(int)(Math.random()*15)+1;
}
}
Could be changed to
for(int row=0; row < card.length; row++){
card[row][0]=(int)(Math.random()*15)+1;
}
Also card[2][2]=0; only needs to happen once, here you're setting it multiple times. This could be changed from
for(int row=0;row<card.length;row++){
for(int col=0;col<card[row].length;col++){
card[row][2]=(int)(Math.random()*15)+31;
card[2][2]=0;
}
}
To
for(int row=0;row<card.length;row++){
card[row][2]=(int)(Math.random()*15)+31;
}
card[2][2]=0;

Don't use the random function like that - instead, fill up an array or ArrayList with all of the potential random numbers. Then randomly remove numbers from that - that will ensure that you cannot get repeated numbers, as only one of each exists.

Fill an ArrayList with numbers from 1 to N, then use a java.util.Random to pick/remove numbers (shuffle is not necessary):
ArrayList<Integer> card = new ArrayList<Integer>(N);
for (int i = 0; i < N; i++)
card.add(i + 1);
Random random = Random();
int pick = card.remove(random.nextInt(card.size()));
You could easily wrap this into a class to organize things.

Here's the way I would have implemented it.
int[][] board = new int[5][5];
ArrayList<Integer> list = new ArrayList<Integer>();
int number = 0;
int index = 0;
int increment = 1;
int col = 0;
//Run a loop until you're at your last column.
while (col < board.length) {
//Ensure uniqueness of your numbers
while (list.size() < 5) {
number = (int) (Math.random() * 15) + increment;
if (!list.contains(number))
list.add(number);
}
//Add elements to the array.
for (int i : list)
board[index++][col] = i;
//Set values for the next iteration.
index = 0;
increment += 15;
list.clear();
col++;
}
board[2][2] = 0;
//Print the board.
System.out.println("B\tI\tN\tG\tO\n");
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
System.out.print(board[i][j] + "\t");
System.out.println("");
}
Results:
B I N G O
9 29 34 59 62
8 23 44 52 64
7 16 0 53 63
1 19 33 46 71
15 17 41 58 61

Create a class which represents a bingo table. Populate an array with numbers from 0 to 99. When generating a new table, shuffle this array and pull numbers from it in order.
public class BingoBoard{
Integer[] randomNumbers;
int[][] grid = new int[5][5];
public BingoBoard(){
randomNumbers = new Integer[100];
for(int i=0;i<randomNumbers.length;i++)
randomNumbers[i] = i;
populateCard();
}
public void set(int x, int y, int value){
grid[x][y] = value;
}
public void populateCard(){
//randomize the numbers you'll pull from.
//Array.asList will be backed by randomNumbers, so this works.
Collections.shuffle(Arrays.asList(randomNumbers));
for(int x=0;x<5;x++){
for(int y=0;y<5;y++){
int num = randomNumbers[x+y*5];
set(x,y,num);
}
}
}
}
This is a very efficient way to populate your grid with random values.

you could keep a list of visited random numbers generated and check it before adding this number to the game like this
boolean[] visited = new boolean[100];
for(int i = 0; i < 100; i ++) visited[i] = false;
and inside each loop use this
for(int row=0; row < card.length; row++){
int num = (int)(Math.random()*15)+1;
if visited[num]{
row --;
continue;
}
visited[num] = true;
card[row][0] = num;
}

Its simple, just use this.
int element = 5;
List<Integer> numbers = new ArrayList<Integer>(element);
for (int i = 1; i <= element * element; i++)
numbers.add(i);
Collections.shuffle(numbers);
int[][] numArr = new int[element][element];
for (int i = 0, counter = 0; i < element; i++)
for (int j = 0; j < element; j++, counter++)
numArr[i][j] = numbers.get(counter);
for (int i = 0; i < numArr.length; i++) {
for (int j = 0; j < numArr[i].length; j++) {
System.out.printf("%-5d", numArr[i][j]);
}
System.out.println();
}

Related

How do I read through a 2D array without searching out of bounds?

I have a 2D array acting as a grid.
int grid[][] = new int[5][5];
How do I search through the grid sequentially as in (0,0), (1,0), (2,0), (3,0), (4,0) then (0,1), (1,1), (2,1) ... without getting any array out of bounds exceptions.
I'm new to programming and I just can't get my head around how to do this.
You know your lengths, now use a for loop to circle through the array.
for (int i = 0;i<5;i++){
for (int j = 0;i<5;i++){
int myInt = grid[i][j];
//do something with my int
}
}
To get the lengths at runtime you could do
int lengthX = grid.length; //length of first array
int lengthY = 0;
if ( lengthX>0){ //this avoids an IndexOutOFBoundsException if you don't know if the array is already initialized yet.
lengthY = grid[0].length; //length of "nested" array
}
and then do the for loop with lengthX and lengthY.
You will need two nested loop in order to access the two dimensions of your array:
int grid[][] = new int[5][5];
for(int i = 0; i < 5; i++ ) {
for(int j = 0; j < 5; j++ ) {
int value = grid[i][j];
}
}
Use 2 forloops like the following example:
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
System.out.println(grid[i][j]);
}
}
Also i would suggest that when initializing an array to write it like this:
int[][] grid = new int[5][5]; // note the double brackets are after int and not grid
Try this:
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
System.out.println(grid[j][i]);
}
}
This code (like the other answers) uses two for loops.
It does however add some handling of edge-cases
static int[] find(int[][] mtx, int valueToLookFor)
{
int rows = mtx.length;
if(rows == 0)
return new int[]{-1,-1};
int cols = mtx[0].length;
if(cols == 0)
return new int[]{-1, -1};
for(int r=0;r<rows;r++)
{
for(int c=0;c<cols;c++)
{
if(mtx[r][c] == valueToLookFor)
return new int[]{r,c};
}
}
return new int[]{-1,-1};
}

Java- finding an index in a 2D array

I have a 2D Array of mostly consecutive integers. I want to take a user's integer input and locate the index of the integer one less than the user's input.
I have manually declared the first two columns in my array, and the remaining twelve columns are randomly assigned integers from a different array.
public static int[][] board = new int[4][14];
public static int[][] deal(int[] cards) {
board[0][0] = 1;
board[0][1] = 0;
board[1][0] = 14;
board[1][1] =0;
board[2][0] = 27;
board[2][1] = 0;
board[3][0] = 40;
board[3][1] = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 12; j++) {
board[i][j + 2] = cards[j + (i * 12)];
}
} return board;
}
I am trying to locate the integer one smaller than the users input and if the following integer (in the same row) is a 0, swap the 0 and the user's input.
I realize there is not a built in function indexOf for an array the following code will not run.
public static int[][] move(int[][] board) {
int input;
int place =0;
if(board.indexOf(input-1) +1 == 0){
place =board.indexOf(input);
board.indexOf(input) = 0;
board.indexOf(input-1) +1 = place;
}
return board;
}
If you really want to use the index of function, you need to switch into List(i.e. ArrayList, Vector etc) of lists . Then you code will be like this
public static ArrayList<ArrayList<Integer>> deal(int[] cards) {
ArrayList<ArrayList<Integer>> board = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < 4; i++) {
board.add(new ArrayList<Integer>);
}
board.get(0).add(1);
board.get(0).add(0);
board.get(1).add(14);
board.get(1).add(0);
board.get(2).add(27);
board.get(2).add(0);
board.get(3).add(40);
board.get(3).add(0);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 12; j++) {
board.get(i).add(cards[j + (i * 12)]);
}
}
return board;
}
Another thing you can do(and which is better because Lists have their overhead in performance) is write your own indexOf() function like below which return an array of integers because in a 2D array index means two integers(row and col):
int[] indexOf(int [] [] ar, int row, int col, int x) { //this function will find x in 2D array ar(with dimension of row*col) and return the index
int [] index = new int [2];
index [0] = index[1] = -1;
for(int i = 0; i<row; i++) {
for(int j = 0; j<col; j++) {
if(ar[i][j] == x) {
index[0] = i;
index[1] = j;
return index;
}
}
}
return index;
}

I have a 2D MATRIX of chars, and having trouble searching the matrix Spirally

I start from the left-bottom and proceeding in clockwise direction till no chars are left. here is my code.
import java.io.*;
import java.util.*;
public class Solution {
static int count = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
char[][] matrix = new char[n][m];
char[] temp = new char[n*m];
for(int r=0;r<n;r++){
for(int col=0;col<m;col++){
matrix[r][col] = sc.next().charAt(col);
}
}
int k=0, l = 0;
while(k < n && l < m){
if(l<m){
for(int i = n-1;i>=k;i--){
temp[count] = matrix[i][l];
count++;
}
l++;
}
for(int i = l;i<m;i++){
temp[count] = matrix[k][i];
count++;
}
k++;
for(int i = k;i<n;i++){
temp[count] = matrix[i][m-1];
count++;
}
m--;
if(k < n){
for(int i = m-1;i>=l;i--){
temp[count] = matrix[n-1][i];
}
n--;
}
}
String code = String.valueOf(temp);
String[] dec = code.split("#");
//System.out.println(dec);
int count2 = dec.length;
System.out.println(count2);
}
}
So can anyone point out where I am going wrong? I start at left bottom, climb up, go right , then go down, go left and continue till no elements left.
There are two issues: incorrect input and missing counter increment.
Incorrect input:
for(int r=0;r<n;r++){
for(int col=0;col<m;col++){
// The following reads a new line each time a new character is needed
matrix[r][col] = sc.next().charAt(col);
}
}
This could be fixed either by lifting sc.next() from the inner loop:
for (int r = 0; r < n; r++) {
String line = sc.next();
for (int col = 0; col < m; col++) {
matrix[r][col] = line.charAt(col);
}
}
Or (preferable) removing inner loop completely:
for (int r = 0; r < n; r++) {
matrix[r] = sc.next().toCharArray();
}
Missing increment (lower part of the spiral):
for(int i = m-1;i>=l;i--){
temp[count] = matrix[n-1][i];
// Counter increment is missing. Add
// counter++;
}
In general, is is better to use StringBuilder for gradual construction of the string. In you case it will look as following:
StringBuilder temp = new StringBuilder();
<...>
temp.append(matrix[n-1][i]);
<...>
String code = temp.toString();
In this code you don't have to estimate possible string size nor manually track current insert position.
starting left bottom in the matrix is
matrix[0][m];
going up the hill will be made by decreasing m to a point where you already had a char inserted.
i would use 4 for loops inside a while loop like is presented:
while (usedRow < (int)0.5*n && usedCol < (int)0.5*m)
{
//usedRow and usedCol start with the value of 0 and will be raised
// by the end of the loop
int i, j;
for (i = m - usedCol; i<=(m-usedCol); i++)
{
matrix[usedRow][m-i] = sc.next().charAt(0);
}
for (j = usedRow; j <= (n-usedRow); j++)
{
matrix[n + j][usedCol] = sc.next.charAt(0);
}
for (i = usedCol; i <= (m-usedCol); i++)
{
matrix [n - usedRow][m+i] = sc.next().chatAt(0);
}
for ( j = n - usedRow; j <= (n - usedRow); j++)
{
matrix[n - j][m - usedCol] = sc.next().charAt(0);
}
usedRow++;
usedCol++;
}
this way you go clockwise and keep the loop within the rows and cols that are not in use.
hope that it helped you in a way.

Insert integers into 2d array

private void enterbtnActionPerformed(java.awt.event.ActionEvent evt) {
int [][] array = new int [4][4]; // my array
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
array[i][j]= ;// use for feeding the code
}
popfield.setText(Arrays.deepToString( array ) );
}
I want to insert integers into 2d array via 2 textfields one for columns and one for rows elements via two text fields xfield and yfield
So.. For create 2D int array:
//int 2 dimensional array
int[][] array = null;
//your fields values
final int xFieldVal = 5;
final int yFieldVal = 7;
//values to fill into array
final int minArrayVal = 50;
final int maxArrayVal = 100;
//create matrix / grid with dimensions (xFieldVal x yFieldVal)
array = new int[xFieldVal][yFieldVal];
(that creates recangle xFieldVal x yFieldVal- or Y*X..)
While you have rectangle array you can acces to all value for filling eg. like that:
//random generator
Random rnd = new Random();
for (int i = 0; i < xFieldVal; i++) {
for (int j = 0; j < yFieldVal; j++) {
//generate new int in interval
array[i][j] = minArrayVal + rnd.nextInt(maxArrayVal- minArrayVal+ 1);
}
}
While you will dont have rectangle array (you can have eg. just 1st dimension fixed, and 2nd not- I mean each "row" can have difference "columns" count), you have to use that loop:
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
//printing next line
System.out.println();
}
Eg. to find min and max value, you can use that:
//set on the max possible (every value should be less than that)
int min=Integer.MAX_VALUE;
//set on the min possible (every value should be more than that)
int max=Integer.MIN_VALUE;
//iteration through 1st index (eg. iteration through rows)
for (int i = 0; i < array.length; i++) {
//iteration through 2nd index of 1st index (eg. through all columns)
for (int j = 0; j < array[i].length; j++) {
//compare and assign if array value is less than actual found min
if(min > array[i][j]){
min = array[i][j];
}
//compare and assign if array value is more than actual found max
if(max < array[i][j]){
max = array[i][j];
}
}
}
I dont think you understand what a 2D array is
think of it as a grid. you insert into a single cell at a time.
a single cell belongs to a row and a column hence has a row number and column number...like an excel work book? cell b5?
so if you want to input numbers all you gotta do is have a single textfield lets call it txt
the rest is as follows
private void enterbtnActionPerformed(java.awt.event.ActionEvent evt)
{
int [][] array = new int [4][4]; // my array
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
array[i][j]= Integer.parseInt(txt.getText());// use for feeding the code
}
popfield.setText(Arrays.deepToString( array ) );
}

Finding the mode of a 2D array

I'm trying to return the mode of a 2D array using a frequency array. I have an array, score, which is of length 10, and has 3 columns. Each column contains an int that is between 0 and 100.
I'm trying to find a way that will iterate through the array and return the modal value. What I have so far is:
int value = 0;
int[] freq = new int[100];
for (int row = 0; row < score.length; row++) {
for (int col = 0; col < score[row].length; col++) {
score[row][col] = value;
freq[value]++;
}
}
int largest = 0;
int mode = -1;
for (int i = 0; i < 100; i++) {
if (freq[i] > largest)
{
largest = freq[i];
mode = i;
}
}
System.out.println("modal score is: " +mode);
Problem is that this is just returning the modal score as 0, which it isn't.
You have a problem on generating the freq array. If I understand correctly, on the first double-for block you are trying to put the frequencies of the numbers inside the freq array.
But all you do is:
int value = 0;
.....
score[row][col] = value;
freq[value]++;`
firstly you are changing the score array,( which is a problem for you I guess...) and the you go to freq[0] and do ++. Obviously modal is 0, that number appears in all of the array.
SOLUTION: in the first double for block you should do:
value = score[row][col];
freq[value]++;
so I think you mixed up the order of the line, it should be the other way around.
private static void printMode(double[][] doubles) {
HashMap<Double , Double> map = new HashMap();
for (int i = 0; i < doubles.length; i++) {
for (int j = 0; j < doubles[i].length; j++) {
double temp = doubles[i][j];
if(map.get(temp)==null){
map.put(doubles[i][j],1.0);
}else {
double temp2 = (double) map.get(temp);
map.put(doubles[i][j],++temp2);
}
}
}
Object[] objects = map.values().stream().sorted().toArray();
Stream stream = map.entrySet().stream().filter(val-> val.getValue().equals(objects[objects.length-1]));
stream.forEach(System.out::println);
}
I think using Stream for finding mode is the best way.
use int instead of double doesn't cause any problems.
int value = 0;
int [] freq = new int [arr.length];
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr[i].length; j++){
value = arr[i][j];
freq[value]++;
}
}
int largest = 0;
int mode = 0;
for (int i = 0; i < freq.length; i++) {
if (freq[i] > largest)
{
largest = freq[i];
mode = i;
}
}
System.out.println("modal score is: " +mode);

Categories

Resources