C语言龟兔赛跑指针示例:如何实现任意一方完成即终止程序?
Fixing the Turtle & Hare Race to Stop at First Finish
Got it, let's tweak this C pointer-based turtle and hare race program so it terminates the second either competitor crosses the finish line. I'll share the full modified code first, then break down the key changes that make this work.
Modified Code
#include <stdio.h> #include <stdlib.h> #include <time.h> #define FINISH_LINE 70 // Total steps needed to win the race // Move the turtle using a pointer to track its position void moveTurtle(int *turtle) { int move = rand() % 10 + 1; // Generate random number 1-10 if (move >= 1 && move <= 5) { *turtle += 3; // Steady fast plod } else { *turtle -= 1; // Slip backwards if (*turtle < 1) { // Prevent going below starting line *turtle = 1; } } } // Move the hare using a pointer to track its position void moveHare(int *hare) { int move = rand() % 10 + 1; // Generate random number 1-10 switch(move) { case 1: case 2: // Hare sleeps, no movement break; case 3: case 4: *hare -= 6; // Big slip backwards if (*hare < 1) { *hare = 1; } break; case 5: case 6: case 7: case 8: *hare += 9; // Big forward hop break; case 9: case 10: *hare += 1; // Small forward hop break; } } // Print the current race progress void printRace(int turtlePos, int harePos) { for (int i = 1; i <= FINISH_LINE; i++) { if (i == turtlePos && i == harePos) { printf("OUCH!!! "); // Collision marker } else if (i == turtlePos) { printf("T "); } else if (i == harePos) { printf("H "); } else { printf(". "); } } printf("\n"); } int main() { int turtle = 1, hare = 1; int *turtlePtr = &turtle; int *harePtr = &hare; srand(time(NULL)); // Seed random number generator for varied races printf("ON YOUR MARK, GET SET\nBANG !!!!!\nAND THEY'RE OFF !!!!!\n"); // Run race until someone crosses the finish line do { moveTurtle(turtlePtr); moveHare(harePtr); printRace(turtle, hare); // Check if either competitor has finished if (*turtlePtr >= FINISH_LINE || *harePtr >= FINISH_LINE) { break; // Exit race loop immediately } } while (1); // Infinite loop broken only by finish condition // Announce the winner if (*turtlePtr >= FINISH_LINE && *harePtr >= FINISH_LINE) { printf("IT'S A TIE!\n"); } else if (*turtlePtr >= FINISH_LINE) { printf("TORTOISE WINS!!! YAY!!!\n"); } else { printf("HARE WINS. YUCH.\n"); } return 0; }
Key Changes Explained
- Immediate Finish Check: After each move and progress print, we add a check using the position pointers. If either
*turtlePtror*harePtrmeets/exceedsFINISH_LINE, we break out of the race loop right away—no waiting for both to complete the course. - Infinite Loop with Early Exit: We switched to a
do { ... } while (1);structure so the race runs continuously until our finish condition triggers. This is cleaner than looping until both competitors finish. - Pointer Usage Preservation: The core pointer-based position tracking remains intact, keeping the example true to its original intent.
- Edge Case Handling: We retained logic to prevent either racer from moving below position 1 (can't go backwards past the starting line).
内容的提问来源于stack exchange,提问作者PvtCaboose
相关产品推荐
相关产品推荐

