From 3c05a00c1a218bc3132ca5ee6743d36d6439cbc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=DF?= Date: Thu, 10 May 2012 11:06:16 +0200 Subject: [PATCH] building on statemachine --- src/de/ctdo/crashtest/Statemachine.java | 87 +++++++++++++++++++++-- src/de/ctdo/crashtest/SteuerungFrame.java | 5 +- 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/src/de/ctdo/crashtest/Statemachine.java b/src/de/ctdo/crashtest/Statemachine.java index 9028ee3..602fcf3 100644 --- a/src/de/ctdo/crashtest/Statemachine.java +++ b/src/de/ctdo/crashtest/Statemachine.java @@ -1,7 +1,13 @@ package de.ctdo.crashtest; public class Statemachine { - public enum STATES { + private final char BLUE_BUTTON = 'd'; + private final char LIGHT_BARRIER = 'k'; + private final char TABLE_ONE = 'c'; + private final char TABLE_TWO = 'v'; + private final char TABLE_THREE = 'b'; + + public enum state { IDLE, ENTERED_ROOM, TABLE_GAME_ONE, @@ -11,20 +17,93 @@ public class Statemachine { TABLE_GAME_FIVE, TABLE_GAME_SIX, TABLE_GAME_SEVEN, - TABLE_GAME_EIGHT, FINISH, } - private STATES currentState = STATES.IDLE; + private state currentState = state.IDLE; - public STATES getCurrentState() { + public state getCurrentState() { return currentState; } public void handleInput(char input) { + state newState = getNewState(input); + + System.out.println("newState = " + newState); + + currentState = newState; + } + + private state getNewState(char input) { + state retVal = currentState; + + switch (currentState) { + case IDLE: + if(input == LIGHT_BARRIER) { + retVal = state.ENTERED_ROOM; + } + break; + case ENTERED_ROOM: + if(input == TABLE_ONE) { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_ONE: + if(input == TABLE_TWO) { + retVal = state.TABLE_GAME_TWO; + } else { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_TWO: + if(input == TABLE_THREE) { + retVal = state.TABLE_GAME_THREE; + } else { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_THREE: + if(input == TABLE_TWO) { + retVal = state.TABLE_GAME_FOUR; + } else { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_FOUR: + if(input == TABLE_THREE) { + retVal = state.TABLE_GAME_FIVE; + } else { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_FIVE: + if(input == TABLE_ONE) { + retVal = state.TABLE_GAME_SIX; + } else { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_SIX: + if(input == TABLE_THREE) { + retVal = state.TABLE_GAME_SEVEN; + } else { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_SEVEN: + if(input == BLUE_BUTTON) { + retVal = state.FINISH; + } else { + retVal = state.TABLE_GAME_ONE; + } + break; + } + + + return retVal; } } diff --git a/src/de/ctdo/crashtest/SteuerungFrame.java b/src/de/ctdo/crashtest/SteuerungFrame.java index 13f0a6d..f7faa93 100644 --- a/src/de/ctdo/crashtest/SteuerungFrame.java +++ b/src/de/ctdo/crashtest/SteuerungFrame.java @@ -9,7 +9,7 @@ public class SteuerungFrame extends JFrame { Statemachine machine = new Statemachine(); public SteuerungFrame() { - setType(Type.UTILITY); + //setType(Type.UTILITY); setBackground(Color.black); setBounds(200,200, 200, 200); @@ -17,7 +17,8 @@ public class SteuerungFrame extends JFrame { addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { - System.out.println(e.getKeyChar()); + //System.out.println(e.getKeyChar()); + machine.handleInput(e.getKeyChar()); } });