Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with the random number generator. keep getting two pair of one number,

ID: 3544262 • Letter: N

Question

Need help with the random number generator.  keep getting two pair of one number, when i only need one pair of each number.


12 02 15 06
01 14 01 06
15 02 04 12
14 01 04 01



import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

public class matchingGame1
{
public static void main(String[] args) throws Exception
{
Scanner keyboard = new Scanner(System.in);
int size;
int r1,r2,c1,c2;
Random rng = new Random();
size = getSize(keyboard);
int done;
int[] board = new int[size * size];
int[][] solutionBoard = new int[size][size];
int[][] solvedBoard = new int[size][size];

hideValues(board, size, rng);
fillSolution(board, solutionBoard, size);
Print2dArrayOfInts(solutionBoard);//call to new method
Populate2dArrayOfInts(solvedBoard, -1);
Print2dArrayOfInts(solvedBoard);//call to new method
//player make move
done = 0;
while (done < size * size)
{

System.out.println("First guess");
do
{
r1 = getNum("row",size,keyboard);
c1 = getNum("column",size,keyboard);

if(solvedBoard[r1][c1] != -1)
System.out.println("That is an occupied space, please select again.");
}
while(solvedBoard[r1][c1] != -1);
System.out.println("Second guess");
do
{r2 = getNum("row",size,keyboard);
c2 = getNum("column",size,keyboard);

Print2dArrayOfInts(solvedBoard);//call to new method

if(r1 == r2 && c1 == c2)
System.out.println("must be different then first space, please select again.");

if(solvedBoard[r2][c2] != -1)
System.out.println("That is an occupied space, please select again.");
}
while(solvedBoard[r2][c2] != -1 || (r1 == r2 && c1 == c2));
solvedBoard[r1][c1] = solutionBoard[r1][c1];
solvedBoard[r2][c2] = solutionBoard[r2][c2];

Print2dArrayOfInts(solvedBoard);//call to new method

if(solutionBoard[r1][c1] != solutionBoard[r2][c2])
{
solvedBoard[r1][c1]=-1;
solvedBoard[r2][c2]=-1;
}
else
done += 2;

}//end body of while
}
public static int getNum(String side,int size,Scanner keyboard)
{int n;
System.out.println("Enter " + side + ", must be 1 through " + size);
n = keyboard.nextInt();
while(n < 1 || n > size)
{System.out.println("Invalid entry");
System.out.println("Enter " + side + ", must be 1 through " + size);
n = keyboard.nextInt();
}
return n-1;
}
public static void fillSolution(int board[],int solutionBoard[][],int size)
{
for(int r = 0, position = 0; r < size; ++r)
{
for(int c = 0; c < size; ++c, position++ )
{
solutionBoard[r][c] = board[position];
}//end inner loop
}//outer for loop

}
public static void hideValues(int board[], int size, Random rng)
{int done,num,possible;
Arrays.fill(board, -1);
done = 0;
while(done < size * size)
{possible = rng.nextInt(size * size); //get number
do
{num = rng.nextInt(size * size); //put in first position
}while(board[num]!=-1);
board[num] = possible;
do
{num = rng.nextInt(size * size); //put in 2nd position
}while(board[num]!= -1);
board[num] = possible;
done += 2;
}//end while

}
public static int getSize(Scanner keyboard)throws Exception
{int size=0;
char difficulty;
while(size == 0)
{
System.out.println("Welcome to the Matching Game.");
System.out.println("Enter E for easy, M for medium, D for difficult");
difficulty = (char)System.in.read();
keyboard.nextLine();//flush

if(difficulty == 'E' ||difficulty == 'e')
size = 4;

else if(difficulty == 'M' ||difficulty == 'm')
size = 6;

else if(difficulty == 'D' ||difficulty == 'd')
size = 8;

if(size == 0)
System.out.println("You must enter E, M or D");
}//end while
return size;
}
public static void Print2dArrayOfInts(int[][] x)
{
for(int[] r: x)
{
for(int c: r)
{
if(c < 10 && c > -1)//I changed this
System.out.print(0 + "" + c + " ");
else
System.out.print(c + " ");
}//end for loop
System.out.println();
}
System.out.println();
}//end method

public static void Populate2dArrayOfInts(int[][] x, int n)
{
for (int r= 0; r < x.length; ++r)
for (int c = 0; c < x[r].length; ++c)
x[r][c]= n;
}//end method

public static void Print2dSolvedBoard(int[][] x, int n)
{
for(int[] r: x)
{
for(int c: r)
{
if(c != n)
System.out.print(c + " ");
else
System.out.print( "- ");
}//end if
System.out.println();
}
System.out.println();
}//end method
}//end class

Explanation / Answer

please rate - thanks

you weren't checking if the number was already used

