From 442a49ad5a48d417345959b903ae6a6d32d55759 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 15 Apr 2022 15:51:30 -0500 Subject: Great C programming fun Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git --- 10_gdb/game.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 10_gdb/game.c (limited to '10_gdb/game.c') diff --git a/10_gdb/game.c b/10_gdb/game.c new file mode 100644 index 0000000..a7459d3 --- /dev/null +++ b/10_gdb/game.c @@ -0,0 +1,44 @@ +#include +#include +#include +int getSecretNumber(void); //prototype, implemented elsewhere. + +int getOtherSN(int which); //prototype, implemented elsewhere. + +int main(void) { + int guessesMade = 0; + int yourGuess; + char buffer[1024]; + int myNumber = getSecretNumber(); + + printf("I'm thinking of a number...\n"); + printf("What number do you guess?\n"); + if(fgets(buffer, 1024, stdin) == NULL) { + printf("Oh no, you are giving up? You lose...\n"); + return EXIT_FAILURE; + } + yourGuess = atoi(buffer); + if(yourGuess != myNumber) { + printf("I'm sorry, that is not right. You lose\n"); + return EXIT_FAILURE; + } + printf("Correct! You win round1!\n"); + + int total = 0; + for (int i = 0; i <= 5678; i++) { + total = total ^ getOtherSN(i); + } + printf("Ok, time for round 2. I have another secret number.\n"); + printf("Your guess:\n"); + if(fgets(buffer, 1024, stdin) == NULL) { + printf("Oh no, you are giving up? You lose...\n"); + return EXIT_FAILURE; + } + yourGuess = atoi(buffer); + if (yourGuess == total) { + printf("You win round 2 also!\n"); + return EXIT_SUCCESS; + } + printf("Sorry, you did not win the second round\n"); + return EXIT_FAILURE; +} -- cgit v1.2.3