From 5afbf52dde09caad1a839a23f59d99505bff53fb Mon Sep 17 00:00:00 2001 From: ElGurkus Date: Wed, 4 Nov 2020 15:09:47 +0100 Subject: [PATCH] changed code -> You can now win, winner is announced, you can still play if the game is won tho. --- code.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/code.js b/code.js index ae4f1a5..866a038 100644 --- a/code.js +++ b/code.js @@ -15,6 +15,10 @@ let rowMax; let selectedPiece; let turnTotal = 0; let turnPlayer; +let piecesX; +let piecesO; +let gameOver = false; +let winner; let somethingIsSelected = false; for(let i = 0; i < 64; i++){ @@ -86,6 +90,7 @@ function selectField(field){ somethingIsSelected = false; } advanceTurn(); + checkWinCondition(); } } function validMovesPlayer1(field){ @@ -275,4 +280,31 @@ function movePiece(field, selectedPiece){ } else { 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!"; + } } \ No newline at end of file