用C语言编写的Simple Simon游戏源代码

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Baidu Nhomakorabea
while(correct) { ++tries; wait_start = clock(); //Generate a sequence of digits and display them srand(time(&seed)); for(unsigned int i = 1; i <= digits; ++i) printf("%u ", rand() % 10); for(;clock() - wait_start < DELAY*CLOCKS_PER_SEC;); //Now overwrite the digit sequence printf("\r"); for(unsigned int i = 1; i <= digits; ++i) printf(" "); if(tries == 1) printf("\nNow you enter the sequence - don't forget" " the spaces\n"); else printf("\r"); srand(seed); for(unsigned int i = 1; i <= digits; ++i) //Read the input sequence & check against the original { scanf("%u", &number); if(number != rand() % 10) { correct = false; break; } } //On every third successful try, increase the sequence length if(correct && ((tries % 3) == 0)) ++digits; printf("%s\n", correct ? "Correct!" : "Wrong!"); } //Calculate and ouput the game score score = 10 * (digits - ((tries % 3) == 1)); total_digits = digits*(((tries % 3) == 0) ? 3 : tries % 3);
用 C 语言编写的 Simple Simon 游戏源代码: //Program 4.13 Simple Simon #include <stdio.h> #include <ctype.h> #include <stdbool.h> #include <stdlib.h> #include <time.h> int main(void) { char another_game = 'Y'; const unsigned int DELAY = 1; bool correct = true; unsigned int tries = 0; unsigned int digits = 0; time_t seed = 0; unsigned int number = 0; time_t wait_start = 0; clock_t start_time = 0; unsigned int score = 0; unsigned int total_digits = 0; unsigned int game_time = 0; //Describe how the game is played printf("\nTo play Simple Simon, "); printf("watch the screen for a sequence of digits."); printf("\nWatch carefully, as the digits are only displayed" " for %u second%s ", DELAY, DELAY > 1 ? "s!":"!"); printf("\nThe computer will remove them, and then prompt you "); printf("to enter the same sequence."); printf("\nWhen you do, you must put spaces between the digits.\n"); printf("\nGood Luck!\nPress Enter to play\n"); scanf("%c", &another_game); //Game loop - one outer loop iteration is a complete game do { //Initiate game correct = true; tries = 0; digits = 2; start_time = clock(); //Inner loop continues as long as sequences are entered correctly
if(digits > 2) total_digits += 3*((digits - 1)*(digits -2)/ - 1); game_time = (clock() - start_time)/CLOCKS_PER_SEC - tries*DELAY; if(total_digits > game_time) score += 10*(game_time - total_digits); printf("\n\nGame time was %u seconds. Your score is %u", game_time, score); fflush(stdin); //Check if new game required printf("\nDo you want to play again (y/n)? "); scanf("%c", &another_game); }while(toupper(another_game) == 'Y'); return 0; }
相关文档
最新文档