2017-08-28 78 views
0

我的第三個else語句條件應該是for循環的結果。我如何使用循環的條件?對else語句中的條件使用for循環語句的結果

 //If player clicks centre on first move go in corner square 
     if (current[4] === playerToken && this.state.stepNumber === 1) { 
      let move = cornerSquares[Math.floor(Math.random() * cornerSquares.length)]; 
      drawSquare(move); 
     } 
     //If player clicks corner square on first move go in centre square 
     else if (this.state.stepNumber === 1) { 
      for (let i = 0; i < cornerSquares.length; i++){ 
      if (current[cornerSquares[i]] === playerToken) { 
       drawSquare(4); 
      } 
      } 
     } 
     //If player or computer has 2 in a row, place in 3rd square to win or block 
     else if (/*CONDITION OF THE BELOW FOR LOOP*/) { 
      for (let i = 0; i < twoInRow.length; i++) { 
      const [a, b, c] = twoInRow[i]; 
      if (current[a] && current[a] === current[b]) { 
       drawSquare(c); 
      } 
      } 
     } 
     //Place in random empty square 
     else { 
     //code to randomly place x/o in random square 
     } 
     } 

回答

0

My third else if statement condition should be the result of the for loop =>沒有。

if {} else ...被同時評估,並且第一個驗證條件被採用。如果你想要一個後評估的一個條件,你必須使用if {} if {} ...

+0

謝謝,我的序列中有太多趕上了 - 含多處如果陳述工作正常:) – Nims

1

如果你可以定義c外面這樣,那麼你可以搭袢出這樣一個單獨的函數:

let c = null; 
    //If player clicks centre on first move go in corner square 
    if (current[4] === playerToken && this.state.stepNumber === 1) { 
     let move = cornerSquares[Math.floor(Math.random() * cornerSquares.length)]; 
     drawSquare(move); 
    } 
    //If player clicks corner square on first move go in centre square 
    else if (this.state.stepNumber === 1) { 
     for (let i = 0; i < cornerSquares.length; i++){ 
     if (current[cornerSquares[i]] === playerToken) { 
      drawSquare(4); 
     } 
     } 
    } 
    //If player or computer has 2 in a row, place in 3rd square to win or block 

    //in JS you can assign inside of an 'if' statement 
    //an assignment evaluates to the value assigned 
    //so we assign the result of 'hasTwoInRow' to 'c' and check if it's not null 
    else if ((c = hasTwoInRow(twoInRow,current)) !== null) { 
     drawSquare(c); 
    } 
    //Place in random empty square 
    else { 
    //code to randomly place x/o in random square 
    } 
    } 

    function hasTwoInRow(twoInRow,current) { 
    for (let i = 0; i < twoInRow.length; i++) { 
     const [a, b, c] = twoInRow[i]; 
     if (current[a] && current[a] === current[b]) { 
     return c; 
     } 
    return null; 
    } 
+0

謝謝@德米特里 - 正在尋找創建一個這樣的功能,傳遞C是我的麻煩。很容易一次顯示:) – Nims

+0

@Nims不客氣。請注意是否有幫助。 – Dmitry

1

你可以使用在move變量有,你找到了一個移動或沒有指示,並使用在未來if(不else):

let move = null; 
//If player clicks centre on first move go in corner square 
if (current[4] === playerToken && this.state.stepNumber === 1) { 
    let move = cornerSquares[Math.floor(Math.random() * cornerSquares.length)]; 
} 
//If player clicks corner square on first move go in centre square 
else if (this.state.stepNumber === 1) { 
    for (let i = 0; i < cornerSquares.length; i++){ 
     if (current[cornerSquares[i]] === playerToken) { 
      move = 4; 
      break; // Don't lose time on looping further 
     } 
    } 
} 
//If player or computer has 2 in a row, place in 3rd square to win or block 
if (move === null) { 
    for (let i = 0; i < twoInRow.length; i++) { 
     const [a, b, c] = twoInRow[i]; 
     if (current[a] && current[a] === current[b]) { 
      move = c; 
      break; // Don't lose time on looping further 
     } 
    } 
} 
//Place in random empty square 
if (move === null { 
    //code to randomly place x/o in random square 
    move = // your logic here 
} 
// Perform move 
drawSquare(move); 
+0

感謝@trincot - 我也喜歡這種方法。有很多方法可以用代碼完成相同的任務:) – Nims