Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Gra w znajdywanie numerka.

Ostatnio zmodyfikowano 2011-12-26 15:54
Autor Wiadomość
marc_xxx
Temat założony przez niniejszego użytkownika
Gra w znajdywanie numerka.
» 2011-12-25 23:27:46
Witam
Napisałem grę w odszukanie wylosowanej wartości. Oto kod:
C/C++
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
    srand( time( NULL ) );
    int x = 0, y = 0, z = 1;
    cout << "Select number between 0 - 100\n";
    while( z ) //Pętla główna działania aplikacji
    {
        x = rand() % 100;
        y = 101;
        while( y != x )
        {
            cin >> y;
            if( y > x )
                 cout << "It's too big\n";
           
            if( y < x )
                 cout << "It's too small\n";
           
        }
       
        cout << "A bull's-eye!!! Secret number is " << x << " :D\n";
        cout << "If you wanna play again you should type another number than 0.\nGame will close if you type 0.";
        cin >> z;
    }
   
    cin.ignore();
    getchar();
    return 0;
}
Zastanawia mnie jakby można zrobić to lepiej. Jak ten kod można skrócić (algorytm!!!!) nie mam na myśli wyświetlania tekstu. Może napisałem dobrze. To jest mała gierka i nawet jak skrócę kod to nie będzie widać różnicy ale jak będę się uczyć pisać jak najlepiej programy od takich małych programów to lepiej wyjdzie to w przyszłości :D :D :D Jak byście to napisali? Co można skrócić? Sorki za takie głupie pytanie ... ;D ;D ;D
P-46271
Terminator3
» 2011-12-25 23:42:40
Jak dla mnie to kod dobry, jedyne co bym zmienił może to typ zmiennej 'z' na bool oraz warunki:
C/C++
if( y > x )
     cout << "It's too big\n";

if( y < x )
     cout << "It's too small\n";
na
C/C++
cout <<( y > x ) ? "It's too big"
    : "It's too small" << endl;
lub jeśli to zbyt nieczytelne
C/C++
if( y > x )
     cout << "It's too big" << endl;
else
     cout << "It's too small" << endl;


Jestem też zwolennikiem pisania endl zamiast w tekście \n
P-46272
pekfos
» 2011-12-26 15:54:09
C/C++
#include <iostream>
#include <ctime>
#include <cstdlib>
int main() {
    srand( time( nullptr ) );
    int s = - 1, c;
    do {
        std::cout << "Find num. 0-100" << std::endl;
        s = rand() % 100;
        for( std::cin >> c; s != c; std::cin >> c ) {
            std::cout <<( c < s ? "too small": "too big" ) << std::endl;
        }
        std::cout << "Good! Again? (y/any)";
        std::cin.ignore();
    } while( std::cin.get() == 'y' );
   
}
obsługę błędów tu bardzo łatwo dorobić :P
P-46314
« 1 »
  Strona 1 z 1