2014-12-21 35 views
-6

幫助我不知道在哪裏放置「你想再玩一次」的命令,我已經嘗試了幾個變化,我碰到了一堵磚牆,因爲它是我的遊戲但我想要額外的分數來完成比賽並提供再次參賽機會?你想再次玩

我使用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"); 

}而(AG == 'N' || AG == 'N'); //這是程序的退出聲明,並確保如果選擇'Y',則可以播放遊戲 printf(「\ n \ n感謝您的遊戲再次見到您再次參加匹配成對遊戲\! \ n \ n「); }

+1

也許在最後? –

+2

我很遺憾地通知你,看起來你實際上是用C編程的,而不是C++。 –

+2

我也想到最後。此外,你的代碼可以從開關case語句中受益。 http://www.cprogramming.com/tutorial/lesson5.html –

回答

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循環內。

+0

嗨@striving_coder我已經實現了你的代碼,但是在} while(ag =='y')之後;它是如何重複的? –

+0

@SimonDownes:循環的主體('do'和'while''之間的所有內容)無論如何都將首次執行。當執行到達'while'時,它會評估條件(在這種情況下'ag'是否等於''y''),如果它是真的,則循環體再次重複執行(即執行從'while'到'for(x = 1; x <100; x ++)'部分)。所以它會一直持續到用戶輸入的內容不是「y」作爲最後一個問題。說得通? –

+0

我現在已經在程序中實現了代碼,我仍然有問題不確定,爲什麼,我使用了你的確切代碼等。@striving_coder –