2013-08-25 2 views
-2

누군가이 "Random Guess My Number Game"에 대한 내 코드를 살펴볼 수 있는지 궁금합니다.C++ 완료 임의 번호 추측 내 번호 게임 둘러보기

여기에 모든 코드입니다 : 나에게 어떤 조언을 줄 수

#include <iostream> 
#include <conio.h> 
#include <stdlib.h> 
#include <time.h> 
using namespace std; 

int PlayerNumber, x; 
bool win = false; 
long foo; 
char milliongame; 
int main(){ 
srand (time(NULL)); 
cout << "Guess your computers number." << endl << endl; 

for (int level = 1, foo = 10; level<=10; level++, foo = foo*2){ 
switch (foo){ 
case 20: 
    foo = 25; 
    break; 
case 200: 
    foo = 250; 
    break; 
case 2000: 
    foo = 2500; 
    break; 
} 

win = false; 
x = (rand()%foo + 1); 
refresh:  

cout << "level " << level << endl; 

cout <<"Computers number is between 1 and "<< foo << endl; 
cout << "Your number: "; cin >> PlayerNumber; 

if (!cin || PlayerNumber > foo || PlayerNumber <= 0){ 
    cout << "That's not a number between 1 and " << foo << " !" << endl; 
    PlayerNumber = 0; 
    system("pause"); 

    system("cls"); 
    goto refresh; 
    } 

if (x > PlayerNumber) 
    cout << "Your number was lower than the computers." << endl; 

if (x < PlayerNumber) 
    cout << "Your number was higher than the computers" << endl; 

if (x == PlayerNumber){ 
     cout << "You Won! \n\nThe computer's number was "<< x << endl; win = true;  
    } 


cout << endl; system("pause"); 
system("cls"); 

if (win != true){ 
    goto refresh; 
    } 
    } 

cout << "You Beat the Game! \n\nDo You Dare Play for a Million? \n[y/n]: "; cin >>   milliongame; 

if (milliongame != 'y') 
return 0; 

mill: 

    cout << "million level!" << endl; 

    x = (rand()%1000000000); 

cout <<"Computers number is between 1 and 1,000,000,000" << endl; 
cout << "Your number: "; cin >> PlayerNumber; 

if (!cin || PlayerNumber > 1000000000 || PlayerNumber <= 0){ 
    cout << "That's not a number between 1 and 1,000,000,000!" << endl; 
    PlayerNumber = 0; 
    system("pause"); 

    system("cls"); 
    goto mill; 
} 

if (x > PlayerNumber) 
    cout << "Your number was lower than the computers." << endl; 

if (x < PlayerNumber) 
    cout << "Your number was higher than the computers" << endl; 

if (x == PlayerNumber) 
    {cout << "You Won! \n\nThe computer's number was "<< x << endl; win =      true; 
} 


} 

누구나 정말 감사하겠습니다.

아이디어는 플레이어의 숫자가 숫자이고 프로그램이 컴퓨터 번호보다 높거나 낮을 지 여부를 알려주는 것입니다.

는 또한 비주얼 C++ 2012

감사로 컴파일 콘솔 응용 프로그램이므로주의!

+6

이 질문은 [Code Review] (http://codereview.stackexchange.com)에 속해 있기 때문에 주제가 아닌 것처럼 보입니다. – chris

답변

3

이 코드 조각 :

if (milliongame == 'y') 
{goto mill;} 
else 
return 0; 

mill: 

    cout << "million level!" << endl; 

당신이 간단하게 교체 할 수 있습니다

if (milliongame != 'y') 
    return 0; 

cout << "million level!" << endl; 

이 :

if (foo == 20) 
    foo = 25; 
if (foo == 200) 
    foo = 250; 
if (foo == 2000) 
    foo = 2500; 

를이와 함께 :

switch (foo) 
{ 
    case 20: 
     foo = 25; 
     break; 
    case 200: 
     foo = 250; 
     break; 
    case 2000: 
     foo = 2500; 
     break; 
} 
,

그리고이 :

if (x > PlayerNumber) 
    cout << "Your number was lower than the computers." << endl; 

if (x < PlayerNumber) 
    cout << "Your number was higher than the computers" << endl; 

if (x == PlayerNumber) 
    {cout << "You Won! \n\nThe computer's number was "<< x << endl; win=true;} 

과 : 당신이

void result(int PlayerNumber, int x) 
{ 
    if (x > PlayerNumber) 
      cout << "Your number was lower than the computers." << endl; 
    else if (x < PlayerNumber) 
      cout << "Your number was higher than the computers" << endl; 
    else 
    { 
     cout << "You Won! \n\nThe computer's number was "<< x << endl; 
     win=true; 
    } 
} 

을하는 함수를 작성하여 호출 할 수 있도록 또한

if (x > PlayerNumber) 
    cout << "Your number was lower than the computers." << endl; 
else if (x < PlayerNumber) 
    cout << "Your number was higher than the computers" << endl; 
else 
{ 
    cout << "You Won! \n\nThe computer's number was "<< x << endl; 
    win=true; 
} 

는 코드의 마지막 조각은, 당신이 프로그램에 두 번 표시

result(PlayerNumber,x); 

이 코드는 어디에 있어야합니다.

+1

@ user2635139 맞습니다.이 문제를 해결했습니다. – cpp