Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- APP.JS
- 'use strict'
- const suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
- const values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
- const Deck = require('./Deck')
- const Player = require('./Player')
- let deck = new Deck()
- let player = new Player()
- deck.createDeck(suits, values)
- console.log(deck.cards)
- deck.shuffle()
- console.log(player)
- PLAYER.JS
- 'use strict'
- class Player {
- constructor () {
- this.hand = []
- }
- playerAmount (players) {
- for (let i = 0; i < players; i++) {
- new Player
- }
- }
- }
- let player = new Player()
- module.exports = Player
- DECK.JS
- 'use strict'
- const suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
- const values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
- const Card = require('./Card')
- class Deck {
- constructor () {
- this.cards = []
- }
- createDeck (suits, values) {
- for (const suit of suits) {
- for (const value of values) {
- this.cards.push(new Card(suit, value))
- }
- }
- return this.cards
- }
- shuffle () {
- let i = this.cards.length // 52, Arrayens längd
- console.log(i)
- let j // Random tal mellan 0-51
- let temp // för att byta värdet
- while (--i > 0) { // sänker i till 51 direkt, tills mindre än 1
- j = Math.floor(Math.random() * (i + 1)) // tal mellan 0- 51
- // console.log(j)
- temp = this.cards[j]
- // console.log(temp)
- this.cards[j] = this.cards[i]
- this.cards[i] = temp
- }
- }
- }
- module.exports = Deck
- CARDS.JS
- 'use strict'
- class Card {
- constructor (suit, value) {
- this.suits = suit
- this.value = value
- }
- }
- module.exports = Card
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement