2013-08-25 80 views
-2

我想知道如果有人可以看看我的代碼這個「隨機猜我的號碼遊戲」。C++完成隨機猜測我的號碼遊戲Lookover

這裏的所有代碼:

#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; 
} 


} 

任何人誰可以給我任何提示將非常感激。

這個想法是,玩家geuss的一個數字,程序會告訴他們它是高於還是低於電腦的數量。

另請注意,它是一個用Visual C++ 2012編譯的控制檯應用程序

謝謝!

+6

這個問題似乎是脫離主題,因爲它屬於[代碼評論](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;} 

有:

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; 
} 

而且,代碼的最後一塊在你的程序中出現兩次,所以你可以創建一個函數

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; 
    } 
} 

,並調用它

result(PlayerNumber,x); 

此代碼應該在哪裏。

+1

@ user2635139,沒錯,我已經解決了這個問題。 – cpp