From d6865a009a6578eeb8444c02b6240f77209f426e Mon Sep 17 00:00:00 2001 From: RoyKuah Date: Mon, 19 Feb 2024 19:30:28 +0800 Subject: [PATCH] First push and commit --- .github/PULL_REQUEST_TEMPLATE.md | 8 ++ index.html | 155 ++++++++++--------------------- script.js | 127 ++++++++++++++++++++++++- style.css | 5 + 4 files changed, 186 insertions(+), 109 deletions(-) create mode 100644 style.css diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index cf6005ad..d69d7867 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -3,17 +3,25 @@ Please fill out the survey before submitting the pull request. Thanks! πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€ How many hours did you spend on this assignment? +2 days Please fill in one error and/or error message you received while working on this assignment. +Undefined on some lines such as the latest if statement on Ace + 10, ace + face cards in the first two round What part of the assignment did you spend the most time on? +if statements, and trying to fix the file sources for the images. Need to google and research a lot to make it more presentable such as background image Comfort Level (1-5): +2 Completeness Level (1-5): +2 What did you think of this deliverable? +Not really great, need more practice... Is there anything in this code that you feel pleased about? +Just happy that the base game works What's one aspect of your code you would like specific, elaborate feedback on? +N/A diff --git a/index.html b/index.html index bbc7dffd..04f4baa3 100644 --- a/index.html +++ b/index.html @@ -1,51 +1,10 @@ - - - - - - - - - - - - - - - - - - - - - Blackjack - Basics - Rocket Academy - - - - - - - - - - - - - - - - - - - - - - - - - + BlackJack + -

- - Rocket Academy - -

-

♣️ Basics - Blackjack ♠️

-
-

Input:

- -
- -

Output:

-
-
- +

β™₯️ ♦️Blackjack♠️ ♣️

+ + +
+
- - - - + + \ No newline at end of file diff --git a/script.js b/script.js index bbe8a293..d2c00286 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,125 @@ -var main = function (input) { - var myOutputValue = 'hello world'; - return myOutputValue; +var player1Cards = document.getElementById("player1Cards"); +var player2Cards = document.getElementById("player2Cards"); +var dealButton = document.getElementById("deal-button"); +var nums = [ + "ace", + "two", + "three", + "four", + "five", + "six", + "seven", + "eight", + "nine", + "ten", + "jack", + "queen", + "king", +]; +var suitsholder = ["Diamonds", "Hearts", "Clubs", "Spades"]; +var imgholder = []; +for (let i = 0; i < 13; i += 1) { + imgholder.push([]); + for (let j = 0; j < 4; j += 1) { + var imagePath = `images/${nums[i]}Of${suitsholder[j]}.jpg`; + console.log("Image Path:", imagePath); + imgholder[i].push(imagePath); + } +} + +// creating a deck of cards +const makeDeck = () => { + var newDeck = []; + for (let i = 1; i <= 13; i += 1) { + var suits = ["♦", "β™₯", "♣", "β™ "]; + for (let j = 0; j < suits.length; j += 1) { + var name = `${i}`; + if (name === "1") { + name = "A"; + } else if (name === "11") { + name = "J"; + } else if (name === "12") { + name = "Q"; + } else if (name === "13") { + name = "K"; + } + var card = { + value: i, + suit: suits[j], + img: imgholder[i - 1][j], + name, + }; + newDeck.push(card); + } + } + return newDeck; }; +// Get a random index ranging from 0 (inclusive) to max (exclusive). +const getRandomIndex = (max) => Math.floor(Math.random() * max); +// Shuffle an array of cards +const shuffleCards = (deck) => { + // Loop over the card deck array once + for (let currentIndex = 0; currentIndex < deck.length; currentIndex += 1) { + // Select a random index in the deck + const randomIndex = getRandomIndex(deck.length); + // Select the card that corresponds to randomIndex + const randomCard = deck[randomIndex]; + // Select the card that corresponds to currentIndex + const currentCard = deck[currentIndex]; + // Swap positions of randomCard and currentCard in the deck + deck[currentIndex] = randomCard; + deck[randomIndex] = currentCard; + } + // Return the shuffled deck + return deck; +}; +var deck = makeDeck(); +var shuffledDeck = shuffleCards(deck); +function dealCard(card, player) { + var cardImage = document.createElement("img"); + cardImage.src = card.img; + player.appendChild(cardImage); +} + +var counter = 0; +var turn = "player1"; +var threshold = 21; +var player1Hand = []; +var player2Hand = []; +function getHandValue(hand) { + var sum = 0; + for (var i = 0; i < hand.length; i++) { + sum += hand[i].value; + } + return sum; +} +dealButton.addEventListener("click", () => { + var card = shuffledDeck.pop(); + if (counter == 0) { + if (getHandValue(player1Hand) < 21) { + dealCard(card, player1Cards); + player1Hand.push(card); + } + counter = 1; + } else { + if (getHandValue(player2Hand) < 21) { + dealCard(card, player2Cards); + player2Hand.push(card); + } + counter = 0; + } + if (counter == 0 && player1Hand.length == 2) { + var hasAce = false; + var hasSpecialCard = false; + for (var i = 0; i < player1Hand.length; i++) { + if (player1Hand[i].value == 1) { + hasAce = true; + } else if (player1Hand[i].value >= 10 && player1Hand[i].value <= 12) { + hasSpecialCard = true; + } + } + if (hasAce && hasSpecialCard) { + alert("Player 1 wins with an Ace and a special card!"); + } + } +}); diff --git a/style.css b/style.css new file mode 100644 index 00000000..00edcd88 --- /dev/null +++ b/style.css @@ -0,0 +1,5 @@ +/* style.css */ +img { + max-width: 20%; + height: auto; +}