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.
Diff: main.cpp
- Revision:
- 1:60ded2252111
- Parent:
- 0:29ea6e039941
- Child:
- 2:7c413a5377b8
diff -r 29ea6e039941 -r 60ded2252111 main.cpp --- a/main.cpp Tue May 05 19:07:37 2015 +0000 +++ b/main.cpp Thu May 07 09:20:55 2015 +0000 @@ -1,3 +1,5 @@ +// Code by Luis Sousa + //#include <stdio.h> //#include <stdlib.h> //#include <time.h> @@ -12,13 +14,13 @@ int main(void) { pc.baud(9600); - int ntent = 3; //tentativas - int nint = 0; //numero introduzido - int nsec = 0; //numero secreto + int ntent = 3; //max tries + int nint = 0; //guess + int nsec = 0; //secret number int acertou = 0; - int valid = 0; //Numero e valido (0 ..9) + int valid = 0; //valid number? int play = 1; - char opcao='N'; + char opcao='N'; //option int n=0; do { @@ -28,63 +30,63 @@ /* NEW GAME */ do { // clrscr(); - pc.printf("Introduza o Numero Secreto (0-9) \n"); + pc.printf("Enter secret number ? (0-9) \n"); nsec=pc.getc() - '0'; //scanf("%d", &nsec); if((nsec>=0)&(nsec<=9)) valid=1; else { valid=0; - pc.printf("Numero invalido. O numero deve estar entre 0 e 9 \n"); + pc.printf("Invalid number. Must be in the 0 .. 9 range \n "); } } while(! valid); ntent=3; acertou=0; -// clrscr(); /* o jogador 2 nao pode ver os ecrans anteriores */ +// clrscr(); /* player 2 can't see previous screen */ for (n=0; n<40; n++) { pc.printf(" \n"); } - /* Tentativa N */ + do { do { - pc.printf("Adivinhe o numero. Tem %d tentativas \n",ntent); + pc.printf("Guess the number. You have %d tries \n",ntent); nint=pc.getc() -'0'; //scanf("%d", &nint); valid =(nint>=0)&(nint<=9); } while (! valid); ntent--; if (nint < nsec) { - pc.printf("Valor baixo. \n"); + pc.printf("Too low. \n"); redLED = 1; greenLED = 1; blueLED = 0; } else if (nint > nsec) { - pc.printf("Valor alto. \n"); + pc.printf("Too high. \n"); redLED = 0; greenLED = 1; blueLED = 1; } else { /* if (nint == nsec) */ - pc.printf("Parabens acertou!! Faltavam %d tentativas! \n", ntent); + pc.printf("You guessed!! %d tries missing! \n", ntent); acertou = 1; redLED = 1; greenLED = 0; blueLED = 1; } if (ntent ==0 & !acertou) { - pc.printf("Perdeu!!! Esgotou as tentativas \n"); + pc.printf("No more tries!!! You lost! \n"); redLED = 0; greenLED = 1; blueLED = 1; - } else if (!acertou) pc.printf("Tente novamente \n"); + } else if (!acertou) pc.printf("Try Again \n"); } while ((!acertou)&(ntent>0)); - pc.printf("Novo Jogo? (S/N) \n"); + pc.printf("New game? (Y/N) \n"); opcao=pc.getc(); // scanf(" %c", &opcao); // opcao = getchar(); */ - if((opcao =='S')||(opcao =='s')) play=1; + if((opcao =='Y')||(opcao =='y')) play=1; else { play=0; // clrscr(); - pc.printf("Jogo Terminado! \n"); + pc.printf("Game Over! \n"); } } while (play); redLED = 1;