#include #include #include #include const char* ssid = "MIT"; const char* password = ; const int redPin = 2; const int greenPin = 3; const int bluePin = 4; // Initial board state (empty tile represented by "") String grid[3][3] = { {"BABA", "IS", "RED"}, {"GREEN", "BLUE", "YELLOW"}, {"CYAN", "MAGENTA", ""} }; int emptyRow = 2; int emptyCol = 2; // Map from color names to RGB values std::map> colorMap = { {"RED", {0, 255, 255}}, {"GREEN", {255, 0, 255}}, {"BLUE", {255, 255, 0}}, {"YELLOW", {0, 0, 255}}, {"CYAN", {255, 0, 0}}, {"MAGENTA", {0, 255, 0}} }; AsyncWebServer server(80); void setup() { Serial.begin(9600); while (!Serial) { delay(100); } pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); // Initialize RGB LED to be off initially analogWrite(redPin, 255); analogWrite(greenPin, 255); analogWrite(bluePin, 255); Serial.print("Connecting to WiFi"); WiFi.begin(ssid, password); // Wait for connection unsigned long startMillis = millis(); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); if (millis() - startMillis > 30000) { Serial.println("\nFailed to connect to Wi-Fi after 30 seconds."); return; } } Serial.println("\nConnected to Wi-Fi."); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); // Generate game page server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ String html = generateGamePage(); request->send(200, "text/html", html); }); // Handle tile movement server.on("/move", HTTP_GET, [](AsyncWebServerRequest *request){ String row = request->getParam("row")->value(); String col = request->getParam("col")->value(); moveTile(row.toInt(), col.toInt()); String html = generateGamePage(); request->send(200, "text/html", html); }); server.begin(); } void loop() { } // Generate HTML for the puzzle String generateGamePage() { String html = ""; html += "
"; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { String tile = grid[i][j]; if (tile == "") { html += "
"; } else { // Call moveTile to update board state and change color if tile clicked html += "
" + tile + "
"; } } } html += "
"; html += ""; html += ""; return html; } // Handle tile movement void moveTile(int row, int col) { // Check if the tile is adjacent to the empty space if ((abs(row - emptyRow) == 1 && col == emptyCol) || (abs(col - emptyCol) == 1 && row == emptyRow)) { // Swap the clicked tile with the empty space grid[emptyRow][emptyCol] = grid[row][col]; grid[row][col] = ""; // Update the empty space position emptyRow = row; emptyCol = col; // After each move, check if any row or column spells "BABA IS " checkForColorChange(); } } // Check if any row or column spells "BABA IS " void checkForColorChange() { for (int i = 0; i < 3; i++) { // Check rows if (grid[i][0] == "BABA" && grid[i][1] == "IS" && colorMap.count(grid[i][2]) > 0) { setColor(grid[i][2]); return; } // Check columns if (grid[0][i] == "BABA" && grid[1][i] == "IS" && colorMap.count(grid[2][i]) > 0) { setColor(grid[2][i]); return; } } analogWrite(redPin, 255); analogWrite(greenPin, 255); analogWrite(bluePin, 255); } // Set RGB LED color void setColor(String color) { if (colorMap.count(color) > 0) { std::array rgb = colorMap[color]; analogWrite(redPin, rgb[0]); analogWrite(greenPin, rgb[1]); analogWrite(bluePin, rgb[2]); } }