Commit 531876c5 authored by Chingiz's avatar Chingiz 💻

Class Deck and RandomNumber func has been created

parent 781db860
import {Card} from "./Card";
import {getRandomNumber} from "./RandomNumber";
export class Deck {
private RANKS: string[] = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
private SUITS: string[] = ["♠", "♥", "♦", "♣"]
private _cards: Card[] = [];
private tookedCardIndexes: number[] =[];
constructor() {
for (let i = 0; i < this.SUITS.length; i++) {
for (let j = 0; j < this.RANKS.length; j++) {
this._cards.push(new Card(this.RANKS[j], this.SUITS[i]))
}
}
}
shuffleDeck() {
for (let i = this._cards.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this._cards[i], this._cards[j]] = [this._cards[j], this._cards[i]];
}
}
takeACard(){
const ranNum: number = getRandomNumber(0, this._cards.length + 1);
if(!this.tookedCardIndexes.includes(ranNum)){
return this._cards[ranNum];
this.tookedCardIndexes.push(ranNum);
}
this.takeACard();
}
}
\ No newline at end of file
export function getRandomNumber(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment