你想再玩一次吗?

时间:2014-12-21 23:35:11

标签: c++

帮助我不知道在哪里放"你想再玩一次"命令,我已经尝试了一些变化,我已经打了一个砖墙我的游戏工作原样,但我想要额外的标记可用于完成游戏并提供再次玩的机会?

我正在使用Visual Studio 2013 Professional和C ++

#include <stdio.h>
#include <stdlib.h>
#include <time.h>       //ensures that the system clock is set

void main()
{
char player1[10];
srand(time(NULL));              //this command is only required at the start and sets the clock

printf("Please enter your name? - ");
scanf("%s", player1);           //this command is used to store the players name for us later on within the game
printf("\n\n");

//The following printf commands issue the rules of the game and ensure that they are printed and viewable
printf("Welcome %s to the Pairs (Matching Game)\n\n", player1);
printf("The rules of the game are as follows\n\n");
printf(" 1. The player should enter a location of card 1 and card 2\n\n");
printf(" 2. The player should only use a numeric format to enter the\n    location of the card i.e. 11 and 44 this would show only\n    Line 1 box 1 and line 4 box 4\n\n");
printf(" 3. Should the player enter a location outside of the table\n    range 'You have incorrectly selected a location, please\n    try again' will be displayed\n\n");
printf(" 4. A player should select 2 cards to be shown if they match\n    the cards are removed\n\n");
printf(" 5. If a player selects a non matching pair 'Please try again\n    - you've selected an non matching pair' should be displayed\n\n");
printf(" 6. The object of the game is to match all the pairs in the \n    least amount of turns possible\n\n");
printf(" 7. The player cannot use any symbol !£$%^&*@~? as these will\n    not be recognised by the game and cause it to freeze resulting\n    in a restarted game\n");

printf("\n");

char ag = 'y' || 'Y';
do {
    char grid[17] = { 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd', 'e', 'e', 'f', 'f', 'g', 'g', 'h', 'h' };     //sets the characters used in the grid
    int high = 15;
    int random, random2, temp, card1, card2;
    int low = 0;
    int counter = 0;
    int x = 0;
    int flag = 0;           //this variable is used to identify potential issues
    int turns = 0;      //identifies the variable for the amount of turns taken

    for (x = 1; x < 100; x++)           //sets the random loop to vary the selection 100 times prior to selecting a game table
    {
        random = rand() % (high - low + 1) + low;
        random2 = rand() % (high - low + 1) + low;
        temp = grid[random];
        grid[random] = grid[random2];
        grid[random2] = temp;
    }

    while ((grid[0] != 'x') || (grid[1] != 'x') || (grid[2] != 'x') || (grid[3] != 'x') || (grid[4] != 'x') || (grid[5] != 'x') || (grid[6] != 'x') || (grid[7] != 'x') || (grid[8] != 'x') || (grid[9] != 'x') || (grid[10] != 'x') || (grid[11] != 'x') || (grid[12] != 'x') || (grid[13] != 'x') || (grid[14] != 'x') || (grid[15] != 'x'))
    {

        for (x = 0; x < 16; x++)
        {
            counter++;
            printf("| %c ", grid[x]);   //The grid is shown using this command for testing purposes only must be removed prior to playing

            if (counter == 4)
            {
                printf("\n");
                counter = 0;
            }

        }

        printf("\n");
        printf(" Please enter Card 1 : ");  //Enter the location of your first card
        scanf_s("%d", &card1);              //Displays your first card
        printf(" Please enter Card 2 : ");  //Enter the location of your next card
        scanf_s("%d", &card2);              //Displays your second card
        printf("");
        turns = turns + 1;                  //the turns variable has been set to count every attempt at matching cards

        if (card1 == 11)                    //This sets the array (location) for card 1
        {
            card1 = 0;
        }
        else if (card1 == 12)
        {
            card1 = 1;
        }
        else if (card1 == 13)
        {
            card1 = 2;
        }
        else if (card1 == 14)
        {
            card1 = 3;
        }
        else if (card1 == 21)
        {
            card1 = 4;
        }
        else if (card1 == 22)
        {
            card1 = 5;
        }
        else if (card1 == 23)
        {
            card1 = 6;
        }
        else if (card1 == 24)
        {
            card1 = 7;
        }
        else if (card1 == 31)
        {
            card1 = 8;
        }
        else if (card1 == 32)
        {
            card1 = 9;
        }
        else if (card1 == 33)
        {
            card1 = 10;
        }
        else if (card1 == 34)
        {
            card1 = 11;
        }
        else if (card1 == 41)
        {
            card1 = 12;
        }
        else if (card1 == 42)
        {
            card1 = 13;
        }
        else if (card1 == 43)
        {
            card1 = 14;
        }
        else if (card1 == 44)
        {
            card1 = 15;
        }
        else
        {
            card1 = -1;
        }

        if (card2 == 11)                            // This will set the arrays (location) for card2
        {
            card2 = 0;
        }
        else if (card2 == 12)
        {
            card2 = 1;
        }
        else if (card2 == 13)
        {
            card2 = 2;
        }
        else if (card2 == 14)
        {
            card2 = 3;
        }
        else if (card2 == 21)
        {
            card2 = 4;
        }
        else if (card2 == 22)
        {
            card2 = 5;
        }
        else if (card2 == 23)
        {
            card2 = 6;
        }
        else if (card2 == 24)
        {
            card2 = 7;
        }
        else if (card2 == 31)
        {
            card2 = 8;
        }
        else if (card2 == 32)
        {
            card2 = 9;
        }
        else if (card2 == 33)
        {
            card2 = 10;
        }
        else if (card2 == 34)
        {
            card2 = 11;
        }
        else if (card2 == 41)
        {
            card2 = 12;
        }
        else if (card2 == 42)
        {
            card2 = 13;
        }
        else if (card2 == 43)
        {
            card2 = 14;
        }
        else if (card2 == 44)
        {
            card2 = 15;
        }
        else
        {
            card2 = -1;
        }
        printf("\n\nCARD 1: %d\n", card1);      //printf shows the location of the card selected from 0 to 15
        printf("CARD 2: %d\n\n", card2);        //printf shows the location of the card selected from 0 to 15

        if (((card1 < 0) || (card1>15)) || ((card2 < 0) || (card2>15)))
        {
            printf("\nYou have incorrectly selected a location, please try again\n\n");
            flag = 1;
            printf("Flag=%d\n", flag);      //This printf command is just for testing remove prior to playing the game
        }

        counter = 0;                        //This resets the counter

        for (x = 0; x < 16; x++)        // this means it will only loop the count 16 times
        {
            counter++;      // this will add 1 to the counter
            printf("|   ");

            if (card1 == x)
            {
                printf("%c", grid[card1]);
            }
            else if (card2 == x)
            {
                printf("%c", grid[card2]);
            }
            else
            {
                printf(" ");
            }

            if (counter > 3)
            {
                printf("\n");
                counter = 0;    // This will reset the counter back to zero 

            }
        }
        if ((grid[card1] == 'x') || (grid[card2] == 'x'))       //This 'if statment' states if you have made the choice 
        {
            printf("\nUnfortunately you have selected a card already match, please try again\n\n");
        }
        if ((grid[card1] == grid[card2]) && (grid[card1] != 'x') && (grid[card2] != 'x') && (flag == 0)) //This is the 'if statment' that will match the pairs and then mark them as 'x' 
        {
            printf("\nCongratulations you have found a pair!!\n");
            grid[card1] = 'x';
            grid[card2] = 'x';
        }

        if (((grid[card1] != grid[card2]) && (grid[card1] != 'x') && (grid[card2] != 'x')) && (flag == 0)) //This 'if statment' states if the guesses are incorrect
        {
            printf("\nOopps remember the cards as you need to try again\n\n");
        }
        }
        printf("\nExcellent %s you have managed to match all the cards correctly in %d turns\n\n", player1, turns);
        printf("Do you want to play again? (y/n)\n\n");
        scanf_s(" %c", &ag);
        printf("\n\n");

} while(ag ==&#39; n&#39; || ag ==&#39; N&#39;); //这是程序的退出声明,并确保如果&#39; Y&#39;被选中     printf(&#34; \ n \ n感谢玩游戏再次见到你的另一场匹配对的游戏!!! \ n \ n&#34;); }

1 个答案:

答案 0 :(得分:0)

你可以做的是在do-while循环(至少执行一次并检查后置条件)中包装你的执行部分(你将在每个新游戏中重复,然后用户决定退出游戏):

        char ag = '';
        do {
            char grid[17] = { 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd', 'e', 'e', 'f', 'f', 'g', 'g', 'h', 'h'         };     //sets the characters used in the grid
            int again = 0;
            int high = 15;
            int low = 0;
            int counter = 0;
            int x = 0;
            int flag = 0;           //this variable is used to identify potential issues
            int turns = 0;      //identifies the variable for the amount of turns taken
            for (x = 1; x < 100; x++)           //sets the random loop to vary the selection 100 times prior to selecting a game table
            ...
            printf("\nExcellent %s you have managed to match all the cards correctly in %d turns\n\n", player1, turns);
            printf("Do you want to play again? (y/n)");
            scanf(" %c", &ag);
        } while (ag=='y' || ag=='Y');
    }

修改

不仅y,而且Y重启游戏。

do-while循环内放置可变初始化。