Hangman game using qp a 16x2 LCD and joystick.

Dependencies:   TextLCD mbed qp

Committer:
tylerjw
Date:
Wed Feb 08 22:20:11 2012 +0000
Revision:
0:1521c946a57b
Child:
1:4efaebc256d3
Problems with lib/qp, please help!
Player class hasn\t been developed yet.
BSP is still missing some features.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerjw 0:1521c946a57b 1 #include "qp_port.h"
tylerjw 0:1521c946a57b 2 #include "hangman.h"
tylerjw 0:1521c946a57b 3 #include "bsp.h"
tylerjw 0:1521c946a57b 4
tylerjw 0:1521c946a57b 5 class Host : public QActive {
tylerjw 0:1521c946a57b 6 private:
tylerjw 0:1521c946a57b 7 char* word;
tylerjw 0:1521c946a57b 8 uint8_t selected_word;
tylerjw 0:1521c946a57b 9 uint8_t num_letters;
tylerjw 0:1521c946a57b 10 uint8_t incorrect_letters;
tylerjw 0:1521c946a57b 11 char* used_letters;
tylerjw 0:1521c946a57b 12
tylerjw 0:1521c946a57b 13 char letter;
tylerjw 0:1521c946a57b 14 char output1[17]; // buffer for display
tylerjw 0:1521c946a57b 15 char output2[17];
tylerjw 0:1521c946a57b 16
tylerjw 0:1521c946a57b 17 public:
tylerjw 0:1521c946a57b 18 Host();
tylerjw 0:1521c946a57b 19
tylerjw 0:1521c946a57b 20 private:
tylerjw 0:1521c946a57b 21 static QState initial(Host *me, QEvent const *e);
tylerjw 0:1521c946a57b 22 static QState welcome(Host *me, QEvent const *e);
tylerjw 0:1521c946a57b 23 static QState playing(Host *me, QEvent const *e);
tylerjw 0:1521c946a57b 24 };
tylerjw 0:1521c946a57b 25
tylerjw 0:1521c946a57b 26 #define BANK_SIZE 4
tylerjw 0:1521c946a57b 27 char* const word_bank[] = { "hangmann", "global", "micro", "sensor" };
tylerjw 0:1521c946a57b 28 char* const win_msg = " You Win!";
tylerjw 0:1521c946a57b 29 char* const loose_msg = " You suck!";
tylerjw 0:1521c946a57b 30 char* const press_play_msg = "Push to continue";
tylerjw 0:1521c946a57b 31
tylerjw 0:1521c946a57b 32
tylerjw 0:1521c946a57b 33 // Local objects -------------------------------------------------------------
tylerjw 0:1521c946a57b 34 static Host l_host; // local Host object
tylerjw 0:1521c946a57b 35
tylerjw 0:1521c946a57b 36 // Public-scope objects ------------------------------------------------------
tylerjw 0:1521c946a57b 37 QActive * const AO_Host = &l_host; // "opaque" AO pointer
tylerjw 0:1521c946a57b 38
tylerjw 0:1521c946a57b 39 //............................................................................
tylerjw 0:1521c946a57b 40 Host::Host() : QActive((QStateHandler)&Host::initial) {
tylerjw 0:1521c946a57b 41 }
tylerjw 0:1521c946a57b 42
tylerjw 0:1521c946a57b 43 //............................................................................
tylerjw 0:1521c946a57b 44 QState Host::initial(Host *me, QEvent const *e) {
tylerjw 0:1521c946a57b 45
tylerjw 0:1521c946a57b 46 QS_OBJ_DICTIONARY(&l_host);
tylerjw 0:1521c946a57b 47 QS_FUN_DICTIONARY(&QHsm::top);
tylerjw 0:1521c946a57b 48 QS_FUN_DICTIONARY(&Host::initial);
tylerjw 0:1521c946a57b 49 QS_FUN_DICTIONARY(&Host::playing);
tylerjw 0:1521c946a57b 50
tylerjw 0:1521c946a57b 51 QS_SIG_DICTIONARY(TERMINATE_SIG, 0); // global signal
tylerjw 0:1521c946a57b 52
tylerjw 0:1521c946a57b 53 QS_SIG_DICTIONARY(START_SIG, me); // signals for Host
tylerjw 0:1521c946a57b 54 QS_SIG_DICTIONARY(SCROLL_SIG, me);
tylerjw 0:1521c946a57b 55 QS_SIG_DICTIONARY(SELECT_SIG, me);
tylerjw 0:1521c946a57b 56
tylerjw 0:1521c946a57b 57 me->subscribe(TERMINATE_SIG);
tylerjw 0:1521c946a57b 58
tylerjw 0:1521c946a57b 59 // output welcome message
tylerjw 0:1521c946a57b 60 BSP_lcdUpdate("Push btn to play mbed hangman!","");
tylerjw 0:1521c946a57b 61 wait(2.0); // pause for 2 seconds
tylerjw 0:1521c946a57b 62
tylerjw 0:1521c946a57b 63 return Q_TRAN(&Host::welcome);
tylerjw 0:1521c946a57b 64 }
tylerjw 0:1521c946a57b 65
tylerjw 0:1521c946a57b 66 //............................................................................
tylerjw 0:1521c946a57b 67 QState Host::welcome(Host *me, QEvent const *e) {
tylerjw 0:1521c946a57b 68 HostEvt *pe;
tylerjw 0:1521c946a57b 69
tylerjw 0:1521c946a57b 70 switch (e->sig) {
tylerjw 0:1521c946a57b 71 case START_SIG: {
tylerjw 0:1521c946a57b 72 uint8_t idx;
tylerjw 0:1521c946a57b 73
tylerjw 0:1521c946a57b 74 // init the game
tylerjw 0:1521c946a57b 75 time_t seconds = time(NULL);
tylerjw 0:1521c946a57b 76 me->selected_word = seconds % BANK_SIZE;
tylerjw 0:1521c946a57b 77 me->word = word_bank[me->selected_word];
tylerjw 0:1521c946a57b 78 me->num_letters = strlen(me->word);
tylerjw 0:1521c946a57b 79 me->incorrect_letters = 0;
tylerjw 0:1521c946a57b 80 for (int i = 0; i < 17; i++) { // clear output
tylerjw 0:1521c946a57b 81 me->output1[i] = ' ';
tylerjw 0:1521c946a57b 82 me->output2[i] = ' ';
tylerjw 0:1521c946a57b 83 }
tylerjw 0:1521c946a57b 84
tylerjw 0:1521c946a57b 85 for (idx = 0; idx < me->num_letters; idx++) {
tylerjw 0:1521c946a57b 86 me->output1[idx] = '_';
tylerjw 0:1521c946a57b 87 }
tylerjw 0:1521c946a57b 88 for (; idx < 16; idx++) {
tylerjw 0:1521c946a57b 89 me->output1[idx] = ' ';
tylerjw 0:1521c946a57b 90 }
tylerjw 0:1521c946a57b 91 me->letter = 'a';
tylerjw 0:1521c946a57b 92 idx = 16;
tylerjw 0:1521c946a57b 93 me->output1[idx] = me->letter;
tylerjw 0:1521c946a57b 94 // clear output2
tylerjw 0:1521c946a57b 95 for (idx = 0; idx < 17; idx++)
tylerjw 0:1521c946a57b 96 me->output2[idx] = ' ';
tylerjw 0:1521c946a57b 97
tylerjw 0:1521c946a57b 98 BSP_lcdUpdate(me->output1, me->output2); // update display
tylerjw 0:1521c946a57b 99
tylerjw 0:1521c946a57b 100 // post play to player
tylerjw 0:1521c946a57b 101 pe = Q_NEW(HostEvt, PLAY_SIG);
tylerjw 0:1521c946a57b 102 pe->scroll_pos = 0; // center
tylerjw 0:1521c946a57b 103 QF::PUBLISH(pe, me);
tylerjw 0:1521c946a57b 104
tylerjw 0:1521c946a57b 105 return Q_TRAN(&Host::playing);
tylerjw 0:1521c946a57b 106 }
tylerjw 0:1521c946a57b 107 case TERMINATE_SIG: {
tylerjw 0:1521c946a57b 108 QF::stop();
tylerjw 0:1521c946a57b 109 return Q_HANDLED();
tylerjw 0:1521c946a57b 110 }
tylerjw 0:1521c946a57b 111 }
tylerjw 0:1521c946a57b 112 return Q_SUPER(&QHsm::top);
tylerjw 0:1521c946a57b 113 }
tylerjw 0:1521c946a57b 114
tylerjw 0:1521c946a57b 115 //............................................................................
tylerjw 0:1521c946a57b 116 QState Host::playing(Host *me, QEvent const *e) {
tylerjw 0:1521c946a57b 117
tylerjw 0:1521c946a57b 118 HostEvt *pe;
tylerjw 0:1521c946a57b 119
tylerjw 0:1521c946a57b 120 switch (e->sig) {
tylerjw 0:1521c946a57b 121 case SCROLL_SIG: {
tylerjw 0:1521c946a57b 122 if (((HostEvt const *)e)->scroll_pos < 0) { // go down
tylerjw 0:1521c946a57b 123 if (me->letter == 'a') // wrap around
tylerjw 0:1521c946a57b 124 me->letter = 'z';
tylerjw 0:1521c946a57b 125 else
tylerjw 0:1521c946a57b 126 me->letter--;
tylerjw 0:1521c946a57b 127 } else if (((HostEvt const *)e)->scroll_pos > 0) { // go up
tylerjw 0:1521c946a57b 128 if (me->letter == 'z') // wrap around
tylerjw 0:1521c946a57b 129 me->letter = 'a';
tylerjw 0:1521c946a57b 130 else
tylerjw 0:1521c946a57b 131 me->letter++;
tylerjw 0:1521c946a57b 132 }
tylerjw 0:1521c946a57b 133 me->output1[16] = me->letter;
tylerjw 0:1521c946a57b 134 BSP_lcdUpdate(me->output1,me->output2);
tylerjw 0:1521c946a57b 135
tylerjw 0:1521c946a57b 136 return Q_HANDLED();
tylerjw 0:1521c946a57b 137 }
tylerjw 0:1521c946a57b 138 case SELECT_SIG: {
tylerjw 0:1521c946a57b 139 // test for letter in word
tylerjw 0:1521c946a57b 140 char * pch;
tylerjw 0:1521c946a57b 141 pch=strchr(me->word,me->letter);
tylerjw 0:1521c946a57b 142 if (pch != NULL) { // found in word
tylerjw 0:1521c946a57b 143 do {
tylerjw 0:1521c946a57b 144 me->word[pch-me->word] = ' ';
tylerjw 0:1521c946a57b 145 me->output1[pch-me->word] = me->letter;
tylerjw 0:1521c946a57b 146 pch=strchr(pch+1,me->letter);
tylerjw 0:1521c946a57b 147 } while (pch!=NULL);
tylerjw 0:1521c946a57b 148 // update screen
tylerjw 0:1521c946a57b 149 BSP_lcdUpdate(me->output1,me->output2);
tylerjw 0:1521c946a57b 150 // test for win
tylerjw 0:1521c946a57b 151 if (strpbrk(me->word,"abcdefghijklmnopqrs") == NULL) { // win!
tylerjw 0:1521c946a57b 152 BSP_lcdScrollIn(win_msg, press_play_msg); // win msg
tylerjw 0:1521c946a57b 153 // post play to player
tylerjw 0:1521c946a57b 154 HostEvt *pe = Q_NEW(HostEvt, WIN_SIG);
tylerjw 0:1521c946a57b 155 pe->scroll_pos = 0; // center
tylerjw 0:1521c946a57b 156 QF::PUBLISH(pe, me);
tylerjw 0:1521c946a57b 157
tylerjw 0:1521c946a57b 158 // go to welcome state
tylerjw 0:1521c946a57b 159 return Q_TRAN(&Host::welcome);
tylerjw 0:1521c946a57b 160 }
tylerjw 0:1521c946a57b 161 } else {
tylerjw 0:1521c946a57b 162 if (++me->incorrect_letters >= MAX_LETTERS) { // loose
tylerjw 0:1521c946a57b 163 BSP_lcdScrollIn(loose_msg, press_play_msg); // message
tylerjw 0:1521c946a57b 164 // post play to player
tylerjw 0:1521c946a57b 165 HostEvt *pe = Q_NEW(HostEvt, WIN_SIG);
tylerjw 0:1521c946a57b 166 pe->scroll_pos = 0; // center
tylerjw 0:1521c946a57b 167 QF::PUBLISH(pe, me);
tylerjw 0:1521c946a57b 168
tylerjw 0:1521c946a57b 169 // go to welcome state
tylerjw 0:1521c946a57b 170 return Q_TRAN(&Host::welcome);
tylerjw 0:1521c946a57b 171 } else {
tylerjw 0:1521c946a57b 172 // post to used letters
tylerjw 0:1521c946a57b 173 me->output2[me->incorrect_letters-1] = (char)(me->letter - 33); // make uppercase
tylerjw 0:1521c946a57b 174 BSP_lcdUpdate(me->output1,me->output2); // update screen
tylerjw 0:1521c946a57b 175 }
tylerjw 0:1521c946a57b 176 }
tylerjw 0:1521c946a57b 177 return Q_HANDLED();
tylerjw 0:1521c946a57b 178 }
tylerjw 0:1521c946a57b 179 case TERMINATE_SIG: {
tylerjw 0:1521c946a57b 180 QF::stop();
tylerjw 0:1521c946a57b 181 return Q_HANDLED();
tylerjw 0:1521c946a57b 182 }
tylerjw 0:1521c946a57b 183 }
tylerjw 0:1521c946a57b 184 return Q_SUPER(&QHsm::top);
tylerjw 0:1521c946a57b 185 }