Showing posts with label Game. Show all posts
Showing posts with label Game. Show all posts

C Program A Really simple hangman game

How to write a C Program A Really simple hangman game in C Programming Language ?


  1. /*
  2.  * hangman.c
  3.  * a REALLY simple hangman game
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. int wrong = 0;
  11. int correct = 0;
  12. int finished = 0;
  13. char solution[8] = "hangman";
  14. int charIsSolved[7] = {0,0,0,0,0,0,0};
  15. char triedLetters[27] = "??????????????????????????";
  16. char input;
  17. char stage1[] = "  -----\n /     \\\n";
  18. char stage2[28] = "    |\n    |\n    |\n";
  19. char *stage2_new = "    |   o\n    |  ^W^\n    |  / \\\n";
  20. char stage3[11] = "    |/\n";
  21. char *stage3_new = "    |/  |\n";
  22. char stage4[11] = "    +----\n";
  23. char *stage4_new = "    +---+\n";
  24.  
  25. void drawGallow() {
  26.  
  27.    if (!wrong) {
  28.       printf("\n########################################\n\n");
  29.       return;
  30.    }
  31.  
  32.    if (wrong < 5) {
  33.       switch (wrong) {
  34.          case 4:
  35.             printf("%s", stage4);
  36.          case 3:
  37.             printf("%s", stage3);
  38.          case 2:
  39.             printf("%s", stage2);
  40.          case 1:
  41.             printf("%s", stage1);
  42.       }
  43.    } else {
  44.       printf("%s", stage4);
  45.       printf("%s", stage3);
  46.       printf("%s", stage2);
  47.       printf("%s", stage1);
  48.    }
  49.  
  50.    printf("\n########################################\n\n");
  51. }
  52.  
  53.  
  54. void checkInput() {
  55.  
  56.    int foundChar = 0;
  57.  
  58.    /* check each letter of the solution whether it matches or not */
  59.    for (int i = 0; i < 7; ++i) {
  60.       if (input == solution[i]) {
  61.          charIsSolved[i] = 1;
  62.          correct++;
  63.          foundChar = 1;
  64.       }
  65.    }
  66.  
  67.    /* if there were no matches, increase the wrong-counter to extend the gallow */
  68.    if (!foundChar) {
  69.       triedLetters[wrong] = input;
  70.       wrong++;
  71.    }
  72. }
  73.  
  74. void drawStatus() {
  75.  
  76.    /* print all the letters which were found out */
  77.    printf("The word: ");
  78.    for (int i = 0; i < 7; ++i) {       
  79.       if (charIsSolved[i]) {
  80.          printf("%c", solution[i]);
  81.       } else {
  82.          printf("_");
  83.       }
  84.    }
  85.    printf("\n");
  86.  
  87.    /* print all the letters that were tried */
  88.    printf("Your tries: ");
  89.    for (int i = 0; i < 26; ++i) {
  90.       if (triedLetters[i] != '?') {
  91.          printf("%c, ", triedLetters[i]);
  92.       } else {
  93.          break;
  94.       }
  95.    }
  96.    printf("\n");
  97. }
  98.  
  99. void guessChar() {
  100.  
  101.    char dummy;
  102.  
  103.    printf("\nYour guess: ");
  104.  
  105.    scanf("%c", &input);
  106.    scanf("%c", &dummy);
  107. }
  108.  
  109. void updateGallow() {
  110.  
  111.    switch (wrong) {
  112.       case 5:
  113.          strcpy(stage4, stage4_new);
  114.          break;
  115.       case 6:
  116.          strcpy(stage3, stage3_new);
  117.          break;
  118.       case 7:
  119.          strcpy(stage2, stage2_new);
  120.          break;
  121.    }
  122. }
  123.  
  124. void nextTurn() {
  125.  
  126.    system("clear");
  127.  
  128.    if (correct == 7) {
  129.       printf("\nCongrats, you've won!\n\n");
  130.       finished = 1;
  131.       return;
  132.    } else if (wrong == 7) {
  133.       drawGallow();
  134.       printf ("\nYou were to bad for staying alive...\n\n");
  135.       finished = 1;
  136.       return;
  137.    } else {
  138.       drawGallow();
  139.       drawStatus();
  140.       guessChar();
  141.       checkInput();
  142.       updateGallow();
  143.    }
  144. }
  145.  
  146.  
  147. int main(void) {
  148.  
  149.    while (!finished) {
  150.       nextTurn();
  151.    }
  152.  
  153.    return 0;
  154. }

C Program to create a solution to the Towers of Hanoi game

How to write a C Program to create a solution to the Towers of Hanoi game in C Programming Language ?


Solution:

  1. /* Program to create a solution to the Towers of Hanoi game*/


  2. #include <stdio.h>
  3. void SparePegAssign(int, int, int&);
  4. void TowerSolver(int, int, int, int);
  5. int main()
  6. {
  7.     int disk, fromPeg, toPeg, sparePeg = 1;
  8.     printf("Enter the number of disks you want to move: ");
  9.     scanf("%d", &disk);
  10.     printf("From which peg: ");
  11.     scanf("%d", &fromPeg);
  12.     printf("To which peg: ");
  13.     scanf("%d,", &toPeg);
  14.     SparePegAssign(fromPeg, toPeg, sparePeg);
  15.     printf("The sequence of moves involved in the Tower of Hanoi are : ");
  16.     TowerSolver(disk, fromPeg, toPeg, sparePeg);
  17.     return 0;
  18. }
  19. // Recursion function,
  20. void TowerSolver(int ldisk, int lfromPeg, int lnewPeg, int lsparePeg)
  21. {
  22.     if (ldisk == 1) { // If statement to determine if it will be the final move.
  23.         printf("\n Move a disk from peg %d to peg %d", lfromPeg, lnewPeg);
  24.         return;
  25.     }
  26.     else { // Used to the move the disks until ldisk results in a 1.
  27.         TowerSolver (ldisk - 1, lfromPeg, lsparePeg, lnewPeg);
  28.         printf("\n Move a disk from peg %d to peg %d", lfromPeg, lnewPeg);
  29.         TowerSolver(ldisk - 1, lsparePeg, lnewPeg, lfromPeg);
  30.     }
  31. }
  32. /* Function put in place to determine what the value of the spare peg will be.
  33.  * It does this by taking the sum of the other pegs
  34.  */
  35. void SparePegAssign (int lfromPeg, int ltoPeg, int& lsparePeg) {
  36.     if ((lfromPeg + ltoPeg) == 3)
  37.         lsparePeg = 3;
  38.     else if ((lfromPeg + ltoPeg) == 4)
  39.         lsparePeg = 2;
  40.     else
  41.         lsparePeg = 1;
  42.     return;
  43. }