Dependencies:   MMA8452 N5110 PowerControl beep mbed

Revision:
3:1974db5993ef
Child:
4:c141e252d786
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GameFSM.h	Mon May 04 15:55:32 2015 +0000
@@ -0,0 +1,223 @@
+/**
+@file GameFSM.h
+
+@brief Header file of Game's Finite State Machine
+*/
+#ifndef GAMEFSM_H
+#define GAMEFSM_H
+
+#include "Game.h"
+#include "Clock.h"
+#include "N5110.h"
+#include "Menu.h"
+
+Game game;
+Clock countdown;
+Menu menu;
+
+void resetButton()
+{
+    BuDFlag = 0;
+    BuUFlag = 0;
+    BuAFlag = 0;
+    BuBFlag = 0;
+};
+class FSM
+{
+public:
+    /**
+    The function contains FSM of the whole programme
+    */
+    void proact();
+};
+
+void FSM::proact()
+{
+    switch(state) {
+        case 0: //Main state 0: MAIN MENU - Cursor at Play line
+            lcd.clear();
+            menu.main();
+            lcd.drawCircle(23,27,2,1);
+            if (BuDFlag) {
+                resetButton();
+                state = 1;
+            }
+            if (BuAFlag) {
+                resetButton();
+                state = 3;
+            }
+            break;
+
+        case 1: //Main state 1: MAIN MENU - Cursor at Score line
+            lcd.clear();
+            menu.main();
+            lcd.drawCircle(20,35,2,1);
+            if (BuUFlag) {
+                resetButton();
+                state = 0;
+            }
+            if (BuAFlag) {
+                resetButton();
+                state = 2;
+            }
+            break;
+
+        case 2: //Main state 2: HIGH SCORE sector
+            lcd.clear();
+            menu.highscore();
+            char HScore[5];
+            sprintf(HScore," %d",game.score);
+            lcd.printString(HScore,32,3);
+            if (BuBFlag) {
+                resetButton();
+                state = 1;
+            }
+            break;
+
+            //This case is a game condition setting case
+            //Clock countdown was set to 60 seconds
+            //Score was set to 0
+        case 3: //Main state 3: RESETING STATE - Before entering GAME MODE menu
+            game.reset();
+            resetButton();
+            state = 4;
+            break;
+//////////GAME MODE SECTOR/////////
+        case 4: //(Main state 4)Sub state 0: GAME MODE sector - Cursor at Easy line
+            lcd.clear();
+            menu.gameset();
+            lcd.drawCircle(23,19,2,1);
+            if (BuAFlag) {
+                resetButton();
+                state = 5;
+            }
+            if (BuBFlag) {
+                resetButton();
+                state = 0;
+            }
+            if (BuDFlag) {
+                resetButton();
+                state = 8; //Go to state 8 of Game set menu
+            }
+            break;
+
+        case 5: //Sub state 1: GAME SCREEN sector - Easy mode
+            lcd.clear();
+            timer.attach(&timerExpired,0.5);
+            gatimer.attach(&gatimerExpired,0.1); //Update time for Easy mode is 0.1s
+            while(CClock > 0) {
+                countdown.countDown();
+                game.easyMode();
+            }
+            if (CClock == 0) {
+                countdown.timeout();
+            }
+            resetButton();
+            state = 10; //Go to state 10: Your score menu
+            break;
+
+        case 6: //Sub state 2: GAME SCREEN - Normal mode
+            lcd.clear();
+            timer.attach(&timerExpired,0.5);
+            gatimer.attach(&gatimerExpired,0.2); //Update time for Normal mode is 0.2s
+            while(CClock > 0) {
+                countdown.countDown();
+                game.norMode(); //Normal mode of game is activated
+            }
+            if (CClock == 0) {
+                countdown.timeout();
+            }
+            resetButton();
+            state = 10; //Go to state 10: Your score menu
+            break;
+
+        case 7: //Sub state 3: GAME SCREEN menu - Hard mode
+            lcd.clear();
+            timer.attach(&timerExpired,0.5);
+            gatimer.attach(&gatimerExpired,0.4); //Update time for Hard mode is 0.4s
+            while(CClock > 0) {
+                countdown.countDown();
+                game.hardMode(); //Hard mode of the game is activated
+            }
+            if (CClock == 0) {
+                countdown.timeout();
+            }
+            resetButton();
+            state = 10; //Go to state 10: Your score menu
+            break;
+
+        case 8: //Sub state 2: GAME MODE menu - Cursor at Normal line
+            lcd.clear();
+            menu.gameset();
+            lcd.drawCircle(17,27,2,1);
+            if (BuAFlag) {
+                resetButton();
+                state = 6; //Go to state 6: Normal mode game
+            }
+            if (BuBFlag) {
+                resetButton();
+                state = 0; //Go back to state 0: Main menu
+            }
+            if (BuUFlag) {
+                resetButton();
+                state = 4; //Go to state 4: Game mode menu
+            }
+            if (BuDFlag) {
+                resetButton();
+                state = 9; //Go to state 9: Game mode menu
+            }
+            break;
+
+        case 9: //Sub state 3: GAME MODE menu - Cursor at Hard line
+            lcd.clear();
+            menu.gameset();
+            lcd.drawCircle(23,35,2,1);
+            if (BuAFlag) {
+                resetButton();
+                state = 7;
+            }
+            if (BuBFlag) {
+                resetButton();
+                state = 0;
+            }
+            if (BuUFlag) {
+                resetButton();
+                state = 8;
+            }
+            break;
+
+        case 10: //Main state 5: YOUR SCORE menu - Cursor at Main menu line
+            lcd.clear();
+            menu.yourscore();
+            lcd.drawCircle(8,27,2,1);
+            char YScore[14];
+            sprintf(YScore,"%d",game.score);
+            lcd.printString(YScore,37,2);
+            if (BuDFlag) {
+                resetButton();
+                state = 11;
+            }
+            if (BuAFlag) {
+                resetButton();
+                state = 0;
+            }
+            break;
+
+        case 11: //Main state 6: YOUR SCORE menu - Cursor at Reset line
+            lcd.clear();
+            menu.yourscore();
+            lcd.drawCircle(8,35,2,1);
+            lcd.printString(YScore,37,2);
+            if (BuUFlag) {
+                resetButton();
+                state = 10;
+            }
+            if (BuAFlag) {
+                resetButton();
+                state = 3;
+            }
+            break;
+    }
+    wait(0.1);
+}
+#endif
\ No newline at end of file