Develop the mineSweeper
function that evaluates a player's move in a grid-based minefield game. The function should analyze the grid position based on provided coordinates and determine if the player has landed on a safe spot or a mine.
function mineSweeper(grid, x, y)
- grid: An array of arrays representing the game grid.
- x: The x-coordinate (horizontal position) in the grid.
- y: The y-coordinate (vertical position) in the grid.
The grid consists of a 3x3 array of strings, where:
- "🟥" represents a mine.
- "🟦" represents a safe spot.
Example:
[
["🟥", "🟦", "🟥"],
["🟦", "🟥", "🟥"],
["🟥", "🟦", "🟦"]
]
The grid is interpreted with x
and y
coordinates ranging from 1 to 3.
x = 1
andy = 1
represents the top-left square.x = 3
andy = 1
represents the top-right square.x = 3
andy = 3
represents the bottom-right square.
- If the coordinates land on a red square ("🟥"), return
"🟥 💀"
. - If the coordinates land on a blue square ("🟦"), return
"🟦 🥳"
. - If
x
ory
is less than 1 or greater than 3, return"invalid coordinates"
.
let grid = [["🟥", "🟦", "🟥"], ["🟦", "🟥", "🟥"], ["🟥", "🟦", "🟦"]];
let result = mineSweeper(grid, 1, 1);
console.log(result);
"🟥 💀"
let grid = [["🟥", "🟦", "🟥"], ["🟦", "🟥", "🟥"], ["🟥", "🟦", "🟦"]];
let result = mineSweeper(grid, 2, 3);
console.log(result);
"🟦 🥳"
let grid = [["🟥", "🟦", "🟥"], ["🟦", "🟥", "🟥"], ["🟥", "🟦", "🟦"]];
let result = mineSweeper(grid, 4, 1);
console.log(result);
"invalid coordinates"