changed code -> You can now win, winner is announced, you can still play if the game is won tho.

master
ElGurkus 4 years ago
parent 999f3455f0
commit 5afbf52dde

@ -15,6 +15,10 @@ let rowMax;
let selectedPiece; let selectedPiece;
let turnTotal = 0; let turnTotal = 0;
let turnPlayer; let turnPlayer;
let piecesX;
let piecesO;
let gameOver = false;
let winner;
let somethingIsSelected = false; let somethingIsSelected = false;
for(let i = 0; i < 64; i++){ for(let i = 0; i < 64; i++){
@ -86,6 +90,7 @@ function selectField(field){
somethingIsSelected = false; somethingIsSelected = false;
} }
advanceTurn(); advanceTurn();
checkWinCondition();
} }
} }
function validMovesPlayer1(field){ function validMovesPlayer1(field){
@ -276,3 +281,30 @@ function movePiece(field, selectedPiece){
return selectedPiece.piece; return selectedPiece.piece;
} }
} }
function checkWinCondition(){
piecesX = 0;
piecesO = 0;
for(let i = 0; i < 64; i++){
fieldId = i.toString();
if(document.getElementById("field"+fieldId).piece === 1){
piecesX++;
} else if(document.getElementById("field"+fieldId).piece === 11){
piecesX += 2;
} else if(document.getElementById("field"+fieldId).piece === 2){
piecesO++;
} else if(document.getElementById("field"+fieldId).piece === 22){
piecesO += 2;
}
}
if(piecesX >= (2 * piecesO)){
gameOver = true;
winner = 1;
document.getElementById("turnTotal").innerText = "Game Over!";
document.getElementById("turnPlayer").innerText = "X Won!";
} else if (piecesO >= (2 * piecesX)){
gameOver = true;
winner = 2;
document.getElementById("turnTotal").innerText = "Game Over!";
document.getElementById("turnPlayer").innerText = "O Won!";
}
}
Loading…
Cancel
Save