2016-04-14 96 views
0

我正在製作一個2人Tic-Tac-Toe項目,而且我遇到了一個問題,我的程序沒有改變播放器並沒有正確切換播放器。另外,我的程序在更新分數時也存在問題,我使用它來通過使用二進制邏輯,按位運算符和數組來確定勝利條件,以確定何時達到勝利條件。Tic-Tac-Toe遊戲不會切換玩家,更新分數或更換玩家

var player1name = ""; 
 
var player2name = ""; 
 
var firstPlayer = player1name; 
 
var player1 = 0; //score for all games 
 
var player2 = 0; //score for all games 
 
var scoreX = 0; //player "X" score on current board 
 
var scoreO = 0; //player "X" score on current board 
 
var value = 0; //button binary value 
 
var player = "X"; //current player's turn X or O 
 
var winner = ""; //stores winner X or O 
 
var winArray = [7, 56, 448, 73, 146, 292, 273, 84]; 
 
var clickedArray = []; 
 
var clicksCount = 0; 
 
var currentPlayer = ""; //get player's name 
 
      
 
function start() 
 
{ 
 
    //alert("start"); 
 
    player1name = prompt("Enter first player's name:", ""); 
 
    player2name = prompt("Enter second player's name:", ""); 
 
    currentPlayer = player1name; 
 
    document.getElementById("player").innerHTML = currentPlayer + " choose a square."; 
 
} 
 
      
 
function playerMoved(id, value) 
 
{ 
 
    changeText(id); 
 
    updateScore(value); 
 
       
 
    //check for winner 
 
    if (player === "X") { 
 
    check4winner(scoreX); 
 
    } else { 
 
    check4winner(scoreO); 
 
    } 
 
    
 
    //was there a winner? 
 
    if (winner !== "") { 
 
    //alert("winner:" + winner); 
 
    //logic to display winner 
 
    document.getElementById("winner").innerHTML = currentPlayer + " won!"; 
 
    startNewGame(); 
 
    } else { 
 
    changePlayer(); 
 
    } 
 
    switchPlayer(); 
 
} 
 
      
 
function changeText(id) 
 
{ 
 
    //alert("changeText: " + player); 
 
    //alert(id.innerHTML); 
 
    if (id.innerHTML === "") { 
 
    //storing id's of clicked div to reset later 
 
    clickedArray[clicksCount] = id; 
 
    clicksCount = clicksCount + 1; 
 
    
 
    if (player === "X") { 
 
     id.innerHTML = "X"; 
 
    } else { 
 
     id.innerHTML = "O"; 
 
    } 
 
    } 
 
} 
 
      
 
function switchPlayer() 
 
{ 
 
    //alert("switchPlayer"); 
 
    //switch player prompt 
 
    if (currentPlayer === player1name) { 
 
    currentPlayer = player2name; 
 
    } else { 
 
    currentPlayer = player1name; 
 
    } 
 
    //re-display current player in h2 
 
    document.getElementById("player").innerHTML = currentPlayer + " choose a square."; 
 
} 
 
      
 
function updateScore(value) 
 
{ 
 
    //alert("updateScore"); 
 
    //alert("Update Score for " + player); 
 
    if (player === "X") { 
 
    scoreX = scorex + value; 
 
    alert("scoreX: " + scoreX); 
 
    } else { 
 
    scoreO = scoreO + value; 
 
    alert("scoreO: " + scoreO); 
 
    } 
 
    alert("PlayerX Score: " + scoreX + " PlayerO Score: " + scoreO); 
 
} 
 
      
 
function check4winner(score) 
 
{ 
 
    //alert("Checking for winner" + score); 
 
    var i; 
 
    for (i = 0; i < winsArray.length; i++) 
 
    { 
 
    if ((winsArray[i] & score) === winsArray[i]) { 
 
     if (player === "X") { 
 
     winner = "X"; 
 
     } else { 
 
     winner = "O"; 
 
     } 
 
     alert(winner + "Won!"); 
 
     i = 99; //break out of for loop 
 
    } 
 
    } 
 
} 
 
      
 
function changePlayer() 
 