made changes to hidevalues



import java.util.*;



import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

public class matchingGame1
{
public static void main(String[] args) throws Exception
{
Scanner keyboard = new Scanner(System.in);
int size;
int r1,r2,c1,c2;
Random rng = new Random();
size = getSize(keyboard);
int done;
int[] board = new int[size * size];
int[][] solutionBoard = new int[size][size];
int[][] solvedBoard = new int[size][size];

hideValues(board, size, rng);
fillSolution(board, solutionBoard, size);
Print2dArrayOfInts(solutionBoard);//call to new method
Populate2dArrayOfInts(solvedBoard, -1);
Print2dArrayOfInts(solvedBoard);//call to new method
//player make move
done = 0;
while (done < size * size)
{

System.out.println("First guess");
do
{
r1 = getNum("row",size,keyboard);
c1 = getNum("column",size,keyboard);

if(solvedBoard[r1][c1] != -1)
System.out.println("That is an occupied space, please select again.");
}
while(solvedBoard[r1][c1] != -1);
System.out.println("Second guess");
do
{r2 = getNum("row",size,keyboard);
c2 = getNum("column",size,keyboard);

Print2dArrayOfInts(solvedBoard);//call to new method

if(r1 == r2 && c1 == c2)
System.out.println("must be different then first space, please select again.");

if(solvedBoard[r2][c2] != -1)
System.out.println("That is an occupied space, please select again.");
}
while(solvedBoard[r2][c2] != -1 || (r1 == r2 && c1 == c2));
solvedBoard[r1][c1] = solutionBoard[r1][c1];
solvedBoard[r2][c2] = solutionBoard[r2][c2];

Print2dArrayOfInts(solvedBoard);//call to new method

if(solutionBoard[r1][c1] != solutionBoard[r2][c2])
{
solvedBoard[r1][c1]=-1;
solvedBoard[r2][c2]=-1;
}
else
done += 2;

}//end body of while
}
public static int getNum(String side,int size,Scanner keyboard)
{int n;
System.out.println("Enter " + side + ", must be 1 through " + size);
n = keyboard.nextInt();
while(n < 1 || n > size)
{System.out.println("Invalid entry");
System.out.println("Enter " + side + ", must be 1 through " + size);
n = keyboard.nextInt();
}
return n-1;
}
public static void fillSolution(int board[],int solutionBoard[][],int size)
{
for(int r = 0, position = 0; r < size; ++r)
{
for(int c = 0; c < size; ++c, position++ )
{
solutionBoard[r][c] = board[position];
}//end inner loop
}//outer for loop

}
public static void hideValues(int board[], int size, Random rng)
{int done,num,possible=0,count=0,i;
int []used=new int[size*size/2];
boolean good;
Arrays.fill(board, -1);
done = 0;
while(done < size * size)
{    good=false;
     while(!good)
        {possible = rng.nextInt(size * size); //get number
         //check if already used
         for(i=0;i<count;i++)
            if(possible==used[i])
               i=count+10;
         if(i==count)
             good=true;
              
    }
used[count]=possible;
count++;
do
{num = rng.nextInt(size * size); //put in first position
}while(board[num]!=-1);
board[num] = possible;
do
{num = rng.nextInt(size * size); //put in 2nd position
}while(board[num]!= -1);
board[num] = possible;
done += 2;
}//end while

}
public static int getSize(Scanner keyboard)throws Exception
{int size=0;
char difficulty;
while(size == 0)
{
System.out.println("Welcome to the Matching Game.");
System.out.println("Enter E for easy, M for medium, D for difficult");
difficulty = (char)System.in.read();
keyboard.nextLine();//flush

if(difficulty == 'E' ||difficulty == 'e')
size = 4;

else if(difficulty == 'M' ||difficulty == 'm')
size = 6;

else if(difficulty == 'D' ||difficulty == 'd')
size = 8;

if(size == 0)
System.out.println("You must enter E, M or D");
}//end while
return size;
}
public static void Print2dArrayOfInts(int[][] x)
{
for(int[] r: x)
{
for(int c: r)
{
if(c < 10 && c > -1)//I changed this
System.out.print(0 + "" + c + " ");
else
System.out.print(c + " ");
}//end for loop
System.out.println();
}
System.out.println();
}//end method

public static void Populate2dArrayOfInts(int[][] x, int n)
{
for (int r= 0; r < x.length; ++r)
for (int c = 0; c < x[r].length; ++c)
x[r][c]= n;
}//end method

public static void Print2dSolvedBoard(int[][] x, int n)
{
for(int[] r: x)
{
for(int c: r)
{
if(c != n)
System.out.print(c + " ");
else
System.out.print( "- ");
}//end if
System.out.println();
}
System.out.println();
}//end method
}//end class



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote