-1

我在 Microsoft Visual Studio 上编写了一小段代码,这是一个简单的程序,用来教别人 C++ 的基础知识。我正在迁移到 Xcode,这对我来说是新的。我需要帮助重新格式化此代码,以便它可以在 Mac 上运行。请帮忙!

#include <iostream>
#include <string>
#include <random> //this needs to be included for the rand() function
#include <time.h> //this needs to be included for the seed time
#include <windows.h>
#include <conio.h>

using namespace std;
//PAB

int random_in_range(int a, int b)//this function will generate a random number between specified range
{
    return (a+rand()%(b-a+1));
}

void hangman(){
    char guess;
    string word="";
    string hidden ="";
    int strikesLeft= 0;


    int random = random_in_range(1,15);

    switch(random){
    case 1:
        word = "bacon";
        hidden ="?????";
        strikesLeft=5;
        break;
    case 2:
        word = "computer";
        hidden ="????????";
        strikesLeft=7;
        break;
    case 3:
        word = "human";
        hidden = "?????";
        strikesLeft=7;
        break;
    case 4:
        word = "desk";
        hidden = "????";
        strikesLeft=7;
        break;
    case 5:
        word = "card";
        hidden = "????";
        strikesLeft=7;
        break;
    case 6: 
        word="keyboard";
        hidden = "????????";
        strikesLeft=7;
        break;
    case 7:
        word="phone";
        hidden="?????";
        strikesLeft=7;
        break;
    case 8:
        word="mouse";
        hidden="?????";
        strikesLeft=7;
        break;
    case 9:
        word="camp";
        hidden="????";
        strikesLeft=7;
        break;
    case 10:
        word="captain";
        hidden="???????";
        strikesLeft=7;
        break;
    case 11:
        word="brother";
        hidden="???????";
        strikesLeft=7;
        break;
    case 12:
        word="beauty";
        hidden="??????";
        strikesLeft=7;
        break;
    case 13:
        word="cave";
        hidden="????";
        strikesLeft=7;
        break;
    case 14:
        word="children";
        hidden="????????";
        strikesLeft=7;
        break;
    case 15:
        word="action";
        hidden="??????";
        strikesLeft=7;
        break;
}

    bool gameOver=false;
    int pos;
    //add strike counter

    cout << "                            ***WELCOME TO HANGMAN***" << endl << endl;
    cout << "Try to guess the word in question marks. But watch out, if you use too many letters not in the word, you will lose." << endl;
    cout << "You start with " << strikesLeft << " strikes." << endl;
    cout << "Good luck..." << endl;

    do{
        cout << "Word is " << hidden << endl << endl;
        cout << "Enter guess: ";
        cin >> guess;

        pos = word.find_first_of(guess);


        if(pos!=-1)
            hidden[pos]=guess;


        else{
            strikesLeft--;
            cout <<"Sorry, " << guess << " is not in this word." << endl;
            cout <<"You have " << strikesLeft << " strikes left." << endl; //also tell them strikes remaining
        }

        if(hidden==word || strikesLeft==0)
            gameOver=true;

    }
    while(gameOver==false);

    if(strikesLeft==0)
    cout << "Game Over! You failed..." << endl;
    cout << "The word was " << word << "." << endl;

    if(strikesLeft>0)
    cout << "Congrats! You completed my game with " << strikesLeft << " strikes left." << endl;



    system("PAUSE");


}

