107 lines
3.1 KiB
JavaScript
107 lines
3.1 KiB
JavaScript
var table;
|
|
var gamearea;
|
|
var move_number;
|
|
|
|
window.addEventListener("load", function(){
|
|
document.getElementById("start").addEventListener("click", function(){
|
|
document.getElementById("message").hidden = true;
|
|
document.getElementById("thegame").hidden = false;
|
|
document.getElementById("for-button").hidden = false;
|
|
if(document.getElementById('optionslist').value !== "2") {
|
|
new_game("x");
|
|
}
|
|
else if(document.getElementById('optionslist').value == "2") {
|
|
new_game("O");
|
|
}
|
|
if (document.getElementById('optionslist').value == "1") {
|
|
alert("Musisz wybrać O albo X!");
|
|
location.reload();
|
|
}
|
|
});
|
|
});
|
|
|
|
document.getElementById("restart").addEventListener("click", function() {
|
|
if(document.getElementById('optionslist').value !== "2") {
|
|
new_game("x");
|
|
}
|
|
else if(document.getElementById('optionslist').value == "2") {
|
|
new_game("O");
|
|
}
|
|
});
|
|
|
|
gamearea = document.getElementById("gamearea");
|
|
gamearea.addEventListener("click", function(eventtarget){
|
|
if(eventtarget.target.nodeName != "TH" || eventtarget.target.hasAttribute("place"))
|
|
return false;
|
|
eventtarget.target.setAttribute("place", gamearea.getAttribute("move"));
|
|
var move = 1;
|
|
if(gamearea.getAttribute("move") == "x")
|
|
{
|
|
move = -1;
|
|
gamearea.setAttribute("move", "O");
|
|
}
|
|
else
|
|
gamearea.setAttribute("move", "x");
|
|
move_number++;
|
|
table[parseInt(eventtarget.target.getAttribute("x"))][parseInt(eventtarget.target.getAttribute("y"))] = move;
|
|
switch(is_end())
|
|
{
|
|
case false: break;
|
|
case -1:
|
|
show_message("Wygrał krzyżyk!");
|
|
break;
|
|
case 1:
|
|
show_message("Wygrało kółko!");
|
|
break;
|
|
case -2:
|
|
show_message("Remis");
|
|
break;
|
|
}
|
|
});
|
|
|
|
|
|
function show_message(areatext)
|
|
{
|
|
document.getElementById("areatext").innerHTML = areatext;
|
|
document.getElementById("whos").innerHTML = "A tym razem?";
|
|
gamearea.removeAttribute("move");
|
|
document.getElementById("message").removeAttribute("hidden");
|
|
document.getElementById("for-button").hidden = true;
|
|
};
|
|
|
|
function new_game(begin)
|
|
{
|
|
move_number = 0;
|
|
table = new Array(3);
|
|
for(var i = 0; i < table.length;i++)
|
|
{
|
|
table[i] = new Array(table.length);
|
|
for(var x = 0; x < table.length; x++)
|
|
table[i][x] = 0;
|
|
}
|
|
|
|
var filling_area = gamearea.querySelectorAll("th[place]");
|
|
for(var i = 0; i < filling_area.length; i++)
|
|
filling_area[i].removeAttribute("place");
|
|
gamearea.setAttribute("move", begin);
|
|
};
|
|
function is_end()
|
|
{
|
|
for(var i = 0; i < 3; i++)
|
|
{
|
|
if(Math.abs(table[0][i] + table[1][i] + table[2][i]) == 3)
|
|
return table[0][i];
|
|
if(Math.abs(table[i][0] + table[i][1] + table[i][2]) == 3)
|
|
return table[i][0];
|
|
}
|
|
|
|
if(Math.abs(table[0][0] + table[1][1] + table[2][2]) == 3)
|
|
return table[0][0];
|
|
if(Math.abs(table[0][2] + table[1][1] + table[2][0]) == 3)
|
|
return table[0][2];
|
|
if(move_number >= 9)
|
|
return -2;
|
|
return false;
|
|
|
|
}
|