Memory Sequence Game - Java Program for Console Based Game

Verified

Added on  2023/06/10

|8
|998
|259
AI Summary
This program represents the Memory-Sequence game where the player can choose a particular difficulty and play the game accordingly. In this game random sequences of numbers are presented to the player and then the screen is wiped clean. Finally, the player is asked to repeat the sequence exactly as it was displayed to him. In the steps to develop this console based application I had to face a handful of problems. However, solving these problems helped me to learn some necessities about Java programming and game designing concepts.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Running head: MEMORY SEQUENCE GAME
Memory Sequence Game
Name of the student:
Name of the University:
Author note:

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
2
MEMORY SEQUENCE GAME
Flowchart
Source Code
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class MemorySequenceGame {
static int[] sequence = new int[15];
static int n, high=1;
public static void generateSeq(int mSecs){
Random rand = new Random();
for(int i=0;i<n;i++) {
sequence[i]=rand.nextInt(99) + 10;
try {
Thread.sleep(mSecs);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Document Page
3
MEMORY SEQUENCE GAME
}
System.out.print(sequence[i]+" ");
}
}
public static boolean play(int mSecs){
Scanner sc = new Scanner(System.in);
boolean winStatus = true;
int i;
while(n<=15) {
System.out.print("\nThe numbers are: ");
generateSeq(mSecs);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
new ProcessBuilder("cmd", "/c",
"cls").inheritIO().start().waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int input;
System.out.println("\n\nEnter all the "+n+" numbers in proper
sequence: ");
for(i=0;i<n;i++) {
input=sc.nextInt();
if(input!=sequence[i]) {
System.out.println("\nSorry!! You are eliminated!!");
winStatus=false;
return winStatus;
}
}
System.out.println("\nHurray!! You are promoted to Level: "+
((++n)-2));
}
return winStatus;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice=1,subChoice;
boolean status;
while(choice != 0) {
n=3;
System.out.println("\n~~ Welcome to the Memory-Sequence-Game ~~");
System.out.println("\nEnter 1: Start a New Game.\nEnter 2: View
Highest Score\nEnter 0: Quit");
Document Page
4
MEMORY SEQUENCE GAME
choice = sc.nextInt();
switch(choice) {
case 1:
subChoice=0;
while(subChoice==0) {
System.out.println("\nChoose Difficulty Level");
System.out.println("\nEnter 1: Easy (Sequence spawn
time: 0.75 secs).\nEnter 2: Medium (Sequence spawn time: 0.50 secs)\nEnter 3:
Difficult (Sequence spawn time: 0.25 secs)");
subChoice = sc.nextInt();
switch(subChoice) {
case 1: status=play(750);
break;
case 2: status=play(500);
break;
case 3: status=play(250);
break;
default: System.out.println("\nWrong Entry. Try
again!!");
continue;
}
if(status) {
System.out.println("\n~~ CONGRATULATIONS ~~ You
are the CHAMPION of the Memory Game !!\n");
high=13;
}
else {
if(n==3)
System.out.println("Sorry!! You could not even
clear the 1st level!!\n");
else
System.out.println("You have Mastered the
Memory Game upto level "+(n-3)+" !!\n");
if((n-3)>high)
high=n-3;
}
}
break;
case 2: System.out.println("\nYour highest achievement: Level
"+high);
break;
case 0: System.out.println("\nThank you!! Have a nice day!!
");
break;
default: System.out.println("\nWrong Entry. Try again!!");
}
}
}
}

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
5
MEMORY SEQUENCE GAME
Output
Document Page
6
MEMORY SEQUENCE GAME
Document Page
7
MEMORY SEQUENCE GAME

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
8
MEMORY SEQUENCE GAME
Program Reflection
This program represents the Memory-Sequence game where the player can choose a
particular difficulty and play the game accordingly. In this game random sequences of numbers
are presented to the player and then the screen is wiped clean. Finally, the player is asked to
repeat the sequence exactly as it was displayed to him. In this way, the length of the sequence is
increased from 3 to 15. Once the user succeeds to answer all 15 questions, the new Highest score
is set and a Congratulation message is displayed.
In the steps to develop this console based application I had to face a handful of problems.
However, solving these problems helped me to learn some necessities about Java programming
and game designing concepts. One very remarkable problem that I had to face was in the
execution of the Screen Clear statement. The following command new ProcessBuilder("cmd",
"/c", "cls").inheritIO().start().waitFor(); was producing run-time errors when the program was
being executed using the Eclipse IDE. Therefore, I did a bit of research and realized that the
Eclipse console does not allow commands to clear its console memory buffer and hence this
command was invalid there. Finally, I had to re-execute this code using BlueJ and the expected
output was produced. The program is also executing perfectly in the Windows Console
environment.
1 out of 8
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]