Last active
July 2, 2018 08:04
-
-
Save netmaniac/126f066b05ab610760dd3669878ebf11 to your computer and use it in GitHub Desktop.
Kod do gry ciepło-zimno. Przykład dla Nettigo Starter Kit https://nettigo.pl/products/nettigo-starter-kit-dla-arduino Pełny opis projektu na: https://starter-kit.nettigo.pl/2018/07/saper-na-arduino/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <LiquidCrystal_PCF8574.h> | |
#include "PCF8574.h" | |
#define LCD_ADDR 0x27 // Adres I2C wyświetlacza LCD | |
#define BTN_ADDR 0x20 // Adres I2C macierzy przycisków | |
const int ROWS[] = {4, 5, 6, 7}; // Do których pinów expandera podłączone są wiersze macierzy? | |
const int COLS[] = {3, 2, 1, 0}; // Do których pinów expandera podłączone są kolumny macierzy? | |
const int NUM_ROWS = sizeof(ROWS)/sizeof(int); // Liczba wierszy (obliczana automatycznie) | |
const int NUM_COLS = sizeof(COLS)/sizeof(int); // Liczba kolumn (obliczana automatycznie) | |
const String HINTS[] = {"Goraco", "Cieplo", "Zimno"}; // Podpowiedzi wyświetlane na ekranie | |
LiquidCrystal_PCF8574 lcd(LCD_ADDR); | |
PCF8574 buttons; | |
int lastKeyReading = 0; | |
int correctKey = 0; | |
int attemptsCount = 0; | |
bool isRandomSeedSet = false; | |
typedef struct { | |
bool sameDiagonal; | |
bool sameColumn; | |
bool sameRow; | |
int distance; | |
} KeyRelationship; | |
void setup() { | |
lcd.begin(16, 2); | |
lcd.setBacklight(true); | |
buttons.begin(BTN_ADDR); | |
for(int x = 0; x < NUM_ROWS; x++) { | |
buttons.pinMode(ROWS[x], OUTPUT); | |
buttons.digitalWrite(ROWS[x], LOW); | |
} | |
for(int x = 0; x < NUM_COLS; x++) { | |
buttons.pinMode(COLS[x], INPUT_PULLUP); | |
} | |
lcd.println("Wcisnij przycisk,"); | |
lcd.setCursor(0, 1); | |
lcd.print("aby rozpoczac :)"); | |
} | |
void loop() { | |
int currentKeyReading = getKey(); | |
if(currentKeyReading != 0 && currentKeyReading != lastKeyReading) { | |
if(!isRandomSeedSet) { | |
randomSeed(millis()); | |
isRandomSeedSet = true; | |
} | |
attemptsCount++; | |
lcd.clear(); | |
if(correctKey == 0) { | |
correctKey = generateRandomKey(); | |
attemptsCount = 0; | |
lcd.print("Odgadnij"); | |
lcd.setCursor(0, 1); | |
lcd.print("przycisk :)"); | |
} else if(currentKeyReading == correctKey) { | |
lcd.print(String("Bingo! ") + attemptsCount + " prob"); | |
lcd.setCursor(0, 1); | |
lcd.print("Nowa gra?"); | |
correctKey = 0; | |
} else { | |
KeyRelationship relationship = calculateRelationship(currentKeyReading, correctKey); | |
if(relationship.distance - 1 >= sizeof(HINTS) / sizeof(String)) { | |
lcd.print(HINTS[sizeof(HINTS) / sizeof(String) - 1]); | |
} else { | |
lcd.print(HINTS[relationship.distance - 1]); | |
} | |
lcd.setCursor(0, 1); | |
if(relationship.sameDiagonal) { | |
lcd.print("Na przekatnej"); | |
}else if(relationship.sameColumn) { | |
lcd.print("W tej kolumnie"); | |
}else if(relationship.sameRow) { | |
lcd.print("W tym wierszu"); | |
} | |
} | |
} | |
lastKeyReading = currentKeyReading; | |
} | |
int getKey() { | |
for(int x = 0; x < NUM_ROWS; x++) { | |
for(int row = 0; row < NUM_ROWS; row++) buttons.digitalWrite(ROWS[row], x != row); | |
for(int y = 0; y < NUM_COLS; y++) { | |
if(!buttons.digitalRead(COLS[y])) return x * NUM_ROWS + y + 1; | |
} | |
} | |
return 0; | |
} | |
int generateRandomKey() { | |
return random(1, NUM_ROWS * NUM_COLS + 1); | |
} | |
KeyRelationship calculateRelationship(int key1, int key2) { | |
KeyRelationship relationship; | |
int row1 = (key1 - 1) / NUM_COLS; | |
int row2 = (key2 - 1) / NUM_COLS; | |
int column1 = ((key1 - 1) % NUM_COLS); | |
int column2 = ((key2 - 1) % NUM_COLS); | |
relationship.sameColumn = column1 == column2; | |
relationship.sameRow = row1 == row2; | |
relationship.sameDiagonal = abs(row1 - row2) == abs(column1 - column2); | |
relationship.distance = round(sqrt(pow(column1 - column2, 2) + pow(row1 - row2, 2))); | |
return relationship; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment