Guess a number. Using mbed serial port and a terminal on the PC, we can play this 2 player game. First player sets the secret number between 0 ..9, second player has 3 tries to guess it.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
lmsousa
Date:
Thu May 07 10:23:53 2015 +0000
Parent:
1:60ded2252111
Commit message:
Fixed serial communication issues with carriage returns.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu May 07 09:20:55 2015 +0000
+++ b/main.cpp	Thu May 07 10:23:53 2015 +0000
@@ -30,55 +30,55 @@
         /* NEW GAME */
         do {
 //         clrscr();
-            pc.printf("Enter secret number ? (0-9) \n");
+            pc.printf("Enter secret number ? (0-9) \r\n");
             nsec=pc.getc() - '0';
             //scanf("%d", &nsec);
             if((nsec>=0)&(nsec<=9)) valid=1;
             else {
                 valid=0;
-                pc.printf("Invalid number. Must be in the 0 .. 9 range \n ");
+                pc.printf("Invalid number. Must be in the 0 .. 9 range \r\n ");
                 }
         } while(! valid);
         ntent=3;
         acertou=0;
 //      clrscr(); /* player 2 can't see previous screen */
         for (n=0; n<40; n++) {
-            pc.printf(" \n");
+            pc.printf(" \r\n");
         }
 
         do {
             do {
-                pc.printf("Guess the number. You have %d tries \n",ntent);
+                pc.printf("Guess the number. You have %d tries \r\n",ntent);
                 nint=pc.getc() -'0';
                 //scanf("%d", &nint);
                 valid =(nint>=0)&(nint<=9);
             } while (! valid);
             ntent--;
             if (nint < nsec) {
-                pc.printf("Too low.  \n");
+                pc.printf("Too low.  ");
                 redLED = 1;
                 greenLED = 1;
                 blueLED = 0;
             } else if (nint > nsec) {
-                pc.printf("Too high.  \n");
+                pc.printf("Too high.  ");
                 redLED = 0;
                 greenLED = 1;
                 blueLED = 1;
             } else { /* if (nint == nsec) */
-                pc.printf("You guessed!! %d tries missing! \n", ntent);
+                pc.printf("You guessed!! %d tries missing! \r\n\n", ntent);
                 acertou = 1;
                 redLED = 1;
                 greenLED = 0;
                 blueLED = 1;
             }
             if (ntent ==0 & !acertou) {
-                pc.printf("No more tries!!! You lost! \n");
+                pc.printf("No more tries!!! You lost! \r\n\n");
                 redLED = 0;
                 greenLED = 1;
                 blueLED = 1;
-            } else if (!acertou) pc.printf("Try Again \n");
+            } else if (!acertou) pc.printf("Try Again \r\n\n");
         } while ((!acertou)&(ntent>0));
-        pc.printf("New game? (Y/N) \n");
+        pc.printf("New game? (Y/N) \r\n");
         opcao=pc.getc();
 //      scanf(" %c", &opcao);
 //      opcao = getchar();    */
@@ -86,7 +86,7 @@
         else {
             play=0;
 //          clrscr();
-            pc.printf("Game Over! \n");
+            pc.printf("Game Over! \r\n");
         }
     } while (play);
     redLED = 1;