# Puzzle Game Example

Let's say we are creating a sliding puzzle game where the player needs to rearrange the tiles to complete the puzzle. Each tile is represented by an image, and the images are stored in an array.

```javascript
let puzzleTiles = [
  "tile1.jpg",
  "tile2.jpg",
  "tile3.jpg",
  "tile4.jpg",
  "tile5.jpg",
  "tile6.jpg",
  "tile7.jpg",
  "tile8.jpg",
  "tile9.jpg"
];
```

We can use the `shuffle()` function to randomly rearrange the elements of the array and create a new puzzle configuration.

```javascript
function shuffle(array) {
  for (let i = array.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
}
shuffle(puzzleTiles);
```

When the player moves a tile, we can use the `splice()` method to remove the tile from its current position and insert it into a new position.

```javascript
function moveTile(currentIndex, newIndex) {
  let tile = puzzleTiles.splice(currentIndex, 1)[0];
  puzzleTiles.splice(newIndex, 0, tile);
}
```

We can also use the `join()` method to check if the puzzle is complete by comparing the current configuration of the puzzleTiles array to the winning configuration.

```javascript
let winningConfiguration = [
  "tile1.jpg",
  "tile2.jpg",
  "tile3.jpg",
  "tile4.jpg",
  "tile5.jpg",
  "tile6.jpg",
  "tile7.jpg",
  "tile8.jpg",
  "tile9.jpg"
];

function checkWin() {
  let currentConfiguration = puzzleTiles.join();
  let winningConfiguration = winningConfiguration.join();
  if (currentConfiguration === winningConfiguration) {
    console.log("You win!");
  }
}
```

In this way, arrays can be used to store and manipulate the data of a puzzle game, such as the images of the tiles, the current configuration of the puzzle, and the winning configuration.

It's worth to mention that this is just one example and arrays can be used in many different ways depending on the complexity of the game and the requirements of the game logic.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://demirels-organization.gitbook.io/javascript-tutorial/arrays/puzzle-game-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
