Hey everyone,
After finishing the JavaScript and GitHub portions of the Full Stack Engineer course, I have completed my first personal project aptly named Simple Poker.
This JavaScript program just shuffles and cuts a deck of cards, and then deals 5 cards into two arrays (you and the computer.) Then each hand is evaluated and a winner is awarded based on the strongest combination of cards.
At this time there are no draws/discards and betting. They are on the list for features to be added. For now its just a straight showdown.
Try it out and let me know what you think!
3 Likes
Very nice. How long have you been working on this ?
1 Like
Thanks! It took me a few months.
It’s concept sounded simple, but when I planned it out on paper it got complicated. And then when I worked on it, it got even more complicated. It was a cycle of roadblocks, taking a break, then having an epiphany. Good times.
You should be very proud of this! As you’ve no doubt discovered, it’s not even writing code that is the challenge - it’s breaking a problem down into it’s smallest logical constituents so that you can feed it one step at a time. Amazing work!
Anyway, I’ll get out of the way and let people leave proper feedback.
2 Likes
Nice work! Great job breaking things down into functions.
I have a few suggestions. You have given each card a name
property. I’d suggest using it rather than the shorthand for the final output. (See below.) You might also consider how you could refactor you showPlayerHand
and showComHand
functions into a single function that takes a parameter (computer or player). The last thing would be a little harder to implement. Consider a function that creates the deck rather than hard coding 52 individual cards.
Suggestions:
const showPlayerHand = () => {
for (const card of deck.playerHand) {
playerAttr.cards.push(card.name) //use name instead of shorthand
}
return playerAttr.cards
}
const showComHand = () => {
for (const card of deck.comHand) {
comAttr.cards.push(card.name) //here too
}
return comAttr.cards
}
// and then:
console.log(`Your cards are:\n`, showPlayerHand().join('\n'))
console.log(evaluateHands(playerAttr))
console.log('')
console.log(`The computer's cards are:\n`, showComHand().join('\n'))
Output:
Your cards are:
4 Of Hearts
Jack Of Clubs
5 Of Clubs
3 Of Clubs
2 Of Spades
High Card
The computer’s cards are:
5 Of Hearts
6 Of Diamonds
Queen Of Diamonds
Ace Of Diamonds
Jack Of Hearts
High Card
The computer wins the Ace Of Diamonds
Here’s a sample deck building function (using your current card object properties):
buildDeck() {
const suits = ['Clubs', 'Spades', 'Hearts', 'Diamonds'];
const faces = ['Jack', 'Queen', 'King', 'Ace'];
for(let val = 2; val < 15; val++) {
let face = String(val);
if(val > 10) {
face = faces[val - 11];
}
for(let suit of suits) {
this._cards.push(
{
name: `${face} Of ${suit}`,
shortHand: `${face}${suit[0]}`,
suit: suit,
value: val
}
)
}
}
console.log(this._cards); //See the deck
},
1 Like
Thanks for the feedback! Now that I have a clearer understanding of how JavaScript handles objects, that deck building function would have spared me several lines of code. Plus I could still use it to debug certain hands.
I guess the short hand notation comes across as somewhat obscure when reading it at first glance.
1 Like
great job , I am new here and I would like to try it but I don’t know how or where to try it , can I have instructions how to try it , sorry for my ignorance.
Thanks. Simplest way is to copy and past the JavaScript code and view the console. You can use Cdoecademy, CodePen or even Visual Studio Code