Skip to content

Commit

Permalink
add points table
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitsmt211 committed Sep 27, 2023
1 parent 2446f72 commit 3143ef7
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 8 deletions.
117 changes: 110 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ let NowPlays = 'black'

playerTurnDisplay.textContent = 'black'

let pointsWhite=0
let pointsBlack=0



const startPieces = [rookBlack,knightBlack,bishopBlack,queenBlack,kingBlack,bishopBlack,knightBlack,rookBlack,
Expand All @@ -20,6 +23,9 @@ const startPieces = [rookBlack,knightBlack,bishopBlack,queenBlack,kingBlack,bish
rookWhite,knightWhite,bishopWhite,kingWhite,queenWhite,bishopWhite,knightWhite,rookWhite]


const pointsMap = {"queen":9,"bishop":3, "knight":3,"rook":5,"pawn":1}


function createBoard(){
startPieces.forEach((startPiece,index)=>{
const square = document.createElement('div')
Expand Down Expand Up @@ -57,6 +63,27 @@ squares.forEach(square=>{
let chosenElement
let startPosition

function displayPoints(currentPlayer){
if(currentPlayer==='black'){
document.querySelector('#points-black').innerText="Black: "+pointsBlack
return
}

document.querySelector('#points-white').innerText="White: "+pointsWhite
}

function updatePoints(currentPlayer,pieceAcquired){
if(currentPlayer==='black'){
pointsBlack+=pointsMap[pieceAcquired]

}

else{
pointsWhite+=pointsMap[pieceAcquired]
}

displayPoints(currentPlayer)
}

function dragstart(e){
startPosition = e.target.parentNode.getAttribute('square-id')
Expand Down Expand Up @@ -84,6 +111,7 @@ function dragdrop(e){
if(isOpponentPiece && isValidMove(e.target)){
e.target.parentNode.append(chosenElement)
e.target.remove()
updatePoints(NowPlays,e.target.classList.item(0))
changePlayer()
}

Expand Down Expand Up @@ -124,10 +152,17 @@ function reverseIds(){
}

function isValidMove(target){


const currentPosition = parseInt(startPosition)
//grabs the class at first index aka colour of chessmen
const chessman = chosenElement.classList.item(0)

if(isCheck() && chessman !=='king'){
console.log("move your king, it's check")
return false
}

//to validate first pawn move
const hasMovedOnce = chosenElement.classList.contains('moved')
const targetPosition = parseInt(target.getAttribute('square-id')) || parseInt(target.parentNode.getAttribute('square-id'))
Expand All @@ -137,38 +172,36 @@ function isValidMove(target){
case 'pawn':
return validatePawnMoves(target,currentPosition,targetPosition,hasMovedOnce)

break;

case 'rook':
return validateRookMoves(target,currentPosition,targetPosition)

break;

case 'knight':
return validateKnightMoves(target,currentPosition,targetPosition)

break;

case 'bishop':
return validateBishopMoves(target,currentPosition,targetPosition)

break;

case 'queen':
return validateQueenMoves(target,currentPosition,targetPosition)

break;

case 'king':
return validateKingMoves(target,currentPosition,targetPosition)

break;
}
}


function validatePawnMoves(target,currentPosition,targetPosition,hasMovedOnce){

// if(isCheck()){
// console.log("king in check, please move king")
// return
// }

if((targetPosition-currentPosition===7)||(targetPosition-currentPosition===9)){
if(target.classList.contains('pieces')){
return true
Expand Down Expand Up @@ -393,3 +426,73 @@ function validateKingMoves(target,currentPosition,targetPosition){
}
}

function isCheck(){
const opponent = NowPlays==='black'?'black':'white'
let opponentPieces
if(opponent==='black'){
opponentPieces = document.querySelectorAll('.pieces.black')

}

else{
opponentPieces = document.querySelectorAll('.pieces.white')
}

opponentPieces.forEach(piece=>{
//check if each piece is attacking king?
let pieceType = piece.classList.item(0)

let king = getKing()
let moved=true

let attacksKing=false
currentPosition = piece.getAttribute('square-id')
targetPosition = king.getAttribute('square-id')

switch (pieceType) {
case 'pawn':
attacksKing = validatePawnMoves(king,currentPosition,targetPosition,moved)

break;

case 'rook':
attacksKing = validateRookMoves(king,currentPosition,targetPosition)

break;

case 'knight':
attacksKing = validateKnightMoves(king,currentPosition,targetPosition)

break;

case 'bishop':
attacksKing = validateBishopMoves(king,currentPosition,targetPosition)

break;

case 'queen':
attacksKing = validateQueenMoves(king,currentPosition,targetPosition)

break;
}

if(attacksKing===true) return true;


})

}

function getKing(){
const kingAttacked = NowPlays==='black'?'black':'white'
let king
if(kingAttacked==='black'){
king = document.querySelector('.pieces.king.black')
}
else{
king = document.querySelector('.pieces.king.white')
}

return king
}

5 changes: 4 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
<div id="chess-board">
</div>
<div id="player"></div>
<div id="info"></div>
<div id="info">
<div id="points-white">White: 0</div>
<div id="points-black">Black: 0</div>
</div>
<script src="pieces.js"></script>
<script src="app.js"></script>
</body>
Expand Down
12 changes: 12 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@
margin-top: 50px;
font-size: 50px;
}


div#info{
height: 90px;
display: flex;
justify-content: space-evenly;
}

div#points-white,div#points-black{
display: inline-block;
font-size: 40px;
}

0 comments on commit 3143ef7

Please sign in to comment.