qq_41397956
qq_41397956
采纳率50%
2019-04-17 00:14 阅读 1.1k

求助C语言的猜数字游戏

C语言课程设计要求

1、 课程设计目的
1、 掌握用C语言进行程序设计的基本方法 。
2、 熟练掌握window控制台应用程序的编写。
3、 初步完成一个应用程序的设计、编码、调试,了解程序设计过程,锻炼实际应用能力。
2、 设计题目及内容
猜数字游戏:
计算机随机生成没有重复的n个数,由用户从键盘输入要猜测的数。每猜一次,电脑给出提示信息。如果用户输入的数跟计算机生成的数,数值和位置都完全相等,则胜出。
基本功能要求:
1. 随机生成没有重复的4个数。(每个数字在0-9范围内)。
2. 用户从键盘输入4个不重复的数,程序能对用户输入的数与随机生成的数进行匹配,并给出提示信息:“有 x个数数值和位置都相同!有 y个数数值相同,但位置不同。”
3. 控制用户的尝试次数为10次。
4. 计算、输出并保存用户的得分,计分规则:总分是100分,每失败一次扣10分。
5. 有操作菜单,能根据不同选择运行不同功能:
1:开始游戏!
2:查看上次成绩!
3:输出帮助信息!
0:退出程序!
扩展功能要求:
1. 对用户输入的数字进行检查,如果用户输入重复的数,给出提示,要求重新输入。
2. 能对游戏进行设置:在操作菜单中增加相应的选项:“4:系统设置”。
(1) 修改要猜的数字的个数n,根据修改后的n值,计算机可随机生成n个数,用户进行n个数的猜测游戏。
(2) 修改可以尝试的次数。
3. 若游戏胜出且成绩在前五名,可输入并记录对应的用户姓名,并添加到积分榜中。操作菜单中相应选项改为“2:积分榜!”,用于把积分榜中保存的前五名成绩及用户姓名显示出来。
4. 能对积分榜的成绩进行排序和输出。
提示:(1)生成一个0~9的随机数,使用rand函数:
#include
#include //使用rand函数必须使用此头文件
#include
int main()
{​int a;
​ ​srand(time(0)); //生成一个种子。整个程序只写一次即可。
​ ​a=rand()%10; //生成一个随机数。
​ printf("%d\n",a);
​ ​return 0;
}
(2)保证生成的n个数不重复:
可用数组记录生成的数,每生成一个数,要与之前已经生成的进行比较,如果有相同的​重新生成,直到不相同。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    sangeng_sama sangeng_sama 2019-04-17 16:22

    我只有C++的
    // name: Kyle Burmark
    // professor: M. Gelotte
    // date: 2/26/02
    // Description: This is a computer version of the game hangman, the user
    // can play against a 2nd player or the computer, and he can enter how many
    // guesses he gets,
    // Start of program
    // Header file definitions

    #include // Standard input/output
    #include // String manipulation
    #include // Character manipulation and testing
    #include // File stream
    #include // Used for random function
    #include // Used for better random number
    #include "draw.h" // Draws hangman

    using namespace std;
    // Function declerations
    void instruction(int& choice); // Gives instructions and gets choice
    void usergame(int i); // Plays 2nd user game
    void compgame(int i); // Plays against computer
    void test(string word, char letter, int& numwrong, string& temp, int i); // Tests current letter and replaces starred word
    void lchosen(char letter, string& letterchosen, int& check, int& chosencounter, int i); // Checks current letter and adds it to letters chosen output if not entered already
    void rnd(string& word, int i); // Gets random word from file
    void drawman(int numguess, int numwrong, int i); // Draws hangman
    inline istream& Flush(istream& stream); // Flushes cin stream

    // Start of main
    int main()
    {

    int i = 0; // Counter variable for loops
    int exit = 0; // Main loop exit variable
    int choice; // Users inputed choice for type of game or to exit
    // Main control loop
    do { // while exit != 1
    system("cls"); // 执行系统命令清屏Clear the screen
    instruction(choice); // Give instructions
    switch(choice)
    {
    case 1:
    usergame(i); // Calls user game
    break;
    case 2:
    compgame(i); // Calls computer game
    break;
    case 3:
    cout << "Goodbye" << endl; // Exits
    exit = 1;
    break;
    default:
    cerr << "Invalid choice - try again" << endl; // Invalid choice erroe
    }
    } while (exit != 1);
    // End main loop
    system("pause"); // 执行系统命令:暂停
    return 0;
    }
    // End main
    // Subprograms
    // ---------------------------------------------------------------------
    // Instructions subprogram

    void instruction(int& choice)
    {

    cout << " -Hangman-" << endl << endl;
    cout << " Created by Kyle Burmark" << endl << endl;
    cout << "*****************************************" << endl;
    cout << endl;
    cout << " Enter -1- to play against user" << endl;
    cout << " Enter -2- to play against computer" << endl;
    cout << " Enter -3- to quit" << endl;
    cout << endl;
    cout << "*****************************************" << endl << endl;
    cout << "Choice: "; // Get user choice
    cin >> choice;
    // Bad data type check
    while (!cin)
    {
    cerr << "Invalid character" << endl
    << "Enter again - choice: ";
    Flush(cin);
    cin >> choice;
    }
    // End check
    system("cls");
    }
    // End instruction
    // User game subprogram
    void usergame(int i)
    {
    int numguess = 0; // Number of guesses player gets
    int numwrong = 0; // Number of wrong guesses player has so far
    int check; // A variable to test whether to check the letter entered in the test function
    int wordcheck; // A variable to check whether the user has entered an invalid word
    int end = 0; // A variable to test what to output to the user and to exit the usergame loop
    int chosencounter = 0;

    // A counter to tell the replace function where to put the letter chosen in the letterchosen string
    char letter; // User inputed letter
    string word; // Word user is trying to guess
    string temp; // Updated output word user sees
    string letterchosen = " ";

    // Defines length of letterchosen string
    do { // while user puts in guesses outside of allowed range
    cout << "How many chances does the person have (4 - 10): ";
    cin >> numguess;
    } while (numguess < 4 || numguess > 10);
    cout << "Enter word 2nd user: ";
    cin >> word;
    do { // while user inputs bad word
    wordcheck = 0;
    for (int i = 0; i < word.length(); i++)
    {
    if (!isalpha(word.at(i)))
    {
    wordcheck = 1;
    }
    }
    if (wordcheck == 1)
    {
    cout << "Invalid - Enter word again: ";
    cin >> word;
    }
    } while (wordcheck == 1);
    temp = word; // Sets temp string length to word string length
    // Replace temp with stars loop
    for (i = 0; i < word.length(); i++)
    {
    temp.replace(i, 1, 1,'*');
    }
    // End loop
    system("cls");
    // Main game loop
    do {
    drawman(numguess, numwrong, i);
    // Tests if user guessed word
    if (word == temp)
    { cout << endl << endl;
    cout << "You guessed it [ " << word << " ]" << endl << endl;
    system("pause");
    end = 1;
    }
    // Tests if user failed to guess word
    if (numwrong == numguess)
    {
    cout << endl << endl;
    cout << "You failed" << endl << endl;
    system("pause");
    end = 2;
    }
    // Play while above conditions aren't true
    if (end == 0)
    {
    cout << endl << endl << endl;
    cout << "Letters chosen: " << letterchosen << endl;
    cout << endl << endl << endl;
    cout << "Guesses left: " << numguess - numwrong << endl << endl;
    cout << " " << temp << endl << endl;
    cout << "Letter: ";
    cin >> letter;
    // Test for bad letter
    while (!isalpha(letter))
    {
    Flush(cin);
    cout << "Not a letter - enter letter: ";
    cin >> letter;
    }
    // End test
    lchosen(letter, letterchosen, check, chosencounter, i);
    // Test whether to check letter against correct word string
    if (check == 0) { test(word, letter, numwrong, temp, i); }
    else { ; }
    // End test
    system("cls");
    }
    // End game
    system("cls");
    } while(end != 1 && end != 2);
    // End main game loop

    // Tests end variable to see what to display
    if (end == 2) 
    {
        cout << "Correct word was [ " << word << " ]" << endl << endl;
        system("pause");
    }
    if (end == 1)   {       cout << " ";    }
    system("cls");
    

    }
    // End user game
    // Start computer game
    // See user game for variable and loop information
    void compgame(int i)
    {
    int numguess = 0;
    int numwrong = 0;
    int check;
    int end = 0;
    int chosencounter = 0;
    char letter;
    string word;
    string temp;
    string letterchosen = " ";
    do {
    cout << "How many chances do you want (4 - 10): ";
    cin >> numguess;
    } while (numguess < 4 || numguess > 10);
    rnd(word, i); // Gets random word
    temp = word;
    for (i = 0; i < word.length(); i++) { temp.replace(i, 1, 1, '*'); }
    system("cls");
    do {
    drawman(numguess, numwrong, i);
    if (word == temp)
    {
    cout << endl << endl;
    cout << "You guessed it [ " << word << " ]" << endl << endl;
    system("pause");
    end = 1;
    }
    if (numwrong == numguess)
    {
    cout << endl << endl;
    cout << "You failed" << endl << endl;
    system("pause");
    end = 2;
    }
    if (end == 0)
    {
    cout << endl << endl << endl;
    cout << "Letters chosen: " << letterchosen << endl;
    cout << endl << endl << endl;
    cout << "Guesses left: " << numguess - numwrong << endl << endl;
    cout << " " << temp << endl << endl;
    cout << "Letter: ";
    cin >> letter;
    while (!isalpha(letter))
    {
    Flush(cin);
    cout << "Not a letter - enter letter: ";
    cin >> letter;
    }

            lchosen(letter, letterchosen, check, chosencounter, i);
            if (check == 0)
            {
                test(word, letter, numwrong, temp, i);
            }
            else    {   ;   }
            system("cls");
        }
        system("cls");
    } while(end != 1 && end != 2);
    if (end == 2) 
        cout << "Correct word was [ " << word << " ]" << endl << endl;
        system("pause");
    if (end == 1)   cout << endl;
    system("cls");
    

    }
    // End computer game
    // Checks current letter chosen by user
    void lchosen(char letter, string& letterchosen, int& check, int& chosencounter, int i)
    {
    check = 0;
    // Check if letter is equal to any letter in letterchosen string loop
    for (i = 0; i < letterchosen.length(); i++)
    {
    if (letter == letterchosen.at(i)) {check = 1; }
    }
    // End loop
    // Test if letter is already chosen
    if (check == 1)
    {
    cout << endl;
    cout << "Letter already chosen" << endl;
    system("pause");
    }
    // Puts letter in string if not chosen
    else
    {
    letterchosen.replace(chosencounter, 1, 1, letter);
    chosencounter++;
    }
    }
    // End lchosen function
    // Tests whether letter is in word string
    void test(string word, char letter, int& numwrong, string& temp, int i)
    {
    int check2 = 0; // Checks whether letter is in word string, = 1 if it is
    // Check for letter match loop
    for (i = 0; i < word.length(); i++)
    {
    if (letter == word.at(i))
    {
    temp.replace(i, 1, 1, letter);
    check2 = 1;
    }
    }
    // End loop
    // Displays "wrong letter" if user inputed bad letter
    if (check2 == 0)
    {
    cout << endl;
    cout << "Wrong letter" << endl;
    system("pause");
    numwrong++;
    }
    }
    // End test function
    // Gets random word
    void rnd(string& word, int i)
    {
    int x; // Random number to pass to word file loop
    ifstream ins; // File stream
    srand(time(NULL)); // Gets better random number based on time
    x = rand()%100; // Gets number 0 - 99
    ins.open("words.txt"); // Opens random word file
    // Tests for bad file and gets word if not a bad file
    if (ins.fail())
    {
    cerr << "Words.txt is not in same folder as hangman.exe, " << endl
    << "put in correct file and run again and make sure it's " << endl
    << "called words.txt" << endl;
    system("pause");
    main();
    }
    else
    {
    for (i = 0; i < (x + 1); i++) { getline(ins, word); }
    }
    // End test
    ins.close(); // Close file
    }
    // End random function
    // Draws hangman
    void drawman(int numguess, int numwrong, int i)
    {
    draw d; // Data object
    // Draw loop based on number of guesses and number of wrong guesses
    for (i = 0; i <= numwrong; i++)
    {
    if (numguess == 4)
    {
    switch(i)
    {
    case 1: d.rope(); cout << endl; d.head();
    cout << endl; d.neck(); break;
    case 2: cout << endl; d.leftarm();
    d.rightarm(); break;
    case 3: cout << endl; d.waisttop(); break;
    case 4: cout << endl; d.leftleg(); d.rightleg();
    cout << endl << endl << "Dead" << endl; break;
    }

    }
    else if (numguess == 5)
    {
    switch(i)
    {
    case 1: d.rope(); break;
    case 2: cout << endl; d.head(); cout << endl; d.neck(); break;
    case 3: cout << endl; d.leftarm(); d.rightarm(); break;
    case 4: cout << endl; d.waisttop(); break;
    case 5: cout << endl; d.leftleg(); d.rightleg();
    cout << endl << endl << "Dead" << endl; break;
    }
    }
    else if (numguess == 6)
    {
    switch(i)
    {
    case 1: d.rope(); break;
    case 2: cout << endl; d.head(); cout << endl; d.neck(); break;
    case 3: cout << endl; d.leftarm(); d.rightarm(); break;
    case 4: cout << endl; d.waisttop(); break;
    case 5: cout << endl; d.leftleg(); break;
    case 6: d.rightleg(); cout << endl << "Dead" << endl; break;
    }
    }
    else if (numguess == 7)
    {
    switch(i)
    {
    case 1: d.rope(); break;
    case 2: cout << endl; d.head(); cout << endl; d.neck(); break;
    case 3: cout << endl; d.leftarm(); break;
    case 4: d.rightarm(); break;
    case 5: cout << endl; d.waisttop(); break;
    case 6: cout << endl; d.leftleg(); break;
    case 7: d.rightleg(); cout << endl << "Dead" << endl; break;
    }
    }
    else if (numguess == 8)
    {
    switch(i)
    {
    case 1: d.rope(); break;
    case 2: cout << endl; d.head(); break;
    case 3: cout << endl; d.neck(); break;
    case 4: cout << endl; d.leftarm(); break;
    case 5: d.rightarm(); break;
    case 6: cout << endl; d.waisttop(); break;
    case 7: cout << endl; d.leftleg(); break;
    case 8: d.rightleg(); cout << endl << "Dead" << endl; break;
    }
    }
    else if (numguess == 9)
    {
    switch(i)
    {
    case 1: d.rope(); break;
    case 2: cout << endl; d.head(); break;
    case 3: cout << endl; d.neck(); break;
    case 4: cout << endl; d.leftarm(); break;
    case 5: d.rightarm(); break;
    case 6: cout << endl; d.waisttop(); break;
    case 7: break;
    case 8: cout << endl; d.leftleg(); break;
    case 9: d.rightleg(); cout << endl << "Dead" << endl; break;
    }
    }
    else if (numguess == 10)
    {
    switch(i)
    {
    case 1: d.rope(); break;
    case 2: cout << endl; d.head(); break;
    case 3: cout << endl; d.neck(); break;
    case 4: cout << endl; d.leftarm(); break;
    case 5: d.rightarm(); break;
    case 6: cout << endl; d.waisttop(); break;
    case 7: break;
    case 8: cout << endl; d.leftleg(); break;
    case 9: d.rightleg();
    cout << "One last chance. What do you want on your tombstone?"; break;
    case 10: cout << endl << "Dead" << endl; break;
    }
    }
    }
    // End draw loop
    }
    // End draw function
    // Flushes cin stream
    inline istream& Flush(istream& stream)
    {
    stream.clear();
    int chars_to_skip = stream.rdbuf()->in_avail();
    return stream.ignore(chars_to_skip);
    }
    // End flush function
    // End subprogams
    // End program

    点赞 2 评论 复制链接分享

相关推荐