Coder Social home page Coder Social logo

Got a card of Rank = 0, is that right? about decks HOT 3 OPEN

dcu avatar dcu commented on July 30, 2024
Got a card of Rank = 0, is that right?

from decks.

Comments (3)

preslavmihaylov avatar preslavmihaylov commented on July 30, 2024

Card of Rank 0 is Ace.

decks/deck.go

Line 29 in 9e34f47

const (

Note that the ranks do not necessarily correspond to the values of a card - e.g. 1 for Ace, as those are different for every game.

If you want to add values to a card, I suggest writing a custom function in your own package. Something like valueOf(c decks.Card).

And if you want to check whether a card you got is of a certain rank, you can do:

if (c.Rank == decks.Ace) {
    // handle Ace
}

Or a range:

if (c.Rank >= decks.Two && c.Rank < decks.Ten) {
    // handle cards [2, 10)
}

from decks.

dcu avatar dcu commented on July 30, 2024

why not starting in iota+1? that would cover the value for most cases

from decks.

preslavmihaylov avatar preslavmihaylov commented on July 30, 2024

The initial implementation used that, yes.

But eventually, I decided to change it as it is now, because that approach causes issues with iterating over cards.

If the Ace starts at value 1, not zero, then there are all sorts of off-by-one errors that could occur.
For example, let's say you want to iterate over all ranks. Intuitively, you might do something like this:

cards := make([]Card, decks.RanksCnt)
for i := 0; i < decks.RanksCnt; i++ {
    cards[i] = decks.Card{Rank: i}
}

Issues with the code above if Ace starts at 1:

  1. Rank of zero is invalid
  2. You won't include the last rank (King)

Even if you do:

cards := make([]Card, decks.RanksCnt)
for i := decks.Ace; i < decks.RanksCnt; i++ {
    cards[i] = decks.Card{Rank: i}
}

This is still wrong, because you will miss the last rank (King) as decks.King == decks.RanksCnt

from decks.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.