1

I'm attempting to build a Treasure Hunting game that is a 3x3 grid of clickable squares. When clicked, the revealed value can either be a "tree" or "treasure. There is only one "treasure".

The part I'm struggling with (at this point) is assigning my squares, a random element of the array. I have successfully altered my original array of 9 items to a random array of "tree"s and one "treasure". But how do I assign those array elements, that is shuffled each time the shuffle function is called, to random squares in my grid?

I attempted to map through the array of trees and my single treasure but failed to find a way to randomly assign those values to random squares as I want the game to have treasure in a different location most of the time.

I have pasted my full code below for reference.

Board.js

import React, { Component } from 'react';
import Square from './Square'


class Board extends Component {
    constructor(props){
        super(props)
        this.state = {
            spaces: [1, 2, 3, 4, 5, 6, 7, 8, 9],
            boxOne: "?",
            boxTwo: "?",
            boxThree: "?",
            boxFour: "?",
            boxFive: "?",
            boxSix: "?",
            boxSeven: "?",
            boxEight: "?",
            boxNine: "?"
        }
    }
shuffle = (arr) => {
    for(let i = arr.length - 1; i > 0; i--) {
      const randomNum = Math.floor(Math.random() * (i + 1))
      [arr[i], arr[randomNum] = arr[randomNum], arr[i]]
    }
    let newArr = []
    arr.map(x => {
      if (x === 7) {
        newArr.push("Treasure")
      } else {
        newArr.push("Tree")
      }
    })
    this.setState({spaces: newArr})
}

  render() {
    return (
        <div className = "container">
            <div className = "board">
                <Square clickEvent={this.shuffle} boxOne={this.state.boxOne}
                boxTwo={this.state.boxTwo}
                boxThree={this.state.boxThree}
                boxFour={this.state.boxFour}
                boxFive={this.state.boxFive}
                boxSix={this.state.boxSix}
                boxSeven={this.state.boxSeven}
                boxEight={this.state.boxEight}
                boxNine={this.state.boxNine}
                newSpaces={this.state.spaces}
                />
            </div>
        </div>
    );
  }
}

export default Board;

Square.js

import React, { Component } from 'react';

class Square extends Component {
    render() {
        return(
            <div>
                <div className = "box" onClick = {this.props.shuffle} >{this.props.boxOne}</div>
                <div className = "box" onClick = {this.props.shuffle} >{this.props.boxTwo}</div>
                <div className = "box" onClick = {this.props.shuffle} >{this.props.boxThree}</div>
                <div className = "box" onClick = {this.props.shuffle} >{this.props.boxFour}</div>
                <div className = "box" onClick = {this.props.shuffle} >{this.props.boxFive}</div>
                <div className = "box" onClick = {this.props.shuffle} >{this.props.boxSix}</div>
                <div className = "box" onClick = {this.props.shuffle} >{this.props.boxSeven}</div>
                <div className = "box" onClick = {this.props.shuffle} >{this.props.boxEight}</div>
                <div className = "box" onClick = {this.props.shuffle} >{this.props.boxNine}</div>
            </div>
        )
    }
}

export default Square

4
  • Take a look here: stackoverflow.com/questions/2450954/…. That might help. Commented Mar 1, 2019 at 0:10
  • Also, when I see variable with name like thingOne, thingTwo, etc. I usually think "make that an array of 'things'!", and then I can iterate over it instead of having to reference each item individually. However, sometimes writing them all out is helpful to analyze the code, and maybe you find out they aren't all the same type of thing after all... Commented Mar 1, 2019 at 0:13
  • Thanks! The shuffling is taken care of essentially. It's the assigning to the different squares that is baffling me. Commented Mar 1, 2019 at 0:40
  • So you have two lists, right? One of "boxes" and one of "treasures". What if you could say "for the current box, I need the nth treasure"? And since the treasure list is shuffled already, you can just say the first box gets the first treasure, etc. Commented Mar 1, 2019 at 0:50

1 Answer 1

3

If I'm understanding you correctly, then I'd do it the simple way...

  1. Assign "Tree" to all of your squares.
  2. Assign "Treasure" to index Math.floor(Math.random() * 9).

This method is simple, and also efficient :)

You don't need to worry about shuffling your squares around, because you're just picking one of them to be the treasure at random.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch! Once I have the new array of 8 trees and one treasure, how would I assign those to my divs? That seems to be the tough part for me....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.