{ 
 
    //alert("Changed Player: " + player); 
 
    if (player === "X") { 
 
    player = "O"; 
 
    } else { 
 
    player = "X"; 
 
    } 
 
    document.getElementById("player").innerHTML = currentPlayer + " choose a square."; 
 
} 
 
      
 
function startNewGame() 
 
{ 
 
    //alert("Start new game"); 
 
    winner = ""; 
 
    scoreO = 0; 
 
    scoreX = 0; 
 
    //reset all clicked squares 
 
    for (i = 0; i < clickedArray.length; i++) 
 
    { 
 
    clickedArray[i].innerHTML = ""; 
 
    } 
 
    //change who goes first 
 
    if (firstPlayer === player1name) { 
 
    firstPlayer = player2name; 
 
    } else { 
 
    firstPlayer = player1name; 
 
    } 
 
    currentPlayer = firstPlayer; 
 
    document.getElementById("player").innerHTML = currentPlayer + " choose a square."; 
 
    //update scores 
 
}
.Table { 
 
    display: table; 
 
} 
 

 
.Row { 
 
    display: table-row; 
 
} 
 
      
 
.Cell { 
 
    display: table-cell; 
 
    border: dashed black 2px; 
 
    padding: 0px 5px 0px 5px; 
 
    height: 50px; 
 
    width: 50px; 
 
    text-align: center; 
 
    vertical-align: middle; 
 
    background-color: #3B653D; 
 
    color: #FFFEFA; 
 
    font-size: xx-large; 
 
}
<body onload="start()"> 
 
     <div class="Table"> 
 
      <div class="Title"> 
 
       <h1>Tic-Tac-Toe</h1> 
 
       <h2 id="winner"></h2> 
 
       <h2 id="player"></h2> 
 
       <div class="Row"> 
 
        <div class="Cell" onclick="playerMoved(this, 1)"></div> 
 
        <div class="Cell" onclick="playerMoved(this, 2)"></div> 
 
        <div class="Cell" onclick="playerMoved(this, 4)"></div> 
 
       </div> 
 
       <div class="Row"> 
 
        <div class="Cell" onclick="playerMoved(this, 8)"></div> 
 
        <div class="Cell" onclick="playerMoved(this, 16)"></div> 
 
        <div class="Cell" onclick="playerMoved(this, 32)"></div> 
 
       </div> 
 
       <div class="Row"> 
 
        <div class="Cell" onclick="playerMoved(this, 64)"></div> 
 
        <div class="Cell" onclick="playerMoved(this, 128)"></div> 
 
        <div class="Cell" onclick="playerMoved(this, 256)"></div> 
 
       </div> 
 
      </div> 
 
     </div> 
 
    </body>

+0

你能否縮小問題所在,並且只是發表幾行代碼來展示具體問題? – RJM

+0

我會的,但我是編程新手,我不確定我的問題在哪裏。抱歉! –

+0

@BenK如果你能寫出你在這裏發佈的內容,我相信你也可以在代碼中編寫'console.log(variable_name);'來追蹤一些錯誤。你必須有一些想法或期望它應該看起來如何。 – jDo

回答

2

你應該學會如何在瀏覽器的Dev Tools錯誤控制檯。在進行移動時,它會提供以下錯誤:

js:137 Uncaught ReferenceError: scorex is not defined

這些提示可幫助您找出代碼中存在問題的時間。例如,在此錯誤中,它顯示在第137行中,您正在使用名爲scorex的變量(請注意小寫字母X)。不過,您的實際變量包含大寫X.一旦JavaScript遇到錯誤,它將停止執行。

由於scorex不是定義的變量,JavaScript會拋出錯誤。將其更改爲scoreX並且它會超過該錯誤。變量區分大小寫。

這之後還有更多的錯誤,所以我會讓你把它們弄清楚。我會給你一個提示:winArraywinsArray

+0

'scorex'未定義,但一旦修復,'winsArray'也未定義。工作小提琴:https://jsfiddle.net/sv1uhkhu/ – Eli

+1

謝謝,因爲把開發工具引起我的注意。我是編程新手,並沒有聽說過很多工具。 –

+0

@Eli「工作」並不是確切的詞,因爲它不會告訴你什麼時候你贏了[例如這種情況](http://i.imgur.com/1JR7msF.png)。 – blex