亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React.js 中 Hangman 游戲的對象比較

React.js 中 Hangman 游戲的對象比較

月關寶盒 2023-05-11 09:50:55
目標:輸出與單詞中字母數量相同的空白div。將兩個對象數組相互比較,并在適當的位置顯示常用字母。當另一個字母被添加到對象數組時,保留單詞顯示中所有前面的字母。問題:將兩個對象相互比較并僅顯示常用字母。word: [{id: 0, val: "w"}{id: 1, val: "o"}{id: 2, val: "r"}{id: 3, val: "d"}{id: 4, val: "d"}]goodAttempts: [{id: 3, val: "d"}{id: 4, val: "d"}]下面是用于捕獲按鍵并將其分配給狀態的代碼。goodAttempts 和 word 的狀態(在別處捕獲并分配)作為 props 傳遞給組件。 handleKeyDown = (event) => {        let match = [];        let repeat = false;        let letterIndex= [];        let correct = false;                // Validate if the key pressed is recurring in allAttempts        this.state.allAttempts.map((value, index) => {            if( this.state.allAttempts[index] === event.key ) {                return repeat = true;            }        })        // Validate if key pressed matches the word        this.state.word.map((value, index) => {               if( this.state.word[index].val === event.key ) {                letterIndex.push(index);                match.push(this.state.word[index]);                correct = true;                return            }         })        // if repeat is false set allAttempts and repeat. else set repeat to true        if( !repeat ) {            this.setState({                allAttempts: this.state.allAttempts.concat(event.key),                goodAttempts: this.state.goodAttempts.concat(match),                repeat: false,                letterIndex: this.state.letterIndex.concat(letterIndex),            });        } else {            this.setState({                repeat: true,            })        }    }在猜到之前,我希望將這封信保留在頁面之外,并希望避免直接從腳本中操作 HTML?;刭?
查看完整描述

2 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

希望這可以幫助。大警告,未經測試,只是在此處輸入作為示例。


 constructor(props) {

        super(props);

        this.state = {

           word: this.buildWordModel("word"),

           attempts : []

        }


 buildWordModel(word) {

        return word.map((char, idx) => {

           return { idx: idx, character: char, found:false};

        });

 }


 handleKeyDown = (event) => {


        const foundLetter = this.state.word.find(letter=> letter.character == event.key);

        

        // you can change condition and return found letter index if you want 

        // player to guess all occurances of each letter

        // for example, you could use findIndex above where found==false

        // the use the index to mod the array


        this.setState({

            word: this.state.word.map(letter=> (letter.character=== foundLetter.character? Object.assign({}, letter, { found:true }) : letter)),

            attempts: this.state.attempts.concat( { character: event.key, successful: !!foundLetter})

        });            

 }


 // then it's easy...


 renderWord() {

     return this.state.word.map(letter=> <div id={letter.idx} className={letter.found?"found":""}>{letter.character}</div>);

 }


查看完整回答
反對 回復 2023-05-11
?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

關鍵是讓一切都保持一種狀態并將其全部傳遞給組件。通過添加found: bool到對象,組件可以很容易地在條件語句中使用它。最終對象看起來像:


this.state.word: [

0: {found: false, val: "w", id: 0}

1: {found: false, val: "o", id: 1}

2: {found: false, val: "r", id: 2}

3: {found: true, val: "d", id: 3}

4: {found: true, val: "d", id: 4

]

在按鍵時,我驗證鍵是否與val對象的匹配并分配found: true. 一旦完成,它就會推送到狀態并更新所有內容。然后將該狀態推送到組件以呈現新信息。


 handleKeyDown = (event) => {

        let match = [...this.state.word];

        let repeat = false;

        let letterIndex= [];

        let correct = false;

        

        // Validate if the key pressed is recurring in allAttempts

        this.state.allAttempts.map((value, index) => {


            if( this.state.allAttempts[index] === event.key ) {

                return repeat = true;

            }


        })


        // Validate if key pressed matches the word

        this.state.word.map((value, index) => {


            console.log(value)


            // console.log(this.state.word[0].val)

            

            if( this.state.word[index].val === event.key ) {


                match[index] = {...match[index], found: true}                

                letterIndex.push(index);

                correct = true;


            }           

        })


        // if repeat is false set allAttempts and repeat. else set repeat to true

        if( !repeat ) {


            this.setState({

                allAttempts: this.state.allAttempts.concat(event.key),

                word: match,

                repeat: false,

                letterIndex: this.state.letterIndex.concat(letterIndex),

            });

            

        } else {


            this.setState({

                repeat: true,

            })


        }

    }

const mapAll = props.word.map((value, index) =>

    

        <div 

            className="card bg-primary letter-card"

            key={value.id}

        >

            <div className="card-body">

                <h5

                    id={index} 

                    className="card-title letter-card"

                >

                    { props.word[index].found ? (

                        

                        <h6>{props.word[index].val}</h6>

                    ): (<></>)}

          

                    

                </h5>

            </div>

        </div>

    

    );


查看完整回答
反對 回復 2023-05-11
  • 2 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號