I'm currently doing a java memory game that makes the user guess duplicate letters by input the coordinate of either a 2x2, 4x4 or 6x6 square, which have 2 sets of repeating letters
ex. for 4x4 it would be
A B C D
E F G H
A B C D
E F G H
but randomized
If they guess correctly the letters would stay revealed, until all are found.
I have successfully randomized the square, and cover it with ( * ), and make them reveal based on the input coordinates
But I don't know how to make that once the 2 revealed letters are the same, the program will keep them revealed through out the game, until all duplicate letters are revealed.
The code right now (You can copy and paste whole thing below, all comments are commented out):
import java.util.Scanner;
import java.io.IOException;
public class a4{
// main method. DO NOT MODIFY
public static void main(String args[]) {
Scanner keyboard = new Scanner( System.in );
System.out.println("Welcome to Memory Game");
int board_side;
//this loop obtains the board size, or more specifically
// the length of the side of the board
do{
System.out.println("\n For 2x2 board game press 2"+
"\n For 4x4 board game press 4"+
"\n For 6x6 board game press 6");
board_side=keyboard.nextInt();
}while(board_side!=2 && board_side!=4 && board_side!=6);
char[][] board = createBoard(board_side);
// a call the the shuffle method
shuffleBoard(board);
// a call to the game playing method
playGame(board);
}
// The following method should shuffle the input 2D array caled board
public static void shuffleBoard(char[][] board)
{
// This creates a 1D array whose size is equal to the size of the board
int N = board.length*board.length;
char[] board1D = new char[N];
// Testing to see the printed square
for ( int i = 0; i < board.length; i++) {
for ( int j = 0; j < board[i].length; j++) {
System.out.print( board[i][j] + " " );
}
System.out.println();
}
System.out.println();
for (int i =0; i < board1D.length; i++) {
System.out.print(board1D[i] + " ");
}
System.out.println();
// Copy the elements of 2D array into that 1D array here
for (int m = 0; m < N; m++){
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board.length; j++, m++){
board1D [m] = board[i][j];
}
}
}
// Shuffle 1D array
// Shuffle the array
for (int i = 0; i < N; i++) {
// Generate an index randomly
int index = (int)(Math.random() * N);
char temp = board1D[i];
board1D[i] = board1D[index];
board1D[index] = temp;
}
//Testing to see the 1D array
System.out.println();
for (int i =0; i < board1D.length; i++) {
System.out.print(board1D[i] + " ");
}
System.out.println();
//Copy shuffled 1D array back into 2D array, i.e., back to the board
for (int m = 0; m < N; m++){
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board.length; j++, m++){
board[i][j] = board1D [m];
}
}
}
//Testing to print the shuffled square
for ( int i = 0; i < board.length; i++) {
for ( int j = 0; j < board[i].length; j++) {
System.out.print( board[i][j] + " " );
}
System.out.println();
}
System.out.println();
System.out.println();
}
// game playing method
public static void playGame(char[][] board)
{
Scanner keyboard = new Scanner( System.in );
// this createst a 2D array indicating what locations are paired, i.e., discovered
// at the begining none are, so default initializaiton to false is ok
boolean[][]discovered=new boolean[board.length][board[0].length];;
for ( int i = 0; i < board.length; i++) {
System.out.print(1 + i + " ");
for ( int j = 0; j < board[i].length; j++) {
System.out.print( "* " );
}
System.out.println();
}
System.out.print(" ");
for ( int x = 0; x < board.length; x++) {
System.out.print(1 + x + " ");
}
System.out.println();
System.out.println();
System.out.println("Enter a pair of undiscovered distinct locations on the board that you want revealed. i.e., a pair of integers in the range [1, 2]");
System.out.println();
int FirstLocationX;
int FirstLocationY;
int SecondLocationX;
int SecondLocationY;
do {
System.out.println("Enter the first location: ");
FirstLocationX=keyboard.nextInt();
FirstLocationY=keyboard.nextInt();
if (FirstLocationX > board.length && FirstLocationY > board.length){
System.out.println("The location is invalid. It is outside of the board. ");
}
} while(FirstLocationX > board.length && FirstLocationY > board.length);
System.out.println();
do {
System.out.println("Enter the second location: ");
SecondLocationX=keyboard.nextInt();
SecondLocationY=keyboard.nextInt();
if (SecondLocationX > board.length && SecondLocationY > board.length){
System.out.println("The location is invalid. It is outside of the board. ");
}
else if (SecondLocationX == FirstLocationX && SecondLocationY == FirstLocationY){
System.out.println("The location is invalid. The second location equal to the first. ");
}
} while(SecondLocationX > board.length && SecondLocationY > board.length && SecondLocationX == FirstLocationX && SecondLocationY == FirstLocationY);
//reveals the letters based on the coordinate user inputed
for ( int i = 0; i < board.length; i++) {
System.out.print(1 + i + " ");
for (int j = 0; j < board[i].length; j++) {
if (FirstLocationX == i+1 && FirstLocationY == j+1){
System.out.print( board[i][j] + " " );
}
else if (SecondLocationX == i+1 && SecondLocationY == j+1){
System.out.print( board[i][j] + " " );
}
/*This part is wrong, reveals the whole square instead of the found duplicates
else if (discovered[0][0] = true){
System.out.print( board[i][j] + " " );
}
else if (discovered[0][2] = true){
System.out.print( board[i][j] + " " );
}
*/
else {
System.out.print( "* " );
}
}
System.out.println();
}
System.out.print(" ");
for ( int x = 0; x < board.length; x++) {
System.out.print(1 + x + " ");
}
System.out.println();
System.out.println();
char[][] FirstInput = new char[FirstLocationX][FirstLocationY];
char[][] SecondInput = new char[SecondLocationX][SecondLocationY];
/*GETTING AN ERROR HERE when I try to trigger a duplicate letter found, since the array is shuffled I don't know how exactly, just testing with the first match right now (A and A), which suppose to be coordinate 0,0 and 0,2 in the non-sorted square matrix
*/
if (board[0][0] == FirstInput[FirstLocationX][FirstLocationY] && board[0][2] == SecondInput[SecondLocationX][SecondLocationY]){
discovered[0][0] = true;
discovered[0][2] = true;
}
waitForPlayer();
do {
playGame(board);
}while(discovered[0][0] == false && discovered[0][2] == false);
}
// createBoard method. DO NOT MODIFY!
/* this method, createBoard, creates the board filled with letters of alphabet,
where each letter appears exactly 2 times
e.g., for 4 x 4, the returned board would look like:
A B C D
E F G H
A B C D
E F G H */
public static char[][] createBoard(int side)
{
char[][] tmp = new char[side][side];
int letter_count=0;
for (int row = 0; row < tmp.length/2; row++){
for(int col = 0; col < tmp[row].length; col++)
{
tmp[row][col]=(char)('A'+letter_count);
tmp[row+tmp.length/2 ][col]=tmp[row][col];
letter_count++;
}
}
return tmp;
}
// waitForPlayer method. Do not modify!
public static void waitForPlayer()
{
System.out.print("Press enter to continue");
try {
System.in.read();
}
catch (IOException e){
System.out.println("Error reading from user");
}
}
}
The current part that is wrong is:
char[][] FirstInput = new char[FirstLocationX][FirstLocationY];
char[][] SecondInput = new char[SecondLocationX][SecondLocationY];
/*when I try to trigger a duplicate letter found, since the array is shuffled I don't know how exactly, just testing with the first match right now (A and A), which suppose to be coordinate 0,0 and 0,2 in the non-sorted square matrix
*/
if (board[0][0] == FirstInput[FirstLocationX][FirstLocationY] && board[0][2] == SecondInput[SecondLocationX][SecondLocationY]){
discovered[0][0] = true;
discovered[0][2] = true;
}
waitForPlayer();
do {
playGame(board);
}while(discovered[0][0] == false && discovered[0][2] == false);
}
and
else if (discovered[0][0] = true){
System.out.print( board[i][j] + " " );
}
else if (discovered[0][2] = true){
System.out.print( board[i][j] + " " );
}
I would suggest making each square as an object instead of having a mere char. That way you can control if they are revealed or not when you display then
Edit
Without objects, you can just create another array of bool that will track what is displayed. Whenever a pay is revealed, set the values inside that alternative board to true, then in your display function check if it is true before showing it