int main(){

cout << "" << endl << endl << endl << endl << endl << endl << endl;
        cout << "                                  *WELCOME*" << endl;

        system("color 0c");

        Sleep(500);

        cout << "                              **TO**" << endl;

        system("color 0f");

        Sleep(500);

        cout << "                     ***PAB***";




        system("color 0a");

        Sleep(3000);

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

        cout << "                             P";
        Sleep(100);
        cout << "E";
        Sleep(100);
        cout << "R";
        Sleep(100);
        cout << "S";
        Sleep(100);
        cout << "O";
        Sleep(100);
        cout << "N";
        Sleep(100);
        cout << "A";
        Sleep(100);
        cout << "L";
        Sleep(1000);
        cout << " A";
        Sleep(100);
        cout << "W";
        Sleep(100);
        cout << "E";
        Sleep(100);
        cout << "S";
        Sleep(100);
        cout << "O";
        Sleep(100);
        cout << "M";
        Sleep(100);
        cout << "E";
        Sleep(1000);
        cout << " B";
        Sleep(150);
        cout << "O";
        Sleep(150);
        cout << "T";
        Sleep(2000);
        system("cls");

        system("color 08");

        cout << "Program loading.";
        Sleep(500);
        system("cls");
        cout << "Program loading..";
        Sleep(500);
        system("cls");
        cout << "Program loading...";
        Sleep(500);
        system("cls");
        cout << "Program loading.";
        Sleep(500);
        system("cls");
        cout << "Program loading..";
        Sleep(500);
        system("cls");
        cout << "Program loading...";
        Sleep(500);
        system("cls");
        cout << "Program loading.";
        Sleep(500);
        system("cls");
        cout << "Program loading..";
        Sleep(500);
        system("cls");



    srand(time(NULL));//seeds the random number generator. Do this before calling the randomInRange function
        string name;
        int friends;
        int DecimalArray[] = {1,2,3,4,5,22,555,85,18,741}; //Create an array of decimal numbers.

        system ("color 0f");

        cout << "Enter name:  ";
        cin >> name;
        cout << "Hi " << name << "." << " My name is PAB, your personal awesome bot." << endl; 

        cout << "Now tell me, how many friends do you have? " << endl;
        cin >> friends;

        if(friends >=75 && friends <300)
            cout << "Gee, " << friends << " friends is a lot. But you could always have one more...ME!!" << endl; 
        if(friends >300)
            cout << "Yeah, maybe on Facebook...But we should still be friends!" << endl;

        if(friends <75)
            cout << "Man, you totally need more friends. I can be one of them!" << endl;


        int randomNum=random_in_range(1,50);
        int numGuess;

        cout << "Now that we're friends, I want to play a game. Now, pick a number between 1 and 50. " << endl;
        do{ //dowhile loop using "getting closer" for when your getting closer to the number
            cin >> numGuess;
            if(numGuess >50)
                cout << "Can you read? It clearly says between 1 and 50. " << endl;
            if(numGuess >randomNum)
                cout << "That's too high guess again. " << endl;
            if(numGuess <randomNum)
                cout << "That's too low please guess again. " << endl;

        }while(numGuess !=randomNum);
        system("cls");
                cout << "Congratulations! You found out my number." << endl;
                cout << "Okay, I agree that was stupid. But, I have another game!" << endl << endl << endl;
        hangman();

        system("cls");
        cout << "Thanks for playing with me today! I hope you had fun." << endl;
        cout << "In the future I will have new games and jokes." << endl << endl << endl << endl << endl << endl;
        cout << "                                   THE END" << endl;

        Sleep(5000);

        system("cls");
        system("color 08");
        cout << "v1.7" << endl;
        cout << "[copyright]" << endl;
        cout << "Ethan MacCumber 2012" << endl;




    system("PAUSE");
    return 0;

}
4

1 回答 1

0

您的主要问题是您的代码并不完全是标准的 C++。您使用了仅在 Windows 上可用的 C++ 代码、windows.h 和 conio.h 头文件中的函数。这些头文件中的任何功能都无法在 Mac 上运行。

如果您希望您的代码在 Mac OS X 上运行,请注释掉 windows.h 和 conio.h 的包含。注释掉特定于 Windows 的标头的包含将产生大量编译器错误,这应该会告诉您特定于 Windows 的代码在哪里。摆脱那些代码并坚持使用标准 C++ 的代码,例如 cin 和 cout。快速浏览一下您的代码,对 Sleep() 和 System("cls") 的调用在 Mac 上不起作用。

于 2012-11-27T21:13:21.840 回答