Giter Site home page Giter Site logo

Comments (2)

coding-horror avatar coding-horror commented on June 19, 2024 2

I agree with you, but I think we should focus on normalizing within the languages first and then later across all languages as a final pass once more implementations are in?

from basic-computer-games.

LukasMurdock avatar LukasMurdock commented on June 19, 2024 1

As an example, simply looking at the implementation of how cards are displayed across languages in Acey Ducey seems to teach a great deal of fundamentals across languages.

And there seems to be two primary implementations:

  1. If/else // switch
  2. Bracket property accessor (probably not the right term all-around, I just know JavaScript)

A normalized folder also encourages comments (and questions!) of why a particular language is implemented differently.

// c#
// private void DisplayCard(int card)
switch (card)
            {
                case 11:
                    cardText = "Jack";
                    break;
                case 12:
                    cardText = "Queen";
                    break;
                case 13:
                    cardText = "King";
                    break;
                case 14:
                    cardText = "Ace";
                    break;
                default:
                    cardText = card.ToString();
                    break;
            }
// java
// public class Card { …
    private void init(int value) {
        this.value = value;
        if (value < 11) {
            this.name = String.valueOf(value);
        } else {
            switch (value) {
                case 11:
                    this.name = "Jack";
                    break;
                case 12:
                    this.name = "Queen";
                    break;
                case 13:
                    this.name = "King";
                    break;
                case 14:
                    this.name = "Ace";
                    break;

                default:
                    this.name = "Unknown";
            }
        }
    }
// javascript
function getCardValue(card) {
  let cardValue = '';
  switch (card) {
    case 11:
        cardValue = 'JACK';
        break;
    case 12:
        cardValue = 'QUEEN';
        break;
    case 13:
        cardValue = 'KING';
        break;
    case 14:
        cardValue = 'ACE';
        break;
     default:
        cardValue = card;
  }
  return cardValue;
}

// javascript alternate (current implementation)
function getCardValue(card) {
    let faceOrAce = {
        11: 'JACK',
        12: 'QUEEN',
        13: 'KING',
        14: 'ACE',
    };
    // If card value matches a key in faceOrAce, use faceOrAce value;
    // Else, return undefined and handle with the Nullish Coalescing Operator (??)
    // and default to card value.
    let cardValue = faceOrAce[card] ?? card;
    return cardValue;
}
// pascal
procedure PrintCard(const ACard: Integer);
begin
  if ACard < 11 then
  begin
    Write(ACard);
  end;
  if ACard = 11 then
  begin
    Write('JACK');
  end;
  if ACard = 12 then
  begin
    Write('QUEEN');
  end;
  if ACard = 13 then
  begin
    Write('KING');
  end;
  if ACard = 14 then
  begin
    Write('ACE');
  end;
end;
# perl
sub nameOfCard
{
    my $value = shift;

    # Note that the Joker isn't used in this game, but since arrays in Perl are
    # 0-based, it's useful to have something there to represent the "0th"
    # position.  This way the rest of the elements match their expected values
    # (e.g., element 1 is Ace, element 7 is 7, and element 12 is Queen).

    my @cardlist = qw(Joker Ace 2 3 4 5 6 7 8 9 10 Jack Queen King);
    return $cardlist[$value];
}
# python
def get_card_name(number):
    """Get card name"""
    card_names = (
        " 2",
        " 3",
        " 4",
        " 5",
        " 6",
        " 7",
        " 8",
        " 9",
        " 10",
        "Jack",
        "Queen",
        "King",
        "Ace",
    )
    return card_names[number]

# python oo
    def __str__(self):
        r = self.rank
        if r == 11:
            r = 'J'
        elif r == 12:
            r = 'Q'
        elif r == 13:
            r = 'K'
        elif r == 14:
            r = 'A'
        return f'{r}{self.suit}'
# ruby
  def card_name(card)
    case card
    when 11
      "JACK"
    when 12
      "QUEEN"
    when 13
      "KING"
    when 14
      "ACE"
    else
      card
    end
  end
' vbnet
'  Private Function DisplayCard(value As Integer) As String
        Select Case value
            Case 2 To 10 ' Case statements can be ranges of values, also multiple values (Case 2,3,4,5,6,7,8,9,10)
                Return value.ToString

            Case 11
                Return "JACK"

            Case 12
                Return "QUEEN"

            Case 13
                Return "KING"

            Case 14
                Return "ACE"

        End Select

from basic-computer-games.

Related Issues (20)

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.