Comments (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.
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:
- If/else // switch
- 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)
- terminal emulator on mobile
- Syncing fork since commit history edit. HOT 3
- terminal_style.css pegs cpu usage HOT 1
- Mastermind.py has trouble counting black and white pegs. HOT 5
- Typo in the Basic and JavaScript versions of SALVO HOT 2
- hangman.rb doesn't randomize the order of puzzle words HOT 9
- Are less mainstream languages allowed? HOT 4
- DEC Basic Computer Games HOT 2
- A little more about the spirit of the project??? HOT 6
- Super Star Trek README has a typo that's a rude word. HOT 1
- Edited latest commit requesting review for pull request (Life for Two.py)
- Super Star Trek - Ship class does not work correctly HOT 2
- A function never called in tictactoe2.py HOT 1
- Hacktoberfest HOT 1
- Confusion between 91_Train and 92_Trap Rust HOT 3
- 84_Super_Star_Trek java issues HOT 1
- Lunar lander initial weight HOT 1
- Battle doesn't seem to accept the num,num format suggested in the readme HOT 4
- Python Acey-Ducey has a "1" card instead of the "10" HOT 5
- Code decomposition question HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from basic-computer-games.