From aea9e6edc9d79a69ac60e0f75ca17a58074cbee2 Mon Sep 17 00:00:00 2001 From: rusydiFly Date: Sat, 9 Dec 2023 00:43:30 +0800 Subject: [PATCH 1/9] able to draw cards for more than 2 players --- script.js | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 164 insertions(+), 3 deletions(-) diff --git a/script.js b/script.js index bbe8a293..55ae7fa0 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,165 @@ -var main = function (input) { - var myOutputValue = 'hello world'; - return myOutputValue; +/* +Introduction +Implement a simplified version of Blackjack. If you're not familiar with Blackjack, refer to this video for game rules. Our simplified rules are the following. Please read all the requirements before starting it! +There will be only two players. One human and one computer (for the Base solution). +The computer will always be the dealer. +Each player gets dealt two cards to start. +The player goes first, and decides if they want to hit (draw a card) or stand (end their turn). +The dealer has to hit if their hand is below 17. +Each players' score is the total of their card ranks. Jacks/Queen/Kings are 10. Aces can be 1 or 11. +The player who is closer to, but not above 21 wins the hand. + +Base +Gameplay Description +The main function runs on each player's turn. The sequence of actions in the game might be the following. +Deck is shuffled. +User clicks Submit to deal cards. +The cards are analysed for game winning conditions, e.g. Blackjack. +The cards are displayed to the user. +The user decides whether to hit or stand, using the submit button to submit their choice. +The user's cards are analysed for winning or losing conditions. +The computer decides to hit or stand automatically based on game rules. +The game either ends or continues. + + +*/ + +var CARDS_TAKEN = 0; +var NUM_OF_PLAYER = 2; + +// deck algorithm +var makeDeck = function () { + // Initialise an empty deck array + var cardDeck = []; + // Initialise an array of the 4 suits in our deck. We will loop over this array. + var suits = ['spades', 'hearts', 'clubs','diamonds']; + + // Loop over the suits array + var suitIndex = 0; + while (suitIndex < suits.length) { + // Store the current suit in a variable + var currentSuit = suits[suitIndex]; + + // Loop from 1 to 13 to create all cards for a given suit + // Notice rankCounter starts at 1 and not 0, and ends at 13 and not 12. + // This is an example of a loop without an array. + var rankCounter = 1; + while (rankCounter <= 13) { + // By default, the card name is the same as rankCounter + var cardName = rankCounter; + + // If rank is 1, 11, 12, or 13, set cardName to the ace or face card's name + if (cardName == 1) { + cardName = 'ace'; + } else if (cardName == 11) { + cardName = 'jack'; + } else if (cardName == 12) { + cardName = 'queen'; + } else if (cardName == 13) { + cardName = 'king'; + } + + // Create a new card with the current name, suit, and rank + var card = { + name: cardName, + suit: currentSuit, + rank: rankCounter, + }; + + // Add the new card to the deck + cardDeck.push(card); + + // Increment rankCounter to iterate over the next rank + rankCounter += 1; + } + + // Increment the suit index to iterate over the next suit + suitIndex += 1; + } + + // Return the completed card deck + return cardDeck; +}; + + +// shuffle deck +var shuffledDeck = function(){ + // shuffle the card's index + let deck = makeDeck() + let shuffled = deck.sort(() => Math.random() - 0.5) + return shuffled +} + +//draw card. draw from the top array CARDS_TAKEN + 1 // i have to find a way to get alternating cards +var drawCard = function(){ + CARDS_TAKEN += 1 + return shuffledDeck()[CARDS_TAKEN]; +} + +//player block +// in player block +/* +1. player gets 2 cards +2. player goes before dealer. choose to hit or stand +3. return player sum +*/ +// var player = function(){ +// let card_A = cardValue() +// let card_B = cardValue() +// console.log(card_A) +// console.log(card_B) +// //assign function to variable so as to not draw another card +// return [card_A, card_B] +// if player hit +var hit = function(){ + let hit_card = drawCard() + console.log(hit_card) + return [hit_card] +} + +var stand = function(){ + return NaN +} + +// i need a way to process a card drawn let function "play" go first +// +var playerCard = function(){ + var val_cards = drawCard(), value + console.log(val_cards) + //by default + value = val_cards.rank; + //unless + // if card drawn == ace + if (val_cards.rank == 1){ + value = 11; + } + // if card drawn == face + if(val_cards.rank > 10){ + value = 10; + } + //return value + return {'value': value, 'card': val_cards.suit}; +} + +//dealer block +/* +The dealer has to hit if their hand is below 17. +*/ + +var playersDraw = function (input) { + var playerCounter = 0, playerResult = [], initalCardsDrawn = NUM_OF_PLAYER * 2, playerInGame = 0, player = {}; + while(playerCounter != initalCardsDrawn){ + // player draws 2 cards here + // let it be that the last in the array is the dealers draw + playerCounter += 1 + playerInGame += 1 + // get the distribution order or player 1 player 2 player 1 player 2 + if(playerInGame > NUM_OF_PLAYER){ + playerInGame = 1 + } + console.log(playerInGame) + player = {player : playerInGame, drawn: playerCard()} + playerResult.push(player) + } + return playerResult; }; From 0da468f4289449b7f39089b1c81101570db5559c Mon Sep 17 00:00:00 2001 From: rusydiFly Date: Sat, 9 Dec 2023 01:14:42 +0800 Subject: [PATCH 2/9] finish off dealers draw --- deck.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ script.js | 15 +++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 deck.js diff --git a/deck.js b/deck.js new file mode 100644 index 00000000..32886f81 --- /dev/null +++ b/deck.js @@ -0,0 +1,52 @@ +var makeDeck = function () { + // Initialise an empty deck array + var cardDeck = []; + // Initialise an array of the 4 suits in our deck. We will loop over this array. + var suits = ['hearts', 'diamonds', 'clubs', 'spades']; + + // Loop over the suits array + var suitIndex = 0; + while (suitIndex < suits.length) { + // Store the current suit in a variable + var currentSuit = suits[suitIndex]; + + // Loop from 1 to 13 to create all cards for a given suit + // Notice rankCounter starts at 1 and not 0, and ends at 13 and not 12. + // This is an example of a loop without an array. + var rankCounter = 1; + while (rankCounter <= 13) { + // By default, the card name is the same as rankCounter + var cardName = rankCounter; + + // If rank is 1, 11, 12, or 13, set cardName to the ace or face card's name + if (cardName == 1) { + cardName = 'ace'; + } else if (cardName == 11) { + cardName = 'jack'; + } else if (cardName == 12) { + cardName = 'queen'; + } else if (cardName == 13) { + cardName = 'king'; + } + + // Create a new card with the current name, suit, and rank + var card = { + name: cardName, + suit: currentSuit, + rank: rankCounter, + }; + + // Add the new card to the deck + cardDeck.push(card); + + // Increment rankCounter to iterate over the next rank + rankCounter += 1; + } + + // Increment the suit index to iterate over the next suit + suitIndex += 1; + } + + // Return the completed card deck + return cardDeck; +}; \ No newline at end of file diff --git a/script.js b/script.js index 55ae7fa0..f7c4ae5f 100644 --- a/script.js +++ b/script.js @@ -26,6 +26,7 @@ The game either ends or continues. var CARDS_TAKEN = 0; var NUM_OF_PLAYER = 2; +var PLAYER_DRAW // deck algorithm var makeDeck = function () { @@ -161,5 +162,19 @@ var playersDraw = function (input) { player = {player : playerInGame, drawn: playerCard()} playerResult.push(player) } + PLAYER_DRAW = playerResult; return playerResult; }; + +var getDealerDraw = function(){ + var draw = PLAYER_DRAW, dealersDraw, i = 0; + console.log(draw) + while(i <= draw.length){ + console.log(draw[i]) + if(draw[i].player == 2){ + dealersDraw = draw + i += 1 + return dealersDraw + } + } +} \ No newline at end of file From ef2efa8fa1824a58e7692ce0566632d1ad8b8588 Mon Sep 17 00:00:00 2001 From: rusydiFly Date: Sat, 9 Dec 2023 17:05:52 +0800 Subject: [PATCH 3/9] get player result --- script.js | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/script.js b/script.js index f7c4ae5f..9b096c17 100644 --- a/script.js +++ b/script.js @@ -167,14 +167,41 @@ var playersDraw = function (input) { }; var getDealerDraw = function(){ - var draw = PLAYER_DRAW, dealersDraw, i = 0; - console.log(draw) - while(i <= draw.length){ - console.log(draw[i]) + var draw = PLAYER_DRAW, dealersDraw = [], i = 0; + for(i=1;i < NUM_OF_PLAYER *2; i++){ if(draw[i].player == 2){ - dealersDraw = draw - i += 1 - return dealersDraw + dealersDraw.push(draw[i]) } } + return dealersDraw; +} +// need to also return player id here. +var getPlayerDraw = function(){ + var draw = PLAYER_DRAW, playersDraw = [], i = 0; + for(i=0;i < NUM_OF_PLAYER *2; i++){ + if(draw[i].player != 2){ + playersDraw.push(draw[i]) + } + } + return playersDraw; +} + +var getResult = function(){ + // player draw cards + playersDraw() + //get dealers draw card + var dealerDraw = getDealerDraw() + //get players draw card + var playerDraw = getPlayerDraw() + //get dealer and player value + var dealerValue = [dealerDraw[0].drawn.value, dealerDraw[1].drawn.value] + var dealerSuit = [dealerDraw[0].drawn.card, dealerDraw[1].drawn.card] + // + var playerValue = [playerDraw[0].drawn.value, playerDraw[1].drawn.value] + var playerSuit = [playerDraw[0].drawn.card, playerDraw[1].drawn.card] + // + console.log(dealerValue) + console.log(dealerSuit) + console.log(playerValue) + console.log(playerSuit) } \ No newline at end of file From 5b02b136c48ef933cc58b55d796a85805ba018fa Mon Sep 17 00:00:00 2001 From: rusydiFly Date: Tue, 12 Dec 2023 12:15:46 +0800 Subject: [PATCH 4/9] refactoring --- script.js | 94 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 42 deletions(-) diff --git a/script.js b/script.js index 9b096c17..81b96d29 100644 --- a/script.js +++ b/script.js @@ -27,6 +27,8 @@ The game either ends or continues. var CARDS_TAKEN = 0; var NUM_OF_PLAYER = 2; var PLAYER_DRAW +var CARDS_DRAW_DROM_DECK = [] +var DECK // deck algorithm var makeDeck = function () { @@ -65,6 +67,7 @@ var makeDeck = function () { name: cardName, suit: currentSuit, rank: rankCounter, + player: "" }; // Add the new card to the deck @@ -94,7 +97,11 @@ var shuffledDeck = function(){ //draw card. draw from the top array CARDS_TAKEN + 1 // i have to find a way to get alternating cards var drawCard = function(){ CARDS_TAKEN += 1 - return shuffledDeck()[CARDS_TAKEN]; + shuffle = shuffledDeck() + draw = shuffle.splice(0, 1) + DECK = shuffle + CARDS_DRAW_DROM_DECK.push(draw) + return draw; } //player block @@ -124,23 +131,23 @@ var stand = function(){ // i need a way to process a card drawn let function "play" go first // -var playerCard = function(){ - var val_cards = drawCard(), value - console.log(val_cards) - //by default - value = val_cards.rank; - //unless - // if card drawn == ace - if (val_cards.rank == 1){ - value = 11; - } - // if card drawn == face - if(val_cards.rank > 10){ - value = 10; - } - //return value - return {'value': value, 'card': val_cards.suit}; -} +// var playerCard = function(){ +// var val_cards = drawCard(), value +// console.log(val_cards) +// //by default +// value = val_cards.rank; +// //unless +// // if card drawn == ace +// if (val_cards.rank == 1){ +// value = 11; +// } +// // if card drawn == face +// if(val_cards.rank > 10){ +// value = 10; +// } +// //return value +// return {'value': value, 'card': val_cards.suit}; +// } //dealer block /* @@ -148,60 +155,63 @@ The dealer has to hit if their hand is below 17. */ var playersDraw = function (input) { - var playerCounter = 0, playerResult = [], initalCardsDrawn = NUM_OF_PLAYER * 2, playerInGame = 0, player = {}; + var playerCounter = 0, playerResult = [], initalCardsDrawn = NUM_OF_PLAYER * 2, playerInGame = 0, player = {}, draw2Cards; while(playerCounter != initalCardsDrawn){ // player draws 2 cards here // let it be that the last in the array is the dealers draw - playerCounter += 1 playerInGame += 1 // get the distribution order or player 1 player 2 player 1 player 2 if(playerInGame > NUM_OF_PLAYER){ playerInGame = 1 } - console.log(playerInGame) - player = {player : playerInGame, drawn: playerCard()} - playerResult.push(player) + //console.log(playerInGame) + draw2Cards = drawCard() + draw2Cards[0].player = playerInGame + console.table(draw2Cards) + //player = {player : playerInGame, drawn: drawCard()} + playerResult.push(draw2Cards) + playerCounter += 1 } PLAYER_DRAW = playerResult; return playerResult; }; var getDealerDraw = function(){ - var draw = PLAYER_DRAW, dealersDraw = [], i = 0; + var draw = PLAYER_DRAW, dealerDraw = [], i = 0; for(i=1;i < NUM_OF_PLAYER *2; i++){ - if(draw[i].player == 2){ - dealersDraw.push(draw[i]) + if(draw[i].player == NUM_OF_PLAYER){ + draw[i].player = 'dealer' + dealerDraw.push(draw[i]); } } - return dealersDraw; + return dealerDraw; } // need to also return player id here. var getPlayerDraw = function(){ - var draw = PLAYER_DRAW, playersDraw = [], i = 0; + var draw = PLAYER_DRAW, playerDraw = [], i = 0; for(i=0;i < NUM_OF_PLAYER *2; i++){ - if(draw[i].player != 2){ - playersDraw.push(draw[i]) + if(draw[i].player != 'dealer'){ + //playerDraw.push(draw[i]) + playerDraw.push(draw[i]); } } - return playersDraw; + return playerDraw; } var getResult = function(){ + // local var + var playerValue = 0, active_players = [] // player draw cards playersDraw() //get dealers draw card var dealerDraw = getDealerDraw() //get players draw card var playerDraw = getPlayerDraw() - //get dealer and player value - var dealerValue = [dealerDraw[0].drawn.value, dealerDraw[1].drawn.value] - var dealerSuit = [dealerDraw[0].drawn.card, dealerDraw[1].drawn.card] - // - var playerValue = [playerDraw[0].drawn.value, playerDraw[1].drawn.value] - var playerSuit = [playerDraw[0].drawn.card, playerDraw[1].drawn.card] - // - console.log(dealerValue) - console.log(dealerSuit) - console.log(playerValue) - console.log(playerSuit) + + console.log('Dealers result: ') + console.table(dealerDraw) + console.log('Player result:') + console.table(playerDraw) + +return 'end of getResult' } \ No newline at end of file From 0034817aec356555407ad0e65e86a3f37bb4b451 Mon Sep 17 00:00:00 2001 From: rusydiFly Date: Wed, 13 Dec 2023 01:00:09 +0800 Subject: [PATCH 5/9] #get the total of both dealer and player --- script.js | 162 +++++++++++++++++++++++++++--------------------------- 1 file changed, 80 insertions(+), 82 deletions(-) diff --git a/script.js b/script.js index 81b96d29..7ca4050f 100644 --- a/script.js +++ b/script.js @@ -25,10 +25,11 @@ The game either ends or continues. */ var CARDS_TAKEN = 0; -var NUM_OF_PLAYER = 2; +var NUM_OF_PLAYER = 3; var PLAYER_DRAW var CARDS_DRAW_DROM_DECK = [] -var DECK +var DEALER_DRAW = [] +var PLAYER_DRAW = [] // deck algorithm var makeDeck = function () { @@ -63,15 +64,11 @@ var makeDeck = function () { } // Create a new card with the current name, suit, and rank - var card = { - name: cardName, - suit: currentSuit, - rank: rankCounter, - player: "" + var cards = {player : "", card: {name: cardName,suit: currentSuit,rank: rankCounter} }; // Add the new card to the deck - cardDeck.push(card); + cardDeck.push(cards); // Increment rankCounter to iterate over the next rank rankCounter += 1; @@ -129,89 +126,90 @@ var stand = function(){ return NaN } -// i need a way to process a card drawn let function "play" go first -// -// var playerCard = function(){ -// var val_cards = drawCard(), value -// console.log(val_cards) -// //by default -// value = val_cards.rank; -// //unless -// // if card drawn == ace -// if (val_cards.rank == 1){ -// value = 11; -// } -// // if card drawn == face -// if(val_cards.rank > 10){ -// value = 10; -// } -// //return value -// return {'value': value, 'card': val_cards.suit}; -// } - -//dealer block -/* -The dealer has to hit if their hand is below 17. -*/ -var playersDraw = function (input) { - var playerCounter = 0, playerResult = [], initalCardsDrawn = NUM_OF_PLAYER * 2, playerInGame = 0, player = {}, draw2Cards; - while(playerCounter != initalCardsDrawn){ - // player draws 2 cards here - // let it be that the last in the array is the dealers draw - playerInGame += 1 - // get the distribution order or player 1 player 2 player 1 player 2 - if(playerInGame > NUM_OF_PLAYER){ - playerInGame = 1 - } - //console.log(playerInGame) - draw2Cards = drawCard() - draw2Cards[0].player = playerInGame - console.table(draw2Cards) - //player = {player : playerInGame, drawn: drawCard()} - playerResult.push(draw2Cards) - playerCounter += 1 +function evaluateCards(name){ + // var suits = ['jack', 'queen', 'king']; + if(name == 'jack' || name == 'queen' || name == 'king'){ + return 10; } - PLAYER_DRAW = playerResult; - return playerResult; -}; + else if(name == 'ace'){ + return 11; + } + else{ + return name; + } +} -var getDealerDraw = function(){ - var draw = PLAYER_DRAW, dealerDraw = [], i = 0; - for(i=1;i < NUM_OF_PLAYER *2; i++){ - if(draw[i].player == NUM_OF_PLAYER){ - draw[i].player = 'dealer' - dealerDraw.push(draw[i]); - } +function alternate(num01, num02){ + if(num01 == num02 ){ + num01 = 0 + return num01 + } + else { + return num01 } - return dealerDraw; } -// need to also return player id here. -var getPlayerDraw = function(){ - var draw = PLAYER_DRAW, playerDraw = [], i = 0; - for(i=0;i < NUM_OF_PLAYER *2; i++){ - if(draw[i].player != 'dealer'){ - //playerDraw.push(draw[i]) - playerDraw.push(draw[i]); + +var playersDraw = function (input) { + //local var + var draw01 = [], player = 0, dealerIndex = [], playerIndex = [], playerArray = [], playerTotal = 0; + //player draw + for(i = 0; i < NUM_OF_PLAYER * 2; i++){ + for(j = 0; j < 1; j++){ + draw01.push(drawCard()) + console.log(i) + player += 1 + if(player == NUM_OF_PLAYER + 1){ + player = 1 + } + draw01[i][0].player = player + // + if(draw01[i][0].player == NUM_OF_PLAYER){ + draw01[i][0].player = 'dealer' + } + // PLAYER_DRAW.push({total : 0, first: draw01[player - 1], second: draw01[(player * 2) - 1]}) + // console.log(draw01) + // console.log(draw01.length) + } + } + for(i = 0; i < draw01.length; i++){ + if(draw01[i][0].player == 'dealer' ){ + console.log("Dealer Index: " + draw01.indexOf(draw01[i])) + dealerIndex.push(draw01.indexOf(draw01[i])) + } + else{ + console.log("Player Index: " + draw01.indexOf(draw01[i])) + playerIndex.push(draw01.indexOf(draw01[i])) } } - return playerDraw; + // console.log(dealerIndex) + // console.log(playerIndex) + // console.log(draw01) + var total = evaluateCards(draw01[dealerIndex[0]][0].card.name) + evaluateCards(draw01[dealerIndex[1]][0].card.name) + DEALER_DRAW.push({total : total, first: draw01[dealerIndex[0]], second: draw01[dealerIndex[1]]}) + players = NUM_OF_PLAYER -1 + for(i = 0; i < playerIndex.length / 2; i++){ + playerTotal = evaluateCards(draw01[playerIndex[i]][0].card.name) + evaluateCards(draw01[playerIndex[i + players]][0].card.name) + console.log(i) + console.log(playerTotal) + //i have to put this in a player : "", index: [] + PLAYER_DRAW.push({total : playerTotal, first: draw01[playerIndex[i]], second: draw01[playerIndex[i + players]]}) + } } + var getResult = function(){ - // local var - var playerValue = 0, active_players = [] - // player draw cards + //local var + var playerResult; + // initiate draw playersDraw() - //get dealers draw card - var dealerDraw = getDealerDraw() - //get players draw card - var playerDraw = getPlayerDraw() - - console.log('Dealers result: ') - console.table(dealerDraw) - console.log('Player result:') - console.table(playerDraw) - -return 'end of getResult' + // + for(i = 0; i < PLAYER_DRAW.length; i++){ + // console.log(PLAYER_DRAW[i]) + // traverse to player total + playerResult = PLAYER_DRAW[i].total + console.log(playerResult) + // traverse to dealer total + // dealer will always be at [0] since its the same "player" + } } \ No newline at end of file From bd669e27652230768c59bab59d040b79ded9006e Mon Sep 17 00:00:00 2001 From: rusydiFly Date: Wed, 13 Dec 2023 22:33:11 +0800 Subject: [PATCH 6/9] can tally result and get the winner --- script.js | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/script.js b/script.js index 7ca4050f..20e3c538 100644 --- a/script.js +++ b/script.js @@ -31,6 +31,15 @@ var CARDS_DRAW_DROM_DECK = [] var DEALER_DRAW = [] var PLAYER_DRAW = [] +function reset (){ + //CARDS_TAKEN = 0; + //NUM_OF_PLAYER = 3; + //PLAYER_DRAW + //CARDS_DRAW_DROM_DECK = [] + DEALER_DRAW = [] + PLAYER_DRAW = [] +} + // deck algorithm var makeDeck = function () { // Initialise an empty deck array @@ -157,7 +166,6 @@ var playersDraw = function (input) { for(i = 0; i < NUM_OF_PLAYER * 2; i++){ for(j = 0; j < 1; j++){ draw01.push(drawCard()) - console.log(i) player += 1 if(player == NUM_OF_PLAYER + 1){ player = 1 @@ -174,11 +182,11 @@ var playersDraw = function (input) { } for(i = 0; i < draw01.length; i++){ if(draw01[i][0].player == 'dealer' ){ - console.log("Dealer Index: " + draw01.indexOf(draw01[i])) + // console.log("Dealer Index: " + draw01.indexOf(draw01[i])) dealerIndex.push(draw01.indexOf(draw01[i])) } else{ - console.log("Player Index: " + draw01.indexOf(draw01[i])) + // console.log("Player Index: " + draw01.indexOf(draw01[i])) playerIndex.push(draw01.indexOf(draw01[i])) } } @@ -190,8 +198,8 @@ var playersDraw = function (input) { players = NUM_OF_PLAYER -1 for(i = 0; i < playerIndex.length / 2; i++){ playerTotal = evaluateCards(draw01[playerIndex[i]][0].card.name) + evaluateCards(draw01[playerIndex[i + players]][0].card.name) - console.log(i) - console.log(playerTotal) + // console.log(i) + // console.log(playerTotal) //i have to put this in a player : "", index: [] PLAYER_DRAW.push({total : playerTotal, first: draw01[playerIndex[i]], second: draw01[playerIndex[i + players]]}) } @@ -200,7 +208,7 @@ var playersDraw = function (input) { var getResult = function(){ //local var - var playerResult; + var playerResult, output = []; // initiate draw playersDraw() // @@ -208,8 +216,32 @@ var getResult = function(){ // console.log(PLAYER_DRAW[i]) // traverse to player total playerResult = PLAYER_DRAW[i].total - console.log(playerResult) + console.log(playerResult) + dealerResult = DEALER_DRAW[0].total + console.log(dealerResult) + if (playerResult == 21){ + player = PLAYER_DRAW[0].first[0].player + output.push(`player ${player} wins`) + } + if(dealerResult == 21){ + // dealer = DEALER_DRAW[i].first[0].player + player = PLAYER_DRAW[0].first[0].player + output.push(`Dealer wins over Player ${player}`) + } + // + if (playerResult > dealerResult && playerResult < 21){ + player = PLAYER_DRAW[0].first[0].player + output.push(`player ${player} wins`) + } + if (dealerResult > playerResult && dealerResult < 21){ + // dealer = DEALER_DRAW[i].first[0].player + player = PLAYER_DRAW[0].first[0].player + output.push(`Dealer wins over Player ${player}`) + } // traverse to dealer total // dealer will always be at [0] since its the same "player" } + //reset hand + reset() + return output; } \ No newline at end of file From 8b2472a1d3ac15cf579022e2299d6dba340f1dfa Mon Sep 17 00:00:00 2001 From: rusydiFly Date: Sat, 16 Dec 2023 23:59:27 +0800 Subject: [PATCH 7/9] algorithm is complete-ish, need to add better output for user to see their drarn cards --- index.html | 32 +++++++++++- script.js | 150 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 145 insertions(+), 37 deletions(-) diff --git a/index.html b/index.html index bbc7dffd..480a0a00 100644 --- a/index.html +++ b/index.html @@ -130,6 +130,8 @@

♣️ Basics - Blackjack ♠️


+ +

Output:

@@ -141,7 +143,35 @@

♣️ Basics - Blackjack ♠️

button.addEventListener("click", function () { // Set result to input value var input = document.querySelector("#input-field"); - var result = main(input.value); + var result = getResult(input.value); + + // Display result in output element + var output = document.querySelector("#output-div"); + output.innerHTML = result; + + // Reset input value + input.value = ""; + }); + // + // + var button = document.querySelector("#hit-button"); + button.addEventListener("click", function () { + // Set result to input value + var input = document.querySelector("#input-field"); + var result = hit(input.value); + + // Display result in output element + var output = document.querySelector("#output-div"); + output.innerHTML = result; + + // Reset input value + input.value = ""; + }); + var button = document.querySelector("#stand-button"); + button.addEventListener("click", function () { + // Set result to input value + var input = document.querySelector("#input-field"); + var result = stand(input.value); // Display result in output element var output = document.querySelector("#output-div"); diff --git a/script.js b/script.js index 20e3c538..7187fdb0 100644 --- a/script.js +++ b/script.js @@ -30,6 +30,22 @@ var PLAYER_DRAW var CARDS_DRAW_DROM_DECK = [] var DEALER_DRAW = [] var PLAYER_DRAW = [] +var GAME_STATE = 1 +var HIT_CARD = [] + +/* game state + 1. player_draw = 1 + 2. player decide to hit of stand = 2 + 2. player_hit = 3 + 3. player_stand = 4 + 4. calculate = 5 +*/ +var PLAYER_TURN = 0 +/* +player turn will determine]p which player's turn to hit or stand +*/ + + function reset (){ //CARDS_TAKEN = 0; @@ -126,13 +142,51 @@ var drawCard = function(){ // return [card_A, card_B] // if player hit var hit = function(){ - let hit_card = drawCard() - console.log(hit_card) - return [hit_card] + /* + if player hit then draw a card + after drawing a card, that card gets assigned to the player card data(their object) + after that, print the new card in hand, + */ + var hitTotal = 0 + draw = drawCard() + + // assign card player using PLAYER_TURN + draw[0].player = PLAYER_TURN + HIT_CARD.push(draw) + PLAYER_DRAW[PLAYER_TURN - 1].hit = HIT_CARD + // console.log(draw) + console.log('Hit Length') + console.log(PLAYER_DRAW[PLAYER_TURN - 1].hit.length) + hitCardLength = HIT_CARD.length + // count total + for(i = 0; i < hitCardLength; i++){ + if(HIT_CARD[i][0].player == PLAYER_TURN){ + console.log('Hello') + hitTotal += evaluateCards(HIT_CARD[i][0].card.name) + console.log('Hit Total') + console.log(hitTotal) + } + } + PLAYER_DRAW[PLAYER_TURN - 1].total += hitTotal + // return PLAYER_DRAW[PLAYER_TURN - 1] HIT_CARD[0][0].card HIT_CARD[1][0].card + // return PLAYER_DRAW[PLAYER_TURN - 1].total + // add more card info here + return `Player ${PLAYER_TURN}, hit or stand?` } + var stand = function(){ - return NaN + // when stand is hit, change player, change PLAYER_TURN += 1 + if(PLAYER_TURN == 'DEALER'){ + //PLAYER_TURN = "DEALER" + GAME_STATE = 5 + return getResult() + } + else{ + PLAYER_TURN +=1 + } + + return getResult() } @@ -210,38 +264,62 @@ var getResult = function(){ //local var var playerResult, output = []; // initiate draw - playersDraw() - // - for(i = 0; i < PLAYER_DRAW.length; i++){ - // console.log(PLAYER_DRAW[i]) - // traverse to player total - playerResult = PLAYER_DRAW[i].total - console.log(playerResult) - dealerResult = DEALER_DRAW[0].total - console.log(dealerResult) - if (playerResult == 21){ - player = PLAYER_DRAW[0].first[0].player - output.push(`player ${player} wins`) - } - if(dealerResult == 21){ - // dealer = DEALER_DRAW[i].first[0].player - player = PLAYER_DRAW[0].first[0].player - output.push(`Dealer wins over Player ${player}`) - } - // - if (playerResult > dealerResult && playerResult < 21){ - player = PLAYER_DRAW[0].first[0].player - output.push(`player ${player} wins`) - } - if (dealerResult > playerResult && dealerResult < 21){ - // dealer = DEALER_DRAW[i].first[0].player - player = PLAYER_DRAW[0].first[0].player - output.push(`Dealer wins over Player ${player}`) + if(GAME_STATE == 1){ + playersDraw() + GAME_STATE = 2 + PLAYER_TURN = 1 + + } + if(GAME_STATE == 2){ + if(PLAYER_TURN == NUM_OF_PLAYER){ + PLAYER_TURN = "DEALER" + return `${PLAYER_TURN}, hit or stand?` + } + return `Player ${PLAYER_TURN}, hit or stand?` + } + if(GAME_STATE == 5){ + GAME_STATE = 1 + for(i = 0; i < PLAYER_DRAW.length; i++){ + // console.log(PLAYER_DRAW[i]) + // traverse to player total + playerResult = PLAYER_DRAW[i].total + console.log(playerResult) + dealerResult = DEALER_DRAW[0].total + console.log(dealerResult) + if (playerResult == 21){ + player = PLAYER_DRAW[i].first[0].player + output.push(`Blackjack!, Player ${player} wins`) + } + if (playerResult > 21){ + player = PLAYER_DRAW[i].first[0].player + output.push(`Player ${player} busted`) + } + if(dealerResult == 21){ + // dealer = DEALER_DRAW[i].first[0].player + player = PLAYER_DRAW[i].first[0].player + output.push(`Blackjack!, Dealer wins over Player ${player}`) + } + if(dealerResult > 21){ + // dealer = DEALER_DRAW[i].first[0].player + player = PLAYER_DRAW[i].first[0].player + output.push(`Dealer lose, Player ${player} gets the win`) + } + // + if (playerResult > dealerResult && playerResult < 21){ + player = PLAYER_DRAW[i].first[0].player + output.push(`Player ${player} wins over Dealer`) + } + if (dealerResult > playerResult && dealerResult < 21){ + // dealer = DEALER_DRAW[i].first[0].player + player = PLAYER_DRAW[i].first[0].player + output.push(`Dealer wins over Player ${player}`) + } + // traverse to dealer total + // dealer will always be at [0] since its the same "player" } - // traverse to dealer total - // dealer will always be at [0] since its the same "player" + //reset hand + reset() + joinOutput = output.join('
') + return joinOutput; } - //reset hand - reset() - return output; } \ No newline at end of file From 0c0216a1c00bdddb86395891dc44b588a6f66718 Mon Sep 17 00:00:00 2001 From: rusydiFly Date: Sun, 17 Dec 2023 20:12:23 +0800 Subject: [PATCH 8/9] added function, if player has 2 ace, automatically revealuate the hand with the second ace being a valueof 1 instead of 11 --- script.js | 48 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/script.js b/script.js index 7187fdb0..3c79cd85 100644 --- a/script.js +++ b/script.js @@ -26,7 +26,6 @@ The game either ends or continues. var CARDS_TAKEN = 0; var NUM_OF_PLAYER = 3; -var PLAYER_DRAW var CARDS_DRAW_DROM_DECK = [] var DEALER_DRAW = [] var PLAYER_DRAW = [] @@ -42,7 +41,7 @@ var HIT_CARD = [] */ var PLAYER_TURN = 0 /* -player turn will determine]p which player's turn to hit or stand +player turn will determinep which player's turn to hit or stand */ @@ -147,7 +146,8 @@ var hit = function(){ after drawing a card, that card gets assigned to the player card data(their object) after that, print the new card in hand, */ - var hitTotal = 0 + + var hitTotal = 0, hitCard = [], hitSuit = [] draw = drawCard() // assign card player using PLAYER_TURN @@ -161,8 +161,15 @@ var hit = function(){ // count total for(i = 0; i < hitCardLength; i++){ if(HIT_CARD[i][0].player == PLAYER_TURN){ - console.log('Hello') - hitTotal += evaluateCards(HIT_CARD[i][0].card.name) + if(PLAYER_DRAW[PLAYER_TURN - 1].first[0].card.name == 'ace' || PLAYER_DRAW[PLAYER_TURN - 1].second[0].card.name == 'ace' && HIT_CARD[i][0].card.name == 'ace'){ + //PLAYER_DRAW[PLAYER_TURN - 1].total -= 10 + hitTotal += 1 + } + else{ + hitTotal += evaluateCards(HIT_CARD[i][0].card.name) + } + hitCard.push(HIT_CARD[i][0].card.name) + hitSuit.push(HIT_CARD[i][0].card.suit) console.log('Hit Total') console.log(hitTotal) } @@ -170,12 +177,24 @@ var hit = function(){ PLAYER_DRAW[PLAYER_TURN - 1].total += hitTotal // return PLAYER_DRAW[PLAYER_TURN - 1] HIT_CARD[0][0].card HIT_CARD[1][0].card // return PLAYER_DRAW[PLAYER_TURN - 1].total - // add more card info here - return `Player ${PLAYER_TURN}, hit or stand?` + // TODO add more card info here + // player first + firstCard = PLAYER_DRAW[PLAYER_TURN - 1].first[0].card.name + firstSuit = PLAYER_DRAW[PLAYER_TURN - 1].first[0].card.suit + // player second + secondCard = PLAYER_DRAW[PLAYER_TURN - 1].second[0].card.name + secondSuit = PLAYER_DRAW[PLAYER_TURN - 1].second[0].card.suit + // hits join() + hitCardJoin = hitCard.join(' & ') + hitSuitJoin = hitSuit.join(' & ') + output = `
Player ${PLAYER_TURN}

FIRST CARD:
Card: ${firstCard}
Suit: ${firstSuit}
SECOND CARD:
Card: ${secondCard}
Suit: ${secondSuit}
HIT CARD(s):
Card: ${hitCardJoin}
Suit: ${hitSuitJoin}
Would you like to hit or stand?` + return output } var stand = function(){ + // there need to be a part where if any of the players (including the dealer), total less than 15, they need to draw another card + // The dealer has to hit if their hand is below 17. // when stand is hit, change player, change PLAYER_TURN += 1 if(PLAYER_TURN == 'DEALER'){ //PLAYER_TURN = "DEALER" @@ -189,6 +208,10 @@ var stand = function(){ return getResult() } +function thirdCardAce(){ + // this function is to re-evaluate hand, if card +} + function evaluateCards(name){ // var suits = ['jack', 'queen', 'king']; @@ -273,9 +296,13 @@ var getResult = function(){ if(GAME_STATE == 2){ if(PLAYER_TURN == NUM_OF_PLAYER){ PLAYER_TURN = "DEALER" - return `${PLAYER_TURN}, hit or stand?` + // get cards here + output = `
${PLAYER_TURN}

hit or stand?` + return output } - return `Player ${PLAYER_TURN}, hit or stand?` + // get cards here + output = `
Player ${PLAYER_TURN}

hit or stand?` + return output } if(GAME_STATE == 5){ GAME_STATE = 1 @@ -314,11 +341,14 @@ var getResult = function(){ player = PLAYER_DRAW[i].first[0].player output.push(`Dealer wins over Player ${player}`) } + // TODO add a condition if dealer and player equals // traverse to dealer total // dealer will always be at [0] since its the same "player" + } //reset hand reset() + // get cards here joinOutput = output.join('
') return joinOutput; } From 7fe91af193b2260546fc816eb0b9188a471c0120 Mon Sep 17 00:00:00 2001 From: rusydiFly Date: Mon, 18 Dec 2023 00:32:34 +0800 Subject: [PATCH 9/9] completes black jack with rest, haven not done thorough testing, might be lacking algorithm. eg, if dealer and player ties, who has the winner suit? --- index.html | 4 +- script.js | 262 ++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 192 insertions(+), 74 deletions(-) diff --git a/index.html b/index.html index 480a0a00..d05eaf55 100644 --- a/index.html +++ b/index.html @@ -129,7 +129,7 @@

♣️ Basics - Blackjack ♠️

Input:


- +

Output:

@@ -139,7 +139,7 @@

♣️ Basics - Blackjack ♠️