Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdbool.h>
- #define buttonPin1 6
- #define buttonPin2 5
- #define buttonPin3 4
- #define buttonPin4 3
- void setup(){
- for(byte i = 6; i < 14; i++){
- pinMode(i, OUTPUT);
- }
- for(byte i = 2; i < 6; i++){
- pinMode(i, INPUT);
- }
- Serial.begin(9600);
- }
- char* generate(int len, bool repeat){
- if (repeat && len > 9){
- return NULL;
- }
- char* code = (char*)malloc(len * sizeof(char));
- randomSeed(analogRead(A4));
- if (repeat){
- strcpy(code[0], random(0,9) + '0');
- strcpy(code[1], random(0,9) + '0');
- strcpy(code[2], random(0,9) + '0');
- strcpy(code[3], random(0,9) + '0');
- Serial.print("CODE : ");
- Serial.println(code);
- } else {
- // todo
- }
- return code;
- }
- void get_score( char* secret, char* guess, int* peg_a, int* peg_b ){
- int a = 0;
- for(int i = 0; i < strlen(secret); i++){
- if(secret[i] == guess[i]){
- a++;
- }
- }
- int b = 0;
- for (int i = 0; i < strlen(secret); i++){
- if(secret[i] != guess[i]){
- for(int j = 0; j < strlen(secret); j++){
- if (secret[i] == guess[j]){
- b++;
- }
- }
- }
- }
- *peg_a = a;
- *peg_b = b;
- }
- void turn_off_leds(){
- digitalWrite(6, LOW);
- digitalWrite(7, LOW);
- digitalWrite(8, LOW);
- digitalWrite(9, LOW);
- digitalWrite(10, LOW);
- digitalWrite(11, LOW);
- digitalWrite(12, LOW);
- digitalWrite(13, LOW);
- }
- void render_led(int peg_a, int peg_b){
- digitalWrite(6, LOW);
- digitalWrite(7, LOW);
- digitalWrite(8, LOW);
- digitalWrite(9, LOW);
- digitalWrite(10, LOW);
- digitalWrite(11, LOW);
- digitalWrite(12, LOW);
- digitalWrite(13, LOW);
- for(int i = 0; i < peg_b; i+=2){
- digitalWrite(i+6, HIGH);
- }
- for(int i = 0; i < peg_a; i+=2){
- digitalWrite(i+7, HIGH);
- }
- }
- void play_game(char* secret){
- for(int pokus = 0; pokus < 10; pokus++) {
- int enter_value = 0;
- int number_1 = 0;
- int number_2 = 0;
- int number_3 = 0;
- int number_4 = 0;
- while( enter_value < 1010 ){
- if( digitalRead(buttonPin1) == HIGH ){
- number_1++;
- }
- if( digitalRead(buttonPin2) == HIGH ){
- number_2++;
- }
- if( digitalRead(buttonPin3) == HIGH ){
- number_3++;
- }
- if( digitalRead(buttonPin4) == HIGH ){
- number_4++;
- }
- if (number_1 > 9){
- number_1 = 0;
- }
- if (number_2 > 9){
- number_2 = 0;
- }
- if (number_3 > 9){
- number_3 = 0;
- }
- if (number_4 > 9){
- number_4 = 0;
- }
- enter_value = analogRead(A0);
- Serial.print("USER CODE : ");
- Serial.print(number_1);
- Serial.print(number_2);
- Serial.print(number_3);
- Serial.print(number_4);
- Serial.println("");
- delay(100);
- while(
- digitalRead(buttonPin1) == HIGH ||
- digitalRead(buttonPin2) == HIGH ||
- digitalRead(buttonPin3) == HIGH ||
- digitalRead(buttonPin4) == HIGH
- ){
- delay(50);
- }
- }
- int a=0, b=0;
- char guess[4];
- guess[0] = number_1 + '0';
- guess[1] = number_2 + '0';
- guess[2] = number_3 + '0';
- guess[3] = number_4 + '0';
- get_score(secret, "", a, b);
- render_led(a,b);
- }
- }
- void loop() {
- char* secret = generate(4, true);
- Serial.print("SECRET : ");
- Serial.println(secret);
- play_game(secret);
- free(secret);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement