Cracking the code
This yr’s Google I/O puzzle challenges gamers to control mild and coloration by prisms to unlock sectors of the sport world. Past the core sport loop, a brand new dimension was added to the gameplay–bonus worlds hidden behind riddles generated with the Gemini API. This weblog will overview how we constructed it!
The Gemini integration: A inventive and scalable resolution
Hidden tiles are dynamically positioned on the map as Gemini fashions generate distinctive riddles for gamers to resolve and discover them. The objective? To create greater engagement by incentivizing gamers to discover new dimensions of the puzzle constructed with AI.
Fairly than manually hardcoding 100s of attainable secret tile places and corresponding riddles, we used AI to assist us scale the characteristic in a manner that was difficult and distinctive.
Our resolution: Dynamic riddle technology
To leverage Gemini’s strengths, we devised an answer that mixed algorithmic precision with AI-powered creativity. A backend algorithm positioned hidden tiles on the map and generated a immediate for the Gemini API based mostly on the sport guidelines describing the situation with three easy directions. This ensured that each riddle had a logical resolution inside the sport’s framework. We used Gemini to rework the algorithmically generated reply right into a intelligent riddle.
Algorithmic immediate technology
Based mostly on the sport’s guidelines we programmatically decided a “secret location” on the sport board that was used because the immediate for Gemini. This ensured that the reply to every riddle was all the time legitimate and solvable.
// Finds a brand new hiding spot for the Gemini token and generates a clue string
getHiddenLocation() {
const geminiCluePositions = GameWorld.getCluePositions() // Returns positions which might be designated as a "Clue" tile. We tag essential tiles when designing a stage. These are usually tiles that aren't movable by the participant.
// We get all of the tiles positions within the stage, a place is a straightforward XY coordinate
const secretLocations = GameWorld.getAllTilePositions()
// we take away tiles that aren't adjoining to a clue place...
.filter((tileA) => geminiCluePositions.some((tileB) => GameWorld.isNextTo(tileA, tileB)))
// we take away invalid positions, comparable to tiles that aren't empty
.filter(({gridX, gridY}) => GameWorld.isValidGeminiPosition(gridX, gridY))
// we randomly select a hiding spot
const randomPosition = secretLocations[Math.floor(Math.random() * secretLocations.length)]
const randomTile = Gameworld.getTileByPosition(randomPosition)
// now that we've got a hiding spot, we generate a clue string
const riddleClues = GameWorld.generateGeminiRiddleClues(tilePosition)
return {
place: randomPosition,
clues: riddleClues,
}
}
The output of the algorithm was easy textual content like:
1. Instantly beneath a wall.
2. Precisely 2 tiles away from a rainbow node.
3. Within the first sector.
Gemini riddle creation
With a constant construction for the immediate to be generated, we then turned to the Gemini API to create a riddle that cryptically described the key tile’s location. By prompting Gemini with the required context and constraints, we had been in a position to create participating and difficult riddles that had been constantly formatted in a manner our entrance finish utility might show them to customers.
// Construct a immediate based mostly on the tile place. We all the time output 3 guidelines on this order:
// Clue 1. The kind of one adjoining tile to the key location
// Clue 2. The sector which accommodates the key location
// Clue 3. The closest coloration node to the key location, and precisely what number of tiles away it's.
generateGeminiRiddleClues(tilePosition) {
const adjacentTiles = GameWorld.getAdjacentTiles(tilePosition) // Get the left, proper, prime and backside neighboring tiles
const locationSector = GameWorld.getTileSector(tilePosition) // get the "sector" of the tile. Ranges are divided in sectors or 'chunks' by the extent designer.
const nodeTiles = GameWorld.getAllNodeTiles() // get each 'Node' tile within the stage
// clue 1
const randomAdjacentTile = adjacentTiles[Math.floor(Math.random() * adjacentTiles.length)]
const course = GameWorld.getDirection(randomAdjacentTile, tilePosition)
const randomTileType = randomAdjacentTile.sort
const firstClue = `Instantly ${course} a ${randomTileType} tile` // e.g. "Instantly above a wall tile"
// clue 2
const secondClue = `In sector ${locationSector}` // e.g. "In sector 3"
// clue 3
const closestNode = nodeTiles.scale back((closest, node) => {
const distance = GameWorld.getDistance(node.place, tilePosition)
if (distance < closest.distance) {
return {node, distance}
}
return closest
}, {node: null, distance: Infinity})
const thirdClue = Precisely ${distance} tiles away from a ${closestNode.node.coloration} node`
const clues = `1. ${firstClue}. 2. ${secondClue}. 3. ${thirdClue}.`
return clues
}
The ensuing riddle was then:
I stand immediately beneath a wall so excessive,
Two tiles from a rainbow node, I lie.
Inside the first sector, my place you will see,
Remedy this and declare the token’s victory.
Why riddles?
Riddles are inherently cryptic and enjoyable, plus a level of ambiguity is anticipated. This allowed us to embrace the occasional “pink herring” or surprising flip of phrase that may come up from AI generated output. Moreover, riddles have interaction gamers’ reasoning expertise, encouraging them to assume creatively and apply their data of the sport’s guidelines, analyzing the structure of the board as they seek for the hidden tile.
Guaranteeing consistency in LLM generated output with System Directions
Working with AI comes with its personal set of challenges. Probably the most vital is the tendency for AI to “hallucinate” or deviate from supplied guidelines. We mitigated this threat by programmatically producing a immediate, offering examples and an outlined JSON output within the System Directions for the immediate:
**Essential Directions:**
- Reply **solely** with the JSON object within the actual format specified.
- Do **not** embrace any explanations, code blocks, or extra textual content.
- Do **not** enclose the JSON in triple backticks or any markdown formatting.
- Guarantee all strings within the JSON are correctly escaped.
- Escape particular characters like newlines (`n`), tabs (`t`), and citation marks (`"`) inside strings.
- Don't use single quotes; use double quotes for all JSON keys and string values.
- Make sure the JSON is legitimate and parsable.
We additionally leaned into the human capability for reasoning. Gamers are adept at decoding and deciphering cryptic clues. By creating riddles that required logical deduction, we empowered gamers to beat any potential inconsistencies in AI output. Finally, it was about discovering the precise stability between AI-generated content material and human ingenuity.
Construct with the Gemini API in your apps as we speak
This yr marked a milestone: the primary Google I/O puzzle that includes the Gemini API. For our design and engineering groups, it was extra than simply integration—it was a considerate exploration into a brand new period of collaborative creation with AI. We weren’t simply constructing a characteristic; we had been pioneering a brand new strategy to interactive experiences. As you think about bringing the Gemini API into your individual tasks, keep in mind these three key classes in figuring out your strategy:
- Creativity: Leverage AI in your merchandise for dynamic content material technology, scalability, and automation in methods you haven’t been in a position to earlier than.
- Design: Check writing efficient prompts and create prototypes in Google AI Studio to check your outcomes with completely different Gemini fashions and capabilities.
- Implementation: Write detailed System Directions to outline output format with examples of your required mannequin response to make your output extra structured and constant in a manner your utility can interpret.
AI is altering how customers work together with our apps and video games, opening doorways to new and thrilling consumer experiences.
Be part of us on-line for Google I/O Could 20-21, for this yr’s thrilling bulletins streaming stay from Shoreline Amphitheatre in Mountain View. We encourage you to experiment with Gemini and discover its potential to create extra useful and enjoyable experiences on your customers; the probabilities are infinite.