Note: This document is a work in progress. You can help improve it.
All Cards on Deck!
Math.random() function as a pseudorandom number generator.
- Understand and implement algorithms
- Create an event-driven user interface
- Your deck should contain 52 unique cards.
- All cards should have a "rank" and a "suit".
- There are four suits: "Clubs", "Diamonds", "Hearts", and "Spades".
- There are 13 ranks: "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", and "King".
You will model these in code, in any way you see fit. It may require you to experiment and try a number of techniques. There are many valid solutions. Your user interface should consist of a face down deck, and a face up "hand" of cards that have been dealt.
Read about, and implement the Fisher–Yates shuffle algorithm:
For our purposes,
for i from n - 1 down to 1 do: j = random integer (where 0 <= j <= i) swap items[i] with items[j]
- The deck should be randomly shuffled when the page is loaded.
- Clicking on the deck should deal a single card, making it visible in the face up hand.
- Implement a way to deal cards into two or more hands
- Implement the game of War
A Hint on Random Numbers
This snippet will give you a random integer,
const z = Math.floor(Math.random() * n)
Let's break this down from the inside out:
- For this example, assume
- We use
Math.random()to generate a floating-point number between
1. Let's assume our random value is
- Multiply that number by
n. If we think of this random value as a percentage, multiplying these gives us a number that is some "percentage" of
n. Their product is
8.4, or 42% of
- We use
Math.floor()to round down to the nearest whole number, i.e.
Because we're rounding down, it's impossible to get
20. This will give us an integer between
19. This technique is perfect for finding a "random" index in an array of length