【问题标题】:Why are cards being lost in Javascript ES6 "Game of War"?为什么在 Javascript ES6“战争游戏”中卡片会丢失?
【发布时间】:2023-03-19 07:52:01
【问题描述】:

为了更好地理解 Javascript ES6 和一般的面向对象编程,我决定尝试编写一个简单的 Game of War。

乍一看,游戏似乎运行良好......它总是返回获胜玩家的姓名,并且因为它是一个机会游戏,它似乎在大约 50% 的时间里返回任何给定玩家。

但是,经过仔细检查,似乎有些地方非常不对劲。 问题在于,在每个连续的回合中,游戏中的卡牌总数都在减少,而不是保持在恒定的 52 张。

该游戏使用了五个不同的类(全部在下面列出)和一个用于实例化游戏并记录获胜者的 war.js 文件。

可以在以下位置找到源文件:https://github.com/johncmunson/war

使用node war.js运行游戏

Card.js

class Card {
    constructor(rank, suit) {
        this.rank = rank
        this.suit = suit
    }
}

module.exports = Card

Deck.js

const Card = require('./Card.js')

class Deck {
    constructor() {
        const suits = [1, 2, 3, 4]
        const ranks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
        this.cards = ranks.map(
            rank => suits.map(
                suit => new Card(rank, suit)
            )
        ).reduce(
            (cum, cur) => cum.concat(cur),
            []
        )
    }

    shuffle() {
        let currentIndex = this.cards.length
        let tempValue, randomIndex
        while (0 !== currentIndex) {
            randomIndex = Math.floor(Math.random() * currentIndex)
            currentIndex -= 1
            tempValue = this.cards[currentIndex]
            this.cards[currentIndex] = this.cards[randomIndex]
            this.cards[randomIndex] = tempValue
        }
    }

    deal(players) {
        let currentIndex = 0
        while (this.cards.length) {
            let dealtCard = this.cards.pop()
            players[currentIndex].receiveCard(dealtCard)
            if (currentIndex === players.length - 1) {
                currentIndex = 0
            } else {
                currentIndex += 1
            }
        }
    }
}

module.exports = Deck

Player.js

class Player {
    constructor(name) {
        this.name = name
        this.cards = []
        this.battleCard = null
    }

    playCard() {
        this.battleCard = this.cards.shift()
    }

    receiveCard(card) {
        this.cards.push(card)
    }

    winBattle(battleCard, potCards) {
        this.cards.push(battleCard)
        if (potCards.length) {
            for (let card of potCards) {
                this.cards.push(card)
            }
        }
    }

    tie(pot) {
        let burnCards = this.cards.length <= 3 ?
            this.cards.splice(0, this.cards.length) :
            this.cards.splice(0, 3)
        pot.receiveBurnCards(burnCards)
        pot.receiveBattleCard(this.battleCard)
    }
}

module.exports = Player

Pot.js

class Pot {
    constructor() {
        this.cards = []
    }

    receiveBattleCard(card) {
        this.cards.push(card)
    }

    receiveBurnCards(burnCards) {
        for (let card of burnCards) {
            this.cards.push(card)
        }
    }

    reset() {
        this.cards = []
    }
}

module.exports = Pot

Game.js

const Card = require('./Card.js')
const Deck = require('./Deck.js')
const Player = require('./Player.js')
const Pot = require('./Pot.js')

class Game {
    constructor(player1, player2, numberRounds) {
        this.player1 = new Player(player1)
        this.player2 = new Player(player2)
        this.rounds = numberRounds
        this.deck = new Deck()
        this.pot = new Pot()
    }

    _compare(card1, card2) {
        const rank1 = card1.rank === 1 ? 14 : card1.rank
        const rank2 = card2.rank === 1 ? 14 : card2.rank
        return rank2 - rank1
    }

    _battle() {
        this.player1.playCard()
        this.player2.playCard()
        const result = this._compare(this.player1.battleCard,                                         
        this.player2.battleCard)
        if (result < 0) {
            this.player1.winBattle(this.player2.battleCard, this.pot.cards)
            this.pot.reset()
        } else if (result > 0) {
            this.player2.winBattle(this.player1.battleCard, this.pot.cards)
            this.pot.reset()
        } else {
            this.player1.tie(this.pot)
            this.player2.tie(this.pot)
        }
    }

    _getWinner() {
        if (this.player1.cards.length > this.player2.cards.length) {
            return this.player1.name
        } else if (this.player1.cards.length < this.player2.cards.length) {
            return this.player2.name
        } else {
            return 'Tie!'
        }
    }

    play() {
        this.deck.shuffle()
        this.deck.deal([this.player1, this.player2])
        if (this.rounds) {
            while (this.rounds !== 0) {
                this._battle()
                this.rounds -= 1
                console.log('Number of Cards: ', this.pot.cards.length + this.player1.cards.length + this.player2.cards.length)
            }
        } else {
            while (this.player1.cards.length && this.player2.cards.length) {
                this._battle()
                console.log('Number of Cards: ', this.pot.cards.length + this.player1.cards.length + this.player2.cards.length)
            }
        }
        return this._getWinner()
    }
}

module.exports = Game

war.js

const Game = require('./Game.js')
const war = new Game('George', 'Abe')
console.log(war.play())

控制台输出

Number of Cards:  52
Number of Cards:  51
Number of Cards:  50
Number of Cards:  49
Number of Cards:  48
Number of Cards:  47
Number of Cards:  47
Number of Cards:  46
Number of Cards:  45
Number of Cards:  45
Number of Cards:  44
Number of Cards:  43
Number of Cards:  42
Number of Cards:  41
Number of Cards:  40
Number of Cards:  39
Number of Cards:  38
Number of Cards:  37
Number of Cards:  36
Number of Cards:  35
Number of Cards:  34
Number of Cards:  33
Number of Cards:  32
Number of Cards:  31
Number of Cards:  30
Number of Cards:  29
Number of Cards:  28
Number of Cards:  27
Number of Cards:  26
Number of Cards:  25
Number of Cards:  24
Number of Cards:  23
Number of Cards:  22
Number of Cards:  21
Number of Cards:  20
Number of Cards:  19
Number of Cards:  18
George

【问题讨论】:

  • 我可以建议在您的 Deck 构造函数中的 reduce 函数中更改参数名称吗?
  • 代码太多。请仅提供重现您的问题所需的代码。
  • 尝试分别打印卡片总和的 3 个分量并检查哪个分量减小了值:this.pot.cards.length、this.player1.cards.length、this.player2.cards。长度

标签: javascript node.js oop ecmascript-6 object-oriented-analysis


【解决方案1】:

那是因为获胜者也应该保留自己的卡片。

你应该在 Player.js 上添加

 winBattle(battleCard, potCards) {
    this.cards.push(this.battleCard) // this one
    this.cards.push(battleCard)
    if (potCards.length) {
        for (let card of potCards) {
            this.cards.push(card)
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2015-03-12
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多