Advertisement
TopGun-50

Bataille Navale

Oct 15th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.21 KB | None | 0 0
  1. /******** SINK THE BATTLESHIP! *********
  2.              
  3.  
  4. This sketch is free to be copied and modified by anyone. There
  5. are obvious bugs in the program, but I'm too much of a noob
  6. to fix them.*/
  7.  
  8. #include <Servo.h>  // Include servo library for locking device
  9. #include <Keypad.h>  // Include Keypad library to use keypad
  10. #include "pitches.h" //piezo buzzer attached to pin 12
  11. // notes in the "success" melody:
  12. int melody[] = {
  13.   NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
  14. };
  15. // note durations: 4 = quarter note, 8 = eighth note, etc.:
  16. int noteDurations[] = {
  17.   4, 8, 8, 4, 4, 4, 4, 4
  18. };
  19. Servo myservo;  // create servo object to control a servo lock
  20. // constants won't change. Used here to set a pin number :
  21. const int ledPin11 =  11;      // number of the "Miss" LED pin
  22. const int ledPin10 =  10;      // number of the "Hit" LED pin
  23. const byte ROWS = 4;       // Four rows in keypad
  24. const byte COLS = 4;       // Three columns in keypad
  25. int Hits = 0;   // Initialize hit counter
  26. int Misses = 0;  // Initialize miss counter
  27.  
  28. char keys[ROWS][COLS] =    // Define the Keymap
  29. {
  30.   {'1','2','3','A'},
  31.   {'4','5','6','B'},
  32.   {'7','8','9','C'},
  33.   {'*','0','#','D'}
  34. };
  35.  
  36. byte rowPins[ROWS] = {9,8,7,6};   // Connect keypad to these Arduino pins.
  37. byte colPins[COLS] = {5,4,3,2};     // Connect keypad to these Arduino pins.
  38. unsigned int i=0,s,z=0;
  39. char name[3];
  40. Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Create the Keypad
  41.  
  42. void setup()
  43. {
  44.   Serial.begin(9600);
  45.   myservo.attach(13);  // attaches the servo on pin 13 to the servo object
  46.   pinMode(ledPin10, OUTPUT);   // set the Hit pin as output
  47.   pinMode(ledPin11, OUTPUT);  // set the Miss pin as output
  48.   myservo.write(20);    // Ensures servo is in locked position
  49.  
  50. // Just some lights to let the player know game has started  
  51. digitalWrite(11, HIGH);    // turn "Miss" lt on
  52. tone(12, 1000, 500);  // play a "Success" note on pin 12 for 1 Second
  53. delay(500);
  54. digitalWrite(11, LOW);    // turn "Miss" lt off
  55. digitalWrite(10, HIGH);    // turn "Hit" lt on
  56. tone(12, 800, 1000);
  57. delay(500);
  58. digitalWrite(10, LOW);    // turn "Hit" lt off
  59. digitalWrite(11, HIGH);    // turn "Miss" lt on
  60. tone(12, 1000, 500);  
  61. delay(500);
  62. digitalWrite(10, HIGH);    // turn "Miss" lt on
  63. digitalWrite(11, LOW);    // turn "Miss" lt off
  64. tone(12, 800, 1000);
  65. delay(500);
  66. digitalWrite(10, LOW);    // turn "Hit" lt off
  67. digitalWrite(11, HIGH);    // turn "Miss" lt on
  68. tone(12, 1000, 500);
  69. delay(500);
  70. digitalWrite(10, HIGH);    // turn "Miss" lt on
  71. digitalWrite(11, LOW);    // turn "Miss" lt off
  72. tone(12, 800, 1000);
  73. delay(500);
  74. digitalWrite(10, LOW);    // turn "Hit" lt off
  75. noTone(12);
  76.  
  77.   }
  78. void loop()
  79. {
  80.  char key= kpd.getKey();                          //continuous get a key-stroke
  81.    if(key!=NO_KEY)                                  // Check for a valid key.
  82.    {
  83.     switch (key)
  84.     {
  85.         case '*':  {                                // TG-50 ajouté a cause de l'erreur crosses initialization of 'char num [s]'
  86.    stop: s=i;                                       //if * pressed than terminate and s becomes size of string
  87.          char num[s];                               //for calculation another string is formed of the size dynamically during run time by input string size
  88.          Serial.println();
  89.          name[i]='\0';                              //terminate string with null character so it is easy to identify the termination of the string
  90.          Serial.println("Le numéro entré est ");
  91.          for(i=0;i<(sizeof(name));i++)              //serially print the input string and copy it to the another string for calulation
  92.          {
  93.            Serial.print(name[i]);
  94.            num[i]=name[i];
  95.            //The following checks if the entry is a hit or a miss
  96.            if (num[0] == 'C')
  97.            {
  98.              if (num[1] == '1')
  99.              {
  100.              digitalWrite(11, HIGH);    // turn "Miss" lt on
  101.              tone(12, 200, 300);  // play a "Fail" note on pin 12 for 2 secs
  102.              delay(300);
  103.              noTone(12);
  104.              digitalWrite(11, LOW);    // turn "Miss" lt off
  105.              Misses = Misses +1;
  106.              }
  107.            }
  108.            
  109.            if (num[0] == 'C')    // C2 est le premier bloc du cuirassé TG-50
  110.            {
  111.              if (num[1] == '2')
  112.              {
  113.              Serial.println( "Atteint!");
  114.              Hits = Hits + 1;
  115.              digitalWrite(10, HIGH);    // turn "Hit" lt on
  116.              tone(12, 500, 500);  // play a "Success" note on pin 12 for 1 Second
  117.              delay(200);
  118.              tone(12, 800, 1000);
  119.              digitalWrite(10, LOW);    // turn "Hit" lt off
  120.              delay(500);
  121.              }
  122.            }
  123.          
  124.            if (num[0] == 'C')   //C3 est le deuxième bloc du cuirassé TG-50
  125.            {
  126.              if (num[1] == '3')
  127.              {
  128.              Serial.println( "Atteint!");
  129.              Hits = Hits + 1;
  130.              digitalWrite(10, HIGH);    // turn "Hit" lt on
  131.              tone(12, 500, 500);  // play a "Success" note on pin 12 for 1 Second
  132.              delay(200);
  133.              tone(12, 800, 1000);
  134.              digitalWrite(10, LOW);    // turn "Hit" lt off
  135.              delay(500);
  136.              }
  137.            }
  138.          
  139.            if (num[0] == 'C')    //C4 est le troisième bloc du cuirassé TG=50
  140.            {
  141.              if (num[1] == '4')
  142.              {
  143.              Serial.println( "Atteint!");
  144.              Hits = Hits + 1;
  145.              digitalWrite(10, HIGH);    // turn "Hit" lt on
  146.              tone(12, 500, 500);  // play a "Success" note on pin 12 for 1 Second
  147.              delay(200);
  148.              tone(12, 800, 1000);
  149.              digitalWrite(10, LOW);    // turn "Hit" lt off
  150.              delay(500);
  151.              }
  152.            }
  153.          
  154.            if (num[0] == 'C')    //C5 est le quatrième bloc du cuirassé TG-50
  155.            {
  156.              if (num[1] == '5')
  157.              {
  158.              Serial.println( "Atteint!");
  159.              Hits = Hits + 1;
  160.              digitalWrite(10, HIGH);    // turn "Hit" lt on
  161.              tone(12, 500, 500);  // play a "Success" note on pin 12 for 1 Second
  162.              delay(200);
  163.              tone(12, 800, 1000);
  164.              digitalWrite(10, LOW);    // turn "Hit" lt off
  165.              delay(500);
  166.              }
  167.            }
  168.            
  169.            if (num[0] == 'C')
  170.            {
  171.              if (num[1] == '6')
  172.              {
  173.              digitalWrite(11, HIGH);    // turn "Miss" lt on
  174.              tone(12, 200, 300);  // play a "Fail" note on pin 12 for 2 secs
  175.              delay(300);
  176.              noTone(12);
  177.              digitalWrite(11, LOW);    // turn "Miss" lt off
  178.              Misses = Misses +1;
  179.              }
  180.            }
  181.            
  182.            if (num[0] == 'C')
  183.            {
  184.              if (num[1] == '7')
  185.              {
  186.              digitalWrite(11, HIGH);    // turn "Miss" lt on
  187.              tone(12, 200, 300);  // play a "Fail" note on pin 12 for 2 secs
  188.              delay(300);
  189.              noTone(12);
  190.              digitalWrite(11, LOW);    // turn "Miss" lt off
  191.              Misses = Misses +1;
  192.              }
  193.            }
  194.            
  195.            if (num[0] == 'C')
  196.            {
  197.              if (num[1] == '8')
  198.              {
  199.              digitalWrite(11, HIGH);    // turn "Miss" lt on
  200.              tone(12, 200, 300);  // play a "Fail" note on pin 12 for 2 secs
  201.              delay(300);
  202.              noTone(12);
  203.              digitalWrite(11, LOW);    // turn "Miss" lt off
  204.              Misses = Misses +1;
  205.              }
  206.            }
  207.            
  208.            if (num[0] == 'C')
  209.            {
  210.              if (num[1] == '9')
  211.              {
  212.              digitalWrite(11, HIGH);    // turn "Miss" lt on
  213.              tone(12, 200, 300);  // play a "Fail" note on pin 12 for 2 secs
  214.              delay(300);
  215.              noTone(12);
  216.              digitalWrite(11, LOW);    // turn "Miss" lt off
  217.              Misses = Misses +1;
  218.              }
  219.            }
  220.              
  221.            else
  222.            { // This is what happens if the entry is a miss:
  223.              digitalWrite(11, HIGH);    // turn "Miss" lt on
  224.              tone(12, 200, 300);  // play a "Fail" note on pin 12 for 2 secs
  225.              delay(300);
  226.              noTone(12);
  227.              digitalWrite(11, LOW);    // turn "Miss" lt off
  228.              Misses = Misses +1;
  229.              }
  230.          
  231.           if (Hits == 8) // If there are four hits, the following happens:
  232.           {
  233.              delay(500);
  234.              Serial.println("Vous avez coulé mon cuirassé!");
  235.              Hits = 0;
  236.              digitalWrite(10, HIGH);    // turn "Hit" lt on
  237.              for (int thisNote = 0; thisNote < 8; thisNote++)  //play "Success!" melody
  238.                 {
  239.                 int noteDuration = 1000 / noteDurations[thisNote];
  240.                 tone(12, melody[thisNote], noteDuration);
  241.                 int pauseBetweenNotes = noteDuration * 1.30;
  242.                 delay(pauseBetweenNotes);
  243.                 noTone(12);  // stop the melody
  244.                 }
  245.                          
  246.              myservo.write(100);    // Move servo To Open position
  247.              delay(5000);
  248.              myservo.write(20);     // Move servo to Closed position after 5 seconds
  249.              digitalWrite(10, LOW);    // turn "Hit" lt off
  250.           }
  251.  
  252.  if (Misses >= 32) // After 10 misses, the failure sequence starts:
  253.              {
  254.                Serial.print("  Plus de munition!");
  255.                noTone(12);
  256.                digitalWrite(11, HIGH);    // turn "Miss" lt on
  257.                tone(12, 500, 500);  // play a "Fail" note on pin 12 for 2 secs
  258.                delay(500);
  259.                tone(12, 400, 500);  
  260.                delay(500);
  261.                tone(12, 300, 500);  
  262.                delay(500);
  263.                tone(12, 200, 500);  
  264.                delay(500);
  265.                tone(12, 100, 500);  
  266.                delay(500);
  267.                noTone(12);
  268.                digitalWrite(11, LOW);    // turn "Miss" lt off
  269.                Misses = 0; // Resets the game for more fun!
  270.                }  
  271.                    
  272.          }
  273.            for(i=0;i<(sizeof(name));i++)            //empty the string for the next turn use if it is not empty then new entered string will overlaped with old one
  274.          {
  275.            name[i]='\0';
  276.          }
  277.          i=0;
  278.          z=0;
  279.          z = atoi(num);                             //convert string to integer        
  280.         }                                           //Ajouté à cause de l'erreur crosses initialization of 'char num [s]'
  281.         break;                                      //break the case'*'
  282.         default:                                    //programm comes here if key other than * is pressed
  283.          if(i==3)                                   //checks if keystroke is greater than 3
  284.          {
  285.           goto stop;                                //if it is greater than 3 then formation of making string stops  
  286.          }
  287.          name[i]=key;                               //put key value to the string name    
  288.          i++;                                       //increment string pointer for placing next key value else the pressed key will overwrite
  289.      }
  290.    }
  291.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement