I need help with this assignment. I am a beginner programming with Java, and am
ID: 3762314 • Letter: I
Question
I need help with this assignment. I am a beginner programming with Java, and am not sure how to approach this.
Create a simple memory game using a single-dimension array.
Give the user a choice between difficult levels:
Easy – User gets 20 turns
Medium – User gets 10 turns
Hard – User gets 5 turns
When the user gets a match, they don't lose the turn.
If they don't get a match, they lose a turn. 5 incorrect guesses = game over.
The user guesses two items, these items are revealed to the user. If they match, the cards stay flipped over when presenting the puzzle. Otherwise re-hide the puzzle and ask for a new guess.
The user wins if they match all the items.
Create a game according to the above rules
Your array should use either numbers or letters. (integers might be easier)
You should have at least 10 items to match (so your array should be at least 20 items) If you do more than 10 items, you might want to give the player more turns.
The positions of the 10 items should be randomized every time the game is played. Note: populating the array is one of the big challenges of this assignment
After every guess "clear the screen" before outputting the puzzle again.
for (int n = 0; n < 10; n++)
System.out.println( " " );
Create a game according to the above rules
Your array should use either numbers or letters. (integers might be easier)
You should have at least 10 items to match (so your array should be at least 20 items) If you do more than 10 items, you might want to give the player more turns.
The positions of the 10 items should be randomized every time the game is played. Note: populating the array is one of the big challenges of this assignment
After every guess "clear the screen" before outputting the puzzle again.
Explanation / Answer
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Random; public class MemoryGame { public static String ReadString() { try { InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); return reader.readLine(); } catch (Exception e) { e.printStackTrace(); return ""; } } public static int ReadInteger() { try { InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); return Integer.parseInt(reader.readLine()); } catch (Exception e) { e.printStackTrace(); return 0; } } public static void main(String[] args) { int a, b, opcode, result; System.out.println("Program for Addition, Subtraction, Multiplication and Division "); while (true) { System.out.print("Enter Your Choice: 1 - Easy, 2 - Medium, 3 - Hard, 4 - Exit: "); opcode = ReadInteger(); if (opcode < 1 || opcode > 4) return; switch (opcode) { case 1: int count=1; System.out.println("In Easy Mode you have 20 chances"); int rand=getRandomNumber(); System.out.println(rand); while(count!=20) { System.out.println("Enter a value between 1 to 20"); a = ReadInteger(); if(a==rand) { System.out.println("$$$$$$$$$ YUP YOU WIN $$$$$$$$$$$"); break; } count++; } break; case 2: System.out.println("In Medium Mode you have 10 chances"); int rand1=getRandomNumber(); int count1=0; while(count1!=10) { System.out.println("Enter a value between 1 to 20"); a = ReadInteger(); if(a==rand1) { System.out.println("$$$$$$$$$ YUP YOU WIN $$$$$$$$$$$"); break; } count1++; } break; case 3: int count2=1; System.out.println("In Hard Mode you have 5 chances"); int rand2=getRandomNumber(); while(count2!=5) { System.out.println("Enter a value between 1 to 20"); a = ReadInteger(); if(a==rand2) { System.out.println("$$$$$$$$$ YUP YOU WIN $$$$$$$$$$$"); break; } count2++; } break; case 4: System.exit(0); break; default: break; } } } private static int getRandomNumber() { int[] myArray={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; Random generator = new Random(); int randomIndex = generator.nextInt(myArray.length); return myArray[randomIndex]; } }