reate a simple text-based console game that implements at least three (3) interfaces
import java.util.*;
import java.lang.*;
interface Number {
int readNumber();
}
interface Choice implements Number {
boolean find(int target);
}
interface ConsoleGame implements Choice {
void success(int target);
}
public class Game implements ConsoleGame {
@Override
int readNumber() {
Random random = new Random();
System.out.printf(
"===========================\n" +
" Game Start \n" +
"===========================\n"
);
return random.nextInt();
}
@Override
boolean find(int target) {
Scanner scanner = new Scanner(System.in);
System.out.printf("Enter number: ");
int number = scanner.nextInt();
if (number < target)
System.out.printf("Lower than the target!\n");
if (number > target)
System.out.printf("Upper than the target!\n");
return number == target;
}
@Override
void success(int target) {
System.out.printf(
"You got the target: %d" +
"===========================\n" +
" YOU WON \n" +
"===========================\n", target
);
}
public static void main(String[] args) {
int target = readNumber();
while (!find(target)) {
System.out.printf("Try again!");
}
success(target);
}
}
Comments
Leave a comment