need help finishing this code please and thank you. I am trying to match the num
ID: 3542792 • Letter: N
Question
need help finishing this code please and thank you. I am trying to match the numbers according to the user selection. If the first and second selection by the user is the same number then those numbers should be visible. The user should be able to play until all the numbers are paired off.
while the game is not over (!IsGameOver(solvedBoard, -1)
{
call Print2dArrayOfInts passing it solvedBoard, Print2dArrayOfInts(solvedBoard);
loop getting 2 positions from the player
get postion 1 from player
trap while not valid or selected already
row and col must be >=0 && <= size -1
row and col position of solved board != -1
print to user value at that space //get postion 2 from player
trap while not valid or selected already
row and col must be >=0 && <= size -1
row and col position of solved board != -1
print to user value at that space
if value of postion 1 and value of position 2 of solutionboard are equal
solvedBoard[position 1 row][position 1 col] = solution[position 1 row][position 1 col]
solvedBoard[position 2 row][position 2 col] = solution[position 2 row][position 2 col]]
end while loop}
end main }
import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
public class matchingGame
{
public static void main(String[] args) throws Exception
{
Scanner keyboard = new Scanner(System.in);
char difficulty;
int size = 0;
int possible;
Random rng = new Random();
boolean selected = false;
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
int[] board = new int[size * size];
//populate array
for(int i = 0; i < (size * size)/2; ++i)
{
board[i] = i;
board[i + (size + size)] = i;
}//end for loop
//create & populate array of random numbers
int[] compGenNum = new int[board.length];
Arrays.fill(compGenNum, -1);
System.out.println(Arrays.toString(compGenNum));
for(int i = 0; i < board.length; ++i)
{
possible = rng.nextInt(board.length);
while(board[possible] == -1)
{
possible = rng.nextInt(board.length);
}//end while
compGenNum[i] = board[possible];
board[possible] = i;
}//end for loop
board = compGenNum;
int[][] solutionBoard = new int[size][size];
//int position = 0;
for(int r = 0, position = 0; r < size; ++r)
{
for(int c = 0; c < size; ++c, position++ )
{
solutionBoard[r][c] = board[position];
//position++;
}//end inner loop
}//outer for loop
Print2dArrayOfInts(solutionBoard);//call to new method
int[][] solvedBoard = new int[size][size];
Populate2dArrayOfInts(solvedBoard, -1);
Print2dArrayOfInts(solvedBoard);//call to new method
//player make move
while (!IsGameOver(solvedBoard, -1))
{
Print2dArrayOfInts(solvedBoard);
System.out.println("Enter row, must be 0 through " + (size -1));
int firstGuessRow = keyboard.nextInt();
while(firstGuessRow < 0 || firstGuessRow > size - 1)
{
System.out.println("Enter row, must be 0 through " + (size -1));
firstGuessRow = keyboard.nextInt();
}
System.out.println("Please enter your column, must be 0 through " + (size - 1));
int firstGuessCol = keyboard.nextInt();
while(firstGuessCol < 0 || firstGuessCol > size -1)
{
System.out.println("Please enter your column, MUST BE 0 through " + (size - 1));
firstGuessCol = keyboard.nextInt();
}
firstGuessRow = firstGuessRow - 1;
firstGuessCol = firstGuessCol - 1;
while(solvedBoard[firstGuessRow][firstGuessCol] == firstGuessRow -1)
{
System.out.println("That is an occupied space, please select again.");
System.out.println("Please enter your row, must be 0 through " + (size - 1));
firstGuessRow = keyboard.nextInt();
while(firstGuessRow < 0 || firstGuessRow > size - 1)
{
System.out.println("Please enter your row, must be 0 through " + (size - 1));
firstGuessRow = keyboard.nextInt();
}
System.out.println("Please enter your col, must be 0 through " + (size - 1));
firstGuessRow = keyboard.nextInt();
while(firstGuessCol < 0 || firstGuessCol > size - 1)
{
System.out.println("Please enter your col, must be 0 through " + (size - 1));
firstGuessCol = keyboard.nextInt();
}
firstGuessRow = firstGuessRow - 1;
firstGuessCol = firstGuessCol - 1;
}//end body of while
}
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 boolean IsGameOver(int[][] x, int n)
{
for(int r = 0; r < x.length; ++r)
{
for(int c = 0; c < x[r].length; ++c)
{
if(x[r][c] == n)
return false;
}//end inner loop
}//end outer for loop
return true;
}//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 had too many arrays
any questions ask, will get back to you in the morning
import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
public class matchingGame
{
public static void main(String[] args) throws Exception
{
Scanner keyboard = new Scanner(System.in);
int size,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);
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));
//if(solutionBoard[r1][c1]==solutionBoard[r2][c2])
// {
solvedBoard[r1][c1]=solutionBoard[r1][c1];
solvedBoard[r2][c2]=solutionBoard[r2][c2];
// done+=2;
// }
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.