From 6e4370adb342135df7054600744b63cba0a762a9 Mon Sep 17 00:00:00 2001 From: Dhairya Shah Date: Sat, 15 Aug 2020 02:07:33 +0530 Subject: [PATCH 1/6] Step 3 --- client/index.ejs | 3 ++ client/js/Game.js | 11 +++++-- client/js/activatePlayer.js | 16 +++++++++ client/js/bluff.js | 18 ++++++++-- client/js/bluffData.js | 56 ++++++++++++++++++++++++++++++++ client/js/cardClick/moveCards.js | 15 ++++++++- client/js/deactivatePlayer.js | 15 +++++++++ client/js/renderDeck.js | 5 +-- 8 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 client/js/activatePlayer.js create mode 100644 client/js/bluffData.js create mode 100644 client/js/deactivatePlayer.js diff --git a/client/index.ejs b/client/index.ejs index bf6cbbd..ca1496d 100644 --- a/client/index.ejs +++ b/client/index.ejs @@ -10,6 +10,9 @@ + + + diff --git a/client/js/Game.js b/client/js/Game.js index 7edd715..f78b7ce 100644 --- a/client/js/Game.js +++ b/client/js/Game.js @@ -3,10 +3,17 @@ // Will take care of the game attributes like creating players, distributing cards and so on. class Game { - constructor () { + constructor (start) { this.players = [] + this._turn = start // Adding a turn to track whose turn it is + this.record = [] // Adding a record to store whether the last player bluffed or not + } + get turn() { + return this._turn + } + set turn(x) { + this._turn = x } - // Creating players based on the user input createPlayers (playerCount, deck) { for (let i = 0; i < playerCount; i++) { diff --git a/client/js/activatePlayer.js b/client/js/activatePlayer.js new file mode 100644 index 0000000..815f93a --- /dev/null +++ b/client/js/activatePlayer.js @@ -0,0 +1,16 @@ +// Activating the next player +function activatePlayer(game){ + let playerDiv = document.querySelectorAll('.PlayerDiv') + playerDiv.forEach((player) => { + let playerName = player.getElementsByTagName('h1')[0].textContent // Variable to store name of the current player + if (playerName === 'Player ' + game.turn) { // Checking if it is the current player or not + let activate = player.querySelectorAll('.Card') + let buttonactive = player.querySelector('.buttons') + activate.forEach((Card) => { + Card.setAttribute('style', 'pointer-events:auto') // Activating all Cards for current player + }) + buttonactive.disabled = false // Activating the button for current player + } + }) + game.turn += 1 // Adding 1 to turn +} \ No newline at end of file diff --git a/client/js/bluff.js b/client/js/bluff.js index c7ed9da..9722944 100644 --- a/client/js/bluff.js +++ b/client/js/bluff.js @@ -18,10 +18,22 @@ window.addEventListener('DOMContentLoaded', () => { newDeck.formDeck() } const finalDeck = newDeck.shuffleDeck() // Creating a final deck to be used after shuffling - const game = new Game() // Create a new Game instance + let start = Math.floor(Math.random() * playerCount) + 1 + const game = new Game(start) // Create a new Game instance game.createPlayers(playerCount, finalDeck) // Creating n players based on user input players = game.distributeCards(playerCount, finalDeck) // Distribute the cards to n players created before for (let i = 0; i < playerCount; i++) { - renderDeck(players[i].playerName, players[i].playerCards) // Rendering the cards of players on the screen + renderDeck(players[i].playerName, players[i].playerCards, game, playerCount) // Rendering the cards of players on the screen } -}) + let playerDiv = document.querySelectorAll('.PlayerDiv') // Storing all elements with class PlayerDiv + playerDiv.forEach((player) => { // Looping through each element to deactivate them + let deactivate = player.querySelectorAll('.Card') + deactivate.forEach((Card) => { + Card.setAttribute('style', 'pointer-events:none') // Deactivating click on each Card + }) + }) + activatePlayer(game) // Activating one player to start the game + // Alerting which player will start the game + let message = 'Player ' + start + ' will start.' + window.alert(message) +}) \ No newline at end of file diff --git a/client/js/bluffData.js b/client/js/bluffData.js new file mode 100644 index 0000000..579cd8e --- /dev/null +++ b/client/js/bluffData.js @@ -0,0 +1,56 @@ +// Storing data if the player bluffed or not +function bluffData(game, centralStack, initialNoOfCards, finalNoOfCards) { + if(typeof(data) === 'undefined') { // Initialising data only for the first time(like static) + data = [] + } + if(data.length === 0) { // Checking if it is the first chance or not + data = window.prompt('Which rank card have you added?(You may BLUFF)') // Asking user of the rank he has added + let message = 'Card Rank: ' + data.toUpperCase() + window.alert(message) + } + let j = 0 // Initialise a flag + for (let i = initialNoOfCards; i < finalNoOfCards; i++) { // Looping through cards added in this chance + if (centralStack[i].value !== 'Joker') { // Checking if value is not a joker + if(centralStack[i].value.toLowerCase() === data) { // Comparing the value of newly added cards added to the data entered + j += 1 // Adding 1 to the flag if true card is added + } + } + else { + j += 1 // Also, adding 1 to the flag if the card added is a joker + } + } + let noOfCardsMoved = finalNoOfCards - initialNoOfCards // Calculating the number of cards added in this chance + // Turn will have a value 2 if last chance was of the last player + // not 1 as I am adding 1 to turn in activatePlayer itself + if(game.turn !== 2) { + if (game.record !== 0) { + let msg = 'Player ' + (game.turn - 2) + ' added ' + noOfCardsMoved + ' card(s) to the stack.' + window.alert(msg) + } + // If value of flag is equal to the number of cards added in this turn + // Set record to not bluffed + if (j === noOfCardsMoved) { + game.record = 'Not Bluffed' + } + // Set record to Bluffed + else { + game.record = 'Bluffed' + } + } + else { + if (game.record !== 0) { + let msg = 'Player ' + game.players.length + ' added ' + noOfCardsMoved + ' card(s) to the stack.' + window.alert(msg) + } + // If value of flag is equal to the number of cards added in this turn + // Set record to not bluffed + if (j === noOfCardsMoved) { + game.record = 'Not Bluffed' + } + // Set record to Bluffed + else { + game.record = 'Bluffed' + } + } + console.log(game.record) // To see whether last player bluffed or not +} \ No newline at end of file diff --git a/client/js/cardClick/moveCards.js b/client/js/cardClick/moveCards.js index 545dcf3..fff21f7 100644 --- a/client/js/cardClick/moveCards.js +++ b/client/js/cardClick/moveCards.js @@ -1,5 +1,6 @@ -function moveCards () { +function moveCards (game, playerCount) { return function () { + let initialNoOfCards = centralStack.length // Calculating no. of cards initially in the central stack arrayId.forEach ((ID) => { const cardElement = document.getElementById(ID) cardElement.parentNode.removeChild(cardElement) @@ -16,6 +17,18 @@ function moveCards () { }) arrayId = [] console.log(centralStack) // To see the current state of central stack + const finalNoOfCards = centralStack.length // Calculating no. of cards in the Central Stack after adding new card(s) + let compare = parseInt(playerCount) + 1 + if (game.turn === compare) { // Checking if it was last player's turn or not + deactivatePlayer(game) // Deactivating the current player + game.turn = 1 // If it was last player change turn to 1 + activatePlayer(game) // Activating the next player + } + else { + deactivatePlayer(game) // Deactivating the current player + activatePlayer(game) // Activating the next player + } + bluffData(game, centralStack, initialNoOfCards, finalNoOfCards) } } diff --git a/client/js/deactivatePlayer.js b/client/js/deactivatePlayer.js new file mode 100644 index 0000000..e237f41 --- /dev/null +++ b/client/js/deactivatePlayer.js @@ -0,0 +1,15 @@ +// Deactivating the previously active player +function deactivatePlayer(game) { + let playerDiv = document.querySelectorAll('.PlayerDiv') + playerDiv.forEach((player) => { + let playerName = player.getElementsByTagName('h1')[0].textContent + if (playerName === 'Player ' + (game.turn - 1)) { // Checking if it is the previous player or not + let activate = player.querySelectorAll('.Card') + let buttonactive = player.querySelector('.buttons') + activate.forEach((Card) => { + Card.setAttribute('style', 'pointer-events:none') // Deactivating all Cards for previous player + }) + buttonactive.disabled = true // Deactivating the button for previous player + } + }) +} \ No newline at end of file diff --git a/client/js/renderDeck.js b/client/js/renderDeck.js index 89b33ad..9847e41 100644 --- a/client/js/renderDeck.js +++ b/client/js/renderDeck.js @@ -4,7 +4,7 @@ * newCard is a child element of #root * all divs are assigned their class names for styling reference */ -function renderDeck (name, deck) { +function renderDeck (name, deck, game, playerCount) { const playerCards = document.createElement('div') // Separate parent div to store cards of individual players playerCards.className = 'PlayerDiv' const playerName = document.createElement('h1') // Seperate element to display the name of the player @@ -34,7 +34,8 @@ function renderDeck (name, deck) { const moveButton = document.createElement("button") moveButton.innerHTML = "Finished selecting" moveButton.className = "buttons" - moveButton.addEventListener("click", moveCards (), false) + moveButton.disabled = true // Deactivating all buttons + moveButton.addEventListener("click", moveCards (game, playerCount), false) playerCards.appendChild(moveButton) document.getElementById('root').appendChild(playerCards) } From 539f8d4826d5ca4ca83baa5e4a14b4452ca534b7 Mon Sep 17 00:00:00 2001 From: Dhairya Shah Date: Sat, 15 Aug 2020 17:22:57 +0530 Subject: [PATCH 2/6] Added a modal for user input --- client/bluff.css | 21 +++++++++++++ client/index.ejs | 26 +++++++++++++++- client/js/Game.js | 1 + client/js/activatePlayer.js | 8 ++--- client/js/bluffData.js | 56 +++++------------------------------ client/js/check.js | 48 ++++++++++++++++++++++++++++++ client/js/deactivatePlayer.js | 8 ++--- client/js/submitCard.js | 18 +++++++++++ 8 files changed, 128 insertions(+), 58 deletions(-) create mode 100644 client/js/check.js create mode 100644 client/js/submitCard.js diff --git a/client/bluff.css b/client/bluff.css index 988b9dd..b8e0034 100644 --- a/client/bluff.css +++ b/client/bluff.css @@ -76,3 +76,24 @@ h1 { text-align: center; } + +.modal { + display: none; /* Hidden by default */ + position: fixed; /* Stay in place */ + z-index: 1; /* Sit on top */ + overflow: auto; /* Enable scroll if needed */ + background-color: #ffffff; + top: 0; + left: 100px; +} + +/* Modal Content/Box */ +.modal-content { + margin: 15% auto; /* 15% from the top and centered */ + padding: 20px; + width: 80%; /* Could be more or less, depending on screen size */ +} + +h2 { + text-align: center; +} \ No newline at end of file diff --git a/client/index.ejs b/client/index.ejs index ca1496d..e7ca242 100644 --- a/client/index.ejs +++ b/client/index.ejs @@ -23,7 +23,31 @@
-

