Giter Club home page Giter Club logo

coursa4's Introduction

blackJack by GhostDolphin (Данило Чогадзе IM-13)

Simple BlackJack simulator to test my programming skills and hopefully defend the coursework at university.

[!] NOT AN ACTUAL GAMBLING GAME - ALL THE FEATURES OF MONEY ARE UNREAL AND PRESENTED JUST FOR THE PROGRAMMING'S SAKE!

[!] The project by default considers that the simulated dealer stops picking cards at the own score of 17 or more.

[!] Player by default starts with the simulated amount of $1000 and all the further bids are restricted to $100.

[!] Some common BlackJack features are not presented at the moment due to the lack of time:

  • 'Split' action is not featured;
  • 'Double' action is not featured as well (and in my opinion it's completely useless if you don't play for a real result);
  • Defence from dealer's blackjack (score of 21 with only 2 cards) is absent;
  • ... and all the other things I simply missed while researching on how the game works

Background source: https://www.pngmagic.com/w87b/website-background-image-size-psd-vector-photo.htm

coursa4's People

Contributors

ghostdolphin avatar

Watchers

 avatar

coursa4's Issues

Review #1

  • Trick: Shuffle - it's a process to sort in random order :)

    coursa4/js/core.js

    Lines 48 to 58 in 05a7bce

    const shuffle = (cards) => {
    const deck = cards,
    len = cards.length;
    for (let i = 0; i < len; i++) {
    const num = getRandom(i, len - 1);
    const temp = deck[i];
    deck[i] = deck[num];
    deck[num] = temp;
    }
    return deck;
    };
const shuffle = (list) => list.sort(() => Math.random() > 0.5 ? 1 : -1; 
// Note: that the array is sorted in place, and no copy is made.
  • It's better to hardcode values intead of generating it

    coursa4/js/core.js

    Lines 31 to 46 in 05a7bce

    const defCards = () => {
    const range = ['J', 'Q', 'K', 'A'],
    suits = [
    'heart',
    'diamond',
    'spade',
    'club'
    ],
    cards = [];
    for (let i = 10; i >= 2; i--)
    range.unshift(i);
    for (const suit of suits)
    for (const val of range)
    cards.push({ suit: suit, value: val });
    return cards;
    };
const range = ['J', 'Q', 'K', 'A'];
for (let i = 10; i >= 2; i--)
   	range.unshift(i);

can be replaced with

const range = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A'];
 const range = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A'];
 const suits = ['heart', 'diamond', 'spade', 'club'];
 const cards = suits.flatMap(suit => range.map(value => ({suit, value})));

Note: you can define card cost too.

  • Avoid making aliases. Just use original methods

    coursa4/js/core.js

    Lines 60 to 61 in 05a7bce

    const findId = (id) => document.getElementById(id);
    const findClass = (className) => document.getElementsByClassName(className);

  • Use .filter method

    coursa4/js/core.js

    Lines 114 to 123 in 05a7bce

    const countAces = (deck) => {
    const aces = [];
    for (const ace of deck) {
    if (ace.value === 'A')
    aces.push(ace);
    }
    return aces;
    };

  • Looks like you want to transform array of cards into single value of score. Try to use .reduce method which is ideal for this kind of operation

    coursa4/js/core.js

    Lines 100 to 143 in 05a7bce

    const countScore = (cards) => {
    let score = 0;
    const defVal = (checkCard) => {
    if (typeof checkCard.value !== 'number') {
    if (checkCard.value === 'A')
    return 11;
    else
    return 10;
    } else {
    return checkCard.value;
    }
    };
    const countAces = (deck) => {
    const aces = [];
    for (const ace of deck) {
    if (ace.value === 'A')
    aces.push(ace);
    }
    return aces;
    };
    for (const card of cards) {
    const val = defVal(card);
    score += val;
    }
    const aces = countAces(cards);
    if (aces.length > 0) {
    let n = 0;
    while (score > 21 && n < aces.length) {
    n++;
    score -= 10;
    }
    }
    return score;
    };

  • Use destructuring

    const card = result.leftInDeck.splice(0, 1)[0],

const [card, ...leftInDeckCards] = result.leftInDeck;
  • Why you search for element by id if you already have this element newCard
    findId(newCard.id).style.transform = 'scale(1)';
  • Are you sure you want to call parseCardClass method instead of hardcoding "hidden"?
    className = 'card' + parseCardClass(false, card),
  • Note: You can fill id value at the init cards phase or you can add getter to your card object
    idOfCard = `card_player_${result.cards.length}`;
  • Seems like a dulpicate

    coursa4/js/core.js

    Lines 145 to 197 in 05a7bce

    const hit = (deck, playerCards) => {
    const result = {
    leftInDeck: deck,
    cards: playerCards
    };
    const card = result.leftInDeck.splice(0, 1)[0],
    newCard = document.createElement('table'),
    className = 'card' + parseCardClass(false, card),
    idOfCard = `card_player_${result.cards.length}`;
    card.id = idOfCard;
    result.cards.push(card);
    newCard.id = idOfCard;
    newCard.className += className;
    newCard.innerHTML = CARD_TEMPLATE;
    findClass('playerCards')[0].appendChild(newCard);
    setTimeout(() => {
    findId(newCard.id).style.transform = 'scale(1)';
    }, 100);
    return result;
    };
    const stand = (deck, dealerCards) => {
    const result = {
    leftInDeck: deck,
    cards: dealerCards
    };
    const card = result.leftInDeck.splice(0, 1)[0],
    newCard = document.createElement('table'),
    className = 'card' + parseCardClass(false, card),
    idOfCard = `card_dealer_${result.cards.length}`;
    card.id = idOfCard;
    result.cards.push(card);
    newCard.id = idOfCard;
    newCard.className += className;
    newCard.innerHTML = CARD_TEMPLATE;
    findClass('dealerCards')[0].appendChild(newCard);
    setTimeout(() => {
    findId(newCard.id).style.transform = 'scale(1)';
    }, 100);
    return result;
    };

===========================================================================
To be continued ....

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.