Skip to content

Instantly share code, notes, and snippets.

@andhieka
Created June 4, 2020 02:54
Show Gist options
  • Save andhieka/4e661f28a5c720e6971b5c02caf4be5a to your computer and use it in GitHub Desktop.
Save andhieka/4e661f28a5c720e6971b5c02caf4be5a to your computer and use it in GitHub Desktop.
MysteryGame - Before
import 'dart:io';
enum GameState {
start,
theKitchen,
success,
dead,
}
class MysteryGame {
GameState state;
List<String> KITCHEN_ACTION_LIST = [
'chop',
'chop',
'chop',
'chop',
'chop',
'fry',
'serve',
];
int completedKitchenActionIndex = 0;
MysteryGame() {
state = GameState.start;
}
String getPrompt() {
switch (state) {
case GameState.start:
return 'Type ready to start the game';
case GameState.theKitchen:
if (completedKitchenActionIndex == 0) {
return 'Welcome to The Kitchen. Do your actions without mistake!';
}
return null;
case GameState.success:
return 'Congratulations! You are successful 🎉';
case GameState.dead:
return 'You are dead. Like this game? Feel free to retry.';
}
}
void process(String input) {
switch (state) {
case GameState.start:
if (input == 'ready') {
state = GameState.theKitchen;
}
break;
case GameState.theKitchen:
if (input == KITCHEN_ACTION_LIST[completedKitchenActionIndex]) {
completedKitchenActionIndex++;
if (completedKitchenActionIndex == KITCHEN_ACTION_LIST.length) {
state = GameState.success;
}
} else {
state = GameState.dead;
}
break;
case GameState.success:
if (input == 'RIP') {
state = GameState.dead;
}
break;
case GameState.dead:
if (input == 'retry') {
state = GameState.start;
}
}
}
}
void main() {
print('MysteryMaze v1.0');
print('---');
MysteryGame game = MysteryGame();
while (true) {
if (game.getPrompt() != null) {
print(game.getPrompt());
}
String input = stdin.readLineSync();
if (input == 'exit') {
break;
} else {
game.process(input);
}
}
print('Good bye! Come MysteryMaze again soon!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment