0% found this document useful (0 votes)
4 views4 pages

Java Console Game Modes Overview

Uploaded by

mario bitago
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Java Console Game Modes Overview

Uploaded by

mario bitago
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Game Project (Per Class Version)

Below are all Java class files included in the project:

[Link]
public interface Displayable {
void display();
}

[Link]
public interface Startable {
void start();
}

[Link]
public interface GameMode {
void play();
}

[Link]
public class StoryMode implements GameMode, Displayable {

@Override
public void display() {
[Link]("You selected STORY MODE.");
}

@Override
public void play() {
[Link]("\n=== STORY MODE ===");
[Link]("You wake up in a peaceful village...");
[Link]("Your adventure begins.\n");
}
}

[Link]
public class SurvivalMode implements GameMode, Displayable {

@Override
public void display() {
[Link]("You selected SURVIVAL MODE.");
}
@Override
public void play() {
[Link]("\n=== SURVIVAL MODE ===");
[Link]("You must survive as long as possible!");
[Link]("Danger lurks everywhere...\n");
}
}

[Link]
public class AdventureMode implements GameMode, Displayable {

@Override
public void display() {
[Link]("You selected ADVENTURE MODE.");
}

@Override
public void play() {
[Link]("\n=== ADVENTURE MODE ===");
[Link]("A long journey awaits across forests and mountains.\n");
}
}

[Link]
import [Link];

public class MainGame implements Startable {

private Scanner sc = new Scanner([Link]);


private GameMode mode;
private String playerName;

@Override
public void start() {

[Link]("-----------------------------------");
[Link](" SIMPLE JAVA CONSOLE GAME ");
[Link]("-----------------------------------");

[Link]("Enter your name: ");


playerName = [Link]();

boolean repeat;
do {
[Link]("\nPress 1, 2, or 3 to select your game mode.");
[Link]("1 - Story");
[Link]("2 - Survival");
[Link]("3 - Adventure");
[Link]("Select: ");

int choice = getValidInt();

switch (choice) {
case 1: mode = new StoryMode(); break;
case 2: mode = new SurvivalMode(); break;
case 3: mode = new AdventureMode(); break;
default:
[Link]("Invalid choice. Defaulting to Story Mode.");
mode = new StoryMode();
}

if (mode instanceof Displayable) {


((Displayable) mode).display();
}

[Link]("Press P to start playing, " + playerName + ": ");


String input = [Link]();

if ([Link]("P")) {
[Link]();
} else {
[Link]("Game canceled.\n");
}

[Link]("Do you want to play again? (Y/N): ");


repeat = [Link]().equalsIgnoreCase("Y");

} while (repeat);

[Link]("\nThank you for playing, " + playerName + "!");


[Link]("-----------------------------------");
}

private int getValidInt() {


while (true) {
try {
return [Link]([Link]());
} catch (NumberFormatException e) {
[Link]("Invalid input. Enter a number: ");
}
}
}
}

[Link]
public class Main {
public static void main(String[] args) {
MainGame game = new MainGame();
[Link]();
}
}

You might also like