He leído todas las preguntas sugeridas y no encuentro la contestación a este error. Tengo tres archivos haciendo un programa simple HTML/JavaScript.
var currentPlayer = "O"
function place(box) {
if(box.innerText != "") return;
box.innerText = currentPlayer;
currentPlayer == "O" ? currentPlayer = "X": currentPlayer = "O";
//Box 1
if(box.id == "0_0"){
checkGameBoard("0_0","1_0","2_0");
checkGameBoard("0_0","1_1","2_2");
checkGameBoard("0_0","1_0","2_0");
}
//Box 2
if(box.id == "1_0"){
checkGameBoard("0_0","1_0","2_0");
checkGameBoard("1_0","1_1","1_2");
}
//Box 3
if(box.id == "2_0"){
checkGameBoard("0_0","1_0","2_0");
checkGameBoard("2_0","1_1","0_2");
checkGameBoard("2_0","2_1","2_2");
}
//Box 4
if(box.id == "0_1"){
checkGameBoard("0_1","1_1","2_1");
checkGameBoard("0_0","0_1","0_2");
}
//Box 5
if(box.id == "1_1"){
checkGameBoard("0_0","1_1","2_2");
checkGameBoard("1_0","1_1","1_2");
checkGameBoard("2_0","1_1","0_2");
checkGameBoard("0_1","1_1","2_1");
}
//Box 6
if(box.id == "2_1"){
checkGameBoard("0_1","1_1","2_1");
checkGameBoard("2_0","2_1","2_2");
}
//Box 7
if(box.id == "0_2"){
checkGameBoard("0_0","0_1","0_2");
checkGameBoard("0_2","1_1","2_0");
checkGameBoard("0_2","1_2","2_2");
}
//Box 8
if(box.id == "1_2"){
checkGameBoard("0_2","1_2","2_2");
checkGameBoard("1_0","1_1","1_2");
}
//Box 9
if(box.id == "2_2"){
checkGameBoard("0_2","1_2","2_2");
checkGameBoard("2_2","1_1","0_0");
checkGameBoard("2_0","2_1","2_2");
}
}
function checkGameBoard(firstCoord,secondCoord,thirdCoord) {
var first = document.getElementById(firstCoord).innerText;
var second = document.getElementById(secondCoord).innerText;
var thrid = document.getElementById(thirdCoord).innerText;
if(first == "") return;
if(first == second && first == thrid){
alert("Winner!")
}
}
//+ attacks a random adjacent oppenent peice ÷ trades places with random adjacent opopnet * places a second copy on empty adjacent square
var noughts = ["O", "O+","O-", "O/", "O*"]
var crosses = ["X", "X+", "X÷", "X*"]
function showOptions() {
document.getElementById("Player1").innerHTML = "";
for(i=0; i<noughts.length; i++) {
document.getElementById("Player1").innerHTML += "<input type='button' value='"+ noughts[i] + "' onclick='chooseThis('" + noughts[i] + "')>";
// alert(document.getElementById("Player1").innerHTML)
}
}
function chooseThis(item) {
currentPlayer = item;
alert(item);
}
.home {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 98vh;
padding: 0;
margin: none;
}
.row {
display: flex;
flex-direction: row;
}
.row div {
padding: 10px;
border: 1px solid;
height: 30px;
width: 30px;
}
<div class="home">
<div class="row">
<div id="0_0" onclick="place(this)"></div>
<div id="1_0" onclick="place(this)"></div>
<div id="2_0" onclick="place(this)"></div>
</div>
<div class="row">
<div id="0_1" onclick="place(this)"></div>
<div id="1_1" onclick="place(this)"></div>
<div id="2_1" onclick="place(this)"></div>
</div>
<div class="row">
<div id="0_2" onclick="place(this)"></div>
<div id="1_2" onclick="place(this)"></div>
<div id="2_2" onclick="place(this)"></div>
</div>
<div id="Player1">
<input type="button" value="Ready Player One?" id="PlayerOne" onclick="showOptions()">
</div>
</div>
Si elimino el atributo de onclick
trabaja bien. Sin embargo cuando uso el atributo recibo el mensaje de unexpected token. He tratado de dividir la sentencia en secciones mas pequeñas pero recibo el mismo error.