#Java My code is at the end of the post. And I am tracked into several problems.
ID: 3754624 • Letter: #
Question
#Java
My code is at the end of the post. And I am tracked into several problems.
My original thinking is to create a 4x4 array first, in this order: (A,A, Q,Q K,K J,J,2, 2,5, 5, 6,6,9, 9), and then shuffled the card.
1. However, when I shuffled the card, some cards were missing, and it will turn out like A,A,A,Q,Q,2,2,2.... (Which means the card is no longer a pair anymore)
2. I found I only can type the row and column of the array I want to guess in 0-3, rather than 1-4
3. When I guess wrong(which means the cards I choose are two different numbers), the card won't face down into $ sign.
4. When I guess the second times, I found the position of the card change again... it's position is from the first time I play.
5. I need to complete the game in OOP...
The question is:
__________________________________________________
Write a program that plays this game of concentration. Use 16 cards that are shuffled and laid out ina 4 by 4 square. These cards should be labeled with pairs of card numbers (A, Q, K, J, 2, 5, 6, 9) Your program should allow the player to specify the cards that she would like to select through a coordinate system For example, in the following layout: 1 2 3 4 1 A S $$ All of the cards are face down indicated by $. The pairs of A which are face up and at coordinates (1,1) and (2,3) . To hide the cards that have been temporarily placed face up, output a large number of newlines to force the old board off the screen (or find something better in the JAVA API)Explanation / Answer
package Question1;
import java.util.Scanner;
public class Shuffle
{
public static String[][] array = {
{" A", " A", "10", "10"},
{" K", " K", " J", " J"},
{" 2", " 2", " 5", " 5"},
{" 6", " 6", " 9", " 9"}
};
// Edit : a boolean array to store if this card has to be face up or down
public static boolean[][] status = {
{ false, false, false, false},
{false, false, false, false},
{false, false, false, false},
{false, false, false, false}
};
public static void main(String[] args)
{
// Edit : this prints the array
// Edit : we will use this in the game play method
// originalCardOrder();
// Edit : shuffling is done first
shuffle();
// Edit : cover card is not needed anymore
// coverCard();
// Edit : the game run
finalTable();
}
// public static void originalCardOrder()
public static void printCardOrder()
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
// Edit : if this is face up. print its face else print $
if( status[i][j] )
System.out.print(" " +array[i][j]+ " ");
else
System.out.print(" " +"$"+ " ");
}
System.out.println();
}
}
// Shuffle the cards randomly
// Edit : this method is okay
public static void shuffle()
{
System.out.println();
for (int i = 0; i <4; i++)
{
for (int j = 0; j <4; j++)
{
int i1 = (int) (Math.random() * 4);
int j1 = (int) (Math.random() * 4);
String temp = array[i][j];
array[i][j] = array[i1][j1];
array[i1][j1] = temp;
}
}
}
// Make a table that makes the cards face down
// instead of a separate method to print face down cards, use boolean array along with the card array
/*public static void coverCard() {
for (int i = 0; i <4; i++) {
for (int j = 0; j <4; j++) {
System.out.print(" " + " $ " + " ");
}
System.out.println();
}
}
*/
// method to check as long as game not over
// Edit : game over when all cards face up
private static boolean isGameOver()
{
for (int i = 0; i <4; i++)
{
for (int j = 0; j <4; j++)
{
// if any status is false, terminate
if( !status[i][j])
return false;
}
}
return true;
}
public static void finalTable()
{
// String[][] finalTable = new String[4][4];
// for (int i = 0; i <4; i++) {
// for (int j = 0; j <4; j++) {
// finalTable[i][j] = " $ ";
// }
//
// }
// int count = 0;
// while (count<8) {
// Scanner scan = new Scanner(System.in);
// System.out.println("Type the row between 1-4 you want: ");
// int rowTest1 = scan.nextInt();
// System.out.println("Type the column between 1-4 you want: ");
// int colTest1 = scan.nextInt();
// finalTable[rowTest1][colTest1] = String.valueOf(array[rowTest1][colTest1]);
//
// System.out.println("Type the row between 1-4 you want: ");
// int rowTest2 = scan.nextInt();
// System.out.println("Type the column between 1-4 you want: ");
// int colTest2 = scan.nextInt();
// finalTable[rowTest2][colTest2] = String.valueOf(array[rowTest2][colTest2]);
//
// for (int i = 0; i <finalTable.length; i++) {
// for (int j = 0; j <finalTable[i].length; j++) {
//
// System.out.print(finalTable[i][j]);
// }
// System.out.println();
//
// }
// if (finalTable[rowTest1][colTest1].equals(finalTable[rowTest2][colTest2])) {
// count++;
// }else{
// System.out.println();
// System.out.println();
// System.out.println();
// finalTable[rowTest1][colTest1] = " $ ";
// finalTable[rowTest2][colTest2] = " $ ";
//
// continue;
// }
//
// }
Scanner scan = new Scanner(System.in);
// the game runs as long as there is a pair left to identify
while( !isGameOver() )
{
printCardOrder();
// ask for a row and a column
System.out.print("Type the row between 1-4 you want: ");
int rowTest1 = scan.nextInt();
System.out.print("Type the column between 1-4 you want: ");
int colTest1 = scan.nextInt();
// store the symbol shown
String lastOpenedCard = array[rowTest1-1][colTest1-1];
if( status[rowTest1-1][colTest1-1] )
continue;
// set the status of this location to true and print the cards
status[rowTest1-1][colTest1-1] = true;
printCardOrder();
// ask for another row and column
System.out.print("Type the row between 1-4 you want: ");
int rowTest2 = scan.nextInt();
System.out.print("Type the column between 1-4 you want: ");
int colTest2 = scan.nextInt();
if( status[rowTest2-1][colTest2-1] )
{
status[rowTest1-1][colTest1-1] = false;
continue;
}
// set this status to true
status[rowTest2-1][colTest2-1] = true;
// print the cards
printCardOrder();
// if this new card is same as the last one opened, let it face up, else make them face down
if( !array[rowTest2-1][colTest2-1].equals(lastOpenedCard))
{
status[rowTest1-1][colTest1-1] = false;
status[rowTest2-1][colTest2-1] = false;
}
// press enter to continue
System.out.println("Press enter to continue...");
scan.nextLine();
scan.nextLine();
// print a lot of lines
for( int i=0; i<1000; i++)
System.out.println();
}
// close the scanner
scan.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.