CentralDeck

+
+

CentralDeck

+

Current Rank:

+
+
diff --git a/client/js/Game.js b/client/js/Game.js index f78b7ce..51704af 100644 --- a/client/js/Game.js +++ b/client/js/Game.js @@ -7,6 +7,7 @@ class Game { this.players = [] this._turn = start // Adding a turn to track whose turn it is this.record = [] // Adding a record to store whether the last player bluffed or not + this.data = '' // Data of which rank is currently being played } get turn() { return this._turn diff --git a/client/js/activatePlayer.js b/client/js/activatePlayer.js index 815f93a..b6d9862 100644 --- a/client/js/activatePlayer.js +++ b/client/js/activatePlayer.js @@ -1,11 +1,11 @@ // Activating the next player function activatePlayer(game){ - let playerDiv = document.querySelectorAll('.PlayerDiv') + const playerDiv = document.querySelectorAll('.PlayerDiv') playerDiv.forEach((player) => { - let playerName = player.getElementsByTagName('h1')[0].textContent // Variable to store name of the current player + const playerName = player.getElementsByTagName('h1')[0].textContent // Variable to store name of the current player if (playerName === 'Player ' + game.turn) { // Checking if it is the current player or not - let activate = player.querySelectorAll('.Card') - let buttonactive = player.querySelector('.buttons') + const activate = player.querySelectorAll('.Card') + const buttonactive = player.querySelector('.buttons') activate.forEach((Card) => { Card.setAttribute('style', 'pointer-events:auto') // Activating all Cards for current player }) diff --git a/client/js/bluffData.js b/client/js/bluffData.js index 579cd8e..9bda880 100644 --- a/client/js/bluffData.js +++ b/client/js/bluffData.js @@ -1,56 +1,14 @@ +/* eslint-disable no-undef */ +/* eslint-disable no-unused-vars */ // Storing data if the player bluffed or not function bluffData(game, centralStack, initialNoOfCards, finalNoOfCards) { - if(typeof(data) === 'undefined') { // Initialising data only for the first time(like static) - data = [] - } - if(data.length === 0) { // Checking if it is the first chance or not - data = window.prompt('Which rank card have you added?(You may BLUFF)') // Asking user of the rank he has added - let message = 'Card Rank: ' + data.toUpperCase() - window.alert(message) - } - let j = 0 // Initialise a flag - for (let i = initialNoOfCards; i < finalNoOfCards; i++) { // Looping through cards added in this chance - if (centralStack[i].value !== 'Joker') { // Checking if value is not a joker - if(centralStack[i].value.toLowerCase() === data) { // Comparing the value of newly added cards added to the data entered - j += 1 // Adding 1 to the flag if true card is added - } - } - else { - j += 1 // Also, adding 1 to the flag if the card added is a joker - } - } - let noOfCardsMoved = finalNoOfCards - initialNoOfCards // Calculating the number of cards added in this chance - // Turn will have a value 2 if last chance was of the last player - // not 1 as I am adding 1 to turn in activatePlayer itself - if(game.turn !== 2) { - if (game.record !== 0) { - let msg = 'Player ' + (game.turn - 2) + ' added ' + noOfCardsMoved + ' card(s) to the stack.' - window.alert(msg) - } - // If value of flag is equal to the number of cards added in this turn - // Set record to not bluffed - if (j === noOfCardsMoved) { - game.record = 'Not Bluffed' - } - // Set record to Bluffed - else { - game.record = 'Bluffed' + if(game.data === '') { // Checking if it is the first chance or not + document.getElementById('cardModal').style.display = "block" // Displaying the modal + document.getElementById('submit').onclick = function() { // Adding an onClick to the submit button of the modal + game.data = submitCard(game,centralStack, initialNoOfCards, finalNoOfCards) // Storing the current rank to game.data } } else { - if (game.record !== 0) { - let msg = 'Player ' + game.players.length + ' added ' + noOfCardsMoved + ' card(s) to the stack.' - window.alert(msg) - } - // If value of flag is equal to the number of cards added in this turn - // Set record to not bluffed - if (j === noOfCardsMoved) { - game.record = 'Not Bluffed' - } - // Set record to Bluffed - else { - game.record = 'Bluffed' - } + check(game, game.data, centralStack, initialNoOfCards, finalNoOfCards) // If not the first turn, check if the player bluffed or not } - console.log(game.record) // To see whether last player bluffed or not } \ No newline at end of file diff --git a/client/js/check.js b/client/js/check.js new file mode 100644 index 0000000..3db40a8 --- /dev/null +++ b/client/js/check.js @@ -0,0 +1,48 @@ +// Function to check whether the player bluffed or not +// Turn will have a value 2 if last chance was of the last player +// not 1 as I am adding 1 to turn in activatePlayer itself +function check(game, data, centralStack, initialNoOfCards, finalNoOfCards) { + let j = 0 // Initialise a flag + for (let i = initialNoOfCards; i < finalNoOfCards; i++) { // Looping through cards added in this chance + if (centralStack[i].value !== 'Joker') { // Checking if value is not a joker + if(centralStack[i].value === data) { // Comparing the value of newly added cards added to the data entered + j += 1 // Adding 1 to the flag if true card is added + } + } + else { + j += 1 // Also, adding 1 to the flag if the card added is a joker + } + } + const noOfCardsMoved = finalNoOfCards - initialNoOfCards // Calculating the number of cards added in this chance + if(game.turn !== 2) { + if (game.record !== 0) { + const msg = 'Player ' + (game.turn - 2) + ' added ' + noOfCardsMoved + ' card(s) to the stack.' + window.alert(msg) + } + // If value of flag is equal to the number of cards added in this turn + // Set record to not bluffed + if (j === noOfCardsMoved) { + game.record = 'Not Bluffed' + } + // Set record to Bluffed + else { + game.record = 'Bluffed' + } + } + else { + if (game.record !== 0) { + const msg = 'Player ' + game.players.length + ' added ' + noOfCardsMoved + ' card(s) to the stack.' + window.alert(msg) + } + // If value of flag is equal to the number of cards added in this turn + // Set record to not bluffed + if (j === noOfCardsMoved) { + game.record = 'Not Bluffed' + } + // Set record to Bluffed + else { + game.record = 'Bluffed' + } + } + console.log(game.record) // To see whether last player bluffed or not +} \ No newline at end of file diff --git a/client/js/deactivatePlayer.js b/client/js/deactivatePlayer.js index e237f41..45bf308 100644 --- a/client/js/deactivatePlayer.js +++ b/client/js/deactivatePlayer.js @@ -1,11 +1,11 @@ // Deactivating the previously active player function deactivatePlayer(game) { - let playerDiv = document.querySelectorAll('.PlayerDiv') + const playerDiv = document.querySelectorAll('.PlayerDiv') playerDiv.forEach((player) => { - let playerName = player.getElementsByTagName('h1')[0].textContent + const playerName = player.getElementsByTagName('h1')[0].textContent if (playerName === 'Player ' + (game.turn - 1)) { // Checking if it is the previous player or not - let activate = player.querySelectorAll('.Card') - let buttonactive = player.querySelector('.buttons') + const activate = player.querySelectorAll('.Card') + const buttonactive = player.querySelector('.buttons') activate.forEach((Card) => { Card.setAttribute('style', 'pointer-events:none') // Deactivating all Cards for previous player }) diff --git a/client/js/submitCard.js b/client/js/submitCard.js new file mode 100644 index 0000000..23df64b --- /dev/null +++ b/client/js/submitCard.js @@ -0,0 +1,18 @@ +// Function which executes when the submit button is clicked +function submitCard(game,centralStack, initialNoOfCards, finalNoOfCards) { + const modal = document.getElementById('cardModal') + modal.style.display = 'none' // Removing the modal from the screen + const selectedCard = document.getElementById('selectCard') // Storing all the options in select menu + let optSelected = null + // Looping thropugh the options to check if the option was selected or not + for ( let i = 0; i < selectedCard.options.length; i++) { + optSelected = selectedCard.options[i] + if ( optSelected.selected === true) { + break // If option was selected + } + } + const h2 = document.getElementsByTagName('h2')[0] + h2.innerHTML = 'Current Rank: ' + optSelected.value // Updating the h2 inside centralStack + check(game, optSelected.value, centralStack, initialNoOfCards, finalNoOfCards) // Calling check for the first time + return optSelected.value // Return the value of selected option +} \ No newline at end of file From b957272fc5e4f415c353fe3ad8965a8aa52a0d12 Mon Sep 17 00:00:00 2001 From: Dhairya Shah Date: Sat, 15 Aug 2020 17:25:28 +0530 Subject: [PATCH 3/6] update index.ejs --- client/index.ejs | 1 + 1 file changed, 1 insertion(+) diff --git a/client/index.ejs b/client/index.ejs index e7ca242..e69677a 100644 --- a/client/index.ejs +++ b/client/index.ejs @@ -15,6 +15,7 @@ + From 200607fd620776ed68b87b696e71d974e529d471 Mon Sep 17 00:00:00 2001 From: Dhairya Shah Date: Sat, 15 Aug 2020 17:27:15 +0530 Subject: [PATCH 4/6] update index.ejs --- client/index.ejs | 1 + 1 file changed, 1 insertion(+) diff --git a/client/index.ejs b/client/index.ejs index e69677a..89cbbc8 100644 --- a/client/index.ejs +++ b/client/index.ejs @@ -16,6 +16,7 @@ + From 5e1ae1f6b6aca2e72a7902f324f21219e4a4f4ae Mon Sep 17 00:00:00 2001 From: Dhairya Shah Date: Tue, 18 Aug 2020 17:35:38 +0530 Subject: [PATCH 5/6] Done recommended changes --- client/index.ejs | 5 ++-- client/js/Game.js | 2 +- client/js/activatePlayer.js | 23 +++++++-------- client/js/bluffData.js | 6 ++-- client/js/cardClick/moveCards.js | 9 ++---- client/js/check.js | 49 ++++++++++++-------------------- client/js/deactivatePlayer.js | 20 ++++++------- client/js/submitCard.js | 2 +- 8 files changed, 47 insertions(+), 69 deletions(-) diff --git a/client/index.ejs b/client/index.ejs index 89cbbc8..39d27c6 100644 --- a/client/index.ejs +++ b/client/index.ejs @@ -27,7 +27,8 @@

CentralDeck

-

Current Rank:

+

Current Rank:

+

Current Player:

- + \ No newline at end of file diff --git a/client/js/Game.js b/client/js/Game.js index 51704af..b5da298 100644 --- a/client/js/Game.js +++ b/client/js/Game.js @@ -7,7 +7,7 @@ class Game { this.players = [] this._turn = start // Adding a turn to track whose turn it is this.record = [] // Adding a record to store whether the last player bluffed or not - this.data = '' // Data of which rank is currently being played + this.currentRank = '' // Data of which rank is currently being played } get turn() { return this._turn diff --git a/client/js/activatePlayer.js b/client/js/activatePlayer.js index b6d9862..095ac4a 100644 --- a/client/js/activatePlayer.js +++ b/client/js/activatePlayer.js @@ -1,16 +1,13 @@ // Activating the next player function activatePlayer(game){ - const playerDiv = document.querySelectorAll('.PlayerDiv') - playerDiv.forEach((player) => { - const playerName = player.getElementsByTagName('h1')[0].textContent // Variable to store name of the current player - if (playerName === 'Player ' + game.turn) { // Checking if it is the current player or not - const activate = player.querySelectorAll('.Card') - const buttonactive = player.querySelector('.buttons') - activate.forEach((Card) => { - Card.setAttribute('style', 'pointer-events:auto') // Activating all Cards for current player - }) - buttonactive.disabled = false // Activating the button for current player - } - }) - game.turn += 1 // Adding 1 to turn + const playerList = document.getElementById('root').children + const currentPlayer = playerList[game.turn + 1] + const activate = currentPlayer.querySelectorAll('.Card') + const buttonactive = currentPlayer.querySelectorAll('.buttons')[0] + activate.forEach((Card) => { + Card.style['pointer-events'] = 'auto' // Activating all Cards for current player + }) + buttonactive.disabled = false // Activating the button for current player + const currentPlayerName = currentPlayer.getElementsByTagName('h1')[0].textContent + document.getElementsByTagName('h2')[1].innerHTML = 'Current Player: ' + currentPlayerName } \ No newline at end of file diff --git a/client/js/bluffData.js b/client/js/bluffData.js index 9bda880..bc26d5b 100644 --- a/client/js/bluffData.js +++ b/client/js/bluffData.js @@ -2,13 +2,13 @@ /* eslint-disable no-unused-vars */ // Storing data if the player bluffed or not function bluffData(game, centralStack, initialNoOfCards, finalNoOfCards) { - if(game.data === '') { // Checking if it is the first chance or not + if(game.currentRank === '') { // Checking if it is the first chance or not document.getElementById('cardModal').style.display = "block" // Displaying the modal document.getElementById('submit').onclick = function() { // Adding an onClick to the submit button of the modal - game.data = submitCard(game,centralStack, initialNoOfCards, finalNoOfCards) // Storing the current rank to game.data + game.currentRank = submitCard(game, centralStack, initialNoOfCards, finalNoOfCards) // Storing the current rank to game.currentRank } } else { - check(game, game.data, centralStack, initialNoOfCards, finalNoOfCards) // If not the first turn, check if the player bluffed or not + check(game, game.currentRank, centralStack, initialNoOfCards, finalNoOfCards) // If not the first turn, check if the player bluffed or not } } \ No newline at end of file diff --git a/client/js/cardClick/moveCards.js b/client/js/cardClick/moveCards.js index fff21f7..ea61b37 100644 --- a/client/js/cardClick/moveCards.js +++ b/client/js/cardClick/moveCards.js @@ -18,17 +18,14 @@ function moveCards (game, playerCount) { arrayId = [] console.log(centralStack) // To see the current state of central stack const finalNoOfCards = centralStack.length // Calculating no. of cards in the Central Stack after adding new card(s) - let compare = parseInt(playerCount) + 1 - if (game.turn === compare) { // Checking if it was last player's turn or not + if (game.turn === parseInt(playerCount)) { // Checking if it was last player's turn or not deactivatePlayer(game) // Deactivating the current player - game.turn = 1 // If it was last player change turn to 1 - activatePlayer(game) // Activating the next player + bluffData(game, centralStack, initialNoOfCards, finalNoOfCards) } else { deactivatePlayer(game) // Deactivating the current player - activatePlayer(game) // Activating the next player + bluffData(game, centralStack, initialNoOfCards, finalNoOfCards) } - bluffData(game, centralStack, initialNoOfCards, finalNoOfCards) } } diff --git a/client/js/check.js b/client/js/check.js index 3db40a8..ff9bb53 100644 --- a/client/js/check.js +++ b/client/js/check.js @@ -1,11 +1,9 @@ // Function to check whether the player bluffed or not -// Turn will have a value 2 if last chance was of the last player -// not 1 as I am adding 1 to turn in activatePlayer itself -function check(game, data, centralStack, initialNoOfCards, finalNoOfCards) { +function check(game, currentRank, centralStack, initialNoOfCards, finalNoOfCards) { let j = 0 // Initialise a flag for (let i = initialNoOfCards; i < finalNoOfCards; i++) { // Looping through cards added in this chance if (centralStack[i].value !== 'Joker') { // Checking if value is not a joker - if(centralStack[i].value === data) { // Comparing the value of newly added cards added to the data entered + if(centralStack[i].value === currentRank) { // Comparing the value of newly added cards added to the data entered j += 1 // Adding 1 to the flag if true card is added } } @@ -14,35 +12,24 @@ function check(game, data, centralStack, initialNoOfCards, finalNoOfCards) { } } const noOfCardsMoved = finalNoOfCards - initialNoOfCards // Calculating the number of cards added in this chance - if(game.turn !== 2) { - if (game.record !== 0) { - const msg = 'Player ' + (game.turn - 2) + ' added ' + noOfCardsMoved + ' card(s) to the stack.' - window.alert(msg) - } - // If value of flag is equal to the number of cards added in this turn - // Set record to not bluffed - if (j === noOfCardsMoved) { - game.record = 'Not Bluffed' - } - // Set record to Bluffed - else { - game.record = 'Bluffed' - } + const msg = 'Player ' + game.turn + ' added ' + noOfCardsMoved + ' card(s) to the stack.' + window.alert(msg) + // If value of flag is equal to the number of cards added in this turn + // Set record to not bluffed + if (j === noOfCardsMoved) { + game.record = 'Not Bluffed' } + // Set record to Bluffed else { - if (game.record !== 0) { - const msg = 'Player ' + game.players.length + ' added ' + noOfCardsMoved + ' card(s) to the stack.' - window.alert(msg) - } - // If value of flag is equal to the number of cards added in this turn - // Set record to not bluffed - if (j === noOfCardsMoved) { - game.record = 'Not Bluffed' - } - // Set record to Bluffed - else { - game.record = 'Bluffed' - } + game.record = 'Bluffed' + } + if(game.turn === game.players.length) { + game.turn = 1 + activatePlayer(game) // Activating the next player + } + else { + game.turn += 1 + activatePlayer(game) // Activating the next player } console.log(game.record) // To see whether last player bluffed or not } \ No newline at end of file diff --git a/client/js/deactivatePlayer.js b/client/js/deactivatePlayer.js index 45bf308..93ba84a 100644 --- a/client/js/deactivatePlayer.js +++ b/client/js/deactivatePlayer.js @@ -1,15 +1,11 @@ // Deactivating the previously active player function deactivatePlayer(game) { - const playerDiv = document.querySelectorAll('.PlayerDiv') - playerDiv.forEach((player) => { - const playerName = player.getElementsByTagName('h1')[0].textContent - if (playerName === 'Player ' + (game.turn - 1)) { // Checking if it is the previous player or not - const activate = player.querySelectorAll('.Card') - const buttonactive = player.querySelector('.buttons') - activate.forEach((Card) => { - Card.setAttribute('style', 'pointer-events:none') // Deactivating all Cards for previous player - }) - buttonactive.disabled = true // Deactivating the button for previous player - } - }) + const playerList = document.getElementById('root').children + const currentPlayer = playerList[game.turn + 1] + const activate = currentPlayer.querySelectorAll('.Card') + const buttonactive = currentPlayer.querySelectorAll('.buttons')[0] + activate.forEach((Card) => { + Card.style['pointer-events'] = 'none' // Activating all Cards for current player + }) + buttonactive.disabled = true // Activating the button for current player } \ No newline at end of file diff --git a/client/js/submitCard.js b/client/js/submitCard.js index 23df64b..ae7539a 100644 --- a/client/js/submitCard.js +++ b/client/js/submitCard.js @@ -1,5 +1,5 @@ // Function which executes when the submit button is clicked -function submitCard(game,centralStack, initialNoOfCards, finalNoOfCards) { +function submitCard(game, centralStack, initialNoOfCards, finalNoOfCards) { const modal = document.getElementById('cardModal') modal.style.display = 'none' // Removing the modal from the screen const selectedCard = document.getElementById('selectCard') // Storing all the options in select menu From 425c516f9ebd7ac72b46bf92cb7215b720ef649e Mon Sep 17 00:00:00 2001 From: Dhairya Shah Date: Tue, 18 Aug 2020 17:48:08 +0530 Subject: [PATCH 6/6] Done recommended changes --- client/js/bluff.js | 2 +- client/js/check.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/js/bluff.js b/client/js/bluff.js index 9722944..01c0dde 100644 --- a/client/js/bluff.js +++ b/client/js/bluff.js @@ -34,6 +34,6 @@ window.addEventListener('DOMContentLoaded', () => { }) activatePlayer(game) // Activating one player to start the game // Alerting which player will start the game - let message = 'Player ' + start + ' will start.' + let message = game.players[game.turn - 1].playerName + ' will start.' window.alert(message) }) \ No newline at end of file diff --git a/client/js/check.js b/client/js/check.js index ff9bb53..36c6e21 100644 --- a/client/js/check.js +++ b/client/js/check.js @@ -12,7 +12,7 @@ function check(game, currentRank, centralStack, initialNoOfCards, finalNoOfCards } } const noOfCardsMoved = finalNoOfCards - initialNoOfCards // Calculating the number of cards added in this chance - const msg = 'Player ' + game.turn + ' added ' + noOfCardsMoved + ' card(s) to the stack.' + const msg = game.players[game.turn - 1].playerName + ' added ' + noOfCardsMoved + ' card(s) to the stack.' window.alert(msg) // If value of flag is equal to the number of cards added in this turn // Set record to not bluffed